Skip to content

Commit

Permalink
cherry pick #16261 to release-3.0
Browse files Browse the repository at this point in the history
Signed-off-by: sre-bot <[email protected]>
  • Loading branch information
eurekaka committed Apr 23, 2020
1 parent ff1989e commit 407bcdc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type Expression interface {
// ConstItem checks if this expression is constant item, regardless of query evaluation state.
// An expression is constant item if it:
// refers no tables.
// refers no correlated column.
// refers no subqueries that refers any tables.
// refers no non-deterministic functions.
// refers no statement parameters.
Expand Down
6 changes: 2 additions & 4 deletions expression/function_traits.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ var IllegalFunctions4GeneratedColumns = map[string]struct{}{
ast.Version: {},
}

// DeferredFunctions stores non-deterministic functions, which can be deferred only when the plan cache is enabled.
// DeferredFunctions stores functions which are foldable but should be deferred as well when plan cache is enabled.
// Note that, these functions must be foldable at first place, i.e, they are not in `unFoldableFunctions`.
var DeferredFunctions = map[string]struct{}{
ast.Now: {},
ast.CurrentTimestamp: {},
Expand All @@ -103,12 +104,9 @@ var DeferredFunctions = map[string]struct{}{
ast.CurrentTime: {},
ast.UTCTimestamp: {},
ast.UnixTimestamp: {},
ast.Sysdate: {},
ast.Curdate: {},
ast.CurrentDate: {},
ast.UTCDate: {},
ast.Rand: {},
ast.UUID: {},
}

// inequalFunctions stores functions which cannot be propagated from column equal condition.
Expand Down
21 changes: 21 additions & 0 deletions expression/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,27 @@ func BuildNotNullExpr(ctx sessionctx.Context, expr Expression) Expression {
return notNull
}

// IsRuntimeConstExpr checks if a expr can be treated as a constant in **executor**.
func IsRuntimeConstExpr(expr Expression) bool {
switch x := expr.(type) {
case *ScalarFunction:
if _, ok := unFoldableFunctions[x.FuncName.L]; ok {
return false
}
for _, arg := range x.GetArgs() {
if !IsRuntimeConstExpr(arg) {
return false
}
}
return true
case *Column:
return false
case *Constant, *CorrelatedColumn:
return true
}
return false
}

// isMutableEffectsExpr checks if expr contains function which is mutable or has side effects.
func isMutableEffectsExpr(expr Expression) bool {
switch x := expr.(type) {
Expand Down
2 changes: 1 addition & 1 deletion planner/core/rule_column_pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (ls *LogicalSort) PruneColumns(parentUsedCols []*expression.Column) error {
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.IsRuntimeConstExpr(ls.ByItems[i].Expr) {
continue
}
ls.ByItems = append(ls.ByItems[:i], ls.ByItems[i+1:]...)
Expand Down

0 comments on commit 407bcdc

Please sign in to comment.