Inconsistent query result caused by SUBSTR zero-based index handling and BIT_LENGTH evaluation in TiDB
- 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
CREATE TABLE comments (
id INT,
post_id INT,
user_id INT,
content VARCHAR(1000),
is_spam INT,
created_at TIMESTAMP NULL
);
INSERT INTO comments VALUES
(1, 1, 2, 'Nice post', 0, '2022-01-20 10:00:00'),
(2, 1, 3, 'Spam here', 1, '2022-01-21 11:00:00'),
(3, 2, 1, 'Thanks', 0, '2022-01-22 12:00:00'),
(4, 4, 5, NULL, 0, '2022-01-23 13:00:00');
SELECT
ref_0.id AS c0
FROM comments AS ref_0
WHERE BIT_LENGTH(
SUBSTR(
'27',
ref_0.is_spam,
ref_0.id
)
) <= ref_0.user_id;
```
### 2. What did you expect to see? (Required)
Expected result: 1
Because:
- SUBSTR('27', start, len) should follow standard string indexing rules
- Invalid or zero-based start position should not be interpreted inconsistently
- BIT_LENGTH(...) evaluation should be consistent across rows
- Only row id = 1 satisfies the condition under standard SQL semantics
### 3. What did you see instead (Required)
TiDB returns:
```TEXT
1, 3, 4
```
while other databases (e.g., PostgreSQL / DuckDB) return:
```TEXT
1
```
This indicates inconsistent evaluation of:
- SUBSTR when start = 0 or derived from integer column
- BIT_LENGTH on resulting string
- WHERE predicate filtering behavior
### 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:28:32
GoVersion: go1.21.6
Race Enabled: false
Check Table Before Drop: false
Store: unistore |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
Contributor guide
Assessment
This issue has not been assessed yet.