Skip to content

[fix](json) fix RuntimeException when comparing JSONB literals#61097

Open
JamesIsHuang wants to merge 2 commits intoapache:masterfrom
JamesIsHuang:feature/fix-json-contains-59692
Open

[fix](json) fix RuntimeException when comparing JSONB literals#61097
JamesIsHuang wants to merge 2 commits intoapache:masterfrom
JamesIsHuang:feature/fix-json-contains-59692

Conversation

@JamesIsHuang
Copy link

@JamesIsHuang JamesIsHuang commented Mar 6, 2026

Proposed changes

Issue Number: close #59692

This PR addresses the remaining failure in reopened issue #59692.

The earlier incorrect-result cases for single JSON_CONTAINS calls were already fixed by #53291. However, chained predicates such as:

SELECT *
FROM table
WHERE JSON_CONTAINS('["1","2","3"]', '"1"')
  AND JSON_CONTAINS('["1","2","3"]', '"2"')
LIMIT 100;

can still fail in FE with:

Not support comparison between JSONB literals

Root cause

During FE optimization phases such as filter estimation and partition pruning, JSON literals may be compared through legacy literal comparison paths. The legacy JsonLiteral.compareLiteral() throws RuntimeException for JSON literal comparisons, which aborts planning before execution reaches the BE where json_contains itself is handled correctly.

Changes

  • Fix legacy JsonLiteral.compareLiteral() to support JSON literal comparison instead of throwing directly.
  • Make Nereids JsonLiteral implement ComparableLiteral with compareTo().
  • Align legacy behavior with Nereids by explicitly handling PlaceHolderExpr, NullLiteral, and MaxLiteral, and rejecting incompatible cross-type comparisons.
  • Add JsonLiteralTest covering both legacy and Nereids comparison behavior.

Behavior

Before:

  • Single JSON_CONTAINS incorrect-result cases were already fixed by #53291.
  • Chained JSON_CONTAINS predicates could still fail during FE optimization with Not support comparison between JSONB literals.

After:

  • Chained JSON_CONTAINS predicates no longer fail because of JSON literal comparison in FE optimization.
  • Direct SQL-level invalid JSON comparisons are still rejected by normal semantic checks.

Further comments

  • This PR does not change SQL semantics for direct JSON comparison predicates such as json_col = json_col.
  • The comparison support here is for internal FE optimization infrastructure only.
  • String-based comparison is acceptable here because JsonLiteral stores normalized string content and the goal is to avoid planner crashes rather than introduce new SQL-visible comparison semantics.

…e#59692)

The legacy JsonLiteral.compareLiteral() threw RuntimeException("Not support
comparison between JSONB literals"), causing queries with json_contains to
crash during FE optimization (filter estimation, partition pruning).

Changes:
- Fix legacy JsonLiteral.compareLiteral() to perform string-based comparison
  instead of throwing RuntimeException.
- Make Nereids JsonLiteral implement ComparableLiteral with compareTo() method.
- Add JsonLiteralTest covering both Nereids and legacy comparison behavior.

Made-with: Cursor
@hello-stephen
Copy link
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

…vior

Legacy compareLiteral() silently fell back to string comparison for
non-JSON types, while Nereids correctly rejected cross-type comparison.
Fix to handle PlaceHolderExpr, NullLiteral, MaxLiteral explicitly and
throw RuntimeException for incompatible types.

Made-with: Cursor
@JamesIsHuang
Copy link
Author

run buildall

@doris-robot
Copy link

TPC-H: Total hot run time: 27617 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit 88dfc64874e861cd70d27500f653490e41185b0e, data reload: false

------ Round 1 ----------------------------------
============================================
q1	17649	4479	4314	4314
q2	q3	10650	777	518	518
q4	4679	371	256	256
q5	7563	1179	1033	1033
q6	174	173	148	148
q7	778	831	670	670
q8	9301	1444	1316	1316
q9	4776	4677	4768	4677
q10	6260	1905	1656	1656
q11	483	263	250	250
q12	707	572	477	477
q13	18044	2930	2173	2173
q14	232	240	218	218
q15	908	796	808	796
q16	727	712	672	672
q17	724	871	395	395
q18	6084	5400	5270	5270
q19	1174	976	604	604
q20	492	508	388	388
q21	4696	2099	1512	1512
q22	399	321	274	274
Total cold run time: 96500 ms
Total hot run time: 27617 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	4728	4657	4554	4554
q2	q3	3864	4314	3801	3801
q4	870	1197	763	763
q5	4060	4392	4298	4298
q6	183	176	140	140
q7	1777	1661	1518	1518
q8	2461	2784	2675	2675
q9	7508	7191	7353	7191
q10	3771	4055	3608	3608
q11	523	425	424	424
q12	469	594	450	450
q13	2732	3255	2366	2366
q14	280	292	273	273
q15	852	807	822	807
q16	713	790	722	722
q17	1182	1422	1396	1396
q18	7171	6702	6630	6630
q19	883	901	897	897
q20	2083	2160	2037	2037
q21	3968	3528	3378	3378
q22	503	428	386	386
Total cold run time: 50581 ms
Total hot run time: 48314 ms

@doris-robot
Copy link

TPC-DS: Total hot run time: 153592 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpcds-tools
TPC-DS sf100 test result on commit 88dfc64874e861cd70d27500f653490e41185b0e, data reload: false

query5	4334	624	518	518
query6	324	228	207	207
query7	4223	462	272	272
query8	340	238	236	236
query9	8716	2733	2727	2727
query10	528	388	349	349
query11	7342	5928	5619	5619
query12	183	133	135	133
query13	1281	453	364	364
query14	6071	3903	3579	3579
query14_1	2889	2838	2789	2789
query15	197	195	182	182
query16	992	487	465	465
query17	1111	725	622	622
query18	2476	456	354	354
query19	215	212	187	187
query20	136	132	128	128
query21	226	144	125	125
query22	4879	5164	5111	5111
query23	16608	16051	15946	15946
query23_1	15799	15847	15630	15630
query24	7350	1677	1249	1249
query24_1	1315	1278	1238	1238
query25	541	465	409	409
query26	1243	264	148	148
query27	2787	464	283	283
query28	4525	1855	1835	1835
query29	828	556	476	476
query30	313	247	211	211
query31	1384	1309	1212	1212
query32	77	72	72	72
query33	506	321	286	286
query34	937	914	554	554
query35	630	678	594	594
query36	1080	1131	980	980
query37	125	95	86	86
query38	2933	3019	2828	2828
query39	1025	857	837	837
query39_1	841	836	837	836
query40	229	157	138	138
query41	61	61	58	58
query42	296	296	309	296
query43	239	248	222	222
query44	
query45	198	189	182	182
query46	875	983	607	607
query47	2108	2131	2047	2047
query48	306	313	241	241
query49	622	454	378	378
query50	675	289	215	215
query51	4088	4087	4139	4087
query52	283	294	283	283
query53	293	338	282	282
query54	291	274	259	259
query55	97	87	82	82
query56	314	329	324	324
query57	1353	1338	1270	1270
query58	296	286	273	273
query59	1348	1493	1307	1307
query60	336	334	318	318
query61	146	145	144	144
query62	632	575	541	541
query63	308	292	280	280
query64	4998	1269	995	995
query65	
query66	1450	488	357	357
query67	16352	16685	16385	16385
query68	
query69	397	303	277	277
query70	966	963	985	963
query71	335	316	309	309
query72	2959	2853	2554	2554
query73	546	543	327	327
query74	10029	9952	9718	9718
query75	2853	2772	2485	2485
query76	2314	1035	676	676
query77	367	381	303	303
query78	11191	11436	10687	10687
query79	2240	758	606	606
query80	1769	609	551	551
query81	576	287	248	248
query82	1034	153	120	120
query83	341	268	246	246
query84	250	129	95	95
query85	1036	481	430	430
query86	420	314	296	296
query87	3180	3164	3001	3001
query88	3573	2699	2685	2685
query89	430	371	344	344
query90	1916	182	182	182
query91	170	158	139	139
query92	78	78	73	73
query93	1232	824	515	515
query94	645	317	306	306
query95	618	401	320	320
query96	642	524	226	226
query97	2479	2479	2434	2434
query98	234	226	220	220
query99	1002	1016	902	902
Total cold run time: 235903 ms
Total hot run time: 153592 ms

@hello-stephen
Copy link
Contributor

FE UT Coverage Report

Increment line coverage 62.50% (10/16) 🎉
Increment coverage report
Complete coverage report

@hello-stephen
Copy link
Contributor

FE Regression Coverage Report

Increment line coverage 0.00% (0/16) 🎉
Increment coverage report
Complete coverage report

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] (json) incorrect results of json_contains

3 participants