-
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
expression: fix incorrect date arithmitical with negative interval #8523
Conversation
Hi contributor, thanks for your PR. This patch needs to be approved by someone of admins. They should reply with "/ok-to-test" to accept this PR for running test automatically. |
{date[0], fcSub, -0.5, date[1]}, | ||
{date[0], fcSub, -1.4, date[1]}, | ||
} | ||
for _, test := range tests { |
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.
s/test/t/
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.
Using test
is consistent with other usages in this file.
@@ -1527,7 +1527,7 @@ func extractSingleTimeValue(unit string, format string) (int64, int64, int64, fl | |||
if err != nil { | |||
return 0, 0, 0, 0, ErrIncorrectDatetimeValue.GenWithStackByArgs(format) | |||
} | |||
iv := int64(fv + 0.5) | |||
iv := int64(math.Round(fv)) |
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.
Should we use floatStrToIntStr
instead of Round
? floatStrToIntStr
does round operation also and it covers corner cases.
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 float value is also needed.
iv
is used in cases that float value should be rounded, like DAY
, WEEK
.
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.
You mean inputs like 1.23a4 DAY
? 1.23a
can pass strconv.ParseFloat
@eurekaka
or something like 1e9223372036854775807
?
I think the change here is ok, a sane person will not use those illegal input, so
strconv.ParseFloat + math.Round is suitable for most of the cases
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.
MySQL(root@127.0.0.1:test) > select adddate("2018-12-09", interval "1e1" DAY);
+-------------------------------------------+
| adddate("2018-12-09", interval "1e1" DAY) |
+-------------------------------------------+
| 2018-12-10 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
MySQL(root@127.0.0.1:test) > select adddate("2018-12-09", interval "10" DAY);
+------------------------------------------+
| adddate("2018-12-09", interval "10" DAY) |
+------------------------------------------+
| 2018-12-19 |
+------------------------------------------+
1 row in set (0.00 sec)
MySQL(root@127.0.0.1:test) > select adddate("2018-12-09", interval "9223372036854775807" DAY);
+-----------------------------------------------------------+
| adddate("2018-12-09", interval "9223372036854775807" DAY) |
+-----------------------------------------------------------+
| NULL |
+-----------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)
MySQL(root@127.0.0.1:test) > show warnings;
+---------+------+--------------------------------------------+
| Level | Code | Message |
+---------+------+--------------------------------------------+
| Warning | 1441 | Datetime function: datetime field overflow |
+---------+------+--------------------------------------------+
1 row in set (0.01 sec)
We should use strconv.ParseFloat()
in this PR. floatStrToIntStr
converts 1e1
to 10, which is not in MySQL. Another discovery is that TiDB does not properly handle the "Datetime function: datetime field overflow" warning:
TiDB(root@127.0.0.1:test) > select adddate("2018-12-09", interval "9223372036854775807" DAY);
+-----------------------------------------------------------+
| adddate("2018-12-09", interval "9223372036854775807" DAY) |
+-----------------------------------------------------------+
| 2018-12-09 |
+-----------------------------------------------------------+
1 row in set (0.00 sec)
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.
You can do it in the next PR, this PR LGTM
LGTM |
Ping |
@zz-jason PTAL |
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.
LGTM
/run-all-tests |
@lerencao Please cherry-pick this commit to release-2.1 and release-2.0 branch, thanks. |
|
Got it! |
What problem does this PR solve?
#8497
What is changed and how it works?
Round the float interval, so that the date arithmetical can behave like mysql:
Check List
Tests
Related changes
This bug affects almost all functions related to date arithmetical functions. May be it should
This change is