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 quarter built-in function #2919

Merged
merged 2 commits into from
Mar 24, 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
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