From c10bea03f683ecc7231140b4522b050097f4a9dd Mon Sep 17 00:00:00 2001 From: Gilbert2189 <309112189@qq.com> Date: Sun, 31 Jul 2016 19:52:49 +0800 Subject: [PATCH 1/2] fix #183 --- proxy/router/planbuilder.go | 16 ++++++++++++++-- proxy/router/router.go | 8 ++++++++ proxy/router/router_test.go | 9 +++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/proxy/router/planbuilder.go b/proxy/router/planbuilder.go index 22b5a803..2d6100bc 100644 --- a/proxy/router/planbuilder.go +++ b/proxy/router/planbuilder.go @@ -39,6 +39,8 @@ type Plan struct { //the rows for insert or replace. Rows map[int]sqlparser.Values + SubTableValueGroups map[int]sqlparser.ValTuple //按照tableIndex存放ValueExpr + InRightToReplace *sqlparser.ComparisonExpr //记录in的右边Expr,用来动态替换不同table in的值 RouteTableIndexs []int RouteNodeIndexs []int RewrittenSqls map[string][]string @@ -386,6 +388,7 @@ 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 { + plan.InRightToReplace=node return plan.getTableIndexs(node) } } @@ -402,17 +405,26 @@ 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 { diff --git a/proxy/router/router.go b/proxy/router/router.go index 5f1e1cc3..356e8560 100644 --- a/proxy/router/router.go +++ b/proxy/router/router.go @@ -591,6 +591,14 @@ 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, diff --git a/proxy/router/router_test.go b/proxy/router/router_test.go index e492ccc8..131e14da 100644 --- a/proxy/router/router_test.go +++ b/proxy/router/router_test.go @@ -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 From ab3961e1db49dc4a38a48643cd4c401e6ad3d76e Mon Sep 17 00:00:00 2001 From: chenfei1 Date: Tue, 2 Aug 2016 16:51:39 +0800 Subject: [PATCH 2/2] bugfix #183 --- core/hack/version.go | 4 ++-- proxy/router/planbuilder.go | 19 +++++++++---------- proxy/router/router.go | 5 ++--- proxy/router/router_test.go | 6 +++--- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/core/hack/version.go b/core/hack/version.go index 1c939fb8..967f59fa 100644 --- a/core/hack/version.go +++ b/core/hack/version.go @@ -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" ) diff --git a/proxy/router/planbuilder.go b/proxy/router/planbuilder.go index 2d6100bc..3e9a8e77 100644 --- a/proxy/router/planbuilder.go +++ b/proxy/router/planbuilder.go @@ -39,11 +39,11 @@ type Plan struct { //the rows for insert or replace. Rows map[int]sqlparser.Values - SubTableValueGroups map[int]sqlparser.ValTuple //按照tableIndex存放ValueExpr - InRightToReplace *sqlparser.ComparisonExpr //记录in的右边Expr,用来动态替换不同table in的值 - 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 { @@ -388,7 +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 { - plan.InRightToReplace=node + //save the node of in operation,will replace in + plan.InRightToReplace = node return plan.getTableIndexs(node) } } @@ -411,16 +412,14 @@ func (plan *Plan) getTableIndexsByTuple(valExpr sqlparser.ValExpr) ([]int, error for _, n := range node { //n.Format() index, err := plan.getTableIndexByValue(n) - if err != nil { return nil, err } valExprs := shardset[index] - - if(valExprs==nil){ + if valExprs == nil { valExprs = make([]sqlparser.ValExpr, 0) } - valExprs=append(valExprs,n) + valExprs = append(valExprs, n) shardset[index] = valExprs } } diff --git a/proxy/router/router.go b/proxy/router/router.go index 356e8560..93a7bd07 100644 --- a/proxy/router/router.go +++ b/proxy/router/router.go @@ -592,13 +592,12 @@ func (r *Router) rewriteSelectSql(plan *Plan, node *sqlparser.Select, tableIndex newLimit = node.Limit } - if(plan.InRightToReplace!=nil){ + if plan.InRightToReplace != nil { //assign corresponding values to different table index - plan.InRightToReplace.Right=plan.SubTableValueGroups[tableIndex] + plan.InRightToReplace.Right = plan.SubTableValueGroups[tableIndex] } - buf.Fprintf("%v%v%v%v%v%s", node.Where, node.GroupBy, diff --git a/proxy/router/router_test.go b/proxy/router/router_test.go index 131e14da..8cab5bf3 100644 --- a/proxy/router/router_test.go +++ b/proxy/router/router_test.go @@ -342,13 +342,13 @@ func checkPlan(t *testing.T, sql string, tableIndexs []int, nodeIndexs []int) { t.Logf("rewritten_sql=%v", plan.RewrittenSqls) } -func TestWhereInPartitionByTableIndex(t *testing.T){ +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}, + []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, + []int{0, 1, 2}, ) }