pingcap / pingcap/tidb

Inconsistent Results with DATE_ADD and BETWEEN Clause

Open
#53,691 0 comments 0 reactions 0 assignees View on GitHub
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)

Create and populate the table to:

```sql
CREATE TABLE t0 (c_0 DATETIME, c_1 INT, c_2 DATETIME);
INSERT INTO t0 VALUES ('6722-01-03 04:01:57.000000', '3525', '6731-08-29 04:01:57.000000');
```

```sql
SELECT c_2, (DATE_ADD(t0.c_0, INTERVAL t0.c_1 DAY)) FROM t0;
```
| c_2 | (DATE_ADD(t0.c_0, INTERVAL t0.c_1 DAY)) |
| ------------------- | --------------------------------------- |
| 6731-08-29 04:01:57 | 6731-08-29 04:01:57 |

Thus c_2 and (DATE_ADD(t0.c_0, INTERVAL t0.c_1 DAY)) have the same value in t0.

Execute this SELECT:

```sql
SELECT t0.c_1 FROM t0 WHERE t0.c_2 BETWEEN -3.1881253607903353e+38 AND '2935-07-28 18:13:52.000000';
```

Output:

| c_1 |
| ---- |
| 3525 |

Change c_2 to (DATE_ADD(t0.c_0, INTERVAL t0.c_1 DAY)) and get a new SELECT:

SELECT t0.c_1 FROM t0 WHERE (DATE_ADD(t0.c_0, INTERVAL t0.c_1 DAY)) BETWEEN -3.1881253607903353e+38 AND '2935-07-28 18:13:52.000000';

Output:

| c_1 |
| ---- |

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

The second SELECT query should return the same result as the first one since c_2 and (DATE_ADD(t0.c_0, INTERVAL t0.c_1 DAY)) have the same value.

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

See part 1.

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

Release Version: v7.5.1
Edition: Community
Git Commit Hash: 7d16cc79e81bbf573124df3fd9351c26963f3e70
Git Branch: heads/refs/tags/v7.5.1
UTC Build Time: 2024-02-27 14:30:59
GoVersion: go1.21.6
Race Enabled: false
Check Table Before Drop: false
Store: tikv

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.