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 issue that results of unix_timestamp()-unix_timestamp(now()) is wrong and not stable #9884

Merged
merged 10 commits into from
Mar 29, 2019

Conversation

qw4990
Copy link
Contributor

@qw4990 qw4990 commented Mar 25, 2019

What problem does this PR solve?

Fix #9710

In MySQL, Now() and UnixTimestamp() will truncate the result while they will round the result in TiDB.

These results below from MySQL 5.7 can prove it.

mysql> select now(6), now(3), now();
+----------------------------+-------------------------+---------------------+
| now(6)                     | now(3)                  | now()               |
+----------------------------+-------------------------+---------------------+
| 2019-03-25 15:57:56.612966 | 2019-03-25 15:57:56.612 | 2019-03-25 15:57:56 |
+----------------------------+-------------------------+---------------------+

mysql> select unix_timestamp(), unix_timestamp(now(0)), now(0), unix_timestamp(now(3)), now(3), now(6);
+------------------+------------------------+---------------------+------------------------+-------------------------+----------------------------+
| unix_timestamp() | unix_timestamp(now(0)) | now(0)              | unix_timestamp(now(3)) | now(3)                  | now(6)                     |
+------------------+------------------------+---------------------+------------------------+-------------------------+----------------------------+
|       1553503194 |             1553503194 | 2019-03-25 16:39:54 |         1553503194.992 | 2019-03-25 16:39:54.992 | 2019-03-25 16:39:54.992969 |
+------------------+------------------------+---------------------+------------------------+-------------------------+----------------------------+
	

What is changed and how it works?

Let Now() and UnixTimestamp() truncate the result instead of rounding it.

Check List

Tests

  • Unit test

@codecov
Copy link

codecov bot commented Mar 27, 2019

Codecov Report

Merging #9884 into master will decrease coverage by 0.1846%.
The diff coverage is 88.2352%.

@@               Coverage Diff                @@
##             master      #9884        +/-   ##
================================================
- Coverage   77.4806%   77.2959%   -0.1847%     
================================================
  Files           404        404                
  Lines         81663      81655         -8     
================================================
- Hits          63273      63116       -157     
- Misses        13679      13854       +175     
+ Partials       4711       4685        -26

Copy link
Contributor

@alivxxx alivxxx left a comment

Choose a reason for hiding this comment

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

LGTM

@alivxxx alivxxx added the status/LGT1 Indicates that a PR has LGTM 1. label Mar 27, 2019
@@ -3371,7 +3371,7 @@ func (s *testSuite3) TestCurrentTimestampValueSelection(c *C) {
tk.MustQuery("select id from t where t0 = ?", t0).Check(testkit.Rows("1"))
tk.MustQuery("select id from t where t1 = ?", t1).Check(testkit.Rows("1"))
tk.MustQuery("select id from t where t2 = ?", t2).Check(testkit.Rows("1"))
time.Sleep(time.Second / 2)
Copy link
Member

Choose a reason for hiding this comment

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

why change this?

Copy link
Contributor Author

@qw4990 qw4990 Mar 28, 2019

Choose a reason for hiding this comment

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

There are two calls of now() around this sleep().
One is used to insert a record t0=now() and the other is used to update it set t0=now().
Before this PR, since now() rounds the result, so we can assume that these two calls of now() must get two time with different seconds and the update operation really happens.
After this PR, this assumption is broken, so I increase the waiting time to ensure they can get two time with different seconds.

expression/builtin_time.go Outdated Show resolved Hide resolved
Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

rest LGTM

types/time.go Outdated Show resolved Hide resolved
types/time.go Show resolved Hide resolved
XuHuaiyu
XuHuaiyu previously approved these changes Mar 28, 2019
Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

LGTM

@XuHuaiyu XuHuaiyu added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Mar 28, 2019
@XuHuaiyu
Copy link
Contributor

/run-all-tests

1 similar comment
@qw4990
Copy link
Contributor Author

qw4990 commented Mar 28, 2019

/run-all-tests

@qw4990 qw4990 changed the title Expression: results of unix_timestamp()-unix_timestamp(now()) is wrong and not stable Expression: fix issue that results of unix_timestamp()-unix_timestamp(now()) is wrong and not stable Mar 28, 2019
@qw4990
Copy link
Contributor Author

qw4990 commented Mar 28, 2019

/run-all-tests

@XuHuaiyu XuHuaiyu changed the title Expression: fix issue that results of unix_timestamp()-unix_timestamp(now()) is wrong and not stable expression: fix issue that results of unix_timestamp()-unix_timestamp(now()) is wrong and not stable Mar 29, 2019
@qw4990
Copy link
Contributor Author

qw4990 commented Mar 29, 2019

/run-all-tests

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