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

evaluator, parser: support UTC_DATE() #1045

Merged
merged 2 commits into from
Apr 5, 2016
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
1 change: 1 addition & 0 deletions evaluator/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ var Funcs = map[string]Func{
"now": {builtinNow, 0, 1},
"second": {builtinSecond, 1, 1},
"sysdate": {builtinSysDate, 0, 1},
"utc_date": {builtinUTCDate, 0, 0},
"week": {builtinWeek, 1, 2},
"weekday": {builtinWeekDay, 1, 1},
"weekofyear": {builtinWeekOfYear, 1, 1},
Expand Down
10 changes: 10 additions & 0 deletions evaluator/builtin_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,16 @@ func builtinCurrentTime(args []types.Datum, _ context.Context) (d types.Datum, e
return convertToDuration(d, fsp)
}

// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_utc-date
func builtinUTCDate(args []types.Datum, _ context.Context) (d types.Datum, err error) {
year, month, day := time.Now().UTC().Date()
t := mysql.Time{
Time: time.Date(year, month, day, 0, 0, 0, 0, time.UTC),
Type: mysql.TypeDate, Fsp: mysql.UnspecifiedFsp}
d.SetMysqlTime(t)
return d, nil
}

// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_extract
func builtinExtract(args []types.Datum, _ context.Context) (d types.Datum, err error) {
unit := args[0].GetString()
Expand Down
8 changes: 8 additions & 0 deletions evaluator/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,14 @@ func (s *testEvaluatorSuite) TestCurrentTime(c *C) {
c.Assert(err, NotNil)
}

func (s *testEvaluatorSuite) TestUTCDate(c *C) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add another case like:
mysql> select utc_date + 1;
+--------------+
| utc_date + 1 |
+--------------+
| 20160402 |
+--------------+

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I will take a look, and maybe modification of current_date() is also needed.

last := time.Now().UTC()
v, err := builtinUTCDate(types.MakeDatums(nil), nil)
c.Assert(err, IsNil)
n := v.GetMysqlTime()
c.Assert(n.String(), GreaterEqual, last.Format(mysql.DateFormat))
}

func (s *testEvaluatorSuite) TestDateArith(c *C) {
ctx := mock.NewContext()

Expand Down
Loading