pingcap / pingcap/tidb

Incorrect result of MIN() with mixed JSON and scalar values after CASE rewrite (inconsistent with MySQL)

Open
#67,682 2 comments 0 reactions 0 assignees View on GitHub
contribution may-affects-7.1 may-affects-7.5 may-affects-8.1 may-affects-8.5 severity/major sig/execution 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)

drop table if exists t0;
create table t0
(
c0 tinyblob null
);

INSERT INTO t0 (c0) VALUES (0x383836323331303239);
INSERT INTO t0 (c0) VALUES (null);

-- baseline {}
SELECT MIN(IF(c0 IS NULL, JSON_OBJECT(), 1)) FROM t0;

-- rewrite 1
SELECT MIN(
CASE
WHEN IF(c0 IS NULL, JSON_OBJECT(), 1) IS NULL
THEN NULL
ELSE IF(c0 IS NULL, JSON_OBJECT(), 1)
END
) FROM t0;

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

Both queries should return the same result.

According to MySQL behavior, the expression:
IF(c0 IS NULL, JSON_OBJECT(), 1)
produces values {} (JSON) and 1 (scalar). During aggregation:
• JSON values are coerced to strings ("{}")
• Scalar 1 is coerced to "1"

String comparison gives:
"1" < "{}"
So the expected result is:
1
Both queries return 1 in MySQL.

### 3. What did you see instead (Required)
In TiDB, the two queries return different results:
• Baseline query returns: {}
• Rewritten query returns: 1

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

Release Version: v8.5.5
Edition: Community
Git Commit Hash: 1fa258b833ff113883beeba40bc130be7ce66610
Git Branch: HEAD
UTC Build Time: 2026-01-14 22:19:19
GoVersion: go1.25.5
Race Enabled: false
Check Table Before Drop: false
Store: tikv

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.