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/time: support CURRENT_TIME(), CURTIME() #755

Merged
merged 7 commits into from
Dec 21, 2015
Merged

builtin/time: support CURRENT_TIME(), CURTIME() #755

merged 7 commits into from
Dec 21, 2015

Conversation

chenyanzhe
Copy link
Contributor

This is for issue #236. The change adds two time functions: CURRENT_TIME and CURTIME.

@ngaut
Copy link
Member

ngaut commented Dec 19, 2015

Thanks @yanzhe-chen
Please add some tests.

@chenyanzhe
Copy link
Contributor Author

@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?

@ngaut
Copy link
Member

ngaut commented Dec 19, 2015

@yanzhe-chen For example, these code are just thoughts:
min := time.Now()
sqlNow := select CURTIME
assert(min < sqlNow < time.Now())

@chenyanzhe
Copy link
Contributor Author

@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"
Copy link
Member

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.

Copy link
Contributor Author

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.

@chenyanzhe
Copy link
Contributor Author

@ngaut @siddontang I have added the missing test and changed the variable name as suggested. Please review the request again.

@ngaut
Copy link
Member

ngaut commented Dec 20, 2015

Good, Please add some unit tests in parser/parser_test.go TestBuiltin.
https://github.com/pingcap/tidb/blob/master/parser/parser_test.go#L329

@@ -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) {
Copy link
Member

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)
Copy link
Member

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.

@chenyanzhe
Copy link
Contributor Author

@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 mysql.Duration instead and added string length checks in test.

@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
Copy link
Member

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

@chenyanzhe
Copy link
Contributor Author

@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. curdate and current_date). Shall we create a new issue to fix these inconsistent implementations?

@ngaut
Copy link
Member

ngaut commented Dec 20, 2015

Good catch! @yanzhe-chen
Please feel free to fire issues.

@coocood
Copy link
Member

coocood commented Dec 20, 2015

LGTM
@yanzhe-chen Good job!

"current_timestamp": {builtinNow, 0, 1, false, false},
"curtime": {builtinCurrentTime, 0, 0, false, false},
Copy link
Member

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.
@chenyanzhe
Copy link
Contributor Author

@siddontang Thanks for catching that! Fix it.

@chenyanzhe chenyanzhe closed this Dec 20, 2015
@chenyanzhe chenyanzhe reopened this Dec 20, 2015
"current_timestamp": {builtinNow, 0, 1, false, false},
"curtime": {builtinCurrentTime, 0, 1, false, false},
"date": {builtinDate, 8, 8, true, false},
Copy link
Contributor Author

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!

@ngaut
Copy link
Member

ngaut commented Dec 20, 2015

Good catch, It's bug which describe here #758

@chenyanzhe
Copy link
Contributor Author

@ngaut Nice, and I tried to fix this issue and fired a new pull request here. Please review it.

@chenyanzhe
Copy link
Contributor Author

@siddontang @ngaut @shenli PTAL if there is anything to improve about this pull request or we can just merge and close this pull.

@ngaut
Copy link
Member

ngaut commented Dec 21, 2015

LGTM, thanks @yanzhe-chen

ngaut added a commit that referenced this pull request Dec 21, 2015
builtin/time: support CURRENT_TIME(), CURTIME()
@ngaut ngaut merged commit 9bc1ea2 into pingcap:master Dec 21, 2015
@sre-bot sre-bot added the contribution This PR is from a community contributor. label Dec 18, 2019
xhebox pushed a commit to xhebox/tidb that referenced this pull request Sep 28, 2021
)

* add UnionStmt and ShowStmt as readonly

* fix UnionStmt
xhebox pushed a commit to xhebox/tidb that referenced this pull request Oct 8, 2021
)

* add UnionStmt and ShowStmt as readonly

* fix UnionStmt
ti-chi-bot pushed a commit that referenced this pull request Oct 9, 2021
* add UnionStmt and ShowStmt as readonly

* fix UnionStmt
rleungx pushed a commit to rleungx/tidb that referenced this pull request Feb 26, 2024
)

* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution This PR is from a community contributor.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants