diff --git a/proxy/plan/decorator_pattern_in_expr.go b/proxy/plan/decorator_pattern_in_expr.go index 36b8c0cb..6e927e02 100644 --- a/proxy/plan/decorator_pattern_in_expr.go +++ b/proxy/plan/decorator_pattern_in_expr.go @@ -15,10 +15,8 @@ package plan import ( - "bytes" "fmt" "io" - "sort" "github.com/XiaoMi/Gaea/parser/ast" @@ -26,6 +24,7 @@ import ( driver "github.com/XiaoMi/Gaea/parser/tidb-types/parser_driver" "github.com/XiaoMi/Gaea/parser/types" "github.com/XiaoMi/Gaea/proxy/router" + "github.com/XiaoMi/Gaea/util" ) // type check @@ -130,10 +129,13 @@ func getPatternInRouteResult(n *ast.ColumnName, isNotIn bool, rule router.Rule, var indexes []int valueMap := make(map[int][]ast.ExprNode) - s := &bytes.Buffer{} for _, vi := range values { - vi.Format(s) - idx, err := rule.FindTableIndex(s.String()) + v, _ := vi.(*driver.ValueExpr) + value, err := util.GetValueExprResult(v) + if err != nil { + return nil, nil, err + } + idx, err := rule.FindTableIndex(value) if err != nil { return nil, nil, err } @@ -141,7 +143,6 @@ func getPatternInRouteResult(n *ast.ColumnName, isNotIn bool, rule router.Rule, indexes = append(indexes, idx) } valueMap[idx] = append(valueMap[idx], vi) - s.Reset() } sort.Ints(indexes) return indexes, valueMap, nil diff --git a/proxy/plan/plan_select_test.go b/proxy/plan/plan_select_test.go index 9fe473ff..aa304be5 100644 --- a/proxy/plan/plan_select_test.go +++ b/proxy/plan/plan_select_test.go @@ -227,6 +227,37 @@ func TestSimpleSelectShardMycatMurmur(t *testing.T) { } } +func TestSimpleSelectShardMycatMurmur_ShardKeyTypeString(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_murmur where id in ('0')", + sqls: map[string]map[string][]string{ + "slice-1": { + "db_mycat_2": {"SELECT * FROM `tbl_mycat_murmur` WHERE `id` IN ('0')"}, + }, + }, + }, + { + db: "db_mycat", + sql: "select * from tbl_mycat_murmur where id = '0'", + sqls: map[string]map[string][]string{ + "slice-1": { + "db_mycat_2": {"SELECT * FROM `tbl_mycat_murmur` WHERE `id`='0'"}, + }, + }, + }, + } + for _, test := range tests { + t.Run(test.sql, getTestFunc(ns, test)) + } +} + func TestSimpleSelectShardMycatString(t *testing.T) { ns, err := preparePlanInfo() if err != nil {