Skip to content

Commit

Permalink
planner: fix group by resolver for multi items with param mark… (#16363)
Browse files Browse the repository at this point in the history
  • Loading branch information
cfzjywxk authored Apr 14, 2020
1 parent 4e035fa commit 85efba0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2130,6 +2130,8 @@ func (b *PlanBuilder) resolveGbyExprs(ctx context.Context, p LogicalPlan, gby *a
}
for _, item := range gby.Items {
resolver.inExpr = false
resolver.exprDepth = 0
resolver.isParam = false
retExpr, _ := item.Expr.Accept(resolver)
if resolver.err != nil {
return nil, nil, errors.Trace(resolver.err)
Expand Down
31 changes: 31 additions & 0 deletions planner/core/prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,34 @@ func (s *testPlanSerialSuite) TestPlanCacheUnionScan(c *C) {
cnt = pb.GetCounter().GetValue()
c.Check(cnt, Equals, float64(6))
}

func (s *testPrepareSuite) TestPrepareForGroupByMultiItems(c *C) {
defer testleak.AfterTest(c)()
store, dom, err := newStoreWithBootstrap()
c.Assert(err, IsNil)
tk := testkit.NewTestKit(c, store)
defer func() {
dom.Close()
store.Close()
}()

tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, c int , index idx(a));")
tk.MustExec("insert into t values(1,2, -1), (1,2, 1), (1,2, -1), (4,4,3);")
tk.MustExec("set @a=1")
tk.MustExec("set @b=3")
tk.MustExec(`set sql_mode=""`)
tk.MustExec(`prepare stmt from "select a, sum(b), c from t group by ?, ? order by ?, ?"`)
tk.MustQuery("select a, sum(b), c from t group by 1,3 order by 1,3;").Check(testkit.Rows("1 4 -1", "1 2 1", "4 4 3"))
tk.MustQuery(`execute stmt using @a, @b, @a, @b`).Check(testkit.Rows("1 4 -1", "1 2 1", "4 4 3"))

tk.MustExec("set @c=10")
err = tk.ExecToErr("execute stmt using @a, @c, @a, @c")
c.Assert(err.Error(), Equals, "Unknown column '10' in 'group statement'")

tk.MustExec("set @v1=1.0")
tk.MustExec("set @v2=3.0")
tk.MustExec(`prepare stmt2 from "select sum(b) from t group by ?, ?"`)
tk.MustQuery(`execute stmt2 using @v1, @v2`).Check(testkit.Rows("10"))
}

0 comments on commit 85efba0

Please sign in to comment.