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
6 changes: 6 additions & 0 deletions expression/constant_fold.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,15 @@ func foldConstant(expr Expression) (Expression, bool) {
return expr, isDeferredConst
}
if value.IsNull() {
if isDeferredConst {
return &Constant{Value: value, RetType: x.RetType, DeferredExpr: x}, true
}
return &Constant{Value: value, RetType: x.RetType}, false
}
if isTrue, err := value.ToBool(sc); err == nil && isTrue == 0 {
if isDeferredConst {
return &Constant{Value: value, RetType: x.RetType, DeferredExpr: x}, true
}
return &Constant{Value: value, RetType: x.RetType}, false
}
return expr, isDeferredConst
Expand Down
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)
}
}