Skip to content

Commit

Permalink
expression: rewrite builtin function: TO_DAYS (pingcap#4161)
Browse files Browse the repository at this point in the history
  • Loading branch information
spongedu authored and hanfei1991 committed Aug 14, 2017
1 parent a00dc40 commit 16bb0ec
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
33 changes: 16 additions & 17 deletions expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2803,35 +2803,34 @@ func (c *toDaysFunctionClass) getFunction(args []Expression, ctx context.Context
if err := c.verifyArgs(args); err != nil {
return nil, errors.Trace(err)
}
sig := &builtinToDaysSig{newBaseBuiltinFunc(args, ctx)}
bf, err := newBaseBuiltinFuncWithTp(args, ctx, tpInt, tpTime)
if err != nil {
return nil, errors.Trace(err)
}
sig := &builtinToDaysSig{baseIntBuiltinFunc{bf}}
return sig.setSelf(sig), nil
}

type builtinToDaysSig struct {
baseBuiltinFunc
baseIntBuiltinFunc
}

// eval evals a builtinToDaysSig.
// evalInt evals a builtinToDaysSig.
// 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) {
args, err := b.evalArgs(row)
if err != nil {
return d, errors.Trace(err)
}
if args[0].IsNull() {
return
}
func (b *builtinToDaysSig) evalInt(row []types.Datum) (int64, bool, error) {
sc := b.ctx.GetSessionVars().StmtCtx
date, err := convertDatumToTime(sc, args[0])
arg, isNull, err := b.args[0].EvalTime(row, sc)
if isNull || err != nil {
return 0, true, errorOrWarning(err, b.ctx)
}
if err != nil {
return d, errorOrWarning(err, b.ctx)
return 0, true, errorOrWarning(err, b.ctx)
}
ret := types.TimestampDiff("DAY", types.ZeroDate, date)
ret := types.TimestampDiff("DAY", types.ZeroDate, arg)
if ret == 0 {
return d, errorOrWarning(types.ErrInvalidTimeFormat, b.ctx)
return 0, true, errorOrWarning(types.ErrInvalidTimeFormat, b.ctx)
}
d.SetInt64(ret)
return
return ret, false, nil
}

type toSecondsFunctionClass struct {
Expand Down
2 changes: 2 additions & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,7 @@ func (s *testEvaluatorSuite) TestToDays(c *C) {
for _, test := range tests {
t := []types.Datum{types.NewDatum(test.param)}
f, err := fc.getFunction(datumsToConstants(t), s.ctx)
c.Assert(f.isDeterministic(), IsTrue)
c.Assert(err, IsNil)
d, err := f.eval(nil)
c.Assert(err, IsNil)
Expand All @@ -1498,6 +1499,7 @@ func (s *testEvaluatorSuite) TestToDays(c *C) {
for _, i := range testsNull {
t := []types.Datum{types.NewDatum(i)}
f, err := fc.getFunction(datumsToConstants(t), s.ctx)
c.Assert(f.isDeterministic(), IsTrue)
c.Assert(err, IsNil)
d, err := f.eval(nil)
c.Assert(err, IsNil)
Expand Down
2 changes: 2 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,8 @@ func (s *testIntegrationSuite) TestTimeBuiltin(c *C) {
result.Check(testkit.Rows("<nil>"))
result = tk.MustQuery("select timediff('2014-1-2 12:00:00', '2014-1-1 12:00:00');")
result.Check(testkit.Rows("24:00:00"))
result = tk.MustQuery("select to_days(950501), to_days('2007-10-07'), to_days('2007-10-07 00:00:59'), to_days('0000-01-01')")
result.Check(testkit.Rows("728779 733321 733321 1"))

// fixed issue #3986
tk.MustExec("SET SQL_MODE='NO_ENGINE_SUBSTITUTION';")
Expand Down
7 changes: 7 additions & 0 deletions plan/typeinfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (s *testPlanSuite) TestInferType(c *C) {
tests = append(tests, s.createTestCase4CompareFuncs()...)
tests = append(tests, s.createTestCase4Miscellaneous()...)
tests = append(tests, s.createTestCase4OpFuncs()...)
tests = append(tests, s.createTestCase4TimeFuncs()...)

for _, tt := range tests {
ctx := testKit.Se.(context.Context)
Expand Down Expand Up @@ -689,3 +690,9 @@ func (s *testPlanSuite) createTestCase4OpFuncs() []typeInferTestCase {
{"1844674.1 is false", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, 1, 0},
}
}

func (s *testPlanSuite) createTestCase4TimeFuncs() []typeInferTestCase {
return []typeInferTestCase{
{"to_days(c_char)", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, 20, 0},
}
}

0 comments on commit 16bb0ec

Please sign in to comment.