Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: fix constItem() and column-pruning in order by clause #11839

Merged
merged 7 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions cmd/explaintest/r/select.result
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,36 @@ 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
drop table if exists t;
create table t(a int, b int);
explain select a from t order by rand();
id count task operator info
Projection_8 10000.00 root test.t.a
└─Sort_4 10000.00 root col_1:asc
└─Projection_9 10000.00 root test.t.a, rand()
└─TableReader_7 10000.00 root data:TableScan_6
└─TableScan_6 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
explain select a, b from t order by abs(2);
id count task operator info
TableReader_8 10000.00 root data:TableScan_7
└─TableScan_7 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
explain select a from t order by abs(rand())+1;
id count task operator info
Projection_8 10000.00 root test.t.a
└─Sort_4 10000.00 root col_1:asc
└─Projection_9 10000.00 root test.t.a, plus(abs(rand()), 1)
└─TableReader_7 10000.00 root data:TableScan_6
└─TableScan_6 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
drop table if exists t1;
create table t1(a int, b int);
drop table if exists t2;
create table t2(a int, b int);
explain select * from t1 where t1.a in (select t2.a as a from t2 where t2.b > t1.b order by t1.b);
id count task operator info
Apply_11 9990.00 root semi join, inner:TableReader_19, equal:[eq(test.t1.a, test.t2.a)]
├─TableReader_14 9990.00 root data:Selection_13
│ └─Selection_13 9990.00 cop not(isnull(test.t1.a))
│ └─TableScan_12 10000.00 cop table:t1, range:[-inf,+inf], keep order:false, stats:pseudo
└─TableReader_19 7992.00 root data:Selection_18
└─Selection_18 7992.00 cop gt(test.t2.b, test.t1.b), not(isnull(test.t2.a))
└─TableScan_17 10000.00 cop table:t2, range:[-inf,+inf], keep order:false, stats:pseudo
14 changes: 14 additions & 0 deletions cmd/explaintest/t/select.test
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,17 @@ 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 order by rand()
drop table if exists t;
create table t(a int, b int);
explain select a from t order by rand();
explain select a, b from t order by abs(2);
explain select a from t order by abs(rand())+1;

# test order by correlated column
drop table if exists t1;
create table t1(a int, b int);
drop table if exists t2;
create table t2(a int, b int);
explain select * from t1 where t1.a in (select t2.a as a from t2 where t2.b > t1.b order by t1.b);
46 changes: 0 additions & 46 deletions executor/sort_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion expression/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (col *CorrelatedColumn) IsCorrelated() bool {

// ConstItem implements Expression interface.
func (col *CorrelatedColumn) ConstItem() bool {
return true
return false
}

// Decorrelate implements Expression interface.
Expand Down
2 changes: 1 addition & 1 deletion expression/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s *testEvaluatorSuite) TestColumn(c *C) {
c.Assert(corCol.Equal(nil, corCol), IsTrue)
c.Assert(corCol.Equal(nil, invalidCorCol), IsFalse)
c.Assert(corCol.IsCorrelated(), IsTrue)
c.Assert(corCol.ConstItem(), IsTrue)
c.Assert(corCol.ConstItem(), IsFalse)
c.Assert(corCol.Decorrelate(schema).Equal(nil, col), IsTrue)
c.Assert(invalidCorCol.Decorrelate(schema).Equal(nil, invalidCorCol), IsTrue)

Expand Down
1 change: 1 addition & 0 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ type Expression interface {
IsCorrelated() bool

// ConstItem checks if this expression is constant item, regardless of query evaluation state.
// A constant item can be eval() when build a plan.
wjhuang2016 marked this conversation as resolved.
Show resolved Hide resolved
// An expression is constant item if it:
// refers no tables.
// refers no subqueries that refers any tables.
Expand Down
10 changes: 5 additions & 5 deletions expression/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,22 +632,22 @@ func BuildNotNullExpr(ctx sessionctx.Context, expr Expression) Expression {
return notNull
}

// isMutableEffectsExpr checks if expr contains function which is mutable or has side effects.
func isMutableEffectsExpr(expr Expression) bool {
// IsMutableEffectsExpr checks if expr contains function which is mutable or has side effects.
func IsMutableEffectsExpr(expr Expression) bool {
switch x := expr.(type) {
case *ScalarFunction:
if _, ok := mutableEffectsFunctions[x.FuncName.L]; ok {
return true
}
for _, arg := range x.GetArgs() {
if isMutableEffectsExpr(arg) {
if IsMutableEffectsExpr(arg) {
return true
}
}
case *Column:
case *Constant:
if x.DeferredExpr != nil {
return isMutableEffectsExpr(x.DeferredExpr)
return IsMutableEffectsExpr(x.DeferredExpr)
}
}
return false
Expand All @@ -661,7 +661,7 @@ func RemoveDupExprs(ctx sessionctx.Context, exprs []Expression) []Expression {
sc := ctx.GetSessionVars().StmtCtx
for _, expr := range exprs {
key := string(expr.HashCode(sc))
if _, ok := exists[key]; !ok || isMutableEffectsExpr(expr) {
if _, ok := exists[key]; !ok || IsMutableEffectsExpr(expr) {
res = append(res, expr)
exists[key] = struct{}{}
}
Expand Down
4 changes: 3 additions & 1 deletion planner/core/rule_column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@ func (la *LogicalAggregation) PruneColumns(parentUsedCols []*expression.Column)
}

// PruneColumns implements LogicalPlan interface.
// If any expression can view as a constant in execution stage, such as correlated column, constant,
// we do prune them. Note that we can't prune the expressions contain non-deterministic functions, such as rand().
func (ls *LogicalSort) PruneColumns(parentUsedCols []*expression.Column) error {
child := ls.children[0]
for i := len(ls.ByItems) - 1; i >= 0; i-- {
cols := expression.ExtractColumns(ls.ByItems[i].Expr)
if len(cols) == 0 {
if !ls.ByItems[i].Expr.ConstItem() {
if expression.IsMutableEffectsExpr(ls.ByItems[i].Expr) {
qw4990 marked this conversation as resolved.
Show resolved Hide resolved
continue
}
ls.ByItems = append(ls.ByItems[:i], ls.ByItems[i+1:]...)
Expand Down