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

Builtin: add builtin function 'TO_DAYS' #2983

Merged
merged 12 commits into from
Apr 7, 2017
19 changes: 18 additions & 1 deletion expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2051,7 +2051,24 @@ type builtinToDaysSig struct {

// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_to-days
func (b *builtinToDaysSig) eval(row []types.Datum) (d types.Datum, err error) {
return d, errFunctionNotExists.GenByArgs("to_days")
args, err := b.evalArgs(row)
if err != nil {
return d, errors.Trace(err)
}
if args[0].IsNull() {
return d, nil
}
sc := b.ctx.GetSessionVars().StmtCtx
date, err := convertDatumToTime(sc, args[0])
if err != nil {
return d, errorOrWarning(err, b.ctx)
}
ret := types.TimestampDiff("DAY", types.ZeroDate, date)
if ret == 0 {
return d, errorOrWarning(types.ErrInvalidTimeFormat, b.ctx)
}
d.SetInt64(ret)
return d, nil
}

type toSecondsFunctionClass struct {
Expand Down
40 changes: 40 additions & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,46 @@ func (s *testEvaluatorSuite) TestGetFormat(c *C) {
c.Assert(result, Equals, test.expect)
}
}

func (s *testEvaluatorSuite) TestToDays(c *C) {
tests := []struct {
param interface{}
expect int64
}{
{950501, 728779},
{"2007-10-07", 733321},
{"2008-10-07", 733687},
{"08-10-07", 733687},
{"0000-01-01", 1},
{"2007-10-07 00:00:59", 733321},
}

fc := funcs[ast.ToDays]
for _, test := range tests {
t := []types.Datum{types.NewDatum(test.param)}
f, err := fc.getFunction(datumsToConstants(t), s.ctx)
c.Assert(err, IsNil)
d, err := f.eval(nil)
c.Assert(err, IsNil)
c.Assert(d.GetInt64(), Equals, test.expect)
}

testsNull := []interface{}{
"0000-00-00",
"1992-13-00",
"2007-10-07 23:59:61",
123456789}

for _, i := range testsNull {
t := []types.Datum{types.NewDatum(i)}
f, err := fc.getFunction(datumsToConstants(t), s.ctx)
c.Assert(err, IsNil)
d, err := f.eval(nil)
c.Assert(err, IsNil)
c.Assert(d.IsNull(), IsTrue)
}
}

func (s *testEvaluatorSuite) TestTimestampAdd(c *C) {
tests := []struct {
unit string
Expand Down
2 changes: 1 addition & 1 deletion plan/typeinferer.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func (v *typeInferrer) handleFuncCallExpr(x *ast.FuncCallExpr) {
tp = types.NewFieldType(mysql.TypeDatetime)
case "microsecond", "second", "minute", "hour", "day", "week", "month", "year",
"dayofweek", "dayofmonth", "dayofyear", "weekday", "weekofyear", "yearweek", "datediff",
"found_rows", "length", "extract", "locate", "unix_timestamp", "quarter", "is_ipv4":
"found_rows", "length", "extract", "locate", "unix_timestamp", "quarter", "is_ipv4", "to_days":
tp = types.NewFieldType(mysql.TypeLonglong)
case "now", "sysdate", "current_timestamp", "utc_timestamp":
tp = types.NewFieldType(mysql.TypeDatetime)
Expand Down
2 changes: 2 additions & 0 deletions plan/typeinferer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ func (ts *testTypeInferrerSuite) TestInferType(c *C) {
{"sign(null)", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag},
{"unix_timestamp()", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag},
{"unix_timestamp('2015-11-13 10:20:19')", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag},
{"to_days('2015-11-13')", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag},
{"to_days(950501)", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag},
{"floor(1.23)", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag},
{"field('foo', null)", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag},
{"find_in_set('foo', 'foo,bar')", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag},
Expand Down