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

expression: fix getIntervalFromDecimal in DATE_ADD() #11297

Merged
merged 6 commits into from
Jul 18, 2019

Conversation

wshwsh12
Copy link
Contributor

What problem does this PR solve?

#11286
Before this pr:

mysql> select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_MICROSECOND);
+------------------------------------------------------------------+
| DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_MICROSECOND) |
+------------------------------------------------------------------+
| 2007-03-28 22:08:30.200000                                       |
+------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_MICROSECOND);
+----------------------------------------------------------------+
| DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_MICROSECOND) |
+----------------------------------------------------------------+
| 2007-03-28 22:08:30.200000                                     |
+----------------------------------------------------------------+
1 row in set (0.01 sec)

mysql> select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MICROSECOND);
+---------------------------------------------------------------+
| DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MICROSECOND) |
+---------------------------------------------------------------+
| 2007-03-28 22:08:30.200000                                    |
+---------------------------------------------------------------+
1 row in set (0.00 sec)

This pr:

mysql> select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_MICROSECOND);
+------------------------------------------------------------------+
| DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 MINUTE_MICROSECOND) |
+------------------------------------------------------------------+
| 2007-03-28 22:08:25.800000                                       |
+------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_MICROSECOND);
+----------------------------------------------------------------+
| DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 HOUR_MICROSECOND) |
+----------------------------------------------------------------+
| 2007-03-28 22:08:25.800000                                     |
+----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MICROSECOND);
+---------------------------------------------------------------+
| DATE_ADD('2007-03-28 22:08:28',INTERVAL -2.2 DAY_MICROSECOND) |
+---------------------------------------------------------------+
| 2007-03-28 22:08:25.800000                                    |
+---------------------------------------------------------------+
1 row in set (0.00 sec)

What is changed and how it works?

When use getIntervalFromDecimal to get interval, It will convert the Decimal to Interval(the type is string).
Before the pr, the func don't consider that the decimal may be a negative number. It get a wrong result.
e.g.
1 DAY_MICROSECOND => 0 00:00:1
-2.2 DAY_MICROSECOND => 0 00:00:-2.2 (wrong)

This pr, before the converting, I judge whether the decimal is negative first.
-2.2 DAY_MICROSECOND => -0 00:00:2.2

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)

Code changes

Side effects

  • Increased code complexity

Related changes

  • Need to cherry-pick to the release branch

@wshwsh12
Copy link
Contributor Author

/run-all-tests

@zz-jason zz-jason requested a review from SunRunAway July 17, 2019 12:16
@qw4990 qw4990 changed the title *: fix getIntervalFromDecimal in DATE_ADD() expression: fix getIntervalFromDecimal in DATE_ADD() Jul 18, 2019
Copy link
Contributor

@qw4990 qw4990 left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -2697,6 +2701,10 @@ func (du *baseDateArithmitical) getIntervalFromDecimal(ctx sessionctx.Context, a
}
}

if neg {
interval = "-" + interval
Copy link
Contributor

@SunRunAway SunRunAway Jul 18, 2019

Choose a reason for hiding this comment

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

It will be an incorrect result in case "SECOND" and default parts in the switch statement above.

@wshwsh12
Copy link
Contributor Author

@SunRunAway PTAL

@codecov
Copy link

codecov bot commented Jul 18, 2019

Codecov Report

Merging #11297 into master will not change coverage.
The diff coverage is n/a.

@@             Coverage Diff             @@
##             master     #11297   +/-   ##
===========================================
  Coverage   81.6571%   81.6571%           
===========================================
  Files           423        423           
  Lines         91818      91818           
===========================================
  Hits          74976      74976           
  Misses        11531      11531           
  Partials       5311       5311

@@ -2684,19 +2688,25 @@ func (du *baseDateArithmitical) getIntervalFromDecimal(ctx sessionctx.Context, a
/* keep interval as original decimal */
case "SECOND":
// Decimal's EvalString is like %f format.
neg = false
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better not to do the extra revise code in these paths.
Also, would you like to add a unit test to cover it?

@wshwsh12
Copy link
Contributor Author

/run-all-tests

@wshwsh12
Copy link
Contributor Author

@SunRunAway PTAL

@wshwsh12 wshwsh12 requested a review from SunRunAway July 18, 2019 11:01
Copy link
Contributor

@SunRunAway SunRunAway left a comment

Choose a reason for hiding this comment

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

LGTM

zz-jason
zz-jason previously approved these changes Jul 18, 2019
Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

LGTM

@zz-jason
Copy link
Member

/run-all-tests

@zz-jason zz-jason added the status/LGT2 Indicates that a PR has LGTM 2. label Jul 18, 2019
@wshwsh12
Copy link
Contributor Author

/run-all-tests

@SunRunAway SunRunAway merged commit 5aef053 into pingcap:master Jul 18, 2019
@sre-bot
Copy link
Contributor

sre-bot commented Jul 18, 2019

cherry pick to release-2.1 failed

@sre-bot
Copy link
Contributor

sre-bot commented Jul 18, 2019

cherry pick to release-3.0 in PR #11325

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants