Skip to content

Commit

Permalink
planner: fix the issue that binding cannot work when sql_select_limit…
Browse files Browse the repository at this point in the history
… is enabled (#29789)
  • Loading branch information
qw4990 authored Nov 23, 2021
1 parent 8b028b8 commit 18381f9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 0 additions & 1 deletion executor/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func (c *Compiler) Compile(ctx context.Context, stmtNode ast.StmtNode) (*ExecStm
if err != nil {
return nil, err
}
stmtNode = plannercore.TryAddExtraLimit(c.Ctx, stmtNode)

finalPlan, names, err := planner.Optimize(ctx, c.Ctx, stmtNode, ret.InfoSchema)
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4719,6 +4719,26 @@ func (s *testIntegrationSuite) TestIssue27797(c *C) {
result.Check(testkit.Rows("<nil>"))
}

func (s *testIntegrationSuite) TestIssue27949(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t27949")
tk.MustExec("create table t27949 (a int, b int, key(b))")
tk.MustQuery("explain format = 'brief' select * from t27949 where b=1").Check(testkit.Rows("IndexLookUp 10.00 root ",
"├─IndexRangeScan(Build) 10.00 cop[tikv] table:t27949, index:b(b) range:[1,1], keep order:false, stats:pseudo",
"└─TableRowIDScan(Probe) 10.00 cop[tikv] table:t27949 keep order:false, stats:pseudo"))
tk.MustExec("create global binding for select * from t27949 where b=1 using select * from t27949 ignore index(b) where b=1")
tk.MustQuery("explain format = 'brief' select * from t27949 where b=1").Check(testkit.Rows("TableReader 10.00 root data:Selection",
"└─Selection 10.00 cop[tikv] eq(test.t27949.b, 1)",
" └─TableFullScan 10000.00 cop[tikv] table:t27949 keep order:false, stats:pseudo"))
tk.MustExec("set @@sql_select_limit=100")
tk.MustQuery("explain format = 'brief' select * from t27949 where b=1").Check(testkit.Rows("Limit 10.00 root offset:0, count:100",
"└─TableReader 10.00 root data:Limit",
" └─Limit 10.00 cop[tikv] offset:0, count:100",
" └─Selection 10.00 cop[tikv] eq(test.t27949.b, 1)",
" └─TableFullScan 10000.00 cop[tikv] table:t27949 keep order:false, stats:pseudo"))
}

func (s *testIntegrationSuite) TestIssue28154(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
10 changes: 6 additions & 4 deletions planner/optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,10 @@ func Optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
useBinding = false
}
}
if useBinding && sessVars.SelectLimit != math.MaxUint64 {
sessVars.StmtCtx.AppendWarning(errors.New("sql_select_limit is set, ignore SQL bindings"))
useBinding = false
if ok {
// add the extra Limit after matching the bind record
stmtNode = plannercore.TryAddExtraLimit(sctx, stmtNode)
node = stmtNode
}

var names types.NameSlice
Expand Down Expand Up @@ -234,7 +235,8 @@ func Optimize(ctx context.Context, sctx sessionctx.Context, node ast.Node, is in
defer func() {
sessVars.StmtCtx.StmtHints = savedStmtHints
}()
if sessVars.EvolvePlanBaselines && bestPlanFromBind != nil {
if sessVars.EvolvePlanBaselines && bestPlanFromBind != nil &&
sessVars.SelectLimit == math.MaxUint64 { // do not evolve this query if sql_select_limit is enabled
// Check bestPlanFromBind firstly to avoid nil stmtNode.
if _, ok := stmtNode.(*ast.SelectStmt); ok && !bindRecord.Bindings[0].Hint.ContainTableHint(plannercore.HintReadFromStorage) {
sessVars.StmtCtx.StmtHints = originStmtHints
Expand Down

0 comments on commit 18381f9

Please sign in to comment.