Skip to content

Commit

Permalink
planner: fix the issue that cannot find column if using question mark…
Browse files Browse the repository at this point in the history
…er in group-by-clause (pingcap#54205) (pingcap#55979)

close pingcap#53872
  • Loading branch information
ti-chi-bot authored Nov 8, 2024
1 parent ba09647 commit 8829b96
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3088,7 +3088,7 @@ func (g *gbyResolver) Enter(inNode ast.Node) (ast.Node, bool) {
return inNode, true
case *driver.ParamMarkerExpr:
g.isParam = true
if g.exprDepth == 1 {
if g.exprDepth == 1 && !n.UseAsValueInGbyByClause {
_, isNull, isExpectedType := getUintFromNode(g.ctx, n, false)
// For constant uint expression in top level, it should be treated as position expression.
if !isNull && isExpectedType {
Expand Down Expand Up @@ -3126,6 +3126,9 @@ func (g *gbyResolver) Leave(inNode ast.Node) (ast.Node, bool) {
} else if ast.HasWindowFlag(ret) {
err = ErrIllegalReference.GenWithStackByArgs(v.Name.OrigColName(), "reference to window function")
} else {
if isParam, ok := ret.(*driver.ParamMarkerExpr); ok {
isParam.UseAsValueInGbyByClause = true
}
return ret, true
}
}
Expand Down
12 changes: 12 additions & 0 deletions planner/core/plan_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,18 @@ func TestIssue41626(t *testing.T) {
tk.MustQuery(`show warnings`).Check(testkit.Rows("Warning 1105 skip prepared plan-cache: '12' may be converted to INT"))
}

func TestIssue53872(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)

tk.MustExec(`use test`)
tk.MustExec(`create table test(id int, col int)`)
tk.MustExec(`prepare stmt from "select id, ? as col1 from test where col=? group by id,col1"`)
tk.MustExec(`set @a=100, @b=100`)
tk.MustQuery(`execute stmt using @a,@b`).Check(testkit.Rows()) // no error
tk.MustQuery(`execute stmt using @a,@b`).Check(testkit.Rows())
}

func TestIssue38269(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
Expand Down
8 changes: 8 additions & 0 deletions types/parser_driver/value_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ type ParamMarkerExpr struct {
Offset int
Order int
InExecute bool

// For "select ? as c from t group by c", the optimizer replaces the `c` in the by-clause to `group by ?`,
// but this conversion conflicts with the original semantic. The original `group by c` means grouping by the column `c`,
// while the converted `group by ?` means grouping by the `?-th` column in the select-list, for example, `group by 3` means
// grouping the result by the 3rd column.
// Use this flag to let the optimizer know whether `group by ?` is converted from this case and if it is, use this
// marker as normal value instead of column index in the by-clause.
UseAsValueInGbyByClause bool
}

// Restore implements Node interface.
Expand Down

0 comments on commit 8829b96

Please sign in to comment.