Skip to content

Commit

Permalink
expression: maintain DeferredExpr in aggressive constant folding. (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
eurekaka authored and zz-jason committed Oct 18, 2018
1 parent 0f587c3 commit 3e3b905
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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,
}
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)
}
}

0 comments on commit 3e3b905

Please sign in to comment.