-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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/time: support CURRENT_TIME(), CURTIME() #755
Conversation
For github issue #236.
Thanks @yanzhe-chen |
@ngaut I am not sure how to test these two functions, since their results are run-time decided and accept no inputs. Could you give me some hints about how to do this? |
@yanzhe-chen For example, these code are just thoughts: |
@ngaut Nice! I'll try that and update this request when done. |
DateFormat = "2006-01-02" | ||
TimeFormat = "2006-01-02 15:04:05" | ||
DateFormat = "2006-01-02" | ||
CurtimeFormat = "15:04:05" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CurTimeFormat or CurrentTimeFormat may be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@siddontang Fine, I will make that change.
@ngaut @siddontang I have added the missing test and changed the variable name as suggested. Please review the request again. |
Good, Please add some unit tests in parser/parser_test.go TestBuiltin. |
@@ -309,6 +309,26 @@ func builtinCurrentDate(args []interface{}, ctx map[interface{}]interface{}) (in | |||
Type: mysql.TypeDate, Fsp: 0}, nil | |||
} | |||
|
|||
// See https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_curtime | |||
func builtinCurrentTime(args []interface{}, ctx map[interface{}]interface{}) (interface{}, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use mysql.Duration instead of mysql.Time, or first you can use the mysql.Time to handle the argument, then convert it to mysql.Duration.
mysql.Time is not for duration, only for datetime, timestamp and date.
@@ -692,6 +694,10 @@ year_month {y}{e}{a}{r}_{m}{o}{n}{t}{h} | |||
return curDate | |||
{current_date} lval.item = string(l.val) | |||
return currentDate | |||
{curtime} lval.item = string(l.val) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curtime is not MySQL keyword and can be used as identifier. So it should be added into NotKeywordToken rule in parser.y. Those tokens can be used as Identifier.
https://github.com/pingcap/tidb/blob/master/parser/parser.y#L1685
current_time is a reserved keyword. So it can not be used as identifier. It is ok now.
@ngaut I have added the corresponding tests in parser. By the way, I forgot the the fsp arguments as stated in the MySQL doc so I added them as well. @coocood I used @shenli Thanks for reminding and I am trying to figure out how to fix that. Please review it again and thanks for your patient help. |
@@ -2137,6 +2139,22 @@ FunctionCallNonKeyword: | |||
{ | |||
$$ = &ast.FuncCallExpr{FnName: model.NewCIStr($1.(string))} | |||
} | |||
| "CURTIME" FuncDatetimePrec |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As "CURTIME" is synonym of "CURRENT_TIME", we can implement one of them, and parse the other one to the same type of token in scanner.l
, like this PR:
#728
… at scanner part.
@coocood Thanks for your hint and I have made that change. BTW I have found some other synonyms in code that are not handled in this way (e.g. |
Good catch! @yanzhe-chen |
LGTM |
"current_timestamp": {builtinNow, 0, 1, false, false}, | ||
"curtime": {builtinCurrentTime, 0, 0, false, false}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the max arg should be 1. 0 means no argument.
The max arg should be 1, which means accept one argument.
@siddontang Thanks for catching that! Fix it. |
"current_timestamp": {builtinNow, 0, 1, false, false}, | ||
"curtime": {builtinCurrentTime, 0, 1, false, false}, | ||
"date": {builtinDate, 8, 8, true, false}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@siddontang Why builtinDate
's minArgs
and maxArgs
are both set to 8
? DATE
should accept exactly one argument in the MySQL doc (DATE(expr)
).
BTW, previously when I set current_time
and curtime
's maxArgs
to 0
, it can still pass all the tests. This makes me doubt whether this kind of checks really work.
Could you help me explain these? Thanks!
Good catch, It's bug which describe here #758 |
@siddontang @ngaut @shenli PTAL if there is anything to improve about this pull request or we can just merge and close this pull. |
LGTM, thanks @yanzhe-chen |
builtin/time: support CURRENT_TIME(), CURTIME()
* add UnionStmt and ShowStmt as readonly * fix UnionStmt
) * fix update keyspace config safe point version Signed-off-by: [email protected] <[email protected]> * fix update keyspace config safe point version Signed-off-by: [email protected] <[email protected]> * fix update keyspace config safe point version Signed-off-by: [email protected] <[email protected]> * fix update keyspace config safe point version Signed-off-by: [email protected] <[email protected]> --------- Signed-off-by: [email protected] <[email protected]>
This is for issue #236. The change adds two time functions:
CURRENT_TIME
andCURTIME
.