pingcap / pingcap/tidb

Unexpected BETWEEN Behavior When Using NULL in TiDB

Open
#60,369 5 comments 0 reactions 1 assignee Claimed by @hawkingrei View on GitHub
severity/moderate sig/planner type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

### 1. Minimal reproduce step (Required)
The following SQL statements create a table, insert sample data, and execute a query that demonstrates unexpected behavior:
```
CREATE TABLE `t1` (
`c1` int
);

INSERT INTO `t1` VALUES (1);

select
ref_1.c1 as c_1,
((ref_1.c1) between (10) and (NULL)) as c_2
from
t1 as ref_1
```

Unexpected Output:
```
+------+------+
| c_1 | c_2 |
+------+------+
| 1 | 0 |
+------+------+
1 row in set (0.01 sec)
```
### 2. What did you expect to see? (Required)
According to SQL standard behavior:

The BETWEEN operator should return NULL when one of the boundary values is NULL.

The expected output for 1 BETWEEN 10 AND NULL should be NULL, not 0.

### 3. What did you see instead (Required)
The query returned 0 instead of NULL.

### 4. What is your TiDB version? (Required)
8.0.11-TiDB
master https://github.com/pingcap/tidb/commit/b6141ec589ed8e73176692dc982a210ad7cf070b

GoVersion: go1.23.4
Race Enabled: false
Check Table Before Drop: false
Store: unistore |

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.