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: some builtin functions can be folded #4756

Merged
merged 6 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all 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/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ var funcs = map[string]functionClass{
ast.UnixTimestamp: &unixTimestampFunctionClass{baseFunctionClass{ast.UnixTimestamp, 0, 1}},
ast.UTCDate: &utcDateFunctionClass{baseFunctionClass{ast.UTCDate, 0, 0}},
ast.UTCTime: &utcTimeFunctionClass{baseFunctionClass{ast.UTCTime, 0, 1}},
ast.UTCTimestamp: &utcTimestampFunctionClass{baseFunctionClass{ast.UnixTimestamp, 0, 1}},
ast.UTCTimestamp: &utcTimestampFunctionClass{baseFunctionClass{ast.UTCTimestamp, 0, 1}},
ast.Week: &weekFunctionClass{baseFunctionClass{ast.Week, 1, 2}},
ast.Weekday: &weekDayFunctionClass{baseFunctionClass{ast.Weekday, 1, 1}},
ast.WeekOfYear: &weekOfYearFunctionClass{baseFunctionClass{ast.WeekOfYear, 1, 1}},
Expand Down
6 changes: 0 additions & 6 deletions expression/builtin_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ func (c *databaseFunctionClass) getFunction(ctx context.Context, args []Expressi
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETString)
bf.tp.Flen = 64
bf.foldable = false
sig := &builtinDatabaseSig{bf}
return sig.setSelf(sig), nil
}
Expand Down Expand Up @@ -119,7 +118,6 @@ func (c *currentUserFunctionClass) getFunction(ctx context.Context, args []Expre
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETString)
bf.tp.Flen = 64
bf.foldable = false
sig := &builtinCurrentUserSig{bf}
return sig.setSelf(sig), nil
}
Expand Down Expand Up @@ -149,7 +147,6 @@ func (c *userFunctionClass) getFunction(ctx context.Context, args []Expression)
return nil, err
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETString)
bf.foldable = false
bf.tp.Flen = 64
sig := &builtinUserSig{bf}
return sig.setSelf(sig), nil
Expand Down Expand Up @@ -179,7 +176,6 @@ func (c *connectionIDFunctionClass) getFunction(ctx context.Context, args []Expr
return nil, err
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETInt)
bf.foldable = false
bf.tp.Flag |= mysql.UnsignedFlag
sig := &builtinConnectionIDSig{bf}
return sig.setSelf(sig), nil
Expand Down Expand Up @@ -212,7 +208,6 @@ func (c *lastInsertIDFunctionClass) getFunction(ctx context.Context, args []Expr
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETInt, argsTp...)
bf.tp.Flag |= mysql.UnsignedFlag
bf.foldable = false

if len(args) == 1 {
sig = &builtinLastInsertIDWithIDSig{bf}
Expand Down Expand Up @@ -260,7 +255,6 @@ func (c *versionFunctionClass) getFunction(ctx context.Context, args []Expressio
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETString)
bf.tp.Flen = 64
bf.foldable = false
sig := &builtinVersionSig{bf}
return sig.setSelf(sig), nil
}
Expand Down
12 changes: 6 additions & 6 deletions expression/builtin_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *testEvaluatorSuite) TestDatabase(c *C) {
ctx := mock.NewContext()
f, err := fc.getFunction(ctx, nil)
c.Assert(err, IsNil)
c.Assert(f.canBeFolded(), IsFalse)
c.Assert(f.canBeFolded(), IsTrue)
d, err := f.eval(nil)
c.Assert(err, IsNil)
c.Assert(d.Kind(), Equals, types.KindNull)
Expand Down Expand Up @@ -76,7 +76,7 @@ func (s *testEvaluatorSuite) TestUser(c *C) {
fc := funcs[ast.User]
f, err := fc.getFunction(ctx, nil)
c.Assert(err, IsNil)
c.Assert(f.canBeFolded(), IsFalse)
c.Assert(f.canBeFolded(), IsTrue)
d, err := f.eval(nil)
c.Assert(err, IsNil)
c.Assert(d.GetString(), Equals, "root@localhost")
Expand All @@ -91,7 +91,7 @@ func (s *testEvaluatorSuite) TestCurrentUser(c *C) {
fc := funcs[ast.CurrentUser]
f, err := fc.getFunction(ctx, nil)
c.Assert(err, IsNil)
c.Assert(f.canBeFolded(), IsFalse)
c.Assert(f.canBeFolded(), IsTrue)
d, err := f.eval(nil)
c.Assert(err, IsNil)
c.Assert(d.GetString(), Equals, "root@localhost")
Expand All @@ -106,7 +106,7 @@ func (s *testEvaluatorSuite) TestConnectionID(c *C) {
fc := funcs[ast.ConnectionID]
f, err := fc.getFunction(ctx, nil)
c.Assert(err, IsNil)
c.Assert(f.canBeFolded(), IsFalse)
c.Assert(f.canBeFolded(), IsTrue)
d, err := f.eval(nil)
c.Assert(err, IsNil)
c.Assert(d.GetUint64(), Equals, uint64(1))
Expand All @@ -117,7 +117,7 @@ func (s *testEvaluatorSuite) TestVersion(c *C) {
fc := funcs[ast.Version]
f, err := fc.getFunction(s.ctx, nil)
c.Assert(err, IsNil)
c.Assert(f.canBeFolded(), IsFalse)
c.Assert(f.canBeFolded(), IsTrue)
v, err := f.eval(nil)
c.Assert(err, IsNil)
c.Assert(v.GetString(), Equals, mysql.ServerVersion)
Expand Down Expand Up @@ -228,5 +228,5 @@ func (s *testEvaluatorSuite) TestLastInsertID(c *C) {

f, err := funcs[ast.LastInsertId].getFunction(s.ctx, []Expression{Zero})
c.Assert(err, IsNil)
c.Assert(f.canBeFolded(), IsFalse)
c.Assert(f.canBeFolded(), IsTrue)
}
5 changes: 0 additions & 5 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,6 @@ func (c *sysDateFunctionClass) getFunction(ctx context.Context, args []Expressio
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETDatetime, argTps...)
bf.tp.Flen, bf.tp.Decimal = 19, 0
bf.foldable = false

var sig builtinFunc
if len(args) == 1 {
Expand Down Expand Up @@ -1715,7 +1714,6 @@ func (c *currentDateFunctionClass) getFunction(ctx context.Context, args []Expre
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETDatetime)
bf.tp.Flen, bf.tp.Decimal = 10, 0
bf.foldable = false
sig := &builtinCurrentDateSig{bf}
return sig.setSelf(sig), nil
}
Expand Down Expand Up @@ -1891,7 +1889,6 @@ func (c *utcDateFunctionClass) getFunction(ctx context.Context, args []Expressio
}
bf := newBaseBuiltinFuncWithTp(ctx, args, types.ETDatetime)
bf.tp.Flen, bf.tp.Decimal = 10, 0
bf.foldable = false
sig := &builtinUTCDateSig{bf}
return sig.setSelf(sig), nil
}
Expand Down Expand Up @@ -1949,7 +1946,6 @@ func (c *utcTimestampFunctionClass) getFunction(ctx context.Context, args []Expr
} else {
bf.tp.Flen, bf.tp.Decimal = 19, 0
}
bf.foldable = false

var sig builtinFunc
if len(args) == 1 {
Expand Down Expand Up @@ -2852,7 +2848,6 @@ func (c *unixTimestampFunctionClass) getFunction(ctx context.Context, args []Exp
}

bf := newBaseBuiltinFuncWithTp(ctx, args, retTp, argTps...)
bf.foldable = false
bf.tp.Flen = retFLen
bf.tp.Decimal = retDecimal

Expand Down
16 changes: 6 additions & 10 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ func (s *testEvaluatorSuite) TestNowAndUTCTimestamp(c *C) {
return tt
}

for i, x := range []struct {
for _, x := range []struct {
fc functionClass
now func() time.Time
}{
Expand All @@ -702,11 +702,7 @@ func (s *testEvaluatorSuite) TestNowAndUTCTimestamp(c *C) {
} {
f, err := x.fc.getFunction(s.ctx, s.datumsToConstants(nil))
c.Assert(err, IsNil)
if i == 0 {
c.Assert(f.canBeFolded(), IsTrue)
} else {
c.Assert(f.canBeFolded(), IsFalse)
}
c.Assert(f.canBeFolded(), IsTrue)
v, err := f.eval(nil)
ts := x.now()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -925,7 +921,7 @@ func (s *testEvaluatorSuite) TestSysDate(c *C) {
varsutil.SetSessionSystemVar(s.ctx.GetSessionVars(), "timestamp", timezone)
f, err := fc.getFunction(s.ctx, s.datumsToConstants(nil))
c.Assert(err, IsNil)
c.Assert(f.canBeFolded(), IsFalse)
c.Assert(f.canBeFolded(), IsTrue)
v, err := f.eval(nil)
last := time.Now()
c.Assert(err, IsNil)
Expand Down Expand Up @@ -1069,7 +1065,7 @@ func (s *testEvaluatorSuite) TestCurrentDate(c *C) {
fc := funcs[ast.CurrentDate]
f, err := fc.getFunction(mock.NewContext(), s.datumsToConstants(nil))
c.Assert(err, IsNil)
c.Assert(f.canBeFolded(), IsFalse)
c.Assert(f.canBeFolded(), IsTrue)
v, err := f.eval(nil)
c.Assert(err, IsNil)
n := v.GetMysqlTime()
Expand Down Expand Up @@ -1466,7 +1462,7 @@ func (s *testEvaluatorSuite) TestUnixTimestamp(c *C) {
fc := funcs[ast.UnixTimestamp]
f, err := fc.getFunction(s.ctx, nil)
c.Assert(err, IsNil)
c.Assert(f.canBeFolded(), IsFalse)
c.Assert(f.canBeFolded(), IsTrue)
d, err := f.eval(nil)
c.Assert(err, IsNil)
c.Assert(d.GetInt64()-time.Now().Unix(), GreaterEqual, int64(-1))
Expand All @@ -1493,7 +1489,7 @@ func (s *testEvaluatorSuite) TestUnixTimestamp(c *C) {
args = []types.Datum{types.NewDatum(nil)}
f, err = fc.getFunction(s.ctx, s.datumsToConstants(args))
c.Assert(err, IsNil)
c.Assert(f.canBeFolded(), IsFalse)
c.Assert(f.canBeFolded(), IsTrue)
d, err = f.eval(nil)
c.Assert(err, IsNil)
c.Assert(d.IsNull(), Equals, true)
Expand Down
25 changes: 21 additions & 4 deletions plan/cacheable_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,26 @@ func Cacheable(node ast.Node) bool {
return checker.cacheable
}

var nonCacheableFunctions = map[string]struct{}{
ast.Now: {},
ast.CurrentTimestamp: {},
ast.UTCTime: {},
ast.Curtime: {},
ast.CurrentTime: {},
ast.UTCTimestamp: {},
ast.UnixTimestamp: {},
ast.Sysdate: {},
ast.Curdate: {},
ast.CurrentDate: {},
ast.UTCDate: {},
ast.Database: {},
ast.CurrentUser: {},
ast.User: {},
ast.ConnectionID: {},
ast.LastInsertId: {},
ast.Version: {},
}

// cacheableChecker checks whether a query's plan can be cached, querys that:
// 1. have ExistsSubqueryExpr, or
// 2. have VariableExpr
Expand All @@ -45,10 +65,7 @@ func (checker *cacheableChecker) Enter(in ast.Node) (out ast.Node, skipChildren
checker.cacheable = false
return in, true
case *ast.FuncCallExpr:
if node.FnName.L == ast.Now || node.FnName.L == ast.CurrentTimestamp ||
node.FnName.L == ast.UTCTime || node.FnName.L == ast.Curtime ||
node.FnName.L == ast.CurrentTime || node.FnName.L == ast.UTCTimestamp ||
node.FnName.L == ast.UnixTimestamp {
if _, found := nonCacheableFunctions[node.FnName.L]; found {
checker.cacheable = false
return in, true
}
Expand Down
20 changes: 5 additions & 15 deletions plan/cacheable_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,12 @@ func (s *testCacheableSuite) TestCacheable(c *C) {
}
c.Assert(Cacheable(stmt), IsTrue)

whereExpr.FnName = model.NewCIStr("now")
c.Assert(Cacheable(stmt), IsFalse)

whereExpr.FnName = model.NewCIStr("current_timestamp")
c.Assert(Cacheable(stmt), IsFalse)

whereExpr.FnName = model.NewCIStr("utc_time")
c.Assert(Cacheable(stmt), IsFalse)

whereExpr.FnName = model.NewCIStr("curtime")
c.Assert(Cacheable(stmt), IsFalse)

whereExpr.FnName = model.NewCIStr("current_time")
c.Assert(Cacheable(stmt), IsFalse)
for funcName := range nonCacheableFunctions {
whereExpr.FnName = model.NewCIStr(funcName)
c.Assert(Cacheable(stmt), IsFalse)
}

whereExpr.FnName = model.NewCIStr("rand")
whereExpr.FnName = model.NewCIStr(ast.Rand)
c.Assert(Cacheable(stmt), IsTrue)

stmt = &ast.SelectStmt{
Expand Down