Skip to content

Commit

Permalink
planner: don't prune sleep function in LogicalProjection (#11927) (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
wjhuang2016 authored and sre-bot committed Nov 6, 2019
1 parent 724963c commit b5d3248
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 5 additions & 0 deletions cmd/explaintest/r/select.result
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,8 @@ Projection_8 10000.00 root minus(4_window_3, test.t.x)
└─Sort_12 10000.00 root test.t.i:asc
└─TableReader_11 10000.00 root data:TableScan_10
└─TableScan_10 10000.00 cop table:t, range:[0,+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
2 changes: 2 additions & 0 deletions cmd/explaintest/t/select.test
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,5 @@ CREATE TABLE t (id int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
);
explain select row_number() over( partition by i ) - x as rnk from t;

explain select 1 from (select sleep(1)) t;
12 changes: 6 additions & 6 deletions planner/core/rule_column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@ 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
}
}
return false
}

// 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)
Expand All @@ -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:]...)
}
Expand Down

0 comments on commit b5d3248

Please sign in to comment.