From e997d38e367c202c418fce0ea77b3686b43a495b Mon Sep 17 00:00:00 2001 From: wjhuang2016 Date: Thu, 29 Aug 2019 13:04:27 +0800 Subject: [PATCH] fix sleep --- cmd/explaintest/r/select.result | 5 +++++ cmd/explaintest/t/select.test | 3 +++ planner/core/rule_column_pruning.go | 12 ++++++------ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/cmd/explaintest/r/select.result b/cmd/explaintest/r/select.result index bce3ecf8ba436..7d76d67db0435 100644 --- a/cmd/explaintest/r/select.result +++ b/cmd/explaintest/r/select.result @@ -432,3 +432,8 @@ Projection_7 10000.00 root 6_aux_0 │ └─TableScan_9 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo └─TableReader_12 10000.00 root data:TableScan_11 └─TableScan_11 10000.00 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo +explain select 1 from (select sleep(1)) t; +id count task operator info +Projection_4 1.00 root 1 +└─Projection_5 1.00 root sleep(1) + └─TableDual_6 1.00 root rows:1 diff --git a/cmd/explaintest/t/select.test b/cmd/explaintest/t/select.test index a5b04f20f463b..6b9da21ff59f2 100644 --- a/cmd/explaintest/t/select.test +++ b/cmd/explaintest/t/select.test @@ -203,3 +203,6 @@ explain select a in (select a+b from t t2 where t2.b = t1.b) from t t1; drop table t; create table t(a int not null, b int); explain select a in (select a from t t2 where t2.b = t1.b) from t t1; + +# test sleep in subquery +explain select 1 from (select sleep(1)) t; diff --git a/planner/core/rule_column_pruning.go b/planner/core/rule_column_pruning.go index 2d8d3cfaf6073..25926ae075db9 100644 --- a/planner/core/rule_column_pruning.go +++ b/planner/core/rule_column_pruning.go @@ -53,17 +53,17 @@ func getUsedList(usedCols []*expression.Column, schema *expression.Schema) ([]bo return used, nil } -// exprHasSetVar checks if the expression has SetVar function. -func exprHasSetVar(expr expression.Expression) bool { +// exprHasSetVarOrSleep checks if the expression has SetVar function or Sleep function. +func exprHasSetVarOrSleep(expr expression.Expression) bool { scalaFunc, isScalaFunc := expr.(*expression.ScalarFunction) if !isScalaFunc { return false } - if scalaFunc.FuncName.L == ast.SetVar { + if scalaFunc.FuncName.L == ast.SetVar || scalaFunc.FuncName.L == ast.Sleep { return true } for _, arg := range scalaFunc.GetArgs() { - if exprHasSetVar(arg) { + if exprHasSetVarOrSleep(arg) { return true } } @@ -71,7 +71,7 @@ func exprHasSetVar(expr expression.Expression) bool { } // PruneColumns implements LogicalPlan interface. -// If any expression has SetVar functions, we do not prune it. +// If any expression has SetVar function or Sleep function, we do not prune it. func (p *LogicalProjection) PruneColumns(parentUsedCols []*expression.Column) error { child := p.children[0] used, err := getUsedList(parentUsedCols, p.schema) @@ -80,7 +80,7 @@ func (p *LogicalProjection) PruneColumns(parentUsedCols []*expression.Column) er } for i := len(used) - 1; i >= 0; i-- { - if !used[i] && !exprHasSetVar(p.Exprs[i]) { + if !used[i] && !exprHasSetVarOrSleep(p.Exprs[i]) { p.schema.Columns = append(p.schema.Columns[:i], p.schema.Columns[i+1:]...) p.Exprs = append(p.Exprs[:i], p.Exprs[i+1:]...) }