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

query result incorrect while date_sub result overflow the datetime filed #27232

Closed
aytrack opened this issue Aug 16, 2021 · 3 comments · Fixed by #27244
Closed

query result incorrect while date_sub result overflow the datetime filed #27232

aytrack opened this issue Aug 16, 2021 · 3 comments · Fixed by #27244
Assignees
Labels
affects-4.0 This bug affects 4.0.x versions. severity/major sig/execution SIG execution type/bug The issue is confirmed as a bug.

Comments

@aytrack
Copy link
Contributor

aytrack commented Aug 16, 2021

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

create table t (a timestamp);
insert into t values ("1970-07-23 10:04:59");
select * from t where date_sub(a, interval 10 month) = date_sub("1970-07-23 10:04:59", interval 10 month);

2. What did you expect to see? (Required)

MySQL > select * from t where date_sub(a, interval 10 month) = date_sub("1970-07-23 10:04:59", interval 10 month);
+---------------------+
| a                   |
+---------------------+
| 1970-07-23 10:04:59 |
+---------------------+

MySQL > explain analyze  select * from t where date_sub(a, interval 10 month) = date_sub("1970-07-23 10:04:59", interval 10 month);
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EXPLAIN                                                                                                                                                                                                                                             |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| -> Filter: ((t.a - interval 10 month) = <cache>(('1970-07-23 10:04:59' - interval 10 month)))  (cost=0.35 rows=1) (actual time=0.057..0.063 rows=1 loops=1)\n    -> Table scan on t  (cost=0.35 rows=1) (actual time=0.048..0.053 rows=1 loops=1)\n |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

3. What did you see instead (Required)

MySQL >  select * from t where date_sub(a, interval 10 month) = date_sub("1970-07-23 10:04:59", interval 10 month);
+---+
| a |
+---+

0 rows in set
Time: 0.154s
MySQL > show warnings;
+---------+------+--------------------------------------------+
| Level   | Code | Message                                    |
+---------+------+--------------------------------------------+
| Warning | 1441 | Datetime function: datetime field overflow |
+---------+------+--------------------------------------------+
1 row in set
Time: 0.061s

MySQL > explain select * from t where date_sub(a, interval 10 month) = date_sub("1970-07-23 10:04:59", interval 10 month);
                          ->
+---------------------+---------+-----------+---------------+----------------------------------------------------------+
| id                  | estRows | task      | access object | operator info                                            |
+---------------------+---------+-----------+---------------+----------------------------------------------------------+
| Selection_7         | 1.00    | root      |               | eq(date_sub(test.t.a, 10, "MONTH"), 1969-09-23 10:04:59) |
| └─TableReader_6     | 1.00    | root      |               | data:TableFullScan_5                                     |
|   └─TableFullScan_5 | 1.00    | cop[tikv] | table:t       | keep order:false, stats:pseudo                           |
+---------------------+---------+-----------+---------------+----------------------------------------------------------+

MySQL > select date_sub("1970-07-23 10:04:59", interval 10 month);
+----------------------------------------------------+
| date_sub("1970-07-23 10:04:59", interval 10 month) |
+----------------------------------------------------+
| 1969-09-23 10:04:59                                |
+----------------------------------------------------+

1 row in set
Time: 0.051s
MySQL > show warnings;
+-------+------+---------+
| Level | Code | Message |
+-------+------+---------+

4. What is your TiDB version? (Required)

v4.0.14, v5.0.3,
master: da8bb5e
release-5.2.0

MySQL > select tidb_version()\G
***************************[ 1. row ]***************************
tidb_version() | Release Version: v5.2.0
Edition: Community
Git Commit Hash: 4c68defe055a70402802f7b8122c09432bdb02fd
Git Branch: heads/refs/tags/v5.2.0
UTC Build Time: 2021-08-13 06:12:54
GoVersion: go1.16.4
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
@aytrack aytrack added type/bug The issue is confirmed as a bug. severity/critical sig/execution SIG execution labels Aug 16, 2021
@aytrack
Copy link
Contributor Author

aytrack commented Aug 16, 2021

similar to timestampadd

MySQL > insert into t values ("2038-01-19 03:14:07");
Query OK, 1 row affected
Time: 0.048s
MySQL > select * from t where timestampadd(hour, 1, a ) = timestampadd(hour, 1, "2038-01-19 03:14:07") ;
+---+
| a |
+---+
0 rows in set

MySQL :

MySQL > select * from t where timestampadd(hour, 1, a ) = timestampadd(hour, 1, "2038-01-19 03:14:07") ;
+---------------------+
| a                   |
+---------------------+
| 2038-01-19 03:14:07 |
+---------------------+

@jebter
Copy link

jebter commented Aug 17, 2021

corner case

@ti-srebot
Copy link
Contributor

Please edit this comment or add a new comment to complete the following information

Not a bug

  1. Remove the 'type/bug' label
  2. Add notes to indicate why it is not a bug

Duplicate bug

  1. Add the 'type/duplicate' label
  2. Add the link to the original bug

Bug

Note: Make Sure that 'component', and 'severity' labels are added
Example for how to fill out the template: #20100

1. Root Cause Analysis (RCA) (optional)

2. Symptom (optional)

3. All Trigger Conditions (optional)

4. Workaround (optional)

5. Affected versions

6. Fixed versions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-4.0 This bug affects 4.0.x versions. severity/major sig/execution SIG execution type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants