pingcap / pingcap/tidb

Incorrect query result when mixing BETWEEN with bitwise operators without parentheses

Open
#66,045 3 comments 0 reactions 0 assignees View on GitHub
affects-8.5 contribution severity/minor sig/planner 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)
```SQL
mysql> DROP TABLE IF EXISTS t0;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE t0 (id INT AUTO_INCREMENT PRIMARY KEY, c0 DOUBLE, INDEX idx_c0 (c0));
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO t0 (c0) VALUES (1), (2.5), (10), (18.8), (-2.4), (-1.4134e10);
Query OK, 6 rows affected (0.00 sec)
Records: 6 Duplicates: 0 Warnings: 0

-- Query 1: Implicit precedence
mysql> SELECT * FROM t0
-> WHERE c0 * -1 < 0
-> AND c0 BETWEEN 2.5 AND c0 >> 1 > 1 - c0
-> AND c0 ^ 3 << 2 < 41
-> ORDER BY c0;
+----+------+
| id | c0 |
+----+------+
| 2 | 2.5 |
| 3 | 10 |
+----+------+
2 rows in set (0.01 sec)

-- Query 2: Explicit precedence (The Correct Behavior)
mysql> SELECT * FROM t0
-> WHERE ((c0 * (-1)) < 0)
-> AND (c0 BETWEEN 2.5 AND ((c0 >> 1) > (1 - c0)))
-> AND (((c0 ^ 3) << 2) < 41)
-> ORDER BY c0;
Empty set (0.00 sec)
```

### 2. What did you expect to see? (Required)
Both queries should return `Empty Set`. The BETWEEN operator has lower precedence than arithmetic, bitwise, and comparison operators.

### 3. What did you see instead (Required)
```SQL
mysql> SELECT * FROM t0
-> WHERE c0 * -1 < 0
-> AND c0 BETWEEN 2.5 AND c0 >> 1 > 1 - c0
-> AND c0 ^ 3 << 2 < 41
-> ORDER BY c0;
+----+------+
| id | c0 |
+----+------+
| 2 | 2.5 |
| 3 | 10 |
+----+------+
2 rows in set (0.01 sec)

mysql> SELECT * FROM t0
-> WHERE ((c0 * (-1)) < 0)
-> AND (c0 BETWEEN 2.5 AND ((c0 >> 1) > (1 - c0)))
-> AND (((c0 ^ 3) << 2) < 41)
-> ORDER BY c0;
Empty set (0.00 sec)
```

### 4. What is your TiDB version? (Required)
```SQL
mysql> select tidb_version();
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version() |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v9.0.0-beta.2.pre-1184-g6ff1adf66f
Edition: Community
Git Commit Hash: 6ff1adf66f959369664e89ae460cb0cd3d38e13b
Git Branch: master
UTC Build Time: 2026-02-04 01:58:34
GoVersion: go1.25.6
Race Enabled: false
Check Table Before Drop: false
Store: unistore
Kernel Type: Classic |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
```

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.