Skip to content

Commit

Permalink
builtin: add quarter built-in function (#2919)
Browse files Browse the repository at this point in the history
Implement the QUARTER built-in function.
  • Loading branch information
codeworm96 authored and coocood committed Mar 24, 2017
1 parent ed56d82 commit bd0220c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
30 changes: 29 additions & 1 deletion expression/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -1729,7 +1729,35 @@ type builtinQuarterSig struct {

// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_quarter
func (b *builtinQuarterSig) eval(row []types.Datum) (d types.Datum, err error) {
return d, errFunctionNotExists.GenByArgs("QUARTER")
args, err := b.evalArgs(row)
if err != nil {
return types.Datum{}, errors.Trace(err)
}

d, err = builtinMonth(args, b.ctx)
if err != nil || d.IsNull() {
return d, errors.Trace(err)
}

mon := int(d.GetInt64())
switch mon {
case 0:
// An undocumented behavior of the mysql implementation
d.SetInt64(0)
case 1, 2, 3:
d.SetInt64(1)
case 4, 5, 6:
d.SetInt64(2)
case 7, 8, 9:
d.SetInt64(3)
case 10, 11, 12:
d.SetInt64(4)
default:
d.SetNull()
return d, errors.Errorf("no quarter for invalid month: %d.", mon)
}

return d, nil
}

type secToTimeFunctionClass struct {
Expand Down
36 changes: 36 additions & 0 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,3 +1013,39 @@ func (s *testEvaluatorSuite) TestTimestamp(c *C) {
c.Assert(err, IsNil)
c.Assert(d.Kind(), Equals, types.KindNull)
}

func (s *testEvaluatorSuite) TestQuarter(c *C) {
tests := []struct {
t string
expect int64
}{
// Test case from https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_quarter
{"2008-04-01", 2},
// Test case for boundary values
{"2008-01-01", 1},
{"2008-03-31", 1},
{"2008-06-30", 2},
{"2008-07-01", 3},
{"2008-09-30", 3},
{"2008-10-01", 4},
{"2008-12-31", 4},
// Test case for month 0
{"2008-00-01", 0},
}
fc := funcs["quarter"]
for _, test := range tests {
arg := types.NewStringDatum(test.t)
f, err := fc.getFunction(datumsToConstants([]types.Datum{arg}), s.ctx)
c.Assert(err, IsNil)
result, err := f.eval(nil)
c.Assert(err, IsNil)
c.Assert(result.GetInt64(), Equals, test.expect)
}

// test invalid input
argInvalid := types.NewStringDatum("2008-13-01")
f, err := fc.getFunction(datumsToConstants([]types.Datum{argInvalid}), s.ctx)
c.Assert(err, IsNil)
result, err := f.eval(nil)
c.Assert(result.IsNull(), IsTrue)
}
2 changes: 1 addition & 1 deletion plan/typeinferer.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,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":
"found_rows", "length", "extract", "locate", "unix_timestamp", "quarter":
tp = types.NewFieldType(mysql.TypeLonglong)
case "now", "sysdate", "current_timestamp", "utc_timestamp":
tp = types.NewFieldType(mysql.TypeDatetime)
Expand Down
1 change: 1 addition & 0 deletions plan/typeinferer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func (ts *testTypeInferrerSuite) TestInferType(c *C) {
{"day('2009-12-31 23:59:59.000010')", mysql.TypeLonglong, charset.CharsetBin},
{"week('2009-12-31 23:59:59.000010')", mysql.TypeLonglong, charset.CharsetBin},
{"month('2009-12-31 23:59:59.000010')", mysql.TypeLonglong, charset.CharsetBin},
{"quarter('2009-12-31 23:59:59.000010')", mysql.TypeLonglong, charset.CharsetBin},
{"year('2009-12-31 23:59:59.000010')", mysql.TypeLonglong, charset.CharsetBin},
{"dayofweek('2009-12-31 23:59:59.000010')", mysql.TypeLonglong, charset.CharsetBin},
{"dayofmonth('2009-12-31 23:59:59.000010')", mysql.TypeLonglong, charset.CharsetBin},
Expand Down

0 comments on commit bd0220c

Please sign in to comment.