pingcap / pingcap/tidb

The return type of `TIMESTAMPADD` is not correct.

Open
#59,439 1 comment 0 reactions 0 assignees View on GitHub
component/expression severity/moderate sig/execution type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

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

```
mysql> SELECT TIMESTAMPADD(DAY, -1, '2023-01-01');
+-------------------------------------+
| TIMESTAMPADD(DAY, -1, '2023-01-01') |
+-------------------------------------+
| 2022-12-31 |
+-------------------------------------+
1 row in set (0.05 sec)

mysql> SELECT TIMESTAMPADD(WEEK, 2, '2023-12-31');
+-------------------------------------+
| TIMESTAMPADD(WEEK, 2, '2023-12-31') |
+-------------------------------------+
| 2024-01-14 |
+-------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(MONTH, 13, '2022-05-15');
+---------------------------------------+
| TIMESTAMPADD(MONTH, 13, '2022-05-15') |
+---------------------------------------+
| 2023-06-15 |
+---------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(QUARTER, 4, '2023-01-01');
+----------------------------------------+
| TIMESTAMPADD(QUARTER, 4, '2023-01-01') |
+----------------------------------------+
| 2024-01-01 |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(DAY, 0, '2023-10-23');
+------------------------------------+
| TIMESTAMPADD(DAY, 0, '2023-10-23') |
+------------------------------------+
| 2023-10-23 |
+------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(DAY, 1, 20231023);
+--------------------------------+
| TIMESTAMPADD(DAY, 1, 20231023) |
+--------------------------------+
| 2023-10-24 |
+--------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(MONTH, 1, '2020-02-29');
+--------------------------------------+
| TIMESTAMPADD(MONTH, 1, '2020-02-29') |
+--------------------------------------+
| 2020-03-29 |
+--------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(MONTH, 12, '2023-10-31');
+---------------------------------------+
| TIMESTAMPADD(MONTH, 12, '2023-10-31') |
+---------------------------------------+
| 2024-10-31 |
+---------------------------------------+
1 row in set (0.00 sec)
```

### 3. What did you see instead (Required)

```
mysql> SELECT TIMESTAMPADD(DAY, -1, '2023-01-01');
+-------------------------------------+
| TIMESTAMPADD(DAY, -1, '2023-01-01') |
+-------------------------------------+
| 2022-12-31 00:00:00 |
+-------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(WEEK, 2, '2023-12-31');
+-------------------------------------+
| TIMESTAMPADD(WEEK, 2, '2023-12-31') |
+-------------------------------------+
| 2024-01-14 00:00:00 |
+-------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(MONTH, 13, '2022-05-15');
+---------------------------------------+
| TIMESTAMPADD(MONTH, 13, '2022-05-15') |
+---------------------------------------+
| 2023-06-15 00:00:00 |
+---------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(QUARTER, 4, '2023-01-01');
+----------------------------------------+
| TIMESTAMPADD(QUARTER, 4, '2023-01-01') |
+----------------------------------------+
| 2024-01-01 00:00:00 |
+----------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(DAY, 0, '2023-10-23');
+------------------------------------+
| TIMESTAMPADD(DAY, 0, '2023-10-23') |
+------------------------------------+
| 2023-10-23 00:00:00 |
+------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(DAY, 1, 20231023);
+--------------------------------+
| TIMESTAMPADD(DAY, 1, 20231023) |
+--------------------------------+
| 2023-10-24 00:00:00 |
+--------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(MONTH, 1, '2020-02-29');
+--------------------------------------+
| TIMESTAMPADD(MONTH, 1, '2020-02-29') |
+--------------------------------------+
| 2020-03-29 00:00:00 |
+--------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT TIMESTAMPADD(MONTH, 12, '2023-10-31');
+---------------------------------------+
| TIMESTAMPADD(MONTH, 12, '2023-10-31') |
+---------------------------------------+
| 2024-10-31 00:00:00 |
+---------------------------------------+
1 row in set (0.00 sec)
```

### 4. What is your TiDB version? (Required)

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the listed TIMESTAMPADD queries in TiDB and trace the TIMESTAMPADD SQL function's return-type handling. Add regression coverage for the DAY, WEEK, MONTH, and QUARTER examples, with completion shown by date-only results matching the expected output rather than values with a midnight time component.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.