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 5 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
46 changes: 46 additions & 0 deletions executor/explain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,49 @@ func (s *testSuite1) checkMemoryInfo(c *C, tk *testkit.TestKit, sql string) {
}
}
}

func (s *testSuite4) TestExplainSortRand(c *C) {
eurekaka marked this conversation as resolved.
Show resolved Hide resolved
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);")

tk.MustQuery("explain select a from t order by rand()").Check(testkit.Rows(
"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",
))

tk.MustQuery("explain select a, b from t order by abs(2)").Check(testkit.Rows(
"TableReader_8 10000.00 root data:TableScan_7",
"└─TableScan_7 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo"))

tk.MustQuery("explain select a from t order by abs(rand())+1").Check(testkit.Rows(
"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",
))
}

func (s *testSuite4) TestExplainSortCorrelatedColumn(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1;")
tk.MustExec("create table t1(a int, b int);")
tk.MustExec("drop table if exists t2;")
tk.MustExec("create table t2(a int, b int);")

tk.MustQuery("explain select * from t1 where t1.a in (select t2.a as a from t2 where t2.b > t1.b order by t1.b);").Check(testkit.Rows(
"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",
))
}
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 @@ -105,6 +105,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 @@ -635,22 +635,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 @@ -664,7 +664,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