Skip to content

Commit

Permalink
Merge pull request #57 from eastfisher/fix-pattern-in-decorator
Browse files Browse the repository at this point in the history
fix pattern in decorator when using string sharding key
  • Loading branch information
teckick authored Nov 1, 2019
2 parents 3b28b27 + f6b11fb commit bcc9542
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
13 changes: 7 additions & 6 deletions proxy/plan/decorator_pattern_in_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
package plan

import (
"bytes"
"fmt"
"io"

"sort"

"github.com/XiaoMi/Gaea/parser/ast"
"github.com/XiaoMi/Gaea/parser/format"
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
Expand Down Expand Up @@ -130,18 +129,20 @@ 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
}
if _, ok := valueMap[idx]; !ok {
indexes = append(indexes, idx)
}
valueMap[idx] = append(valueMap[idx], vi)
s.Reset()
}
sort.Ints(indexes)
return indexes, valueMap, nil
Expand Down
31 changes: 31 additions & 0 deletions proxy/plan/plan_select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bcc9542

Please sign in to comment.