Skip to content

[fix](mtmv) Check agg/window partition keys only for tracked partition outputs#65393

Open
foxtail463 wants to merge 2 commits into
apache:masterfrom
foxtail463:fix/mv-partition-window
Open

[fix](mtmv) Check agg/window partition keys only for tracked partition outputs#65393
foxtail463 wants to merge 2 commits into
apache:masterfrom
foxtail463:fix/mv-partition-window

Conversation

@foxtail463

@foxtail463 foxtail463 commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Problem Summary:
MV partition tracking could reject valid partitioned MVs when an aggregate, window, or rewritten PartitionTopN exists on a branch that does not carry the MV partition column. A typical case is a left join where the MV partitions by the left table column, while the right subquery has row_number() or group by on unrelated keys.

Solution:
Gate aggregate/window/partition-topn partition-key validation by whether the current plan output contains a tracked MV partition slot. Add LogicalPartitionTopN handling for rewritten row_number ... rn = 1

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

@foxtail463

Copy link
Copy Markdown
Contributor Author
DROP DATABASE IF EXISTS mv_partition_unrelated_right_branch_repro;
CREATE DATABASE mv_partition_unrelated_right_branch_repro;
USE mv_partition_unrelated_right_branch_repro;

CREATE TABLE mv_pr_left (
    sale_ord_id BIGINT NOT NULL,
    reg_no VARCHAR(20),
    dt DATE NOT NULL
)
DUPLICATE KEY(sale_ord_id, reg_no, dt)
PARTITION BY RANGE(dt) (
    PARTITION p20260701 VALUES [('2026-07-01'), ('2026-07-02'))
)
DISTRIBUTED BY HASH(sale_ord_id) BUCKETS 1
PROPERTIES ("replication_num" = "1");

CREATE TABLE mv_pr_right (
    order_id BIGINT NOT NULL,
    erp VARCHAR(20),
    dept_2_new VARCHAR(20),
    create_time DATETIME,
    dt DATE NOT NULL,
    is_bypass_tp_ic_cmpl TINYINT
)
DUPLICATE KEY(order_id, erp, dept_2_new, create_time, dt)
PARTITION BY RANGE(dt) (
    PARTITION p20260701 VALUES [('2026-07-01'), ('2026-07-02'))
)
DISTRIBUTED BY HASH(order_id) BUCKETS 1
PROPERTIES ("replication_num" = "1");

INSERT INTO mv_pr_left VALUES
    (1, 'r1', '2026-07-01'),
    (2, 'r2', '2026-07-01');

INSERT INTO mv_pr_right VALUES
    (1, 'e1_old', 'd1', '2026-07-01 10:00:00', '2026-07-01', 1),
    (1, 'e1_new', 'd2', '2026-07-01 11:00:00', '2026-07-01', 1),
    (2, 'e2', 'd3', '2026-07-01 12:00:00', '2026-07-01', 1);

DROP MATERIALIZED VIEW IF EXISTS mv_pr_right_window_rn;
CREATE MATERIALIZED VIEW mv_pr_right_window_rn
    BUILD IMMEDIATE REFRESH ON MANUAL
    PARTITION BY (dt)
    DISTRIBUTED BY RANDOM BUCKETS 1
    PROPERTIES ("replication_num" = "1")
AS
SELECT
    a.reg_no,
    a.sale_ord_id,
    a.dt,
    r.erp,
    r.dept_2_new
FROM mv_pr_left AS a
LEFT JOIN (
    SELECT
        order_id,
        erp,
        dept_2_new,
        ROW_NUMBER() OVER (PARTITION BY order_id ORDER BY create_time DESC NULLS LAST) AS rn,
        dt AS r_dt
    FROM mv_pr_right
    WHERE dt = '2026-07-01' AND is_bypass_tp_ic_cmpl = 1
) AS r
    ON a.sale_ord_id = r.order_id
    AND r.rn = 1
WHERE a.dt = '2026-07-01';

DROP MATERIALIZED VIEW IF EXISTS mv_pr_right_agg;
CREATE MATERIALIZED VIEW mv_pr_right_agg
    BUILD IMMEDIATE REFRESH ON MANUAL
    PARTITION BY (dt)
    DISTRIBUTED BY RANDOM BUCKETS 1
    PROPERTIES ("replication_num" = "1")
AS
SELECT
    a.reg_no,
    a.sale_ord_id,
    a.dt,
    r.max_create_time
FROM mv_pr_left AS a
LEFT JOIN (
    SELECT
        order_id,
        MAX(create_time) AS max_create_time
    FROM mv_pr_right
    WHERE dt = '2026-07-01'
    GROUP BY order_id
) AS r
    ON a.sale_ord_id = r.order_id
WHERE a.dt = '2026-07-01';

SHOW CREATE MATERIALIZED VIEW mv_pr_right_window_rn;
SHOW CREATE MATERIALIZED VIEW mv_pr_right_agg;

@foxtail463

Copy link
Copy Markdown
Contributor Author

run buildall

@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17619	4040	3999	3999
q2	2115	317	200	200
q3	10208	1464	791	791
q4	4691	482	342	342
q5	7621	877	570	570
q6	181	173	134	134
q7	747	848	622	622
q8	9369	1575	1532	1532
q9	5694	4370	4352	4352
q10	6736	1791	1534	1534
q11	495	340	310	310
q12	722	552	420	420
q13	18118	3460	2679	2679
q14	267	256	235	235
q15	q16	796	781	712	712
q17	1000	966	952	952
q18	6968	5729	5498	5498
q19	1333	1261	1093	1093
q20	779	623	547	547
q21	5916	2593	2404	2404
q22	439	357	302	302
Total cold run time: 101814 ms
Total hot run time: 29228 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	4346	4257	4279	4257
q2	287	316	218	218
q3	4577	4919	4458	4458
q4	2031	2172	1361	1361
q5	4408	4308	4292	4292
q6	229	181	126	126
q7	1736	1625	2036	1625
q8	2546	2150	2175	2150
q9	8068	8136	7817	7817
q10	4736	4760	4326	4326
q11	569	406	380	380
q12	779	767	538	538
q13	3229	3615	2943	2943
q14	317	306	279	279
q15	q16	692	736	642	642
q17	1363	1323	1325	1323
q18	7927	7333	7361	7333
q19	1119	1112	1100	1100
q20	2192	2202	1916	1916
q21	5180	4565	4379	4379
q22	526	451	386	386
Total cold run time: 56857 ms
Total hot run time: 51849 ms

@hello-stephen

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

query5	4330	638	482	482
query6	465	217	197	197
query7	4870	607	356	356
query8	369	215	191	191
query9	8777	3986	3969	3969
query10	488	357	301	301
query11	5884	2356	2102	2102
query12	163	103	102	102
query13	1305	596	460	460
query14	6261	5264	4899	4899
query14_1	4258	4220	4243	4220
query15	224	204	176	176
query16	984	481	431	431
query17	932	699	562	562
query18	2419	470	334	334
query19	204	180	143	143
query20	108	104	102	102
query21	229	151	130	130
query22	13693	13698	13334	13334
query23	17423	16566	16067	16067
query23_1	16243	16281	16253	16253
query24	7514	1761	1300	1300
query24_1	1303	1305	1306	1305
query25	588	456	392	392
query26	1354	348	201	201
query27	2617	592	383	383
query28	4495	1986	2009	1986
query29	1143	647	503	503
query30	344	259	234	234
query31	1118	1093	980	980
query32	117	63	63	63
query33	539	327	260	260
query34	1183	1142	678	678
query35	764	805	694	694
query36	1444	1418	1230	1230
query37	157	100	86	86
query38	1850	1710	1622	1622
query39	928	918	872	872
query39_1	886	871	874	871
query40	274	154	134	134
query41	63	64	60	60
query42	91	92	91	91
query43	321	318	275	275
query44	1472	767	789	767
query45	202	193	176	176
query46	1077	1165	753	753
query47	2295	2336	2240	2240
query48	394	410	296	296
query49	570	421	309	309
query50	1040	423	318	318
query51	10562	10718	10697	10697
query52	94	86	84	84
query53	259	269	200	200
query54	271	231	223	223
query55	77	69	66	66
query56	279	289	292	289
query57	1423	1417	1330	1330
query58	285	248	247	247
query59	1583	1650	1411	1411
query60	339	268	252	252
query61	150	148	140	140
query62	703	640	583	583
query63	237	208	204	204
query64	2864	1038	890	890
query65	4863	4779	4805	4779
query66	1821	515	380	380
query67	29514	29440	29314	29314
query68	3210	1640	1053	1053
query69	467	297	265	265
query70	1050	889	945	889
query71	346	330	293	293
query72	3082	2736	2365	2365
query73	830	837	420	420
query74	5106	5007	4733	4733
query75	2620	2570	2227	2227
query76	2353	1182	787	787
query77	356	380	298	298
query78	12316	12282	11715	11715
query79	1274	1139	761	761
query80	612	542	442	442
query81	452	324	277	277
query82	229	156	125	125
query83	313	319	288	288
query84	299	159	122	122
query85	928	603	513	513
query86	341	289	269	269
query87	1821	1817	1749	1749
query88	3646	2768	2765	2765
query89	438	407	359	359
query90	2245	190	194	190
query91	199	191	171	171
query92	65	66	62	62
query93	1536	1478	1003	1003
query94	582	371	324	324
query95	800	499	593	499
query96	997	857	356	356
query97	2704	2701	2583	2583
query98	211	210	206	206
query99	1162	1176	1024	1024
Total cold run time: 264201 ms
Total hot run time: 179480 ms

@hello-stephen

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

query1	0.01	0.00	0.01
query2	0.11	0.05	0.05
query3	0.26	0.13	0.13
query4	1.60	0.14	0.14
query5	0.24	0.22	0.22
query6	1.26	1.13	1.07
query7	0.05	0.01	0.01
query8	0.05	0.03	0.04
query9	0.38	0.31	0.33
query10	0.56	0.55	0.55
query11	0.20	0.14	0.15
query12	0.17	0.15	0.14
query13	0.48	0.49	0.49
query14	1.03	1.02	0.99
query15	0.60	0.59	0.58
query16	0.30	0.32	0.32
query17	1.13	1.09	1.12
query18	0.21	0.21	0.21
query19	2.05	1.87	2.04
query20	0.01	0.01	0.02
query21	15.44	0.20	0.12
query22	4.99	0.06	0.05
query23	16.12	0.31	0.13
query24	2.94	0.41	0.33
query25	0.13	0.07	0.05
query26	0.75	0.21	0.15
query27	0.05	0.03	0.04
query28	3.52	0.94	0.57
query29	12.51	4.10	3.22
query30	0.28	0.15	0.15
query31	2.78	0.59	0.31
query32	3.22	0.59	0.48
query33	3.11	3.24	3.18
query34	15.58	4.23	3.54
query35	3.54	3.54	3.56
query36	0.57	0.45	0.41
query37	0.09	0.06	0.07
query38	0.05	0.04	0.03
query39	0.04	0.03	0.03
query40	0.18	0.15	0.14
query41	0.09	0.03	0.03
query42	0.04	0.03	0.03
query43	0.04	0.03	0.04
Total cold run time: 96.76 s
Total hot run time: 24.96 s

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 57.14% (16/28) 🎉
Increment coverage report
Complete coverage report

@foxtail463

Copy link
Copy Markdown
Contributor Author

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 57.14% (16/28) 🎉
Increment coverage report
Complete coverage report

@morrySnow morrySnow requested a review from seawinde July 9, 2026 08:16
@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17712	4055	4032	4032
q2	2027	321	201	201
q3	10311	1394	827	827
q4	4682	469	345	345
q5	7536	906	580	580
q6	183	167	135	135
q7	799	904	634	634
q8	9392	1683	1624	1624
q9	5656	4447	4424	4424
q10	6737	1848	1509	1509
q11	503	342	314	314
q12	698	551	423	423
q13	18091	3419	2783	2783
q14	273	264	246	246
q15	q16	787	782	712	712
q17	933	1020	1024	1020
q18	7398	5815	5503	5503
q19	1315	1285	1043	1043
q20	809	623	582	582
q21	6045	2631	2425	2425
q22	439	357	299	299
Total cold run time: 102326 ms
Total hot run time: 29661 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	4348	4262	4293	4262
q2	297	313	210	210
q3	4596	4954	4352	4352
q4	2028	2126	1357	1357
q5	4407	4342	4317	4317
q6	223	172	132	132
q7	1737	2181	1735	1735
q8	2654	2199	2173	2173
q9	8041	8152	7727	7727
q10	4791	4743	4247	4247
q11	558	424	375	375
q12	779	755	558	558
q13	3310	3638	3070	3070
q14	294	301	267	267
q15	q16	706	744	651	651
q17	1353	1302	1312	1302
q18	7878	7441	7165	7165
q19	1121	1112	1123	1112
q20	2207	2212	1956	1956
q21	5231	4536	4407	4407
q22	512	454	396	396
Total cold run time: 57071 ms
Total hot run time: 51771 ms

@morrySnow morrySnow 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.

Thank you for this fix! The approach of gating partition-key validation on whether the current plan output actually contains a tracked partition column is sound and correctly handles the described scenario (aggregate/window/PartitionTopN on a branch that doesn't carry the partition column).

I have one inline comment:

PartitionIncrementMaintainer.java, new visitLogicalPartitionTopN method: The fail-reason string "window partition sets doesn't contain the target partition" appears to be copy-pasted from visitLogicalWindow (line 438). In this context, the node being checked is a LogicalPartitionTopN, not a window — the message should reference "partition topN" or similar instead of "window". This would be misleading during debugging when a LogicalPartitionTopN triggers a partition-tracking failure.

Everything else looks good — the refactoring of checkWindowPartition to delegate to checkPartitionKeysContainPartitionToCheck is clean, the new planOutputContainsPartitionColumnToCheck guard is well-commented and applied consistently across all three visitor methods, and the regression test changes correctly reflect the newly-accepted MV patterns.

@hello-stephen

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

query5	4306	643	482	482
query6	467	225	200	200
query7	4882	616	360	360
query8	348	187	166	166
query9	8787	4116	4103	4103
query10	479	343	296	296
query11	5955	2372	2181	2181
query12	160	105	101	101
query13	1260	602	460	460
query14	6231	5298	4989	4989
query14_1	4361	4324	4316	4316
query15	224	209	183	183
query16	1030	474	473	473
query17	980	756	604	604
query18	2441	474	342	342
query19	204	181	148	148
query20	109	106	105	105
query21	230	167	142	142
query22	13686	13636	13430	13430
query23	17487	16583	16052	16052
query23_1	16305	16258	16356	16258
query24	7560	1738	1321	1321
query24_1	1351	1330	1318	1318
query25	547	455	372	372
query26	1344	355	199	199
query27	2667	588	387	387
query28	4491	2025	2012	2012
query29	1061	613	478	478
query30	332	280	230	230
query31	1118	1087	997	997
query32	98	65	68	65
query33	514	325	255	255
query34	1186	1097	658	658
query35	745	795	670	670
query36	1407	1382	1232	1232
query37	149	110	92	92
query38	1875	1654	1629	1629
query39	923	940	894	894
query39_1	882	910	879	879
query40	247	161	145	145
query41	66	69	63	63
query42	101	97	98	97
query43	342	341	288	288
query44	1422	795	779	779
query45	212	197	181	181
query46	1098	1281	770	770
query47	2366	2341	2198	2198
query48	415	439	289	289
query49	604	443	319	319
query50	1106	418	341	341
query51	10545	10593	10696	10593
query52	90	87	83	83
query53	267	297	209	209
query54	311	253	231	231
query55	81	73	68	68
query56	328	296	286	286
query57	1438	1400	1314	1314
query58	329	262	269	262
query59	1622	1648	1483	1483
query60	314	282	265	265
query61	170	179	177	177
query62	695	650	617	617
query63	253	213	215	213
query64	2934	1125	835	835
query65	4874	4817	4783	4783
query66	1869	507	388	388
query67	29604	29515	29328	29328
query68	3245	1638	1066	1066
query69	428	291	279	279
query70	1101	1009	939	939
query71	361	330	301	301
query72	3107	2799	2278	2278
query73	794	771	425	425
query74	5120	4950	4748	4748
query75	2620	2591	2239	2239
query76	2322	1217	802	802
query77	343	384	288	288
query78	12319	12368	11742	11742
query79	1383	1171	740	740
query80	1341	543	483	483
query81	524	326	277	277
query82	624	161	121	121
query83	387	324	289	289
query84	327	154	134	134
query85	956	599	500	500
query86	436	292	290	290
query87	1848	1827	1776	1776
query88	3758	2838	2785	2785
query89	484	400	357	357
query90	1950	205	200	200
query91	199	188	163	163
query92	63	65	53	53
query93	1641	1612	1019	1019
query94	728	339	320	320
query95	787	528	551	528
query96	1103	862	338	338
query97	2731	2686	2539	2539
query98	213	209	204	204
query99	1148	1168	1053	1053
Total cold run time: 266755 ms
Total hot run time: 181432 ms

@hello-stephen

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

query1	0.01	0.00	0.01
query2	0.10	0.05	0.05
query3	0.25	0.13	0.13
query4	1.61	0.14	0.14
query5	0.25	0.22	0.23
query6	1.26	1.10	1.05
query7	0.04	0.00	0.01
query8	0.06	0.04	0.03
query9	0.39	0.31	0.33
query10	0.53	0.58	0.57
query11	0.19	0.15	0.15
query12	0.19	0.14	0.14
query13	0.47	0.49	0.48
query14	1.01	1.00	0.99
query15	0.62	0.59	0.60
query16	0.33	0.33	0.31
query17	1.08	1.13	1.08
query18	0.23	0.22	0.21
query19	2.03	1.94	1.88
query20	0.02	0.01	0.01
query21	15.46	0.21	0.13
query22	4.90	0.06	0.05
query23	16.15	0.30	0.12
query24	3.01	0.40	0.33
query25	0.12	0.05	0.05
query26	0.75	0.21	0.16
query27	0.05	0.04	0.03
query28	3.48	0.97	0.56
query29	12.49	4.06	3.22
query30	0.27	0.16	0.15
query31	2.77	0.59	0.31
query32	3.22	0.59	0.50
query33	3.17	3.20	3.20
query34	15.70	4.26	3.48
query35	3.54	3.49	3.55
query36	0.56	0.43	0.42
query37	0.09	0.07	0.07
query38	0.06	0.04	0.04
query39	0.04	0.03	0.03
query40	0.17	0.16	0.15
query41	0.08	0.03	0.03
query42	0.04	0.03	0.03
query43	0.05	0.04	0.04
Total cold run time: 96.84 s
Total hot run time: 24.93 s

public Void visitLogicalPartitionTopN(LogicalPartitionTopN<? extends Plan> partitionTopN,
PartitionIncrementCheckContext context) {
if (!planOutputContainsPartitionColumnToCheck(partitionTopN, context)) {
return super.visitLogicalPartitionTopN(partitionTopN, context);

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.

The error message says "window partition sets" but this method handles LogicalPartitionTopN, not a window. Consider changing to "PartitionTopN partition keys does not contain the target partition" to avoid misleading debug output.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants