Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support force index #102

Merged
merged 4 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions proxy/plan/decorator_table_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/XiaoMi/Gaea/parser/ast"
"github.com/XiaoMi/Gaea/parser/format"
"github.com/XiaoMi/Gaea/proxy/router"
"github.com/pingcap/errors"
)

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

if len(n.IndexHints) != 0 {
return nil, fmt.Errorf("TableName does not support IndexHints in sharding")
}

ret := &TableNameDecorator{
origin: n,
rule: rule,
Expand Down Expand Up @@ -115,6 +112,12 @@ func (t *TableNameDecorator) Restore(ctx *format.RestoreCtx) error {
ctx.WriteName(fmt.Sprintf("%s_%04d", t.origin.Name.String(), tableIndex))
}

for _, value := range t.origin.IndexHints {
ctx.WritePlain(" ")
if err := value.Restore(ctx); err != nil {
return errors.Annotate(err, "An error occurred while splicing IndexHints")
}
}
return nil
}

Expand Down
38 changes: 37 additions & 1 deletion proxy/plan/plan_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ func TestMycatSelectSubqueryInTableRefs(t *testing.T) {
},
{
db: "db_mycat",
sql: "select id from (select user from tbl_mycat_unknown) as a", //unshard plan
sql: "select id from (select user from tbl_mycat_unknown) as a", //unshard plan
sqls: map[string]map[string][]string{
"slice-0": {
"db_mycat_0": {"SELECT `id` FROM (SELECT `user` FROM (`tbl_mycat_unknown`)) AS `a`"},
Expand Down Expand Up @@ -3407,6 +3407,42 @@ func TestSelectMycatOrderByDatabase(t *testing.T) {
}
}

func TestSelectForceIndexDatabase(t *testing.T) {
ns, err := preparePlanInfo()
if err != nil {
t.Fatalf("prepare namespace error: %v", err)
}

tests := []SQLTestcase{
{
db: "db_mycat",
sql: "select * from tbl_mycat force index(id, name) where id > 100 and name = `zhangsan`",
sqls: map[string]map[string][]string{
"slice-0": {
"db_mycat_0": {
"SELECT * FROM `tbl_mycat` FORCE INDEX (`id`, `name`) WHERE `id`>100 AND `name`=`zhangsan`",
},
"db_mycat_1": {
"SELECT * FROM `tbl_mycat` FORCE INDEX (`id`, `name`) WHERE `id`>100 AND `name`=`zhangsan`",
},
},
"slice-1": {
"db_mycat_2": {
"SELECT * FROM `tbl_mycat` FORCE INDEX (`id`, `name`) WHERE `id`>100 AND `name`=`zhangsan`",
},
"db_mycat_3": {
"SELECT * FROM `tbl_mycat` FORCE INDEX (`id`, `name`) WHERE `id`>100 AND `name`=`zhangsan`",
},
},
},
},
}

for _, test := range tests {
t.Run(test.sql, getTestFunc(ns, test))
}
}

func prepareShardKingshardRouter() (*router.Router, error) {
nsStr := `
{
Expand Down