Skip to content

[feature](fe) Support top-N correlated scalar subqueries - #64254

Draft
morrySnow wants to merge 1 commit into
apache:masterfrom
morrySnow:try-window-correlated
Draft

[feature](fe) Support top-N correlated scalar subqueries#64254
morrySnow wants to merge 1 commit into
apache:masterfrom
morrySnow:try-window-correlated

Conversation

@morrySnow

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: None

Related PR: #64251

Problem Summary: Correlated scalar subqueries with ORDER BY and LIMIT 1 were rejected during analysis. This change preserves the top-N shape and rewrites it to ROW_NUMBER, partitioned by the correlated inner columns and ordered by the subquery sort keys, then filters rank 1. The resulting scalar apply becomes a left outer join so unmatched rows return NULL.

Release note

Support correlated scalar subqueries with ORDER BY and LIMIT 1.

Check List (For Author)

  • Test: Unit Test and Regression test
    • ./run-fe-ut.sh --run org.apache.doris.nereids.rules.analysis.AnalyzeSubQueryTest
    • ./run-fe-ut.sh --run org.apache.doris.nereids.rules.analysis.AnalyzeSubQueryTest#testCorrelatedScalarSubqueryWithTopN
    • ./run-regression-test.sh --run -d query_p0/subquery -s correlated_scalar_subquery
  • Behavior changed: Yes. Correlated scalar subqueries with ORDER BY and LIMIT 1 are now supported.
  • Does this need documentation: No

### What problem does this PR solve?

Issue Number: None

Related PR: apache#64251

Problem Summary: Correlated scalar subqueries with ORDER BY and LIMIT 1 were rejected during analysis. Preserve the top-N shape, rewrite it to ROW_NUMBER partitioned by correlated inner columns and ordered by the subquery sort keys, then filter rank 1. The resulting scalar apply becomes a left outer join so unmatched rows return NULL.

### Release note

Support correlated scalar subqueries with ORDER BY and LIMIT 1.

### Check List (For Author)

- Test: Unit Test and Regression test
    - ./run-fe-ut.sh --run org.apache.doris.nereids.rules.analysis.AnalyzeSubQueryTest
    - ./run-regression-test.sh --run -d query_p0/subquery -s correlated_scalar_subquery
- Behavior changed: Yes. Correlated scalar subqueries with ORDER BY and LIMIT 1 are now supported.
- Does this need documentation: No
@hello-stephen

Copy link
Copy Markdown
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?

@morrySnow

Copy link
Copy Markdown
Contributor Author

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-H: Total hot run time: 28753 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit 0c3cc2499247fd26eabf5c522776643ca17d7522, data reload: false

------ Round 1 ----------------------------------
orders	Doris	NULL	NULL	0	0	0	NULL	0	NULL	NULL	2023-12-26 18:27:23	2023-12-26 18:42:55	NULL	utf-8	NULL	NULL	
============================================
q1	17627	4046	4003	4003
q2	q3	10775	1382	808	808
q4	4691	474	338	338
q5	7556	856	595	595
q6	176	168	134	134
q7	759	890	617	617
q8	9586	1599	1598	1598
q9	6798	4501	4488	4488
q10	6794	1832	1537	1537
q11	444	265	253	253
q12	658	423	292	292
q13	18173	3764	2760	2760
q14	279	258	244	244
q15	q16	818	772	708	708
q17	1323	1132	761	761
q18	6730	5674	5503	5503
q19	1437	1300	1007	1007
q20	526	417	267	267
q21	6170	2857	2521	2521
q22	450	374	319	319
Total cold run time: 101770 ms
Total hot run time: 28753 ms

----- Round 2, with runtime_filter_mode=off -----
orders	Doris	NULL	NULL	150000000	42	6422171781	NULL	22778155	NULL	NULL	2023-12-26 18:27:23	2023-12-26 18:42:55	NULL	utf-8	NULL	NULL	
============================================
q1	4878	4807	4713	4713
q2	q3	5069	5199	4666	4666
q4	2145	2221	1410	1410
q5	4824	4621	4821	4621
q6	237	203	140	140
q7	1883	1716	1554	1554
q8	2412	2122	2101	2101
q9	7423	7367	7391	7367
q10	4748	4704	4259	4259
q11	540	396	363	363
q12	737	735	528	528
q13	2999	3358	2780	2780
q14	280	280	252	252
q15	q16	676	702	617	617
q17	1272	1256	1250	1250
q18	7287	6869	6792	6792
q19	1156	1111	1128	1111
q20	2222	2212	1941	1941
q21	5297	4598	4479	4479
q22	534	477	446	446
Total cold run time: 56619 ms
Total hot run time: 51390 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 169583 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 0c3cc2499247fd26eabf5c522776643ca17d7522, data reload: false

query5	4335	632	496	496
query6	464	214	183	183
query7	4863	526	300	300
query8	387	229	219	219
query9	8769	4018	4026	4018
query10	474	320	265	265
query11	5902	2366	2183	2183
query12	157	106	101	101
query13	1262	620	434	434
query14	6431	5413	5075	5075
query14_1	4443	4467	4419	4419
query15	211	196	186	186
query16	1040	427	456	427
query17	1132	718	604	604
query18	2481	482	363	363
query19	207	193	155	155
query20	118	114	108	108
query21	229	145	121	121
query22	13804	13655	13346	13346
query23	17444	16475	16161	16161
query23_1	16321	16378	16352	16352
query24	7494	1770	1306	1306
query24_1	1332	1333	1321	1321
query25	584	479	415	415
query26	1337	329	176	176
query27	2633	570	337	337
query28	4447	2010	2019	2010
query29	1103	642	515	515
query30	322	238	204	204
query31	1142	1089	963	963
query32	110	64	63	63
query33	565	338	274	274
query34	1246	1146	669	669
query35	757	811	692	692
query36	1412	1420	1242	1242
query37	152	103	91	91
query38	3198	3166	3065	3065
query39	929	929	910	910
query39_1	887	875	858	858
query40	217	125	102	102
query41	64	63	62	62
query42	96	92	93	92
query43	320	326	272	272
query44	
query45	193	186	177	177
query46	1130	1216	763	763
query47	2353	2404	2244	2244
query48	391	420	302	302
query49	629	483	359	359
query50	999	351	262	262
query51	4319	4481	4214	4214
query52	90	89	78	78
query53	242	270	194	194
query54	279	247	196	196
query55	82	76	70	70
query56	254	230	235	230
query57	1443	1438	1325	1325
query58	245	204	218	204
query59	1572	1635	1459	1459
query60	305	255	232	232
query61	163	159	161	159
query62	699	647	589	589
query63	231	199	186	186
query64	2558	795	637	637
query65	
query66	1795	481	357	357
query67	29070	29732	29553	29553
query68	
query69	410	322	263	263
query70	977	932	955	932
query71	289	205	213	205
query72	3016	2718	2484	2484
query73	828	726	427	427
query74	5122	4966	4830	4830
query75	2645	2591	2284	2284
query76	2349	1162	784	784
query77	357	377	293	293
query78	12495	12402	12054	12054
query79	1571	1043	799	799
query80	1242	476	391	391
query81	526	283	252	252
query82	603	156	122	122
query83	326	282	249	249
query84	
query85	921	527	436	436
query86	440	289	312	289
query87	3432	3380	3175	3175
query88	3605	2729	2719	2719
query89	429	378	340	340
query90	1869	179	180	179
query91	173	162	139	139
query92	67	67	58	58
query93	1649	1427	800	800
query94	710	356	318	318
query95	671	497	336	336
query96	1038	794	355	355
query97	2715	2726	2585	2585
query98	215	225	216	216
query99	1167	1204	1058	1058
Total cold run time: 252021 ms
Total hot run time: 169583 ms

@hello-stephen

Copy link
Copy Markdown
Contributor

FE UT Coverage Report

Increment line coverage 81.11% (73/90) 🎉
Increment coverage report
Complete coverage report

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 86.81% (79/91) 🎉
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.

2 participants