Skip to content

[improvement](fe) Prune partition keys from partition topn sort - #63379

Merged
CalvinKirs merged 14 commits into
apache:masterfrom
CalvinKirs:range_partition_key_ck
Jul 3, 2026
Merged

CalvinKirs merged 14 commits into
apache:masterfrom
CalvinKirs:range_partition_key_ck

Conversation

@CalvinKirs

@CalvinKirs CalvinKirs commented May 19, 2026 •

Copy link
Copy Markdown
Member

What problem does this PR solve?

Issue Number: close #xxx

Related PR: #xxx

Problem Summary: PartitionTopN still passed partition key expressions to BE sort info when the same original slot also appeared in window order keys. These keys are constant inside each partition, so keeping them causes redundant comparison work and noisy explain output, especially after slot remapping changes ExprIds.

|   1:VPartitionTopN(42)                                                     |
|   |  functions: rank                                                       |
|   |  partition by: c_region[#3]                                            |
|   |  order by: c_region[#5] ASC, c_custkey[#6] ASC                         |
|   |  has global limit: false                                               |
|   |  partition limit: 1                                                    |
|   |  partition topn phase: TWO_PHASE_LOCAL_PTOPN                           |
|   |  distribute expr lists: c_custkey[#4]       

after

|   1:VPartitionTopN(42)                                                     |
|   |  functions: rank                                                       |
|   |  partition by: c_region[#3]                                            |
|   |  order by: c_custkey[#6] ASC                                           |
|   |  has global limit: false                                               |
|   |  partition limit: 1                                                    |
|   |  partition topn phase: TWO_PHASE_LOCAL_PTOPN                           |
|   |  distribute expr lists: c_custkey[#4]                                  |
|   |                                            

This PR also adds a guard for self-join cases where the partition key and order key come from the same original table column, but from different join inputs.

Example:

EXPLAIN
SELECT *
FROM (
    SELECT
        l.c2 AS lc2,
        r.c2 AS rc2,
        row_number() OVER (
            PARTITION BY l.c2
            ORDER BY r.c2
        ) AS rn
    FROM test_global_partition_topn_plan l
    JOIN test_global_partition_topn_plan r
      ON l.c1 = r.c1
) tmp
WHERE rn <= 1;

Here l.c2 and r.c2 both come from test_global_partition_topn_plan.c2, but they are different slots after the self join. PARTITION BY l.c2 only guarantees that
the left-side c2 is constant inside each partition. It does not guarantee that the right-side r.c2 is constant, so ORDER BY r.c2 must not be pruned.

The expected EXPLAIN should still keep the order key in VPartitionTopN:

  1:VPartitionTopN
  |  functions: row_number
  |  partition by: c2[#0]
  |  order by: c2[#3] ASC
  |  has global limit: false
  |  partition limit: 1
  |  partition topn phase: TWO_PHASE_LOCAL_PTOPN

  The different slot ids, c2[#0] and c2[#3], show that the partition key and order key are different join outputs. If the order key were pruned here, the row
  selected by rn <= 1 could become nondeterministic or incorrect.

@CalvinKirs

Copy link
Copy Markdown
Member Author

run buildall

@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?

@CalvinKirs

Copy link
Copy Markdown
Member Author

/review

@CalvinKirs
CalvinKirs force-pushed the range_partition_key_ck branch from 60ecee1 to 3fed697 Compare May 19, 2026 02:36
@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-H: Total hot run time: 31096 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit 3fed697aab5dbb781d3730288d073c9c6d201d12, 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	17712	3924	3910	3910
q2	q3	10737	1446	835	835
q4	4682	479	353	353
q5	7569	2282	2110	2110
q6	385	173	138	138
q7	949	792	622	622
q8	9371	1617	1586	1586
q9	6982	4931	4843	4843
q10	6443	2144	1812	1812
q11	436	271	247	247
q12	695	431	294	294
q13	18275	3313	2797	2797
q14	270	249	243	243
q15	q16	816	776	702	702
q17	913	880	949	880
q18	7115	5682	5632	5632
q19	1317	1297	1059	1059
q20	546	403	264	264
q21	5742	2657	2462	2462
q22	435	362	307	307
Total cold run time: 101390 ms
Total hot run time: 31096 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	4206	4124	4147	4124
q2	q3	4481	4914	4324	4324
q4	2091	2183	1388	1388
q5	4368	4268	4289	4268
q6	234	235	229	229
q7	2013	1799	1688	1688
q8	2508	2092	2050	2050
q9	7746	7791	7556	7556
q10	4551	4521	4108	4108
q11	764	417	376	376
q12	720	733	535	535
q13	3378	3655	2954	2954
q14	311	312	287	287
q15	q16	739	736	651	651
q17	1351	1317	1318	1317
q18	7759	7185	7128	7128
q19	1085	1093	1113	1093
q20	2213	2196	1928	1928
q21	5308	4622	4468	4468
q22	519	457	418	418
Total cold run time: 56345 ms
Total hot run time: 50890 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 169530 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 3fed697aab5dbb781d3730288d073c9c6d201d12, data reload: false

query5	4310	642	518	518
query6	347	218	199	199
query7	4224	511	301	301
query8	323	242	227	227
query9	8883	4020	4008	4008
query10	465	346	301	301
query11	5804	2367	2199	2199
query12	186	130	131	130
query13	1321	587	424	424
query14	6013	5368	5080	5080
query14_1	4407	4385	4347	4347
query15	218	205	188	188
query16	1029	474	452	452
query17	1163	759	609	609
query18	2756	499	355	355
query19	227	215	179	179
query20	144	135	135	135
query21	223	141	118	118
query22	13685	13559	13344	13344
query23	17190	16322	16102	16102
query23_1	16135	16238	16206	16206
query24	7531	1765	1296	1296
query24_1	1286	1321	1311	1311
query25	635	496	437	437
query26	1325	311	177	177
query27	2685	566	328	328
query28	4442	1933	1953	1933
query29	1029	647	528	528
query30	308	236	198	198
query31	1133	1060	940	940
query32	98	80	76	76
query33	539	369	307	307
query34	1185	1118	631	631
query35	782	823	701	701
query36	1327	1325	1169	1169
query37	153	104	89	89
query38	3226	3144	3076	3076
query39	932	927	907	907
query39_1	874	878	868	868
query40	231	149	136	136
query41	66	64	62	62
query42	111	112	111	111
query43	322	328	287	287
query44	
query45	211	200	197	197
query46	1091	1213	765	765
query47	2317	2304	2205	2205
query48	402	432	286	286
query49	633	483	390	390
query50	1061	360	265	265
query51	4379	4302	4365	4302
query52	109	108	97	97
query53	260	281	210	210
query54	316	276	262	262
query55	93	89	87	87
query56	319	309	299	299
query57	1426	1403	1303	1303
query58	302	275	253	253
query59	1535	1613	1385	1385
query60	323	330	317	317
query61	160	158	155	155
query62	671	617	556	556
query63	239	207	210	207
query64	2394	801	643	643
query65	
query66	1670	481	366	366
query67	30032	29960	29126	29126
query68	
query69	470	342	314	314
query70	1042	998	968	968
query71	303	278	275	275
query72	3042	2685	2465	2465
query73	845	763	412	412
query74	5104	4873	4729	4729
query75	2661	2646	2259	2259
query76	2283	1132	778	778
query77	404	419	337	337
query78	12242	12200	11706	11706
query79	1488	1034	724	724
query80	1356	557	462	462
query81	503	284	237	237
query82	1279	159	122	122
query83	345	281	245	245
query84	263	147	110	110
query85	946	540	456	456
query86	459	337	299	299
query87	3483	3333	3231	3231
query88	3553	2724	2659	2659
query89	459	382	339	339
query90	1921	182	175	175
query91	172	165	134	134
query92	77	76	74	74
query93	1477	1538	841	841
query94	736	349	298	298
query95	676	466	343	343
query96	1096	791	351	351
query97	2684	2651	2602	2602
query98	248	243	230	230
query99	1111	1117	1003	1003
Total cold run time: 254979 ms
Total hot run time: 169530 ms

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 11.73% (44/375) 🎉
Increment coverage report
Complete coverage report

// partition key should on child tuple, sort key should on partition top's tuple
TupleDescriptor sortTuple = generateTupleDesc(partitionTopN.child().getOutput(), null, context);
final SortInfo sortInfo = getSortInfo(partitionTopN.getOrderKeys(), context, sortTuple);
List<OrderKey> orderKeys = prunePartitionKeys(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should impl this feature in org.apache.doris.nereids.trees.plans.logical.LogicalWindow#pushPartitionLimitThroughWindow

@CalvinKirs
CalvinKirs force-pushed the range_partition_key_ck branch from 3fed697 to fa14cc5 Compare May 25, 2026 07:01
@CalvinKirs

Copy link
Copy Markdown
Member Author

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-H: Total hot run time: 31516 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit fa14cc5f42c40fbd080491c3d6866ad50e8b9795, 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	17773	3989	4003	3989
q2	q3	10801	1411	823	823
q4	4677	478	349	349
q5	7619	2257	2130	2130
q6	281	186	140	140
q7	971	790	643	643
q8	9365	1660	1603	1603
q9	6710	4967	4964	4964
q10	6431	2256	1879	1879
q11	452	282	252	252
q12	698	426	296	296
q13	18245	3465	2807	2807
q14	264	256	238	238
q15	q16	829	798	710	710
q17	923	962	991	962
q18	6944	5710	5600	5600
q19	1200	1292	1129	1129
q20	529	406	266	266
q21	5653	2651	2428	2428
q22	434	361	308	308
Total cold run time: 100799 ms
Total hot run time: 31516 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	4324	4238	4302	4238
q2	q3	4533	4913	4345	4345
q4	2131	2217	1402	1402
q5	4451	4316	4775	4316
q6	261	218	158	158
q7	2123	1836	1693	1693
q8	2485	2183	2160	2160
q9	7977	8409	7899	7899
q10	4866	4803	4462	4462
q11	581	420	390	390
q12	767	761	544	544
q13	3208	3603	3047	3047
q14	303	323	289	289
q15	q16	755	765	649	649
q17	1386	1367	1355	1355
q18	8012	7460	6793	6793
q19	1112	1135	1105	1105
q20	2237	2231	1952	1952
q21	5349	4589	4451	4451
q22	524	471	422	422
Total cold run time: 57385 ms
Total hot run time: 51670 ms

@hello-stephen

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

query5	4316	655	526	526
query6	331	212	196	196
query7	4321	585	325	325
query8	322	240	225	225
query9	8842	4153	4104	4104
query10	453	346	305	305
query11	5830	2611	2274	2274
query12	183	127	130	127
query13	1302	651	439	439
query14	6114	5545	5205	5205
query14_1	4553	4530	4501	4501
query15	213	212	189	189
query16	1024	466	464	464
query17	1158	746	616	616
query18	2743	496	379	379
query19	227	202	173	173
query20	149	135	133	133
query21	225	152	121	121
query22	13535	13613	13407	13407
query23	17473	16549	16320	16320
query23_1	16289	16440	16456	16440
query24	7351	1799	1309	1309
query24_1	1326	1305	1367	1305
query25	590	507	443	443
query26	1313	320	172	172
query27	2697	598	344	344
query28	4441	2047	2045	2045
query29	1038	635	549	549
query30	298	242	203	203
query31	1121	1086	946	946
query32	88	72	76	72
query33	533	352	287	287
query34	1222	1169	668	668
query35	766	809	681	681
query36	1429	1399	1258	1258
query37	150	103	91	91
query38	3227	3180	3066	3066
query39	915	919	898	898
query39_1	898	862	888	862
query40	237	143	125	125
query41	85	68	62	62
query42	110	110	116	110
query43	333	338	297	297
query44	
query45	214	207	197	197
query46	1109	1216	708	708
query47	2421	2438	2211	2211
query48	411	414	310	310
query49	634	497	397	397
query50	1010	362	259	259
query51	4338	4382	4198	4198
query52	106	110	95	95
query53	255	277	201	201
query54	315	288	261	261
query55	95	89	84	84
query56	294	300	297	297
query57	1460	1422	1332	1332
query58	298	267	262	262
query59	1645	1692	1483	1483
query60	323	357	313	313
query61	160	158	163	158
query62	693	651	594	594
query63	242	200	205	200
query64	2394	813	658	658
query65	
query66	1665	485	351	351
query67	30333	30092	29930	29930
query68	
query69	464	352	294	294
query70	1090	1049	1025	1025
query71	309	275	264	264
query72	2941	2663	2396	2396
query73	844	762	450	450
query74	5100	4918	4804	4804
query75	2692	2615	2267	2267
query76	2272	1138	747	747
query77	395	420	339	339
query78	12289	12518	11923	11923
query79	1450	1018	773	773
query80	651	538	466	466
query81	459	288	248	248
query82	1382	166	125	125
query83	360	279	251	251
query84	266	144	110	110
query85	885	561	475	475
query86	386	329	333	329
query87	3402	3404	3275	3275
query88	3623	2796	2749	2749
query89	438	394	344	344
query90	1973	186	179	179
query91	179	175	139	139
query92	78	81	72	72
query93	1488	1393	897	897
query94	540	371	338	338
query95	691	426	349	349
query96	1035	815	353	353
query97	2767	2763	2577	2577
query98	235	226	223	223
query99	1190	1158	1006	1006
Total cold run time: 255183 ms
Total hot run time: 173195 ms

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 92.31% (12/13) 🎉
Increment coverage report
Complete coverage report

@hello-stephen

Copy link
Copy Markdown
Contributor

FE UT Coverage Report

Increment line coverage 92.31% (12/13) 🎉
Increment coverage report
Complete coverage report

@CalvinKirs
CalvinKirs force-pushed the range_partition_key_ck branch from fa14cc5 to c9ea01e Compare May 28, 2026 09:48
### What problem does this PR solve?

Issue Number: N/A

Related PR: N/A

Problem Summary: PartitionTopN could keep partition key expressions in its executable order keys when the same key also appeared in the window ORDER BY. These keys are constant inside each partition, so keeping them leaks redundant order keys into BE SortInfo. This change prunes exact partition-key matches when LogicalWindow creates LogicalPartitionTopN, keeps PhysicalPartitionTopN executable order keys separate from its output order property, and leaves PhysicalPlanTranslator as a pure translator of physical plan fields.

### Release note

None

### Check List (For Author)

- Test: Unit Test

    - Unit Test: ./run-fe-ut.sh --run org.apache.doris.nereids.rules.implementation.ImplementationTest#physicalPartitionTopNSeparatesSortKeysFromOutputOrderKeys

    - Unit Test: ./run-fe-ut.sh --run org.apache.doris.nereids.rules.rewrite.GeneratePartitionTopnFromWindowTest

    - Unit Test: ./run-fe-ut.sh --run org.apache.doris.nereids.glue.translator.PhysicalPlanTranslatorTest#testPartitionTopNPrunesPartitionKeyFromSortInfo

    - Static check: git diff --check

    - Regression Test: Not run in this turn. The explain regression case is covered by planner/unit assertions here; no FE/BE regression cluster was started.

- Behavior changed: Yes. PartitionTopN no longer sends redundant partition key expressions to BE SortInfo when they exactly match partition keys; physical output ordering still advertises partition keys for property derivation.

- Does this need documentation: No
@CalvinKirs
CalvinKirs force-pushed the range_partition_key_ck branch from c9ea01e to 84dafa4 Compare May 28, 2026 10:37
@CalvinKirs

Copy link
Copy Markdown
Member Author

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-H: Total hot run time: 32231 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit 84dafa4a4b5f158bc338591168990c0b37e88836, 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	17645	4159	4147	4147
q2	q3	10762	1417	796	796
q4	4687	473	352	352
q5	7608	2268	2164	2164
q6	248	183	143	143
q7	974	796	630	630
q8	9357	1752	1663	1663
q9	5235	4967	4987	4967
q10	6373	2203	1882	1882
q11	457	283	250	250
q12	646	438	306	306
q13	18116	3369	2776	2776
q14	273	261	243	243
q15	q16	838	757	721	721
q17	927	1083	944	944
q18	7052	5913	5493	5493
q19	1349	1322	1266	1266
q20	558	480	301	301
q21	6394	2975	2875	2875
q22	457	382	312	312
Total cold run time: 99956 ms
Total hot run time: 32231 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	5050	4953	4878	4878
q2	q3	5055	5257	4649	4649
q4	2147	2238	1422	1422
q5	4980	4841	4809	4809
q6	253	193	140	140
q7	1937	1788	1561	1561
q8	2515	2235	2292	2235
q9	7919	7451	7420	7420
q10	4759	4660	4226	4226
q11	549	412	392	392
q12	741	746	541	541
q13	3084	3411	2794	2794
q14	282	281	246	246
q15	q16	687	718	632	632
q17	1316	1271	1268	1268
q18	7335	7145	6886	6886
q19	1120	1106	1104	1104
q20	2231	2239	1964	1964
q21	5413	4846	4584	4584
q22	546	478	416	416
Total cold run time: 57919 ms
Total hot run time: 52167 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 172233 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 84dafa4a4b5f158bc338591168990c0b37e88836, data reload: false

query5	4319	679	530	530
query6	355	236	205	205
query7	4238	581	329	329
query8	336	250	234	234
query9	8764	4133	4103	4103
query10	461	342	312	312
query11	5756	2564	2221	2221
query12	193	132	128	128
query13	1277	619	483	483
query14	6113	5498	5187	5187
query14_1	4538	4517	4444	4444
query15	216	202	185	185
query16	1026	433	423	423
query17	1132	741	583	583
query18	2618	504	356	356
query19	211	206	159	159
query20	136	133	137	133
query21	210	144	121	121
query22	13685	13644	13319	13319
query23	17411	16604	16226	16226
query23_1	16265	16316	16367	16316
query24	7454	1824	1328	1328
query24_1	1349	1327	1306	1306
query25	591	518	474	474
query26	1316	335	180	180
query27	2674	568	353	353
query28	4429	2047	2000	2000
query29	1012	666	523	523
query30	312	244	202	202
query31	1134	1089	965	965
query32	99	82	82	82
query33	563	375	312	312
query34	1190	1165	669	669
query35	794	842	691	691
query36	1395	1422	1284	1284
query37	162	113	100	100
query38	3267	3168	3112	3112
query39	937	915	892	892
query39_1	898	865	876	865
query40	238	155	131	131
query41	76	69	69	69
query42	115	114	111	111
query43	343	354	313	313
query44	
query45	220	207	201	201
query46	1111	1196	756	756
query47	2349	2406	2257	2257
query48	440	422	337	337
query49	673	509	414	414
query50	1045	367	262	262
query51	4424	4315	4268	4268
query52	108	110	99	99
query53	267	291	208	208
query54	368	294	279	279
query55	98	100	92	92
query56	329	322	338	322
query57	1474	1427	1335	1335
query58	313	295	290	290
query59	1599	1749	1522	1522
query60	340	374	316	316
query61	160	153	157	153
query62	699	654	591	591
query63	249	205	214	205
query64	2427	828	647	647
query65	
query66	1678	489	358	358
query67	29751	29687	29489	29489
query68	
query69	469	355	306	306
query70	1076	1016	1038	1016
query71	310	289	268	268
query72	3126	2679	2485	2485
query73	868	765	452	452
query74	5147	4966	4782	4782
query75	2707	2605	2275	2275
query76	2274	1182	779	779
query77	408	427	360	360
query78	12360	12417	11854	11854
query79	1473	1059	757	757
query80	669	555	457	457
query81	461	285	245	245
query82	1422	157	123	123
query83	354	279	255	255
query84	262	144	112	112
query85	892	539	455	455
query86	428	341	335	335
query87	3439	3411	3280	3280
query88	3654	2749	2717	2717
query89	442	406	350	350
query90	1972	192	186	186
query91	181	168	148	148
query92	83	80	74	74
query93	1509	1464	875	875
query94	545	344	279	279
query95	683	490	346	346
query96	1008	816	337	337
query97	2706	2704	2588	2588
query98	236	237	234	234
query99	1178	1151	1027	1027
Total cold run time: 254994 ms
Total hot run time: 172233 ms

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 37.25% (19/51) 🎉
Increment coverage report
Complete coverage report

@morrySnow

Copy link
Copy Markdown
Contributor

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review result: no blocking issues found.

Critical checkpoint conclusions:

  • Goal and proof: The PR separates executable PartitionTopN sort keys from the property-facing output order so exact partition-key duplicates are not sent to BE SortInfo. Added FE planner/unit tests and explain regression coverage exercise pruning and the self-join non-pruning case.
  • Scope: The change is focused on LogicalWindow creation, logical-to-physical conversion, physical property derivation, and targeted tests.
  • Concurrency and lifecycle: No new concurrency, locking, static lifecycle, or persistence behavior is introduced.
  • Configuration and compatibility: No new config, protocol field, storage format, or rolling-upgrade compatibility concern identified.
  • Parallel paths: The translator now consumes PhysicalPartitionTopN executable order keys directly, while output-order property derivation keeps the partition keys for the existing window property contract. I did not find another PartitionTopN construction path that needs a separate change.
  • Error handling and observability: No new error-handling or logging path is introduced.
  • Transaction/data correctness: No transaction or storage visibility behavior is touched. For rank/dense_rank and row_number cases reviewed, pruning exact partition-key order expressions is semantically safe because those expressions are constant inside each partition. The self-join case keeps distinct ExprIds and is covered.
  • Performance: The change removes redundant BE sort comparisons for exact duplicate partition/order expressions without adding meaningful overhead.
  • Tests: Tests cover logical pruning, physical key separation, translator SortInfo pruning, explain output, and the distinct-slot self-join guard. I did not run the test suite during this automated review.

User focus response: No additional user-provided focus points were present.

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 11.31% (19/168) 🎉
Increment coverage report
Complete coverage report

### What problem does this PR solve?

Issue Number: None

Related PR: None

Problem Summary: Window required order properties kept duplicate partition keys when order keys already contained the same expressions. This could make the two-phase global PartitionTopN output property fail to satisfy the upper Window and introduce an unnecessary sort.

### Release note

None

### Check List (For Author)

- Test: Regression test / Manual test
    - ./run-regression-test.sh --run -d query_p0/explain -s test_global_partition_topn_plan -c jdbc:mysql://127.0.0.1:27030/?useLocalSessionState=true\&allowLoadLocalInfile=true\&zeroDateTimeBehavior=round -ha 127.0.0.1:26030 -u root
    - Manual EXPLAIN verified VPartitionTopN order by excludes partition key and no PhysicalQuickSort in the two-phase global shape plan
- Behavior changed: Yes. Avoids redundant sort property requirements when Window order keys duplicate partition keys.
- Does this need documentation: No
### What problem does this PR solve?

Issue Number: None

Related PR: None

Problem Summary: Clarify why Window physical required properties prune order keys that duplicate partition keys, and how this avoids redundant sort insertion above PartitionTopN.

### Release note

None

### Check List (For Author)

- Test: FE style check
    - mvn -pl fe-core -am validate -DskipTests
- Behavior changed: No
- Does this need documentation: No
@CalvinKirs

Copy link
Copy Markdown
Member Author

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 15.45% (19/123) 🎉
Increment coverage report
Complete coverage report

@CalvinKirs

Copy link
Copy Markdown
Member Author

run buildall

@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17582	4056	4030	4030
q2	2062	316	185	185
q3	10286	1517	805	805
q4	4676	465	341	341
q5	7558	859	590	590
q6	180	169	136	136
q7	770	847	627	627
q8	9322	1632	1651	1632
q9	5543	4556	4553	4553
q10	6779	1800	1531	1531
q11	437	273	251	251
q12	631	430	296	296
q13	18082	3420	2829	2829
q14	268	261	237	237
q15	q16	801	779	711	711
q17	1045	1009	1002	1002
q18	6958	5605	5463	5463
q19	1287	1345	1058	1058
q20	505	419	280	280
q21	6277	2898	2594	2594
q22	454	387	316	316
Total cold run time: 101503 ms
Total hot run time: 29467 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	5085	4853	4783	4783
q2	345	385	228	228
q3	4969	5248	4691	4691
q4	2071	2160	1422	1422
q5	4910	4650	4672	4650
q6	228	178	128	128
q7	1932	1705	1558	1558
q8	2390	2108	2077	2077
q9	7944	7533	7597	7533
q10	4697	4674	4178	4178
q11	524	381	348	348
q12	740	737	544	544
q13	2979	3352	2766	2766
q14	284	286	255	255
q15	q16	676	691	610	610
q17	1285	1250	1249	1249
q18	7388	6623	6768	6623
q19	1108	1124	1112	1112
q20	2231	2226	1969	1969
q21	5265	4576	4474	4474
q22	514	460	427	427
Total cold run time: 57565 ms
Total hot run time: 51625 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 171757 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 873a17ce987942a08697e56478cbcc2228391a21, data reload: false

query5	4342	641	493	493
query6	446	197	199	197
query7	4841	553	312	312
query8	336	185	166	166
query9	8757	4025	4026	4025
query10	440	312	264	264
query11	5929	2360	2165	2165
query12	165	105	99	99
query13	1264	617	438	438
query14	6288	5290	4963	4963
query14_1	4292	4281	4282	4281
query15	214	206	186	186
query16	1073	474	458	458
query17	1183	721	555	555
query18	2581	478	336	336
query19	205	205	162	162
query20	116	107	104	104
query21	221	138	115	115
query22	13523	13679	13294	13294
query23	17369	16602	16244	16244
query23_1	16221	16157	16315	16157
query24	7460	1772	1283	1283
query24_1	1299	1309	1277	1277
query25	558	438	360	360
query26	1314	325	169	169
query27	2592	561	321	321
query28	4392	2048	2019	2019
query29	1070	620	484	484
query30	317	247	203	203
query31	1113	1082	940	940
query32	129	61	60	60
query33	523	324	252	252
query34	1180	1163	636	636
query35	772	769	674	674
query36	1376	1355	1213	1213
query37	156	108	90	90
query38	1896	1733	1662	1662
query39	923	920	891	891
query39_1	864	879	870	870
query40	230	132	107	107
query41	69	73	68	68
query42	90	90	97	90
query43	323	329	288	288
query44	1486	801	803	801
query45	215	201	182	182
query46	1086	1242	761	761
query47	2378	2328	2236	2236
query48	419	433	313	313
query49	601	458	328	328
query50	987	359	277	277
query51	4441	4387	4391	4387
query52	84	84	75	75
query53	261	268	195	195
query54	300	249	236	236
query55	76	72	72	72
query56	258	243	232	232
query57	1424	1428	1316	1316
query58	269	226	223	223
query59	1552	1611	1417	1417
query60	296	260	244	244
query61	176	178	174	174
query62	725	648	594	594
query63	232	194	213	194
query64	2560	807	669	669
query65	4919	4814	4787	4787
query66	1858	473	343	343
query67	28827	28735	28614	28614
query68	3217	1566	993	993
query69	408	303	274	274
query70	1049	960	972	960
query71	298	241	216	216
query72	2872	2611	2348	2348
query73	826	813	452	452
query74	5144	4955	4783	4783
query75	2583	2544	2188	2188
query76	2296	1204	817	817
query77	364	390	291	291
query78	12407	12461	11998	11998
query79	1400	1235	771	771
query80	603	488	405	405
query81	459	282	240	240
query82	921	163	123	123
query83	353	285	250	250
query84	273	151	113	113
query85	858	541	402	402
query86	379	317	280	280
query87	1852	1830	1775	1775
query88	3785	2822	2784	2784
query89	449	392	332	332
query90	1945	184	188	184
query91	173	162	136	136
query92	66	62	57	57
query93	1528	1508	870	870
query94	552	364	340	340
query95	684	371	370	370
query96	1008	817	370	370
query97	2688	2683	2569	2569
query98	220	207	202	202
query99	1174	1163	1055	1055
Total cold run time: 257144 ms
Total hot run time: 171757 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
ClickBench: Total hot run time: 25.14 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit 873a17ce987942a08697e56478cbcc2228391a21, data reload: false

query1	0.01	0.01	0.00
query2	0.10	0.05	0.05
query3	0.26	0.13	0.13
query4	1.60	0.14	0.15
query5	0.24	0.23	0.22
query6	1.22	1.11	1.04
query7	0.04	0.01	0.01
query8	0.06	0.04	0.03
query9	0.38	0.30	0.30
query10	0.55	0.59	0.55
query11	0.19	0.13	0.14
query12	0.18	0.14	0.16
query13	0.47	0.48	0.48
query14	1.01	1.03	1.00
query15	0.61	0.60	0.60
query16	0.31	0.33	0.33
query17	1.12	1.15	1.10
query18	0.23	0.21	0.21
query19	2.02	1.88	1.93
query20	0.02	0.02	0.01
query21	15.46	0.23	0.14
query22	4.83	0.06	0.05
query23	16.16	0.31	0.12
query24	2.92	0.41	0.33
query25	0.10	0.05	0.04
query26	0.73	0.20	0.15
query27	0.04	0.04	0.04
query28	3.55	0.93	0.52
query29	12.50	4.32	3.47
query30	0.28	0.15	0.15
query31	2.77	0.62	0.31
query32	3.22	0.60	0.48
query33	3.23	3.21	3.20
query34	15.59	4.21	3.52
query35	3.52	3.50	3.50
query36	0.57	0.43	0.43
query37	0.08	0.06	0.06
query38	0.05	0.04	0.03
query39	0.04	0.03	0.03
query40	0.18	0.15	0.16
query41	0.08	0.03	0.02
query42	0.04	0.02	0.02
query43	0.04	0.03	0.04
Total cold run time: 96.6 s
Total hot run time: 25.14 s

@hello-stephen

Copy link
Copy Markdown
Contributor

FE UT Coverage Report

Increment line coverage 84.21% (16/19) 🎉
Increment coverage report
Complete coverage report

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 54.29% (19/35) 🎉
Increment coverage report
Complete coverage report

@CalvinKirs

Copy link
Copy Markdown
Member Author

run p0

…topn rule reorder

Removing CreatePartitionTopNFromWindow from the bottom-up PUSH_DOWN_FILTERS
rule set changes rewrite timing for query44, which has no partition-by
window key. The two symmetric branches (asceding/i1 vs descending/i2) now
tie-break to the opposite side in the cost-based join reorder; verified
against a live matched FE+BE cluster that this is a pure left/right swap
of two structurally identical subtrees, with no change to PartitionTopN
itself. query67 (the actual partition-key-pruning target) is unaffected
across all shape_check variants.
@CalvinKirs

Copy link
Copy Markdown
Member Author

run buildall

@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17656	3944	3931	3931
q2	2019	309	193	193
q3	10509	1349	840	840
q4	4682	478	332	332
q5	7523	854	564	564
q6	180	167	133	133
q7	786	859	628	628
q8	9572	1673	1655	1655
q9	6198	4405	4411	4405
q10	6769	1813	1549	1549
q11	503	337	313	313
q12	732	558	432	432
q13	18136	3279	2710	2710
q14	265	267	251	251
q15	q16	784	788	716	716
q17	1014	956	998	956
q18	6846	5711	5575	5575
q19	1221	1258	1124	1124
q20	792	657	582	582
q21	5734	2706	2518	2518
q22	444	360	297	297
Total cold run time: 102365 ms
Total hot run time: 29704 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	4324	4233	4205	4205
q2	290	323	210	210
q3	4583	4917	4436	4436
q4	2055	2138	1382	1382
q5	4426	4320	4308	4308
q6	236	177	125	125
q7	1730	2007	1740	1740
q8	2441	2168	2071	2071
q9	7791	7718	7767	7718
q10	4790	4774	4318	4318
q11	579	410	390	390
q12	738	748	525	525
q13	3445	3637	2991	2991
q14	288	296	284	284
q15	q16	727	753	673	673
q17	1317	1304	1321	1304
q18	8041	7319	7127	7127
q19	1137	1080	1118	1080
q20	2214	2214	1935	1935
q21	5229	4531	4423	4423
q22	522	464	411	411
Total cold run time: 56903 ms
Total hot run time: 51656 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 174061 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 02ebe0fb80678060747c4a33476c2a5319a08c80, data reload: false

query5	4310	627	502	502
query6	454	230	211	211
query7	4834	611	356	356
query8	348	187	174	174
query9	8758	4020	4009	4009
query10	467	351	306	306
query11	5839	2372	2130	2130
query12	156	104	99	99
query13	1250	600	430	430
query14	6282	5308	4940	4940
query14_1	4327	4355	4460	4355
query15	221	207	197	197
query16	1007	486	458	458
query17	939	754	593	593
query18	2453	476	353	353
query19	206	196	155	155
query20	113	108	109	108
query21	234	158	132	132
query22	13611	13631	13324	13324
query23	17380	16643	16539	16539
query23_1	16582	16508	16236	16236
query24	7587	1769	1336	1336
query24_1	1336	1283	1331	1283
query25	570	477	403	403
query26	1349	366	210	210
query27	2618	566	378	378
query28	4482	2105	1994	1994
query29	1053	606	467	467
query30	331	254	228	228
query31	1123	1089	978	978
query32	109	58	59	58
query33	532	317	249	249
query34	1187	1144	670	670
query35	761	807	681	681
query36	1420	1447	1194	1194
query37	152	105	91	91
query38	1885	1707	1641	1641
query39	918	914	904	904
query39_1	873	888	885	885
query40	244	165	136	136
query41	64	60	64	60
query42	90	90	89	89
query43	320	326	276	276
query44	1401	802	813	802
query45	195	201	185	185
query46	1122	1228	740	740
query47	2359	2334	2256	2256
query48	411	413	283	283
query49	580	424	333	333
query50	1133	410	334	334
query51	4480	4424	4344	4344
query52	86	87	77	77
query53	269	280	204	204
query54	268	236	223	223
query55	74	72	73	72
query56	287	273	286	273
query57	1436	1407	1323	1323
query58	294	250	256	250
query59	1615	1641	1436	1436
query60	300	261	251	251
query61	154	146	143	143
query62	707	646	590	590
query63	242	208	209	208
query64	2553	743	605	605
query65	4901	4750	4746	4746
query66	1836	509	389	389
query67	29621	29699	28765	28765
query68	3143	1555	974	974
query69	413	305	268	268
query70	1066	935	1006	935
query71	344	332	312	312
query72	2838	2614	2310	2310
query73	823	783	431	431
query74	5126	4959	4774	4774
query75	2613	2592	2229	2229
query76	2322	1182	823	823
query77	356	393	284	284
query78	12333	12453	12014	12014
query79	1389	1113	762	762
query80	714	526	467	467
query81	474	339	282	282
query82	566	163	121	121
query83	417	319	289	289
query84	318	161	127	127
query85	931	572	510	510
query86	381	302	275	275
query87	1846	1836	1755	1755
query88	3758	2842	2827	2827
query89	454	395	365	365
query90	1814	197	192	192
query91	200	191	157	157
query92	64	56	67	56
query93	1562	1630	936	936
query94	599	362	336	336
query95	789	568	468	468
query96	1137	774	353	353
query97	2709	2695	2585	2585
query98	213	205	197	197
query99	1175	1149	1027	1027
Total cold run time: 258416 ms
Total hot run time: 174061 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
ClickBench: Total hot run time: 25.07 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit 02ebe0fb80678060747c4a33476c2a5319a08c80, data reload: false

query1	0.01	0.01	0.01
query2	0.10	0.06	0.05
query3	0.26	0.13	0.13
query4	1.61	0.14	0.13
query5	0.24	0.27	0.23
query6	1.25	1.10	1.05
query7	0.03	0.00	0.00
query8	0.06	0.03	0.04
query9	0.38	0.33	0.31
query10	0.56	0.57	0.59
query11	0.20	0.14	0.14
query12	0.18	0.16	0.14
query13	0.48	0.48	0.49
query14	1.01	0.99	1.00
query15	0.62	0.58	0.61
query16	0.32	0.32	0.31
query17	1.11	1.10	1.04
query18	0.23	0.21	0.22
query19	2.03	1.98	1.91
query20	0.02	0.01	0.02
query21	15.43	0.21	0.14
query22	4.86	0.06	0.06
query23	16.13	0.31	0.13
query24	2.92	0.42	0.32
query25	0.09	0.06	0.04
query26	0.74	0.20	0.14
query27	0.04	0.04	0.03
query28	3.48	0.88	0.53
query29	12.47	4.29	3.45
query30	0.30	0.16	0.15
query31	2.77	0.58	0.31
query32	3.23	0.60	0.49
query33	3.20	3.23	3.19
query34	15.58	4.25	3.46
query35	3.48	3.53	3.51
query36	0.55	0.43	0.43
query37	0.10	0.06	0.07
query38	0.05	0.03	0.04
query39	0.05	0.03	0.03
query40	0.18	0.16	0.15
query41	0.08	0.03	0.03
query42	0.04	0.03	0.03
query43	0.04	0.04	0.04
Total cold run time: 96.51 s
Total hot run time: 25.07 s

@CalvinKirs

Copy link
Copy Markdown
Member Author

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review summary for PR 63379 (head 02ebe0f).

Review opinion: I did not find a new, non-duplicate blocking issue in the current head. The changed PartitionTopN split keeps the full output order property via PhysicalPartitionTopN#getOutputOrderKeys()/ChildOutputPropertyDeriver, while PhysicalPlanTranslator sends only the pruned executable order keys to BE PartitionSortNode. I also traced the BE rank/dense-rank path for empty executable sort keys; it treats all rows as one peer group, so I did not find a distinct truncation issue beyond the already-open semantic threads.

Critical checkpoints:

  • Goal and proof: the PR aims to prune partition keys from PartitionTopN sort keys without losing the parent window's required order. The code and added coverage address logical pruning, physical output-order declaration, translator SortInfo pruning, and explain shape coverage.
  • Scope: the change is focused on Nereids window/PartitionTopN order-key handling plus related tests and generated shape baselines.
  • Concurrency/lifecycle/config/compatibility/persistence/writes: I found no new concurrency, lifecycle, config item, protocol/storage-format, persistence, or data-write path introduced by this PR.
  • Parallel paths/performance: I checked RuleSet/Rewriter, LogicalWindow#getPushDownWindowFuncAndLimit, request/output property derivation, logical-to-physical PartitionTopN, FE-to-BE translation, and BE PartitionSorter behavior.
  • Tests/results: the changed query44 shape outputs are paired branch/name swaps, and git diff --check FETCH_HEAD..HEAD -- is clean. I did not run FE UTs or regression tests because this checkout is missing thirdparty/installed and thirdparty/installed/bin/protoc, which blocks the FE build prerequisite.

Duplicate-suppressed existing review context:

  • EliminateOrderByKey explicit/non-ranking ROWS and default-ROWS concerns are already covered by discussion_r3417828852 and discussion_r3419555208.
  • The stale EliminateOrderByKeyTest expectation is already covered by discussion_r3450891995.
  • The self-join explain slot-identity false-pass is already covered by discussion_r3459388086.

Subagent conclusions:

  • optimizer-rewrite found no new non-duplicate optimizer/rewrite candidate and returned NO_NEW_VALUABLE_FINDINGS in convergence round 1.
  • tests-session-config found no new non-duplicate test/session/config candidate and returned NO_NEW_VALUABLE_FINDINGS in convergence round 1.
  • Main merged result: no inline comments proposed; final review is summary-only.

User focus: no additional user-provided review focus was present.

@CalvinKirs

Copy link
Copy Markdown
Member Author

run external

@CalvinKirs

Copy link
Copy Markdown
Member Author

run p0

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 44.19% (19/43) 🎉
Increment coverage report
Complete coverage report

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 42.22% (19/45) 🎉
Increment coverage report
Complete coverage report

1 similar comment
@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 42.22% (19/45) 🎉
Increment coverage report
Complete coverage report

@github-actions github-actions Bot added the approved Indicates a PR has been approved by one committer. label Jul 2, 2026
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

PR approved by at least one committer and no changes requested.

@CalvinKirs
CalvinKirs merged commit 07bcd31 into apache:master Jul 3, 2026
33 checks passed
yiguolei pushed a commit that referenced this pull request Sep 20, 2026
…n sort #63379 (#68171)

#63379

---------

Signed-off-by: Aurélien Pupier <[email protected]>
Co-authored-by: Aurélien Pupier <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by one committer. dev/4.2.0-merged reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants