Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner/core: fix resolve rule for group by expression (#12307) #12351

Merged
merged 3 commits into from Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions planner/core/logical_plan_builder.go
Expand Up @@ -1437,16 +1437,25 @@ type gbyResolver struct {
err error
inExpr bool
isParam bool

exprDepth int // exprDepth is the depth of current expression in expression tree.
}

func (g *gbyResolver) Enter(inNode ast.Node) (ast.Node, bool) {
g.exprDepth++
switch n := inNode.(type) {
case *ast.SubqueryExpr, *ast.CompareSubqueryExpr, *ast.ExistsSubqueryExpr:
return inNode, true
case *driver.ParamMarkerExpr:
newNode := expression.ConstructPositionExpr(n)
g.isParam = true
return newNode, true
if g.exprDepth == 1 {
_, isNull, isExpectedType := getUintFromNode(g.ctx, n)
// For constant uint expression in top level, it should be treated as position expression.
if !isNull && isExpectedType {
return expression.ConstructPositionExpr(n), true
}
}
return n, true
case *driver.ValueExpr, *ast.ColumnNameExpr, *ast.ParenthesesExpr, *ast.ColumnName:
default:
g.inExpr = true
Expand Down
25 changes: 25 additions & 0 deletions planner/core/prepare_test.go
Expand Up @@ -345,3 +345,28 @@ func (s *testPrepareSuite) TestPrepareWithWindowFunction(c *C) {
tk.MustExec("set @a=0, @b=1;")
tk.MustQuery("execute stmt2 using @a, @b").Check(testkit.Rows("0", "0"))
}

func (s *testPrepareSuite) TestPrepareForGroupByItems(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(id int, v int)")
tk.MustExec("insert into t(id, v) values(1, 2),(1, 2),(2, 3);")
tk.MustExec("prepare s1 from 'select max(v) from t group by floor(id/?)';")
tk.MustExec("set @a=2;")
tk.MustQuery("execute s1 using @a;").Sort().Check(testkit.Rows("2", "3"))

tk.MustExec("prepare s1 from 'select max(v) from t group by ?';")
tk.MustExec("set @a=2;")
err = tk.ExecToErr("execute s1 using @a;")
c.Assert(err.Error(), Equals, "Unknown column '2' in 'group statement'")
tk.MustExec("set @a=2.0;")
tk.MustQuery("execute s1 using @a;").Check(testkit.Rows("3"))
}