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 3 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
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