Skip to content

Commit

Permalink
planner: not convert to TableDual if empty range is derived from defe…
Browse files Browse the repository at this point in the history
…rred constants (#7808)
  • Loading branch information
dbjoa authored and eurekaka committed Oct 16, 2018
1 parent 12c8aed commit 1aa2528
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
43 changes: 43 additions & 0 deletions executor/prepared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,46 @@ func generateBatchSQL(paramCount int) (sql string, paramSlice []interface{}) {
}
return "insert into t values " + strings.Join(placeholders, ","), params
}

func (s *testSuite) TestPreparedIssue7579(c *C) {
orgEnable := plannercore.PreparedPlanCacheEnabled()
orgCapacity := plannercore.PreparedPlanCacheCapacity
defer func() {
plannercore.SetPreparedPlanCache(orgEnable)
plannercore.PreparedPlanCacheCapacity = orgCapacity
}()
flags := []bool{false, true}
for _, flag := range flags {
plannercore.SetPreparedPlanCache(flag)
plannercore.PreparedPlanCacheCapacity = 100
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int, index a_idx(a))")
tk.MustExec("insert into t values (1,1), (2,2), (null,3)")

r := tk.MustQuery("select a, b from t order by b asc;")
r.Check(testkit.Rows("1 1", "2 2", "<nil> 3"))

tk.MustExec(`prepare stmt from 'select a, b from t where ? order by b asc'`)

r = tk.MustQuery(`execute stmt using @param;`)
r.Check(nil)

tk.MustExec(`set @param = true`)
r = tk.MustQuery(`execute stmt using @param;`)
r.Check(testkit.Rows("1 1", "2 2", "<nil> 3"))

tk.MustExec(`set @param = false`)
r = tk.MustQuery(`execute stmt using @param;`)
r.Check(nil)

tk.MustExec(`set @param = 1`)
r = tk.MustQuery(`execute stmt using @param;`)
r.Check(testkit.Rows("1 1", "2 2", "<nil> 3"))

tk.MustExec(`set @param = 0`)
r = tk.MustQuery(`execute stmt using @param;`)
r.Check(nil)
}
}
2 changes: 1 addition & 1 deletion planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (ds *DataSource) tryToGetMemTask(prop *property.PhysicalProperty) (task tas
// tryToGetDualTask will check if the push down predicate has false constant. If so, it will return table dual.
func (ds *DataSource) tryToGetDualTask() (task, error) {
for _, cond := range ds.pushedDownConds {
if _, ok := cond.(*expression.Constant); ok {
if con, ok := cond.(*expression.Constant); ok && con.DeferredExpr == nil {
result, err := expression.EvalBool(ds.ctx, []expression.Expression{cond}, chunk.Row{})
if err != nil {
return nil, errors.Trace(err)
Expand Down
2 changes: 1 addition & 1 deletion planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func (b *PlanBuilder) buildSelection(p LogicalPlan, where ast.ExprNode, AggMappe
}
cnfItems := expression.SplitCNFItems(expr)
for _, item := range cnfItems {
if con, ok := item.(*expression.Constant); ok {
if con, ok := item.(*expression.Constant); ok && con.DeferredExpr == nil {
ret, err := expression.EvalBool(b.ctx, expression.CNFExprs{con}, chunk.Row{})
if err != nil || ret {
continue
Expand Down

0 comments on commit 1aa2528

Please sign in to comment.