pingcap / pingcap/tidb

Incorrect MAX() result when comparing JSON and numeric values (returns 0 instead of JSON)

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

drop table if exists t1;
create table t1
(
c1 decimal unsigned null
);

INSERT INTO t1 (c1) VALUES (2140366325);
INSERT INTO t1 (c1) VALUES (null);

SELECT MAX(
IF(c1 IS NOT NULL, JSON_OBJECT('k', c1), 0)
)
FROM t1;

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

According to MySQL semantics, the result should be:
{"k": 2140366325}
Reason:

The expression:
IF(c1 IS NOT NULL, JSON_OBJECT('k', c1), 0)
produces the following values:
• For c1 = 2140366325 → {"k": 2140366325} (JSON)
• For c1 = NULL → 0 (numeric)

When applying MAX() over mixed types (JSON and numeric), MySQL implicitly converts both values to strings for comparison:
• {"k": 2140366325} → string
• 0 → '0'

String comparison:
'{"k": 2140366325}' > '0'
Therefore, the correct result is:
{"k": 2140366325}

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

TiDB returns:
0
This indicates that TiDB does not follow MySQL-compatible type coercion rules when comparing JSON and numeric values inside aggregate functions.

### 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.