pingcap / pingcap/tidb

JSON_EXTRACT can raise inconsistent errors depending on predicate order

Open
#67,614 0 comments 0 reactions 0 assignees View on GitHub
component/expression may-affects-8.5 severity/moderate 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)

Use the following anonymized repro:

```sql
CREATE TABLE t_json_filter_order (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
d DATE NOT NULL,
category VARCHAR(64) NOT NULL,
payload VARCHAR(2048) DEFAULT NULL,
KEY idx_d (d)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

INSERT INTO t_json_filter_order (d, category, payload) VALUES ('2026-03-29', 'OTHER', '');
INSERT INTO t_json_filter_order (d, category, payload) VALUES ('2026-03-29', 'OTHER', 'not-json-{broken');
INSERT INTO t_json_filter_order (d, category, payload) VALUES ('2026-03-29', 'TARGET', '{"k":"CHN","other":1}');
```

Then run these two logically equivalent queries:

```sql
SELECT *
FROM t_json_filter_order
WHERE d = STR_TO_DATE('20260329', '%Y%m%d')
AND category = 'TARGET'
AND JSON_EXTRACT(payload, '$.k') = 'CHN';
```

```sql
SELECT *
FROM t_json_filter_order
WHERE d = STR_TO_DATE('20260329', '%Y%m%d')
AND JSON_EXTRACT(payload, '$.k') = 'CHN'
AND category = 'TARGET';
```

I reproduced this on the current `master` branch with a local testkit-based repro.

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

These two equivalent queries should behave consistently.

Either:
- both queries return the same single matching row, or
- both queries raise the same error.

Predicate order in an `AND` chain should not change whether the query succeeds.

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

The first query succeeds and returns the matching row:

```text
3 | 2026-03-29 | TARGET | {"k":"CHN","other":1}
```

The second query fails with:

```text
[tikv:3140]Invalid JSON text: The document is empty
```

So changing only the order of conjuncts changes query behavior.

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

Reproduced on `master` at commit:

```text
d7e3546c96baf79f3b78fb1de33e691d25a9090e
```

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.