Skip to content

Commit

Permalink
Merge pull request #221 from flike/feature-refactor-in
Browse files Browse the repository at this point in the history
refactor in operation
  • Loading branch information
Fei Chen authored Aug 2, 2016
2 parents 3dcdc90 + ab3961e commit a5dab68
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
4 changes: 2 additions & 2 deletions core/hack/version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package hack

const (
Version = "2016-03-17 14:04:39 +0800 @358bd8f"
Compile = "2016-03-22 10:52:25 +0800 by go version go1.6 darwin/amd64"
Version = "2016-08-02 16:17:09 +0800 @3f334a4"
Compile = "2016-08-02 16:18:18 +0800 by go version go1.6 darwin/amd64"
)
21 changes: 16 additions & 5 deletions proxy/router/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ type Plan struct {
//the rows for insert or replace.
Rows map[int]sqlparser.Values

RouteTableIndexs []int
RouteNodeIndexs []int
RewrittenSqls map[string][]string
SubTableValueGroups map[int]sqlparser.ValTuple //按照tableIndex存放ValueExpr
InRightToReplace *sqlparser.ComparisonExpr //记录in的右边Expr,用来动态替换不同table in的值
RouteTableIndexs []int
RouteNodeIndexs []int
RewrittenSqls map[string][]string
}

func (plan *Plan) notList(l []int) []int {
Expand Down Expand Up @@ -386,6 +388,8 @@ func (plan *Plan) getTableIndexByBoolExpr(node sqlparser.BoolExpr) ([]int, error
left := plan.getValueType(node.Left)
right := plan.getValueType(node.Right)
if left == EID_NODE && right == LIST_NODE {
//save the node of in operation,will replace in
plan.InRightToReplace = node
return plan.getTableIndexs(node)
}
}
Expand All @@ -402,17 +406,24 @@ func (plan *Plan) getTableIndexByBoolExpr(node sqlparser.BoolExpr) ([]int, error

//获得(12,14,23)对应的table index
func (plan *Plan) getTableIndexsByTuple(valExpr sqlparser.ValExpr) ([]int, error) {
shardset := make(map[int]bool)
shardset := make(map[int]sqlparser.ValTuple)
switch node := valExpr.(type) {
case sqlparser.ValTuple:
for _, n := range node {
//n.Format()
index, err := plan.getTableIndexByValue(n)
if err != nil {
return nil, err
}
shardset[index] = true
valExprs := shardset[index]
if valExprs == nil {
valExprs = make([]sqlparser.ValExpr, 0)
}
valExprs = append(valExprs, n)
shardset[index] = valExprs
}
}
plan.SubTableValueGroups = shardset
shardlist := make([]int, len(shardset))
index := 0
for k := range shardset {
Expand Down
7 changes: 7 additions & 0 deletions proxy/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,13 @@ func (r *Router) rewriteSelectSql(plan *Plan, node *sqlparser.Select, tableIndex
//do not change limit
newLimit = node.Limit
}

if plan.InRightToReplace != nil {
//assign corresponding values to different table index
plan.InRightToReplace.Right = plan.SubTableValueGroups[tableIndex]

}

buf.Fprintf("%v%v%v%v%v%s",
node.Where,
node.GroupBy,
Expand Down
9 changes: 9 additions & 0 deletions proxy/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,15 @@ func checkPlan(t *testing.T, sql string, tableIndexs []int, nodeIndexs []int) {
t.Logf("rewritten_sql=%v", plan.RewrittenSqls)

}
func TestWhereInPartitionByTableIndex(t *testing.T) {
var sql string
//2016-03-06 13:37:26
sql = "select * from test1 where id in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22) "
checkPlan(t, sql,
[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
[]int{0, 1, 2},
)
}

func TestDatePlan(t *testing.T) {
var sql string
Expand Down

0 comments on commit a5dab68

Please sign in to comment.