Skip to content

Commit 9d70803

Browse files
authored
support force index (#102)
1 parent 7e598c0 commit 9d70803

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

proxy/plan/decorator_table_name.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/XiaoMi/Gaea/parser/ast"
2121
"github.com/XiaoMi/Gaea/parser/format"
2222
"github.com/XiaoMi/Gaea/proxy/router"
23+
"github.com/pingcap/errors"
2324
)
2425

2526
// TableNameDecorator decorate TableName
@@ -62,10 +63,6 @@ func CreateTableNameDecorator(n *ast.TableName, rule router.Rule, result *RouteR
6263
return nil, fmt.Errorf("TableName does not support PartitionNames in sharding")
6364
}
6465

65-
if len(n.IndexHints) != 0 {
66-
return nil, fmt.Errorf("TableName does not support IndexHints in sharding")
67-
}
68-
6966
ret := &TableNameDecorator{
7067
origin: n,
7168
rule: rule,
@@ -115,6 +112,12 @@ func (t *TableNameDecorator) Restore(ctx *format.RestoreCtx) error {
115112
ctx.WriteName(fmt.Sprintf("%s_%04d", t.origin.Name.String(), tableIndex))
116113
}
117114

115+
for _, value := range t.origin.IndexHints {
116+
ctx.WritePlain(" ")
117+
if err := value.Restore(ctx); err != nil {
118+
return errors.Annotate(err, "An error occurred while splicing IndexHints")
119+
}
120+
}
118121
return nil
119122
}
120123

proxy/plan/plan_select_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ func TestMycatSelectSubqueryInTableRefs(t *testing.T) {
15001500
},
15011501
{
15021502
db: "db_mycat",
1503-
sql: "select id from (select user from tbl_mycat_unknown) as a", //unshard plan
1503+
sql: "select id from (select user from tbl_mycat_unknown) as a", //unshard plan
15041504
sqls: map[string]map[string][]string{
15051505
"slice-0": {
15061506
"db_mycat_0": {"SELECT `id` FROM (SELECT `user` FROM (`tbl_mycat_unknown`)) AS `a`"},
@@ -3407,6 +3407,42 @@ func TestSelectMycatOrderByDatabase(t *testing.T) {
34073407
}
34083408
}
34093409

3410+
func TestSelectForceIndexDatabase(t *testing.T) {
3411+
ns, err := preparePlanInfo()
3412+
if err != nil {
3413+
t.Fatalf("prepare namespace error: %v", err)
3414+
}
3415+
3416+
tests := []SQLTestcase{
3417+
{
3418+
db: "db_mycat",
3419+
sql: "select * from tbl_mycat force index(id, name) where id > 100 and name = `zhangsan`",
3420+
sqls: map[string]map[string][]string{
3421+
"slice-0": {
3422+
"db_mycat_0": {
3423+
"SELECT * FROM `tbl_mycat` FORCE INDEX (`id`, `name`) WHERE `id`>100 AND `name`=`zhangsan`",
3424+
},
3425+
"db_mycat_1": {
3426+
"SELECT * FROM `tbl_mycat` FORCE INDEX (`id`, `name`) WHERE `id`>100 AND `name`=`zhangsan`",
3427+
},
3428+
},
3429+
"slice-1": {
3430+
"db_mycat_2": {
3431+
"SELECT * FROM `tbl_mycat` FORCE INDEX (`id`, `name`) WHERE `id`>100 AND `name`=`zhangsan`",
3432+
},
3433+
"db_mycat_3": {
3434+
"SELECT * FROM `tbl_mycat` FORCE INDEX (`id`, `name`) WHERE `id`>100 AND `name`=`zhangsan`",
3435+
},
3436+
},
3437+
},
3438+
},
3439+
}
3440+
3441+
for _, test := range tests {
3442+
t.Run(test.sql, getTestFunc(ns, test))
3443+
}
3444+
}
3445+
34103446
func prepareShardKingshardRouter() (*router.Router, error) {
34113447
nsStr := `
34123448
{

0 commit comments

Comments
 (0)