Skip to content

[improvement](be) Keep one NTILE bucket per function in non-spill windows - #69686

Merged
HappenLee merged 1 commit into
apache:masterfrom
mrhhsg:improvement/compact-ntile-nonspill-argument
Oct 29, 2026
Merged

HappenLee merged 1 commit into
apache:masterfrom
mrhhsg:improvement/compact-ntile-nonspill-argument

Conversation

@mrhhsg

@mrhhsg mrhhsg commented Oct 27, 2026

Copy link
Copy Markdown
Member

What problem does this PR solve?

Issue Number: None

Related PR: #68500

Problem Summary: The non-spill analytic sink expanded each NTILE literal bucket argument to every input row and appended it to a buffered argument column. NTILE reads only the first value, so the extra materialization and storage grew with the number of retained rows. Store one bucket value per NTILE function from the first nonempty input Block and retain it when old input rows are pruned. Other window functions continue to use their row-wise argument columns. Query results are unchanged.

Release note

None

Check List (For Author)

  • Test:
    • Unit Test: ASAN AnalyticSinkOperatorTest.* (65 tests), including the new multi-Block, multi-partition pruning case.
    • Regression test: spill_p0/window_function_spill and query_p0/sql_functions/window_functions/test_ntile_function (one suite each, zero failures).
    • Build and static checks: ASAN BE/FE build (including FE Checkstyle), clang-format, check-format, build hygiene, clang-tidy on changed lines, and git diff --check.
  • Behavior changed: No (SQL results are unchanged; NTILE argument storage in the non-spill path is reduced).
  • Does this need documentation: No

### What problem does this PR solve?

Issue Number: None

Related PR: apache#68500

Problem Summary: The non-spill analytic sink expanded each NTILE literal bucket argument once per input row and retained the resulting column, although NTILE reads only its first value. Store one bucket per function on the first nonempty Block and preserve it when pruning buffered rows. Argument storage now stays constant per NTILE function rather than growing with retained input rows; SQL results remain unchanged.

### Release note

None

### Check List (For Author)

- Test: Unit Test and Regression test
    - ASAN BE build and 65 AnalyticSinkOperatorTest cases, including the new multi-Block and pruning case
    - window_function_spill and test_ntile_function SQL regressions
- Behavior changed: No (internal memory use is reduced; SQL results are unchanged)
- 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?

@mrhhsg

mrhhsg commented Oct 27, 2026

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.

No verified correctness or maintainability finding was identified at b9f59b5. Both changed files and the upstream/downstream NTILE execution path were reviewed. No existing inline findings or additional user focus points were supplied; a fresh PR comment readback was also empty.

Critical checkpoint conclusions:

  • Goal and evidence: the non-spill sink copies one positive NTILE bucket per function from the first nonempty Block and preserves it across Blocks and partitions. The new unit test checks two distinct buckets, empty input, a cross-Block partition, pruning, scalar cardinality, and complete output values.
  • Scope and clarity: the implementation is limited to NTILE materialization/pruning, one concrete .cpp include, and one focused unit test. Nearby comments explain the scalar and pruning invariants; lint suppressions are narrow and documented.
  • Concurrency and locking: the scalar belongs to the sink local state. The source consumes completed output Blocks through existing locks; no new shared mutation, lock ordering, or blocking operation is introduced.
  • Lifecycle and memory: insert_from copies the integer into the existing type-matched column storage. Partition resets preserve the bucket; close releases the columns. Existing tracked allocation and reservation paths are retained; no new static initializer or ownership cycle is introduced.
  • Configuration: no configuration item or dynamic-update behavior changes.
  • Compatibility: no function signature, wire/storage format, or external ABI contract changes; no new libc API is introduced. Release artifacts were not built or audited.
  • Thrift/Protobuf: neither directory is changed.
  • Parallel paths: non-NTILE arguments retain row-wise materialization/pruning. The separate spill path already retains one bucket per function and is selected before the changed non-spill path.
  • Conditional checks and errors: empty Blocks cannot initialize the bucket. FE validation, reinforced during final expression translation, requires a positive nonnullable integer literal; ColumnConst unwrapping preserves its concrete width. Expression execution errors remain propagated with RETURN_IF_ERROR. The NTILE evaluator reads only argument index zero.
  • Test coverage: expected NTILE(2)/NTILE(3) outputs and the pruning trigger were checked against the execution logic. Existing FE and SQL tests cover invalid/nonconstant/NULL/unfolded buckets and planner-folding boundaries; existing spill tests cover large buckets and multiple batches.
  • Test results: no result files are changed; the new expected values are consistent with partition sizes 4, 2, 3, 3, 3.
  • Observability: existing aggregate-computation, memory, and pruning counters/timers cover the affected work; no additional critical instrumentation need was identified.
  • Transactions and persistence: no transaction, visibility-version, delete-bitmap, EditLog, replay, or failover behavior changes.
  • Data writes and atomicity: this is query-local window evaluation; no write/commit path changes.
  • FE-BE variables: no new field or variable must be transmitted.
  • Performance: the source establishes bucket storage proportional to the number of NTILE functions, eliminating repeated row expansion and argument buffering. This is a static complexity conclusion, not a measured benchmark.
  • Other risks: all initial scalar/type and pruning/consumer concerns were dismissed with concrete producer/consumer evidence. No unresolved source-level candidate remains.

Validation limits: static inspection only; no builds, tests, benchmarks, style/static-analysis tools, or artifact audits were executed, and author-reported runs were not independently reproduced. The added test directly exercises BIGINT buckets and NTILE-only combinations at the BE_TEST pruning threshold; smaller integer widths, mixed row-wise functions, and the production 256-Block threshold were traced statically rather than exercised here.

Convergence: converged after 2 review rounds, including an unchanged-payload final pass by both complete-review agents and the separate scalar/pruning risk reviewer. No new inline comments or existing blocking comment IDs are warranted.

Review converged after 2 round(s).

@mrhhsg

mrhhsg commented Oct 27, 2026

Copy link
Copy Markdown
Member Author

run buildall

@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17614	5249	5108	5108
q2	2111	330	302	302
q3	10185	1415	843	843
q4	4680	462	337	337
q5	7452	815	534	534
q6	185	182	154	154
q7	757	794	620	620
q8	9248	1396	1127	1127
q9	5582	4265	4225	4225
q10	6826	1292	1008	1008
q11	431	256	235	235
q12	653	415	291	291
q13	18096	3769	3073	3073
q14	276	276	258	258
q15	q16	733	713	682	682
q17	1230	757	582	582
q18	6947	6195	6118	6118
q19	1133	966	620	620
q20	396	349	227	227
q21	5446	2521	2484	2484
q22	398	342	275	275
Total cold run time: 100379 ms
Total hot run time: 29103 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	6048	6162	6205	6162
q2	742	602	596	596
q3	5691	5855	5305	5305
q4	2050	2198	1539	1539
q5	5659	5786	5582	5582
q6	244	193	144	144
q7	2271	2061	1832	1832
q8	3038	2629	2700	2629
q9	8040	7718	7489	7489
q10	3627	3602	3103	3103
q11	597	425	377	377
q12	669	651	502	502
q13	3371	3645	3072	3072
q14	302	280	267	267
q15	q16	690	676	631	631
q17	7726	7054	6971	6971
q18	12961	12225	12826	12225
q19	950	841	838	838
q20	2154	2158	1931	1931
q21	5708	4588	4786	4588
q22	513	477	421	421
Total cold run time: 73051 ms
Total hot run time: 66204 ms

@hello-stephen

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

query5	4312	609	457	457
query6	439	212	193	193
query7	4806	445	230	230
query8	320	166	162	162
query9	8807	3263	3260	3260
query10	484	311	246	246
query11	6062	3882	3569	3569
query12	147	92	87	87
query13	1258	471	331	331
query14	6081	3986	3664	3664
query14_1	3545	3453	3461	3453
query15	207	200	184	184
query16	936	428	448	428
query17	872	670	517	517
query18	2409	412	300	300
query19	188	170	128	128
query20	86	83	81	81
query21	205	127	105	105
query22	12973	13129	12802	12802
query23	13482	12783	12417	12417
query23_1	12549	12480	12457	12457
query24	7303	859	471	471
query24_1	484	482	481	481
query25	510	400	329	329
query26	1003	256	142	142
query27	2740	444	275	275
query28	4598	2044	2041	2041
query29	1426	581	445	445
query30	298	224	184	184
query31	993	948	818	818
query32	138	91	91	91
query33	528	291	235	235
query34	900	867	520	520
query35	739	743	640	640
query36	851	812	731	731
query37	122	96	87	87
query38	1791	1734	1674	1674
query39	785	788	756	756
query39_1	737	723	738	723
query40	252	112	98	98
query41	67	66	63	63
query42	89	85	81	81
query43	343	365	311	311
query44	1395	707	708	707
query45	178	183	160	160
query46	846	984	564	564
query47	2808	2961	2902	2902
query48	299	328	219	219
query49	564	395	294	294
query50	704	296	213	213
query51	10589	10581	10347	10347
query52	80	79	73	73
query53	193	211	149	149
query54	234	211	192	192
query55	71	70	69	69
query56	246	207	225	207
query57	1678	1631	1560	1560
query58	276	266	254	254
query59	2175	2180	1940	1940
query60	287	265	230	230
query61	146	144	147	144
query62	394	355	296	296
query63	202	157	160	157
query64	2426	960	813	813
query65	3367	3282	3267	3267
query66	1744	424	306	306
query67	20197	20145	19798	19798
query68	2992	1034	615	615
query69	378	277	247	247
query70	867	787	769	769
query71	302	237	205	205
query72	2746	2535	2308	2308
query73	533	539	306	306
query74	4647	4509	4402	4402
query75	2353	2306	1927	1927
query76	2318	1036	630	630
query77	355	386	280	280
query78	9608	9590	9237	9237
query79	924	899	549	549
query80	502	409	363	363
query81	524	320	271	271
query82	217	138	108	108
query83	192	201	181	181
query84	293	127	106	106
query85	765	431	366	366
query86	259	241	233	233
query87	1977	1963	1828	1828
query88	3755	2812	2753	2753
query89	323	288	250	250
query90	2094	178	171	171
query91	157	151	125	125
query92	98	90	93	90
query93	1038	1008	572	572
query94	450	314	277	277
query95	587	414	302	302
query96	684	507	242	242
query97	2483	2430	2312	2312
query98	170	165	151	151
query99	762	768	646	646
Total cold run time: 232935 ms
Total hot run time: 151290 ms

@hello-stephen

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

query1	0.00	0.00	0.01
query2	0.10	0.06	0.05
query3	0.26	0.08	0.08
query4	1.61	0.10	0.10
query5	0.24	0.25	0.24
query6	1.18	0.67	0.68
query7	0.04	0.00	0.00
query8	0.06	0.04	0.04
query9	0.40	0.34	0.35
query10	0.62	0.57	0.57
query11	0.19	0.16	0.14
query12	0.18	0.16	0.15
query13	0.49	0.49	0.48
query14	1.02	1.01	1.01
query15	0.63	0.60	0.62
query16	0.35	0.36	0.36
query17	1.19	1.14	1.12
query18	0.23	0.19	0.21
query19	2.04	2.04	1.96
query20	0.02	0.01	0.02
query21	15.51	0.20	0.13
query22	4.90	0.05	0.06
query23	16.29	0.33	0.12
query24	2.78	0.60	0.64
query25	0.10	0.07	0.05
query26	1.50	0.22	0.15
query27	0.07	0.04	0.03
query28	3.79	0.77	0.37
query29	12.51	4.18	3.28
query30	0.29	0.15	0.11
query31	2.77	0.57	0.32
query32	3.22	0.60	0.52
query33	3.21	3.27	3.18
query34	15.46	3.98	3.36
query35	3.35	3.35	3.37
query36	0.54	0.45	0.42
query37	0.09	0.07	0.06
query38	0.05	0.04	0.04
query39	0.04	0.03	0.03
query40	0.17	0.15	0.15
query41	0.09	0.03	0.04
query42	0.03	0.02	0.02
query43	0.04	0.03	0.04
Total cold run time: 97.65 s
Total hot run time: 24.51 s

@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17595	5180	5049	5049
q2	2151	341	259	259
q3	10179	1383	806	806
q4	4680	463	332	332
q5	7432	799	526	526
q6	181	185	148	148
q7	751	801	611	611
q8	9272	1318	1005	1005
q9	5549	4213	4174	4174
q10	6850	1297	1035	1035
q11	444	250	231	231
q12	638	397	278	278
q13	18097	3615	3074	3074
q14	283	265	242	242
q15	q16	716	718	680	680
q17	1197	751	571	571
q18	7115	6182	6107	6107
q19	1249	949	614	614
q20	390	337	226	226
q21	5478	2442	2471	2442
q22	389	333	262	262
Total cold run time: 100636 ms
Total hot run time: 28672 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	5985	5911	6112	5911
q2	716	616	544	544
q3	5739	6416	5211	5211
q4	2046	2154	1577	1577
q5	5721	5829	5752	5752
q6	247	184	144	144
q7	2313	2008	1801	1801
q8	2974	2596	2651	2596
q9	7970	7500	7497	7497
q10	3644	3551	3133	3133
q11	590	423	378	378
q12	673	663	504	504
q13	3309	3566	3048	3048
q14	284	293	269	269
q15	q16	670	676	625	625
q17	7751	7072	6848	6848
q18	12905	12156	12844	12156
q19	896	776	766	766
q20	2141	2178	1905	1905
q21	5608	4590	4690	4590
q22	505	450	410	410
Total cold run time: 72687 ms
Total hot run time: 65665 ms

@hello-stephen

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

query5	4318	593	434	434
query6	415	211	195	195
query7	4797	435	225	225
query8	322	171	153	153
query9	8837	3209	3148	3148
query10	475	328	256	256
query11	6004	3856	3581	3581
query12	157	94	88	88
query13	1248	450	364	364
query14	6122	3862	3671	3671
query14_1	3429	3392	3378	3378
query15	208	200	178	178
query16	946	438	409	409
query17	850	651	529	529
query18	2432	437	310	310
query19	203	180	140	140
query20	84	84	87	84
query21	216	127	107	107
query22	12893	12966	12841	12841
query23	13641	12651	12408	12408
query23_1	12472	12512	12558	12512
query24	7339	826	478	478
query24_1	491	499	489	489
query25	532	428	369	369
query26	1128	259	140	140
query27	2732	441	275	275
query28	4589	2066	2066	2066
query29	1540	607	477	477
query30	303	226	187	187
query31	1022	935	817	817
query32	158	99	102	99
query33	539	306	247	247
query34	884	860	507	507
query35	748	760	644	644
query36	807	798	731	731
query37	128	96	82	82
query38	1791	1727	1640	1640
query39	789	793	743	743
query39_1	729	730	740	730
query40	221	120	100	100
query41	75	68	70	68
query42	89	83	85	83
query43	348	368	312	312
query44	1334	715	725	715
query45	176	192	167	167
query46	835	1015	561	561
query47	2855	3023	2760	2760
query48	291	299	222	222
query49	564	399	298	298
query50	680	264	210	210
query51	10196	10352	10250	10250
query52	77	78	71	71
query53	189	206	149	149
query54	236	206	194	194
query55	71	65	63	63
query56	222	222	209	209
query57	1645	1614	1560	1560
query58	292	268	251	251
query59	2146	2160	1982	1982
query60	304	244	230	230
query61	150	149	142	142
query62	391	329	282	282
query63	184	157	154	154
query64	2589	931	737	737
query65	3323	3274	3236	3236
query66	1745	415	296	296
query67	20218	20200	20141	20141
query68	2971	997	620	620
query69	379	286	249	249
query70	849	775	793	775
query71	303	234	212	212
query72	2767	2631	2310	2310
query73	516	523	296	296
query74	4605	4506	4377	4377
query75	2307	2271	1891	1891
query76	2340	1005	589	589
query77	343	368	263	263
query78	9867	9818	9138	9138
query79	1000	860	542	542
query80	1282	443	342	342
query81	599	311	273	273
query82	768	126	102	102
query83	303	198	185	185
query84	309	129	104	104
query85	855	422	364	364
query86	403	231	236	231
query87	2013	1922	1801	1801
query88	3787	2784	2784	2784
query89	340	278	244	244
query90	1901	166	166	166
query91	162	146	126	126
query92	95	93	86	86
query93	980	993	581	581
query94	656	306	271	271
query95	570	330	306	306
query96	686	488	239	239
query97	2441	2435	2296	2296
query98	168	155	147	147
query99	765	765	651	651
Total cold run time: 235904 ms
Total hot run time: 151117 ms

@hello-stephen

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

query1	0.01	0.01	0.01
query2	0.10	0.05	0.04
query3	0.27	0.09	0.09
query4	1.61	0.11	0.10
query5	0.23	0.24	0.24
query6	1.17	0.67	0.67
query7	0.04	0.01	0.01
query8	0.06	0.04	0.04
query9	0.41	0.35	0.35
query10	0.57	0.58	0.59
query11	0.20	0.15	0.14
query12	0.18	0.15	0.15
query13	0.49	0.49	0.49
query14	1.02	1.02	1.01
query15	0.61	0.60	0.59
query16	0.35	0.36	0.36
query17	1.12	1.13	1.12
query18	0.22	0.17	0.19
query19	2.02	2.02	1.95
query20	0.02	0.02	0.01
query21	15.46	0.20	0.13
query22	4.82	0.05	0.06
query23	16.27	0.31	0.13
query24	3.07	0.73	0.32
query25	0.11	0.05	0.06
query26	1.25	0.21	0.16
query27	0.04	0.04	0.03
query28	3.53	0.75	0.35
query29	12.50	4.22	3.24
query30	0.28	0.13	0.12
query31	2.78	0.56	0.33
query32	3.21	0.60	0.51
query33	3.23	3.18	3.18
query34	15.44	4.02	3.40
query35	3.33	3.37	3.35
query36	0.56	0.44	0.42
query37	0.09	0.07	0.06
query38	0.05	0.03	0.04
query39	0.04	0.02	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.03
Total cold run time: 97.11 s
Total hot run time: 24.23 s

@mrhhsg

mrhhsg commented Oct 27, 2026

Copy link
Copy Markdown
Member Author

run nonConcurrent

@mrhhsg
mrhhsg marked this pull request as ready for review October 28, 2026 00:54

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

Reviewed the NTILE argument producer/consumer and pruning paths. Retaining one positive literal bucket per function preserves existing results and avoids repeated materialization. No blocking findings. A mixed NTILE/SUM pruning test would be a useful follow-up. Current CI checks are passing; local builds and tests were not rerun as part of this review.

@HappenLee
HappenLee merged commit c285974 into apache:master Oct 29, 2026
42 checks passed
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.

3 participants