Incorrect results when using prefix index for range scan on strings starting with space
- Dominant language
- Go
- Stars
- 40.5k
- Forks
- 6.2k
- PR merge metrics
- PR metrics pending
Description
## Bug Report
### 1. Minimal reproduce step
```
-- Create test database and table
DROP DATABASE IF EXISTS db7_min_repro;
CREATE DATABASE db7_min_repro;
USE db7_min_repro;
-- Table with TEXT column and utf8mb4_bin collation
CREATE TABLE t0(
c0 TEXT NOT NULL,
c1 DOUBLE NOT NULL,
c2 CHAR(1) NOT NULL DEFAULT 'x',
PRIMARY KEY (c2, c1, c0(128))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-- Insert test data - key row has c0 starting with space (0x20)
INSERT INTO t0(c0, c1, c2) VALUES (' ^kAI[[_', 428.1106320573271, 'x');
INSERT INTO t0(c0, c1, c2) VALUES ('V', 343.2264651012424, 'a');
INSERT INTO t0(c0, c1, c2) VALUES ('E6', 109.27778225906448, 'a');
INSERT INTO t0(c0, c1, c2) VALUES ('8', 0.8593559742149127, '2');
INSERT INTO t0(c0, c1, c2) VALUES ('x}', 109.27778225906448, 'i');
-- Create prefix index on first character of c0
CREATE INDEX prefix_idx ON t0 (c0(1));
ANALYZE TABLE t0;
-- Query without index hint (correct result)
SELECT t0.c0, t0.c2 FROM t0 WHERE ((t0.c0)>('')) ORDER BY t0.c0;
-- Query with USE_INDEX hint forcing prefix index (incorrect result)
SELECT /*+ USE_INDEX(t0, prefix_idx) */ t0.c0, t0.c2 FROM t0 WHERE ((t0.c0)>('')) ORDER BY t0.c0;
```
### 2. What did you expect to see?
Both queries should return the same 5 rows, including the row where `c0 = ' ^kAI[[_'` (starts with space character 0x20):
```
+----------+----+
| c0 | c2 |
+----------+----+
| ^kAI[[_ | x |
| 8 | 2 |
| E6 | a |
| V | a |
| x} | i |
+----------+----+
5 rows in set
```
The range condition `c0 > ''` should match all non-empty strings, regardless of whether the prefix index is used.
### 3. What did you see instead?
The query with USE_INDEX(t0, prefix_idx) returns only 4 rows, incorrectly skipping the row where c0 starts with space:
```
+----+----+
| c0 | c2 |
+----+----+
| 8 | 2 |
| E6 | a |
| V | a |
| x} | i |
+----+----+
4 rows in set
```
### 4. What is your TiDB version?
**v8.5.5**
**Additional context**:
This bug only manifests when using a prefix index (e.g., c0(1))
The issue is specific to range scans with lower bound '' (empty string)
Rows starting with space (0x20) are incorrectly excluded from the index scan
Full table scan or primary key scan returns correct results
Contributor guide
Assessment
This issue has not been assessed yet.