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: maintain DeferredExpr in aggressive constant folding. #7915

Merged
merged 9 commits into from
Oct 17, 2018
17 changes: 11 additions & 6 deletions expression/constant_fold.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package expression

import (
"github.com/pingcap/tidb/ast"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -118,10 +119,10 @@ func foldConstant(expr Expression) (Expression, bool) {
return expr, isDeferredConst
}
if value.IsNull() {
return &Constant{Value: value, RetType: x.RetType}, false
return foldedConstForScalarFunc(value, x, isDeferredConst), isDeferredConst
}
if isTrue, err := value.ToBool(sc); err == nil && isTrue == 0 {
return &Constant{Value: value, RetType: x.RetType}, false
return foldedConstForScalarFunc(value, x, isDeferredConst), isDeferredConst
}
return expr, isDeferredConst
}
Expand All @@ -130,10 +131,7 @@ func foldConstant(expr Expression) (Expression, bool) {
log.Warnf("fold constant %s: %s", x.ExplainInfo(), err.Error())
return expr, isDeferredConst
}
if isDeferredConst {
return &Constant{Value: value, RetType: x.RetType, DeferredExpr: x}, true
}
return &Constant{Value: value, RetType: x.RetType}, false
return foldedConstForScalarFunc(value, x, isDeferredConst), isDeferredConst
case *Constant:
if x.DeferredExpr != nil {
value, err := x.DeferredExpr.Eval(chunk.Row{})
Expand All @@ -146,3 +144,10 @@ func foldConstant(expr Expression) (Expression, bool) {
}
return expr, false
}

func foldedConstForScalarFunc(value types.Datum, sf *ScalarFunction, isDeferred bool) *Constant {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, this function is somewhat wired.
Can we put this in constant.go and name it as NewConstant()

if isDeferred {
return &Constant{Value: value, RetType: sf.RetType, DeferredExpr: sf}
}
return &Constant{Value: value, RetType: sf.RetType}
}
28 changes: 28 additions & 0 deletions expression/constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,31 @@ func (*testExpressionSuite) TestConstantFolding(c *C) {
c.Assert(newConds.String(), Equals, tt.result, Commentf("different for expr %s", tt.condition))
}
}

func (*testExpressionSuite) TestDeferredExprNullConstantFold(c *C) {
defer testleak.AfterTest(c)()
nullConst := &Constant{
Value: types.NewDatum(nil),
RetType: types.NewFieldType(mysql.TypeTiny),
DeferredExpr: Null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Null is really confusing...
I thought it is nil when seeing at first.

}
tests := []struct {
condition Expression
deferred string
}{
{
condition: newFunction(ast.LT, newColumn(0), nullConst),
deferred: "lt(test.t.0, <nil>)",
},
}
for _, tt := range tests {
comment := Commentf("different for expr %s", tt.condition)
sf, ok := tt.condition.(*ScalarFunction)
c.Assert(ok, IsTrue, comment)
sf.GetCtx().GetSessionVars().StmtCtx.InNullRejectCheck = true
newConds := FoldConstant(tt.condition)
newConst, ok := newConds.(*Constant)
c.Assert(ok, IsTrue, comment)
c.Assert(newConst.DeferredExpr.String(), Equals, tt.deferred, comment)
}
}