From 71416825ed3244282003dd4c247ffb5166ec3103 Mon Sep 17 00:00:00 2001 From: maidoudou Date: Fri, 4 Sep 2020 11:04:52 +0800 Subject: [PATCH 1/4] support force index --- proxy/plan/decorator_table_name.go | 12 ++++++---- proxy/plan/plan_select_test.go | 38 +++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/proxy/plan/decorator_table_name.go b/proxy/plan/decorator_table_name.go index 924b3b65..1d4002e0 100644 --- a/proxy/plan/decorator_table_name.go +++ b/proxy/plan/decorator_table_name.go @@ -62,10 +62,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, @@ -115,6 +111,14 @@ func (t *TableNameDecorator) Restore(ctx *format.RestoreCtx) error { ctx.WriteName(fmt.Sprintf("%s_%04d", t.origin.Name.String(), tableIndex)) } + if len(t.origin.IndexHints) <= 0 { + return nil + } + + ctx.WritePlain(" ") + for i := 0; i < len(t.origin.IndexHints); i++ { + t.origin.IndexHints[i].Restore(ctx) + } return nil } diff --git a/proxy/plan/plan_select_test.go b/proxy/plan/plan_select_test.go index f641983f..70a783d2 100644 --- a/proxy/plan/plan_select_test.go +++ b/proxy/plan/plan_select_test.go @@ -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`"}, @@ -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 := ` { From 0ae0449e48e0fc235f2e6f67af4d6e156c4577ba Mon Sep 17 00:00:00 2001 From: maidoudou Date: Fri, 4 Sep 2020 11:12:52 +0800 Subject: [PATCH 2/4] check length of indexhint --- proxy/plan/decorator_table_name.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy/plan/decorator_table_name.go b/proxy/plan/decorator_table_name.go index 1d4002e0..c94fb49e 100644 --- a/proxy/plan/decorator_table_name.go +++ b/proxy/plan/decorator_table_name.go @@ -111,7 +111,7 @@ func (t *TableNameDecorator) Restore(ctx *format.RestoreCtx) error { ctx.WriteName(fmt.Sprintf("%s_%04d", t.origin.Name.String(), tableIndex)) } - if len(t.origin.IndexHints) <= 0 { + if len(t.origin.IndexHints) == 0 { return nil } From d82ddd157d91398df409c21843b0fb4d626b9d43 Mon Sep 17 00:00:00 2001 From: maidoudou Date: Fri, 4 Sep 2020 11:19:20 +0800 Subject: [PATCH 3/4] fix table name decorator --- proxy/plan/decorator_table_name.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/proxy/plan/decorator_table_name.go b/proxy/plan/decorator_table_name.go index c94fb49e..3ac1ff4c 100644 --- a/proxy/plan/decorator_table_name.go +++ b/proxy/plan/decorator_table_name.go @@ -17,6 +17,8 @@ package plan import ( "fmt" + "github.com/pingcap/errors" + "github.com/XiaoMi/Gaea/parser/ast" "github.com/XiaoMi/Gaea/parser/format" "github.com/XiaoMi/Gaea/proxy/router" @@ -111,13 +113,11 @@ func (t *TableNameDecorator) Restore(ctx *format.RestoreCtx) error { ctx.WriteName(fmt.Sprintf("%s_%04d", t.origin.Name.String(), tableIndex)) } - if len(t.origin.IndexHints) == 0 { - return nil - } - - ctx.WritePlain(" ") - for i := 0; i < len(t.origin.IndexHints); i++ { - t.origin.IndexHints[i].Restore(ctx) + 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 } From de692b18f2d696a5c5e88c204203e380943a1f22 Mon Sep 17 00:00:00 2001 From: maidoudou Date: Fri, 4 Sep 2020 11:22:28 +0800 Subject: [PATCH 4/4] goimports -w . --- proxy/plan/decorator_table_name.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/proxy/plan/decorator_table_name.go b/proxy/plan/decorator_table_name.go index 3ac1ff4c..8f3a7029 100644 --- a/proxy/plan/decorator_table_name.go +++ b/proxy/plan/decorator_table_name.go @@ -17,11 +17,10 @@ package plan import ( "fmt" - "github.com/pingcap/errors" - "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