-
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
add ast evaluation for date arith #754
add ast evaluation for date arith #754
Conversation
@hhkbp2 Thank you very much, please format the file you modified with 'go fmt`, otherwise, the Travis CI build would fail. |
@qiuyesuifeng @coocood It's somewhat tricky to handle all the type castings and return type variations. I change a few test cases to adjust the behavior a little bit. 1. type cast from float string to intervalIn mysql:
Since casting float to int will round the float value, but casting string to int (which contains a float value) will truncate the float value. I couldn't find a existing function in 2. loose conversion from string to intIn mysql, it supports a loose conversion from string to int, so we get:
In this PR, the above cases are reported as errors because |
@@ -1375,3 +1375,40 @@ func ExtractTimeValue(unit string, format string) (int64, int64, int64, time.Dur | |||
return 0, 0, 0, 0, errors.Errorf("invalid singel timeunit - %s", unit) | |||
} | |||
} | |||
|
|||
// IsHourMinuteSecondUnit returns true when unit is interval unit with hour, minutes or seconds. | |||
func IsHourMinuteSecondUnit(unit string) bool { |
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.
For go time package, it uses Date() for year, month, and day, uses Clock() for hour, minute and second, so I think here we can use IsClockUnit instead.
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.
Agree. IsClockUnit()
is a better name.
@hhkbp2 The existing test cases are copied from You can reference the old implement to make test cases pass with out changing it. In TiDB, we enforce strict mode, so when data is truncated, it returns error instead of warning. |
@coocood I don't quite understand the evolvement/history of copying code from |
@hhkbp2 The old |
@hhkbp2 Any updates? |
@ngaut Referring to the code in |
PTAL @coocood @siddontang |
func IsClockUnit(unit string) bool { | ||
switch strings.ToUpper(unit) { | ||
case "MICROSECOND", "SECOND", "MINUTE", "HOUR": | ||
fallthrough |
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.
fallthrough
is unnecessary, we can just list the rest of the cases in a new line.
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.
Aha, right.
LGTM |
if mysql.IsClockUnit(v.Unit) { | ||
fieldType = mysql.TypeDatetime | ||
} | ||
resultField = types.NewFieldType(fieldType) |
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.
mybe resultField := types.NewFieldType(mysql.TypeDatetime)
is enough. Don't need the code lines from line 866 to 886.
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.
These lines of code 866 - 886 make the return data type of this function compatible with the counterpart in Mysql, which returns DATE
or DATETIME
due to the different date types of function arguments.
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.
@hhkbp2 Actually, when we pass the argument to the function, the original type of the argument doesn't matter, we can force convert the argument to DATETIME
, then do the calculation.
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.
@coocood It seems to be a choice of how much compatible you would like to keep with Mysql.
I just followed the function spec in mysql docs. It would be OK to take the other force conversion way. It's simpler for both usage and implementation.
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.
@hhkbp2 Converting the argument to DATETIME
is the implementation detail, it makes the code cleaner.
If it doesn't give a different result, we can say it is compatible.
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.
@coocood Precisely speaking, force conversion to DATETIME
would return the same time value but different data type(and different output format in command line), which is not strictly consistent with the function spec in mysql docs.
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.
@hhkbp2 Oh, you are right, but the test case didn't cover it, even if I force convert to DATETIME
, the test still pass. Would you please add test case to cover it.
Thank you!
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.
@coocood The corresponding test cases are added. And all test cases for date arith are re-organized to a better form.
LGTM |
LGTM |
add ast evaluation for date arith
* add Performance Schema Functions format_bytes and format_nano_time Signed-off-by: gauss <[email protected]> * remove a test case Signed-off-by: gauss <[email protected]> Co-authored-by: kennytm <[email protected]>
* add Performance Schema Functions format_bytes and format_nano_time Signed-off-by: gauss <[email protected]> * remove a test case Signed-off-by: gauss <[email protected]> Co-authored-by: kennytm <[email protected]>
* add Performance Schema Functions format_bytes and format_nano_time Signed-off-by: gauss <[email protected]> * remove a test case Signed-off-by: gauss <[email protected]> Co-authored-by: kennytm <[email protected]>
Signed-off-by: disksing <[email protected]> Co-authored-by: Yuqing Bai <[email protected]>
Hi there,
When I try to implement some time functions listed in issue #236, I find those functions
adddate, subdate, date_add, date_sub
, which are ticked as done, are not fully working. If they are used ininterpreter
, they just returnNULL
, as the below:This PR add ast evalutions for date arith to fix this problem.