Skip to content

[fix](nereids) fix limit + offset overflow when pushing down TopN/Limit - #64633

Merged
924060929 merged 1 commit into
apache:masterfrom
924060929:fix-push-down-topn-union-overflow
Jul 2, 2026
Merged

[fix](nereids) fix limit + offset overflow when pushing down TopN/Limit#64633
924060929 merged 1 commit into
apache:masterfrom
924060929:fix-push-down-topn-union-overflow

Conversation

@924060929

@924060929 924060929 commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Proposed changes

When combining a limit and an offset into the number of rows a child must keep — pushing a
TopN/Limit down, building a two-phase sort, splitting a limit into local + global phases, etc. —
the raw limit + offset overflows the long range when both are close to BIGINT_MAX
(e.g. LIMIT 9223372036854775807 OFFSET 9223372036854775807) and wraps to a negative value.

A negative limit is an illegal plan. On the BE side it is reinterpreted as a huge unsigned value
(uint64_t limit = _offset + _limit in the sorter / _heap_size(limit + offset) in HeapSorter),
so a trivial query that should immediately return an empty set instead runs until it hits the query
timeout, or triggers UBSan (signed integer overflow is undefined behavior in C++).

Minimal reproducer (no table required)

select count(*) as c from (
    select id from (
        select 1 as id union all select 2 as id union all select 3 as id
    ) t
    order by id limit 9223372036854775807 offset 9223372036854775807
) s;
  • Original planner, or Nereids with PUSH_DOWN_TOP_N_THROUGH_UNION disabled: returns 0 immediately.
  • Nereids with the rule enabled: times out (before this fix).

Fix

Add Utils.addOverflows(long, long) to detect when limit + offset exceeds the long range.
On overflow, the behavior depends on whether the code path can be declined:

Skip the optimization (can decline — the unoptimized plan is correct):

  • TopN/Limit push-down through Union / Join / Project-Join / Window and their distinct
    variants skip the rewrite; the parent TopN/Limit still applies the original limit/offset.
  • The topn_opt_limit_threshold gates (PushDownTopNThroughJoin / PushDownTopNDistinctThroughJoin /
    PushDownLimitDistinctThroughJoin / LimitAggToTopNAgg / PushTopnToAgg) treat overflow as
    over-threshold and do not fire.
  • The score / vector TopN-into-OlapScan push-downs and the sort-limit-into-OlapScan translation
    in PhysicalPlanTranslator are skipped.
  • CollectLimitAboveConsumer skips recording the consumer row count on overflow, leaving the CTE
    producer unbounded (the consumer's own limit still applies).

Report an error (cannot decline — overflowing limit + offset cannot be correctly represented):

  • LogicalTopNToPhysicalTopN: the two-phase local sort needs limit + offset rows, and even a
    single-phase GATHER_SORT would pass the overflowing values to BE where HeapSorter also computes
    limit + offset (triggering UBSan). Since no valid physical plan can be produced, report an error.
  • SplitLimit: the two-phase local limit needs limit + offset; an unsplit ORIGIN limit is silently
    dropped during translation, and Long.MAX_VALUE is not treated as "unlimited" by the legacy
    PlanNode layer (hasLimit() returns limit > -1). Report an error.
  • MergeLimits.mergeOffset / MergeTopNs: consecutive limits/TopNs must be merged, and the merged
    offset (upperOffset + bottomOffset) cannot be represented on overflow. Report an error.

For non-overflowing inputs the behavior is unchanged.

Defense-in-depth: the LogicalTopN / PhysicalTopN / LogicalLimit / PhysicalLimit
constructors now assert that limit and offset are non-negative. Any future path that reintroduces
an overflowing limit + offset fails fast during planning instead of silently hanging in BE.

Tests

  • UtilsTest#testAddOverflows covers the overflow detection boundary.
  • Regression cases in push_down_top_n_through_union assert that the reproducer (both TopN and Limit
    paths with BIGINT_MAX limit/offset) reports an error instead of timing out.

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

@924060929

Copy link
Copy Markdown
Contributor Author

run buildall

@924060929

Copy link
Copy Markdown
Contributor 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.

Requesting changes because the overflow fix is incomplete.

Critical checkpoint conclusions:

  • Goal/test proof: The PR adds a saturating helper and applies it to several rewrite rules, but it does not accomplish the stated goal of clamping limit + offset everywhere a pushed child/scan/agg limit is derived. A plain TopN can still become a negative local physical TopN in LogicalTopNToPhysicalTopN, and several Limit/TopN parallel paths still use raw addition. The tests cover the helper and one Union reproducer only, so they do not prove the end-to-end fix.
  • Scope/focus: The helper is small and clear, but the applied scope is inconsistent with the PR description and leaves equivalent code paths unfixed.
  • Concurrency/lifecycle/config/compatibility: No new concurrency, lifecycle, config, persistence, FE-BE protocol, or rolling-upgrade compatibility concerns found.
  • Parallel code paths: Not fully handled. Remaining raw arithmetic exists in TopN implementation/post-processing, Limit distinct pushdown, Limit window pushdown, TopN-to-agg, scan sort/vector/score pushdowns, and translator sort-limit pushdown.
  • Special checks: Threshold checks that compare against raw limit + offset can still flip from huge positive to negative and enable optimizations that should be disabled.
  • Test coverage/results: UtilsTest is useful, but the regression case uses direct Groovy assertions for a deterministic result and does not update the .out file, which violates the local regression-test standard. Additional regression coverage is needed for a forced two-phase TopN and at least one Limit path.
  • Observability/performance/data correctness: No extra observability appears necessary. The intended clamp is semantically safe for non-negative limits and avoids the BE timeout behavior, but only once every equivalent derived-limit path is fixed.
  • User focus: No additional user-provided focus points were present.

* no relation can hold more than {@code Long.MAX_VALUE} rows, clamping to {@code Long.MAX_VALUE}
* ("all rows") is the semantically correct upper bound and never drops rows the parent may need.
*/
public static long saturatedAdd(long a, long b) {

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.

This helper is the right primitive, but the PR does not route all equivalent limit + offset row-count derivations through it. A plain TopN still follows this path:

LogicalTopN(limit=Long.MAX_VALUE, offset=Long.MAX_VALUE, order by id)
  Scan/Union

LogicalTopNToPhysicalTopN.twoPhaseSort then builds a local child with logicalTopN.getLimit() + logicalTopN.getOffset() in fe/fe-core/src/main/java/org/apache/doris/nereids/rules/implementation/LogicalTopNToPhysicalTopN.java:50, so the physical tree can still contain:

PhysicalTopN(MERGE_SORT limit=MAX offset=MAX)
  PhysicalTopN(LOCAL_SORT limit=-2 offset=0)

With SET sort_phase_num = 2 this is not just an unused alternative. The same raw computation also remains in PushDownLimit.java, PushDownLimitDistinctThroughJoin.java, PushDownLimitDistinctThroughUnion.java, LimitAggToTopNAgg.java, PushTopnToAgg.java, the score/vector TopN scan pushdowns, and PhysicalPlanTranslator#setSortLimit. Please replace these parallel derived child/scan/agg limits, or centralize the operation on Limit/TopN, otherwise the same negative limit can still reach BE outside the specific Union rewrite covered here.

) s;
"""
assertEquals(1, topnUnionOverflowRes.size())
assertEquals(0L, topnUnionOverflowRes[0][0])

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.

This deterministic result should be recorded with a qt_... query and the generated .out file, not Groovy assertions. The local regression-test rules require determined expected results to be generated through qt_sql/order_qt style output files; here count(*) always returns one row with 0, but that expected result is invisible in regression-test/data/nereids_rules_p0/push_down_top_n/push_down_top_n_through_union.out. Please convert this to a qt_topn_union_overflow block and regenerate the .out file.

@morrySnow
morrySnow marked this pull request as draft June 18, 2026 08:36
@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 70.00% (14/20) 🎉
Increment coverage report
Complete coverage report

@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17618	3987	3991	3987
q2	2013	356	186	186
q3	10304	1492	835	835
q4	4686	476	343	343
q5	7515	859	571	571
q6	182	166	137	137
q7	778	833	627	627
q8	9336	1719	1626	1626
q9	5870	4508	4490	4490
q10	6804	1782	1539	1539
q11	438	277	243	243
q12	641	420	293	293
q13	18140	3425	2795	2795
q14	267	268	247	247
q15	q16	790	795	709	709
q17	997	1008	1052	1008
q18	7353	5867	5501	5501
q19	1307	1286	1057	1057
q20	488	407	254	254
q21	5899	2578	2455	2455
q22	435	361	302	302
Total cold run time: 101861 ms
Total hot run time: 29205 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	4382	4440	4264	4264
q2	331	362	226	226
q3	4604	4922	4441	4441
q4	2081	2173	1371	1371
q5	4447	4286	4273	4273
q6	238	177	128	128
q7	1723	1909	1891	1891
q8	2579	2200	2215	2200
q9	8076	8376	7995	7995
q10	4806	4778	4353	4353
q11	571	444	393	393
q12	772	768	553	553
q13	3256	3564	2995	2995
q14	308	315	278	278
q15	q16	708	730	627	627
q17	1367	1293	1323	1293
q18	7922	7600	7143	7143
q19	1130	1118	1064	1064
q20	2215	2244	1946	1946
q21	5282	4563	4449	4449
q22	518	455	389	389
Total cold run time: 57316 ms
Total hot run time: 52272 ms

@hello-stephen

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

query5	4306	628	488	488
query6	441	199	184	184
query7	4850	576	312	312
query8	364	215	205	205
query9	8717	4113	4107	4107
query10	445	303	255	255
query11	5907	2342	2130	2130
query12	160	106	98	98
query13	1345	613	431	431
query14	6390	5393	5163	5163
query14_1	4437	4405	4389	4389
query15	205	198	179	179
query16	1013	458	472	458
query17	1129	712	584	584
query18	2701	483	352	352
query19	206	186	154	154
query20	118	109	105	105
query21	219	143	119	119
query22	13727	13695	13359	13359
query23	17543	16523	16197	16197
query23_1	16291	16194	16301	16194
query24	7562	1768	1317	1317
query24_1	1357	1314	1344	1314
query25	565	467	360	360
query26	1313	302	169	169
query27	2621	545	340	340
query28	4389	2031	2011	2011
query29	1043	593	469	469
query30	314	233	196	196
query31	1121	1081	981	981
query32	113	61	56	56
query33	505	309	247	247
query34	1143	1169	666	666
query35	759	761	675	675
query36	1407	1408	1206	1206
query37	171	105	87	87
query38	3189	3131	3095	3095
query39	935	913	907	907
query39_1	884	872	881	872
query40	217	122	98	98
query41	64	63	61	61
query42	95	98	95	95
query43	324	323	283	283
query44	1448	778	784	778
query45	211	183	181	181
query46	1115	1192	757	757
query47	2368	2362	2224	2224
query48	406	420	298	298
query49	613	447	345	345
query50	984	352	258	258
query51	4433	4239	4233	4233
query52	91	88	77	77
query53	250	271	184	184
query54	267	218	190	190
query55	78	74	70	70
query56	248	224	212	212
query57	1446	1420	1312	1312
query58	236	212	211	211
query59	1608	1661	1417	1417
query60	296	249	220	220
query61	152	148	151	148
query62	696	643	587	587
query63	227	188	191	188
query64	2475	750	584	584
query65	4863	4755	4783	4755
query66	1711	448	327	327
query67	29848	29708	29521	29521
query68	3179	1604	990	990
query69	412	305	263	263
query70	1045	1003	1005	1003
query71	287	238	213	213
query72	3147	2594	2289	2289
query73	812	838	423	423
query74	5122	4977	4752	4752
query75	2664	2607	2251	2251
query76	2297	1181	758	758
query77	371	374	294	294
query78	12571	12653	11816	11816
query79	1425	1239	804	804
query80	649	554	434	434
query81	451	283	242	242
query82	892	156	122	122
query83	356	282	247	247
query84	264	149	117	117
query85	857	508	408	408
query86	375	302	286	286
query87	3374	3332	3247	3247
query88	3680	2800	2770	2770
query89	451	372	337	337
query90	1880	183	179	179
query91	173	162	133	133
query92	61	57	56	56
query93	1452	1490	913	913
query94	546	351	265	265
query95	692	388	350	350
query96	1040	799	356	356
query97	2710	2691	2567	2567
query98	213	204	196	196
query99	1181	1155	1011	1011
Total cold run time: 261706 ms
Total hot run time: 175320 ms

@hello-stephen

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

query1	0.01	0.01	0.01
query2	0.11	0.05	0.05
query3	0.26	0.14	0.14
query4	1.61	0.14	0.14
query5	0.25	0.23	0.23
query6	1.24	1.11	1.11
query7	0.04	0.01	0.00
query8	0.06	0.04	0.03
query9	0.41	0.32	0.34
query10	0.59	0.61	0.57
query11	0.19	0.15	0.15
query12	0.19	0.15	0.14
query13	0.51	0.51	0.50
query14	1.06	1.03	1.05
query15	0.65	0.63	0.64
query16	0.33	0.31	0.33
query17	1.17	1.13	1.11
query18	0.23	0.22	0.21
query19	2.05	1.98	1.97
query20	0.01	0.02	0.01
query21	15.44	0.25	0.13
query22	4.79	0.05	0.06
query23	16.13	0.32	0.12
query24	3.01	0.44	0.32
query25	0.12	0.06	0.04
query26	0.73	0.21	0.14
query27	0.04	0.04	0.04
query28	3.54	0.96	0.53
query29	12.54	4.44	3.50
query30	0.27	0.16	0.16
query31	2.77	0.66	0.32
query32	3.22	0.63	0.50
query33	3.19	3.25	3.20
query34	15.67	4.69	3.63
query35	3.67	3.63	3.72
query36	0.56	0.45	0.43
query37	0.10	0.07	0.06
query38	0.05	0.04	0.04
query39	0.04	0.03	0.03
query40	0.19	0.16	0.15
query41	0.09	0.03	0.03
query42	0.04	0.02	0.02
query43	0.04	0.03	0.04
Total cold run time: 97.21 s
Total hot run time: 25.76 s

@924060929
924060929 force-pushed the fix-push-down-topn-union-overflow branch from e9fd2a7 to 9407729 Compare June 22, 2026 02:55
@924060929 924060929 changed the title [fix](nereids) clamp limit + offset overflow when pushing down TopN/Limit [fix](nereids) fix limit + offset overflow when pushing down TopN/Limit Jun 22, 2026
@924060929

Copy link
Copy Markdown
Contributor Author

run buildall

@924060929

Copy link
Copy Markdown
Contributor 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:

The PR is aimed at fixing limit + offset overflow for TopN/Limit pushdown. The helper itself is correct, but the current patch is not complete: the project-over-join TopN rewrite variants still bypass the existing topn_opt_limit_threshold gate and can push a saturated Long.MAX_VALUE child TopN under joins. Existing review threads also already cover the remaining raw limit + offset production paths and the regression assertion style issue.

Critical checkpoint conclusions:

  • Goal/test proof: not fully satisfied; helper arithmetic is tested, but most changed rule paths are not covered.
  • Scope/focus: the change is focused, but the fix is inconsistent across parallel rule shapes.
  • Concurrency/lifecycle/persistence/protocol: not applicable; this is planner rewrite logic with no new locks, lifecycle, storage format, or FE-BE protocol state.
  • Configuration: existing topn_opt_limit_threshold remains relevant, but two project-over-join paths bypass it.
  • Parallel paths: issues remain; see inline comments and the existing raw-addition thread.
  • Test coverage/results: insufficient; the new deterministic regression also needs generated .out coverage per the existing thread.

Subagent conclusions: optimizer candidates OR-1 and OR-2 became inline comments; OR-3 was suppressed as a duplicate of the existing Utils.java:143 thread. Test candidate TS-2 became an inline comment; TS-1 and TS-3 were suppressed as duplicates of existing review threads. Convergence round 1 ended with both live subagents replying NO_NEW_VALUABLE_FINDINGS for the same ledger/comment set.

List<Slot> childRequired = requiredOutputSlots.stream()
.filter(childOutputSet::contains)
.collect(Collectors.toList());
if (childRequired.isEmpty()) {

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 direct TopN -> Join pattern now uses topnOptLimitThreshold >= Utils.saturatedAdd(...) before it pushes, but the sibling TopN -> Project -> Join pattern has no equivalent guard and still reaches this helper. For a reachable shape like TopN(limit=MAX, offset=MAX, order by l.k) -> Project(l.k, ...) -> LeftOuterJoin, the project branch calls pushLimitThroughJoin and this changed line inserts a child LogicalTopN(limit=Long.MAX_VALUE, offset=0) under the join. With the default topn_opt_limit_threshold=1024, the same plan without the project would be rejected by the direct branch. Please apply the same threshold/context predicate to the project-over-join branch, or centralize the saturated-limit refusal inside pushLimitThroughJoin before constructing child TopNs.

List<OrderKey> pushedOrderKeys = getPushedOrderKeys(groupBySlots,
join.left().getOutputSet(), topN.getOrderKeys());
if (!pushedOrderKeys.isEmpty()) {
LogicalTopN<Plan> left = topN.withLimitOrderKeyAndChild(

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 direct distinct join pattern is gated by topnOptLimitThreshold >= Utils.saturatedAdd(...), but the TopN -> Aggregate(distinct) -> Project(allSlots) -> Join pattern above it has no equivalent guard and still reaches this changed child TopN construction. With limit=MAX and offset=MAX, the direct branch is disabled under the default finite threshold, while this project branch can push LogicalTopN(limit=Long.MAX_VALUE, offset=0) plus PlanUtils.distinct(child) under the join. Please add the same threshold/context predicate to the project branch, or check it once inside pushTopNThroughJoin before constructing child TopNs.

}

@Test
public void testSaturatedAdd() {

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.

This unit test proves the arithmetic helper, but it does not prove that the overflow fix is wired through the changed planner rules. The PR changes SplitLimit, both TopN-through-join rules, distinct-union, and window pushdown, while the only new SQL overflow case covers plain UNION ALL and is already missing generated .out coverage in the existing regression thread. Please add generated-output regressions or focused planner tests for the overflow boundary through at least split limit and a session-gated join/project-join path; otherwise the rule wiring and threshold behavior can regress while this helper test still passes.

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 57.69% (15/26) 🎉
Increment coverage report
Complete coverage report

@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17700	3980	4037	3980
q2	2022	316	189	189
q3	10282	1428	824	824
q4	4683	468	336	336
q5	7495	860	573	573
q6	183	170	138	138
q7	804	837	612	612
q8	9343	1508	1605	1508
q9	5836	4514	4501	4501
q10	6762	1781	1537	1537
q11	436	279	245	245
q12	630	414	296	296
q13	18185	3366	2755	2755
q14	266	268	235	235
q15	q16	784	781	709	709
q17	985	1032	1032	1032
q18	7161	5890	5582	5582
q19	1311	1232	1152	1152
q20	470	413	266	266
q21	5933	2674	2348	2348
q22	439	360	299	299
Total cold run time: 101710 ms
Total hot run time: 29117 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	4386	4282	4351	4282
q2	344	366	233	233
q3	4702	4933	4424	4424
q4	2085	2174	1365	1365
q5	4463	4373	4325	4325
q6	236	176	129	129
q7	1755	1840	1899	1840
q8	2617	2353	2306	2306
q9	8212	8361	8077	8077
q10	4974	4754	4342	4342
q11	566	414	401	401
q12	776	788	532	532
q13	3293	3683	2987	2987
q14	299	304	265	265
q15	q16	724	739	648	648
q17	1400	1372	1483	1372
q18	7802	7398	7375	7375
q19	1199	1085	1132	1085
q20	2211	2220	1941	1941
q21	5338	4575	4583	4575
q22	527	475	416	416
Total cold run time: 57909 ms
Total hot run time: 52920 ms

@hello-stephen

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

query5	4319	635	478	478
query6	436	196	181	181
query7	4842	553	300	300
query8	361	206	193	193
query9	8755	4145	4125	4125
query10	468	319	256	256
query11	5918	2327	2140	2140
query12	166	104	98	98
query13	1268	621	416	416
query14	6367	5455	5119	5119
query14_1	4426	4445	4445	4445
query15	203	198	179	179
query16	1044	458	456	456
query17	1157	726	594	594
query18	2712	491	354	354
query19	217	188	149	149
query20	128	113	106	106
query21	221	140	118	118
query22	13717	13761	13473	13473
query23	17420	16659	16179	16179
query23_1	16344	16353	16307	16307
query24	7576	1748	1336	1336
query24_1	1324	1317	1315	1315
query25	562	460	399	399
query26	1296	323	177	177
query27	2612	586	344	344
query28	4373	2069	2038	2038
query29	1101	626	501	501
query30	311	237	201	201
query31	1123	1087	960	960
query32	110	64	62	62
query33	555	333	257	257
query34	1203	1168	694	694
query35	741	769	686	686
query36	1394	1393	1271	1271
query37	154	108	93	93
query38	1877	1716	1661	1661
query39	930	913	885	885
query39_1	887	876	890	876
query40	222	121	99	99
query41	65	61	60	60
query42	86	86	86	86
query43	331	327	282	282
query44	1474	780	783	780
query45	193	188	183	183
query46	1065	1208	746	746
query47	2371	2332	2224	2224
query48	381	432	288	288
query49	615	458	350	350
query50	1026	354	270	270
query51	4316	4280	4215	4215
query52	81	82	69	69
query53	257	274	194	194
query54	263	226	194	194
query55	75	69	66	66
query56	231	227	209	209
query57	1438	1416	1306	1306
query58	235	207	212	207
query59	1570	1658	1485	1485
query60	289	249	230	230
query61	154	142	147	142
query62	687	650	574	574
query63	242	191	195	191
query64	2476	756	595	595
query65	4889	4816	4774	4774
query66	1724	454	344	344
query67	29873	29775	29617	29617
query68	3173	1601	929	929
query69	403	296	250	250
query70	1101	1014	980	980
query71	296	236	214	214
query72	2922	2786	2377	2377
query73	855	760	457	457
query74	5137	4985	4798	4798
query75	2612	2591	2233	2233
query76	2319	1197	807	807
query77	376	379	278	278
query78	12304	12400	11730	11730
query79	1377	1151	747	747
query80	1292	477	391	391
query81	516	277	243	243
query82	593	163	120	120
query83	333	275	252	252
query84	260	143	113	113
query85	892	502	413	413
query86	439	296	294	294
query87	1837	1827	1764	1764
query88	3737	2798	2793	2793
query89	422	377	335	335
query90	1921	184	182	182
query91	174	158	129	129
query92	60	61	55	55
query93	1524	1406	909	909
query94	714	343	300	300
query95	663	395	335	335
query96	1059	782	350	350
query97	2721	2684	2583	2583
query98	213	207	201	201
query99	1180	1151	1033	1033
Total cold run time: 258784 ms
Total hot run time: 173359 ms

@hello-stephen

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

query1	0.00	0.00	0.01
query2	0.10	0.04	0.05
query3	0.26	0.14	0.14
query4	1.61	0.13	0.13
query5	0.26	0.23	0.22
query6	1.24	1.08	1.07
query7	0.07	0.01	0.01
query8	0.05	0.04	0.04
query9	0.37	0.33	0.32
query10	0.53	0.58	0.55
query11	0.19	0.14	0.14
query12	0.19	0.15	0.14
query13	0.47	0.46	0.46
query14	1.01	1.01	1.01
query15	0.61	0.60	0.62
query16	0.31	0.31	0.33
query17	1.10	1.11	1.11
query18	0.24	0.22	0.21
query19	2.06	2.01	1.96
query20	0.01	0.02	0.01
query21	15.44	0.23	0.13
query22	4.82	0.06	0.06
query23	16.13	0.30	0.12
query24	2.93	0.52	0.37
query25	0.12	0.06	0.04
query26	0.73	0.22	0.14
query27	0.04	0.04	0.04
query28	3.54	0.93	0.53
query29	12.50	4.36	3.49
query30	0.27	0.16	0.15
query31	2.77	0.57	0.32
query32	3.23	0.60	0.50
query33	3.32	3.20	3.16
query34	15.54	4.23	3.52
query35	3.58	3.51	3.54
query36	0.56	0.44	0.42
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.16	0.15
query41	0.09	0.03	0.03
query42	0.04	0.02	0.02
query43	0.05	0.04	0.04
Total cold run time: 96.73 s
Total hot run time: 25.33 s

@924060929
924060929 force-pushed the fix-push-down-topn-union-overflow branch from 9407729 to 996d1a7 Compare June 22, 2026 09:48
@924060929 924060929 changed the title [fix](nereids) fix limit + offset overflow when pushing down TopN/Limit [fix](nereids) skip TopN/Limit pushdown when limit + offset overflows Jun 22, 2026
@924060929

Copy link
Copy Markdown
Contributor Author

run buildall

@924060929

Copy link
Copy Markdown
Contributor Author

/review

@924060929
924060929 force-pushed the fix-push-down-topn-union-overflow branch from 996d1a7 to 5b351d3 Compare June 22, 2026 10:01
@924060929

Copy link
Copy Markdown
Contributor Author

run buildall

@924060929

Copy link
Copy Markdown
Contributor Author

/review

@924060929
924060929 force-pushed the fix-push-down-topn-union-overflow branch from 5b351d3 to 54a2b74 Compare June 22, 2026 10:11
@924060929

Copy link
Copy Markdown
Contributor Author

run buildall

@924060929

Copy link
Copy Markdown
Contributor Author

/review

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

@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 completed for PR 64633.

I reviewed the authoritative PR file list and the related Nereids limit/topN rewrite paths. I am not adding new inline comments because every substantiated suspicious point I found is already covered by existing review threads:

  • SplitLimit / LogicalTopNToPhysicalTopN still turn overflowing MAX LIMIT/OFFSET shapes into user-visible errors: already covered by r3466290098, r3466290104, r3479484089, and r3479484084.
  • MergeLimits / MergeTopNs nested offset overflow behavior: already covered by r3465721367, r3465800781, and r3465800793.
  • Project-over-join branches that bypass the direct branch topn_opt_limit_threshold / ConnectContext gate: already covered by r3449585806, r3449585809, r3465721375, and r3465800790.
  • Test coverage gaps around generated outputs and rule-wiring coverage: already covered by r3434307880 and r3449585811.

Critical checkpoint conclusions:

  • Goal: The PR adds overflow detection for limit + offset and wires it through the changed TopN/Limit pushdown paths. The main remaining semantic concerns are already present in existing review threads.
  • Scope/focus: The patch is narrowly focused on Nereids limit/topN overflow handling.
  • Concurrency/lifecycle: No new concurrency or lifecycle-sensitive state was introduced in the reviewed FE rewrite paths.
  • Config/session behavior: No new config was added. Existing session-threshold propagation gaps in project-over-join paths are already threaded.
  • Compatibility: No FE-BE protocol or storage format change was introduced; the change prevents negative pushed limits from reaching legacy plan nodes.
  • Parallel paths: Union, join, distinct, window, scan, aggregate, CTE consumer, physical translator, and constructor invariant paths were checked. No new non-duplicate parallel-path issue was found.
  • Tests: The added helper test and regression error cases exist, but broader rule-wiring/generated-output concerns are already covered by existing comments.
  • Observability, transaction/persistence, and data-write paths: Not applicable to this FE planner-only change.
  • Performance: The guarded paths either skip an optimization or fail before constructing invalid negative limits; known threshold-related pushdown concerns are already threaded.

Subagent conclusions:

  • optimizer-rewrite: no new valuable findings after duplicate suppression; candidate areas overlapped existing threads.
  • tests-session-config: no new valuable findings after duplicate suppression; remaining test/config concerns overlapped existing threads.
  • Final convergence round 1: both live subagents replied NO_NEW_VALUABLE_FINDINGS for the same current ledger and empty proposed inline comment set.

Validation:

  • git diff --check 32a2651f66b9f0f341c90e95f8ed304b34927111 88eadf46e1f3583ac1a0baf7ab5e3ae2d92ae654 -- $(cat .code-review.tHgXba/pr_changed_files.txt) passed.
  • FE build/tests/regression tests were not run because thirdparty/installed/bin/protoc is absent in this runner.

User focus:

  • No additional user-provided focus points were supplied.

@924060929

Copy link
Copy Markdown
Contributor Author

run buildall

@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 completed for PR 64633 at live head 88eadf46e1f3583ac1a0baf7ab5e3ae2d92ae654.

I reviewed the authoritative 24-file PR scope and related Nereids limit/topN rewrite paths. I am not adding inline comments because every substantiated suspicious point I found is already covered by existing review threads:

  • SplitLimit / LogicalTopNToPhysicalTopN still turn overflowing MAX LIMIT/OFFSET shapes into user-visible errors: already covered by r3466290098, r3466290104, r3479484089, and r3479484084.
  • MergeLimits / MergeTopNs nested offset overflow behavior: already covered by r3465721367, r3465800781, and r3465800793.
  • Project-over-join branches that bypass the direct branch topn_opt_limit_threshold / ConnectContext gate: already covered by r3449585806, r3449585809, r3465721375, and r3465800790.
  • Test coverage gaps around generated outputs, semantic error expectations, and rule-wiring/session coverage: already covered by r3434307880, r3449585811, r3466290098, and r3466290104.

Critical checkpoint conclusions:

  • Goal/test proof: The PR adds limit + offset overflow checks and wires them through many changed TopN/Limit paths. Remaining semantic and test concerns are already in existing threads.
  • Scope/focus: The reviewed live patch is limited to FE Nereids overflow handling and the associated regression/helper tests.
  • Concurrency/lifecycle: No new concurrency or lifecycle-sensitive state was introduced.
  • Config/session behavior: No new config was added. Existing session-threshold gaps are already threaded.
  • Compatibility: No FE-BE protocol or storage format change was introduced.
  • Parallel paths: Union, join, distinct, window, scan, aggregate, CTE consumer, physical translator, and constructor invariant paths were checked. No new non-duplicate parallel-path issue was found.
  • Tests: Added helper and regression error cases exist, but broader rule-wiring/generated-output concerns are already covered by existing comments.
  • Observability, transaction/persistence, and data-write paths: Not applicable to this FE planner-only change.
  • Performance: Guarded paths skip optimizations or fail before constructing invalid negative limits; known threshold-related pushdown concerns are already threaded.

Subagent conclusions:

  • optimizer-rewrite: NO_NEW_VALUABLE_FINDINGS after duplicate suppression.
  • tests-session-config: live candidates were duplicates or stale-bundle evidence; final convergence returned NO_NEW_VALUABLE_FINDINGS.
  • Final convergence round 1 ended with both live subagents reporting NO_NEW_VALUABLE_FINDINGS for the same ledger and empty proposed inline comment set.

Validation:

  • git diff --check c02808aae79b29e8c02e48f06baf89b43d4c4152..88eadf46e1f3583ac1a0baf7ab5e3ae2d92ae654 -- $(cat .code-review.cDnlMI/pr_changed_files.txt) passed.
  • FE build/tests/regression tests were not run because thirdparty/installed/bin/protoc is absent in this runner.

User focus:

  • No additional user-provided focus points were supplied.

@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Codex automated review failed and did not complete.

Error: Codex completed, but no new pull request review was submitted for the current head SHA.
Workflow run: https://github.com/apache/doris/actions/runs/28490720354

Please inspect the workflow logs and rerun the review after the underlying issue is resolved.

@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17604	4115	4009	4009
q2	2012	337	195	195
q3	10279	1443	848	848
q4	4685	475	344	344
q5	7508	864	566	566
q6	180	178	136	136
q7	786	825	622	622
q8	9330	1546	1707	1546
q9	5661	4473	4436	4436
q10	6735	1791	1535	1535
q11	510	342	331	331
q12	722	571	436	436
q13	18101	3368	2749	2749
q14	264	256	249	249
q15	q16	789	780	715	715
q17	988	1067	1012	1012
q18	7024	5761	5557	5557
q19	1326	1335	1077	1077
q20	769	664	579	579
q21	6464	2898	2637	2637
q22	469	392	319	319
Total cold run time: 102206 ms
Total hot run time: 29898 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	5204	4869	4784	4784
q2	308	341	218	218
q3	4896	5320	4684	4684
q4	2123	2143	1376	1376
q5	4971	4648	4789	4648
q6	239	181	129	129
q7	1975	1761	1606	1606
q8	2465	2232	2111	2111
q9	7697	7224	7160	7160
q10	4776	4679	4257	4257
q11	537	393	358	358
q12	720	734	524	524
q13	3034	3346	2799	2799
q14	288	277	263	263
q15	q16	682	705	615	615
q17	1296	1274	1269	1269
q18	7399	6832	6809	6809
q19	1152	1123	1101	1101
q20	2227	2215	1928	1928
q21	5313	4637	4519	4519
q22	521	451	403	403
Total cold run time: 57823 ms
Total hot run time: 51561 ms

@hello-stephen

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

query5	4317	645	482	482
query6	474	225	199	199
query7	4905	603	359	359
query8	340	200	187	187
query9	8766	4035	4026	4026
query10	496	344	293	293
query11	5932	2361	2152	2152
query12	161	113	102	102
query13	1307	619	450	450
query14	6264	5316	4973	4973
query14_1	4259	4323	4280	4280
query15	220	202	181	181
query16	1007	471	472	471
query17	957	764	595	595
query18	2438	482	359	359
query19	221	194	160	160
query20	117	109	109	109
query21	236	160	134	134
query22	13636	13559	13375	13375
query23	17461	16520	16149	16149
query23_1	16290	16229	16258	16229
query24	7476	1757	1310	1310
query24_1	1288	1310	1270	1270
query25	574	453	391	391
query26	1364	352	210	210
query27	2600	592	382	382
query28	4490	2068	2039	2039
query29	1086	636	502	502
query30	328	265	236	236
query31	1118	1099	982	982
query32	108	63	65	63
query33	563	331	260	260
query34	1183	1147	665	665
query35	764	800	674	674
query36	1410	1448	1207	1207
query37	156	108	91	91
query38	1919	1721	1601	1601
query39	919	916	892	892
query39_1	872	867	888	867
query40	246	165	140	140
query41	66	64	65	64
query42	92	92	99	92
query43	333	330	277	277
query44	1426	794	780	780
query45	195	198	179	179
query46	1027	1189	725	725
query47	2355	2341	2238	2238
query48	407	413	298	298
query49	581	416	308	308
query50	1117	437	332	332
query51	4438	4377	4303	4303
query52	85	86	77	77
query53	268	280	202	202
query54	297	230	218	218
query55	74	71	66	66
query56	303	292	293	292
query57	1400	1388	1305	1305
query58	301	264	253	253
query59	1582	1631	1446	1446
query60	299	267	251	251
query61	153	151	155	151
query62	689	652	584	584
query63	245	205	210	205
query64	2520	774	612	612
query65	4849	4800	4796	4796
query66	1834	497	394	394
query67	29698	29704	29533	29533
query68	3181	1507	1013	1013
query69	412	308	276	276
query70	1071	968	957	957
query71	349	346	295	295
query72	2924	2637	2358	2358
query73	819	766	458	458
query74	5094	4921	4777	4777
query75	2625	2590	2220	2220
query76	2366	1185	790	790
query77	362	383	271	271
query78	12313	12450	11868	11868
query79	1443	1194	754	754
query80	1283	544	476	476
query81	552	337	282	282
query82	598	158	123	123
query83	371	320	293	293
query84	320	159	134	134
query85	959	601	488	488
query86	429	306	275	275
query87	1841	1832	1765	1765
query88	3720	2824	2803	2803
query89	458	408	359	359
query90	1965	196	200	196
query91	218	193	160	160
query92	68	64	59	59
query93	1687	1581	958	958
query94	724	365	305	305
query95	827	505	566	505
query96	1093	821	366	366
query97	2698	2706	2586	2586
query98	219	207	199	199
query99	1169	1151	1023	1023
Total cold run time: 259494 ms
Total hot run time: 174241 ms

@hello-stephen

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

query1	0.01	0.01	0.00
query2	0.09	0.05	0.05
query3	0.25	0.13	0.13
query4	1.61	0.15	0.14
query5	0.26	0.23	0.22
query6	1.28	1.08	1.05
query7	0.04	0.00	0.00
query8	0.06	0.04	0.04
query9	0.39	0.31	0.33
query10	0.55	0.57	0.59
query11	0.19	0.15	0.15
query12	0.18	0.15	0.15
query13	0.48	0.46	0.46
query14	1.04	1.00	1.00
query15	0.61	0.61	0.59
query16	0.32	0.35	0.31
query17	1.13	1.08	1.07
query18	0.23	0.22	0.21
query19	2.07	2.00	1.99
query20	0.01	0.01	0.02
query21	15.45	0.22	0.13
query22	4.77	0.05	0.06
query23	16.15	0.33	0.12
query24	2.95	0.43	0.32
query25	0.11	0.06	0.04
query26	0.72	0.23	0.16
query27	0.04	0.03	0.04
query28	3.47	0.98	0.55
query29	12.48	4.44	3.49
query30	0.27	0.16	0.16
query31	2.77	0.62	0.32
query32	3.22	0.60	0.49
query33	3.17	3.14	3.30
query34	15.60	4.23	3.53
query35	3.58	3.53	3.54
query36	0.55	0.44	0.40
query37	0.08	0.07	0.06
query38	0.05	0.04	0.04
query39	0.04	0.03	0.03
query40	0.20	0.16	0.15
query41	0.09	0.04	0.03
query42	0.04	0.03	0.03
query43	0.05	0.04	0.04
Total cold run time: 96.65 s
Total hot run time: 25.29 s

When combining a limit and an offset into the number of rows a child must keep,
the raw limit + offset overflows the long range when both are close to BIGINT_MAX
and wraps to a negative value, producing an illegal plan that hangs in BE.

Add Utils.addOverflows and, on overflow, decline the optimization and fall back to
correct unoptimized execution. For paths that cannot decline (twoPhaseSort, SplitLimit,
MergeLimits, MergeTopNs), report an error since the overflowing limit + offset cannot
be correctly represented anywhere in the FE-BE pipeline.

As defense-in-depth, the LogicalTopN / PhysicalTopN / LogicalLimit / PhysicalLimit
constructors assert that limit and offset are non-negative.
@924060929
924060929 force-pushed the fix-push-down-topn-union-overflow branch from 88eadf4 to bb2f83c Compare July 1, 2026 06:59
@924060929

Copy link
Copy Markdown
Contributor Author

run buildall

@924060929

Copy link
Copy Markdown
Contributor 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 follow-up review (2026-07-01 UTC)

Review opinion: I found no new non-duplicate inline comments in this pass. The existing review threads remain the relevant unresolved context for the overflow-to-error semantics, nested merge overflow handling, and project-over-join threshold gates; I am not re-posting those issues here.

Critical checkpoint conclusions:

  • Goal and tests: The PR aims to prevent limit + offset overflow from producing negative child limits. The helper and many pushdown paths are wired, with unit/regression coverage added; the semantic concern that some valid empty-result queries now error is already covered by existing threads.
  • Scope: FE/Nereids-only changes, scoped to TopN/Limit overflow handling and related test coverage.
  • Concurrency, lifecycle, persistence, compatibility: No new concurrency, special lifecycle, edit-log, storage-format, or FE-BE thrift compatibility surface found in this diff.
  • Configuration/session behavior: No new config items. Existing sort_phase_num and topn_opt_limit_threshold behavior was reviewed; remaining threshold mismatch concerns are already covered by existing project-over-join threads.
  • Parallel paths: Checked union, join, distinct join/union, window, scan pushdown, aggregate pushdown, physical translation, constructor invariants, and remaining raw limit + offset sites. No distinct new issue was substantiated beyond existing threads.
  • Tests: UtilsTest#testAddOverflows covers the arithmetic helper. The added regression blocks are test { exception ... }, so generated .out output is not required for those blocks; the concern that they encode the error semantics is already raised in existing review context.
  • Validation: git diff --check 9d93a33fa1a95db77348af7812d65f02e894fde9..bb2f83ceafb3e9c3487c5934735179f69d33d142 -- passed. I did not run FE or regression tests because thirdparty/installed and thirdparty/installed/bin/protoc are missing in this runner.

User focus: No additional user-provided review focus was supplied.

Subagent conclusions:

  • optimizer-rewrite: recorded duplicate notes for the TopN implementation throw, SplitLimit throw, nested limit/topn merge throws, and project-over-join threshold bypasses. Convergence round 1 returned NO_NEW_VALUABLE_FINDINGS for the current ledger and empty proposed inline set.
  • tests-session-config: recorded one duplicate note for the overflow regression tests expecting limit + offset overflows. Convergence round 1 returned NO_NEW_VALUABLE_FINDINGS for the current ledger and empty proposed inline set.

@hello-stephen

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

------ Round 1 ----------------------------------
============================================
q1	17654	4129	4181	4129
q2	2010	330	199	199
q3	10301	1446	856	856
q4	4687	469	342	342
q5	7475	857	568	568
q6	180	175	139	139
q7	809	861	627	627
q8	9393	1511	1656	1511
q9	6064	4518	4444	4444
q10	6783	1822	1517	1517
q11	538	347	325	325
q12	712	547	457	457
q13	18160	3371	2809	2809
q14	271	267	252	252
q15	q16	796	794	711	711
q17	1073	1041	1053	1041
q18	7221	5786	5664	5664
q19	1354	1353	1117	1117
q20	762	708	545	545
q21	5948	2709	2449	2449
q22	458	370	307	307
Total cold run time: 102649 ms
Total hot run time: 30009 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	4484	4414	4470	4414
q2	285	321	210	210
q3	4634	5029	4406	4406
q4	2121	2190	1395	1395
q5	4542	4359	4395	4359
q6	236	178	128	128
q7	2452	1958	1715	1715
q8	2649	2315	2268	2268
q9	8455	8001	7937	7937
q10	4808	4744	4307	4307
q11	618	451	377	377
q12	754	776	541	541
q13	3318	3632	2914	2914
q14	311	310	284	284
q15	q16	711	745	631	631
q17	1371	1479	1403	1403
q18	7787	7296	7194	7194
q19	1157	1061	1113	1061
q20	2214	2196	1926	1926
q21	5338	4632	4490	4490
q22	532	467	432	432
Total cold run time: 58777 ms
Total hot run time: 52392 ms

@hello-stephen

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

query5	4326	630	491	491
query6	453	216	199	199
query7	4889	589	338	338
query8	332	198	176	176
query9	8754	4046	4027	4027
query10	439	356	288	288
query11	5923	2343	2108	2108
query12	157	107	103	103
query13	1256	635	456	456
query14	6255	5324	5047	5047
query14_1	4322	4284	4305	4284
query15	216	202	180	180
query16	999	468	453	453
query17	939	710	597	597
query18	2454	522	331	331
query19	194	185	141	141
query20	111	109	104	104
query21	228	155	137	137
query22	13576	13596	13437	13437
query23	17409	16560	16197	16197
query23_1	16272	16318	16200	16200
query24	7603	1774	1301	1301
query24_1	1315	1311	1293	1293
query25	538	432	373	373
query26	1322	352	205	205
query27	2596	609	390	390
query28	4467	2007	2018	2007
query29	1066	615	478	478
query30	336	266	228	228
query31	1114	1101	978	978
query32	106	59	61	59
query33	514	324	245	245
query34	1178	1136	643	643
query35	754	779	671	671
query36	1420	1433	1240	1240
query37	163	113	96	96
query38	1901	1705	1686	1686
query39	933	926	909	909
query39_1	875	916	883	883
query40	245	167	136	136
query41	64	62	63	62
query42	94	94	91	91
query43	325	328	286	286
query44	1434	778	772	772
query45	206	194	179	179
query46	1108	1212	747	747
query47	2370	2362	2206	2206
query48	395	428	285	285
query49	569	472	320	320
query50	1057	423	331	331
query51	4410	4388	4259	4259
query52	84	88	74	74
query53	263	279	203	203
query54	279	231	201	201
query55	73	71	66	66
query56	279	269	290	269
query57	1447	1431	1296	1296
query58	296	273	258	258
query59	1598	1645	1435	1435
query60	300	267	245	245
query61	154	149	142	142
query62	692	650	589	589
query63	261	203	205	203
query64	2539	758	600	600
query65	4830	4779	4789	4779
query66	1862	544	380	380
query67	29832	29698	29650	29650
query68	3191	1623	944	944
query69	403	303	265	265
query70	1072	912	988	912
query71	358	325	307	307
query72	2921	2819	2498	2498
query73	850	824	438	438
query74	5116	4982	4740	4740
query75	2628	2600	2255	2255
query76	2354	1211	778	778
query77	364	393	304	304
query78	12493	12591	11861	11861
query79	1405	1215	760	760
query80	669	563	544	544
query81	457	330	273	273
query82	582	156	121	121
query83	385	315	290	290
query84	327	159	130	130
query85	930	614	503	503
query86	362	273	282	273
query87	1834	1818	1791	1791
query88	3704	2828	2792	2792
query89	464	406	362	362
query90	1971	203	203	203
query91	198	195	161	161
query92	66	60	56	56
query93	1528	1573	1065	1065
query94	549	345	294	294
query95	834	599	467	467
query96	1080	766	375	375
query97	2715	2729	2552	2552
query98	217	207	196	196
query99	1228	1157	1021	1021
Total cold run time: 258332 ms
Total hot run time: 173783 ms

@hello-stephen

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

query1	0.00	0.00	0.01
query2	0.09	0.05	0.05
query3	0.26	0.14	0.14
query4	1.62	0.16	0.14
query5	0.24	0.24	0.21
query6	1.23	1.10	1.09
query7	0.04	0.00	0.01
query8	0.06	0.04	0.04
query9	0.40	0.31	0.31
query10	0.54	0.59	0.58
query11	0.20	0.15	0.14
query12	0.18	0.15	0.15
query13	0.48	0.48	0.47
query14	1.01	1.00	1.03
query15	0.61	0.61	0.61
query16	0.32	0.34	0.32
query17	1.07	1.07	1.10
query18	0.22	0.22	0.21
query19	2.10	1.93	1.87
query20	0.02	0.02	0.02
query21	15.43	0.23	0.13
query22	4.88	0.05	0.05
query23	16.14	0.30	0.12
query24	2.96	0.40	0.33
query25	0.11	0.05	0.05
query26	0.74	0.20	0.16
query27	0.04	0.04	0.03
query28	3.53	0.93	0.53
query29	12.48	4.31	3.48
query30	0.27	0.16	0.16
query31	2.77	0.59	0.32
query32	3.22	0.59	0.50
query33	3.18	3.30	3.19
query34	15.65	4.24	3.57
query35	3.56	3.55	3.51
query36	0.57	0.44	0.44
query37	0.08	0.07	0.08
query38	0.05	0.04	0.04
query39	0.04	0.03	0.03
query40	0.18	0.16	0.14
query41	0.09	0.04	0.03
query42	0.04	0.04	0.03
query43	0.04	0.04	0.03
Total cold run time: 96.74 s
Total hot run time: 25.36 s

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 22.83% (21/92) 🎉
Increment coverage report
Complete coverage report

@hello-stephen

Copy link
Copy Markdown
Contributor

FE UT Coverage Report

Increment line coverage 20.63% (13/63) 🎉
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.

@924060929 924060929 changed the title [fix](nereids) skip TopN/Limit pushdown when limit + offset overflows [fix](nereids) fix limit + offset overflow when pushing down TopN/Limit Jul 2, 2026
@924060929
924060929 merged commit 3cfbf22 into apache:master Jul 2, 2026
35 checks passed
@924060929
924060929 deleted the fix-push-down-topn-union-overflow branch July 2, 2026 03:30
airborne12 added a commit that referenced this pull request Sep 1, 2026
…5821 (#67327)

### What problem does this PR solve?

Issue Number: N/A

Related PR: #65821 (master), picked from commit
2b6a45e

Problem Summary:

Backport of #65821 to branch-4.1. Search score TopN pushdown may return
incorrect results when the search predicate is combined with additional
predicates, because the pushed TopN limit can be applied before the
remaining predicates are evaluated. This change disables the pushed
search TopN limit in those cases while preserving the virtual score
column pushdown, and adds regression coverage (search + equality / range
/ match / score range / multiple search predicates, plus limit+offset
overflow).

**Hunk audit (source diff → this PR):**

| Source hunk | Status |
|---|---|
| `PushDownScoreTopNIntoOlapScan.java` `@@ -194,17 +194,22 @@` (overflow
guard rework + pushedScoreLimit) | **Adapted ×2**: ① branch-4.1 never
had the #64633 overflow-guard block, so the hunk's removed lines have no
counterpart here; ② `Utils.addOverflows` does not exist on 4.1 (#64633
not backported) — inlined the equivalent check `topN.getLimit() >
Long.MAX_VALUE - topN.getOffset()` (identical to the master helper's
implementation). |
| `PushDownScoreTopNIntoOlapScan.java` `@@ -243,6 +248,19 @@`
(`shouldDisableSearchTopN` helper) | Ported |
| `test_search_score_topn_predicates.out` (new) | Ported (verbatim) |
| `test_search_score_topn_predicates.groovy` (new) | **Adapted**:
dropped `set enable_segment_limit_pushdown = true` — the variable comes
from #62222 which is not on 4.1; it defaults to true on master and only
controls a BE-side segment limit optimization, unrelated to this
FE-plan-level fix. |

**Local verification on this branch:** full ASAN BE+FE build green;
`run-regression-test.sh -d inverted_index_p0 -s
test_search_score_topn_predicates` → 1 suite, 0 failed against a local
1FE+1BE cluster built from this PR. No FE UT exists for this rule on 4.1
and the source PR added none (its coverage is the regression suite
above).

### Release note

None

### Check List (For Author)

- Test <!-- At least one of them must be included. -->
    - [x] Regression test
    - [ ] Unit Test
    - [ ] Manual test (add detailed scripts or steps below)
    - [ ] No need to test or manual test. Explain why:
- [ ] This is a refactor/code format and no logic has been changed.
        - [ ] Previous test can cover this change.
        - [ ] No code files have been changed.
        - [ ] Other reason <!-- Add your reason?  -->

- Behavior changed:
    - [x] No.
    - [ ] Yes. <!-- Explain the behavior change -->

- Does this need documentation?
    - [x] No.
- [ ] Yes. <!-- Add document PR link here. eg:
apache/doris-website#1214 -->

### Check List (For Reviewer who merge this PR)

- [ ] Confirm the release note
- [ ] Confirm test cases
- [ ] Confirm document
- [ ] Add branch pick label <!-- Add branch pick label that this PR
should merge into -->

Co-authored-by: liangj777 <106017102+LIANG751234313@users.noreply.github.com>
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.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants