Skip to content

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

Open
924060929 wants to merge 1 commit into
branch-4.2from
auto-pick-64633-branch-4.2
Open

924060929 wants to merge 1 commit into
branch-4.2from
auto-pick-64633-branch-4.2

Conversation

@924060929

Copy link
Copy Markdown
Contributor

Cherry-picked from #64633

@924060929
924060929 requested a review from yiguolei as a code owner September 18, 2026 09:29
@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

@924060929
924060929 force-pushed the auto-pick-64633-branch-4.2 branch from d23c224 to c0fe46d Compare September 18, 2026 09:52
@924060929
924060929 changed the base branch from auto-pick-64793-branch-4.2 to branch-4.2 September 18, 2026 09:52
@924060929

Copy link
Copy Markdown
Contributor Author

run buildall

@924060929

Copy link
Copy Markdown
Contributor Author

/review

@github-actions

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/35329862054

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

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

Static review of exact head c0fe46df32ebe1aaaff325d230eb9fb6c3ba14eb against base 5799884109545ec74e2858bf01eab1d926bdae33: requesting changes for three P2 correctness issues.

  • The score-specific overflow fallback now loses the intended diagnostic and contradicts an existing regression oracle.
  • The new nested-Limit merge exception rejects individually valid inputs whose result is provably empty, including the equivalent Exchange fold.
  • The new compatible-TopN merge exception has the same optimizer-induced rejection on a distinct ordered-query path.

I reviewed all 24 changed files plus the relevant rewrite ordering, implementation/translation paths, downstream sort/exchange behavior, constructor invariants, specialized score/vector paths, and regression coverage. Three bounded normal/risk review rounds converged on this exact comment set; all final-round lanes returned NO_NEW_VALUABLE_FINDINGS, and the live inline-review context contains no duplicates. Other changed overflow guards and pushdown fallbacks did not yield another substantiated PR-introduced issue.

Validation was static-only as required: no build or test was run. git diff --check passed. The existing score regression was inspected but not executed, and new coverage does not exercise the two nested-merge boundaries. No additional user-provided review focus was present.

// unmaterialized and reporting an unrelated score() usage error.
if (topN.getLimit() > Long.MAX_VALUE - topN.getOffset()) {
throw new AnalysisException("limit + offset overflows the long range");
// negative value; skip the push-down and let the TopN above the scan apply limit/offset.

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.

[P2] Preserve the overflow error for score() queries

Returning null here leaves score() unmaterialized. The same rewrite batch immediately runs CheckScoreUsage, so a query that has MATCH, ORDER BY, and LIMIT now fails with the unrelated "score() function requires ..." message before LogicalTopNToPhysicalTopN can report the overflow. This also contradicts the existing regression at test_search_score_topn_predicates.groovy:191-201, and the base version of this hunk explicitly threw the standard overflow error to avoid exactly this path. Please keep the fail-fast AnalysisException here (or validate overflow before CheckScoreUsage).

* and an overflowing combined offset cannot be represented as a single offset, so fail fast
* instead of wrapping to a negative offset.
*/
public static long mergeOffset(long upperOffset, long bottomOffset) {

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.

[P2] Preserve valid nested LIMITs when only the merged offset overflows

Two nested LIMIT 1 OFFSET 9223372036854775806 nodes are individually representable because each limit + offset equals Long.MAX_VALUE, and the outer node necessarily returns zero rows because its offset exceeds the child's one-row output. This guard nevertheless turns that valid query into an analysis error even though mergeLimit already derives a zero limit. Please canonicalize this case to a representable empty limit; PhysicalPlanTranslator's Exchange fold at lines 2263-2265 needs the same zero-limit handling so it does not call mergeOffset after deriving zero.

@@ -58,6 +60,10 @@ public Rule build() {
long limit = topN.getLimit();
long childOffset = childTopN.getOffset();
long childLimit = childTopN.getLimit();

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.

[P2] Keep individually valid nested TopNs executable

Compatible nested TopNs can hit this guard even when each node is valid. For example, two TopNs with limit 1 and offset 9223372036854775806 each have limit + offset == Long.MAX_VALUE; the outer one must return zero rows, but their combined offsets overflow and this code throws before lines 68-73 derive newLimit = 0. The ordered derived-table shape is already known to reach MERGE_TOP_N in test_merge_topn_offset.groovy. Please canonicalize the empty result with a representable offset before this check, and add a boundary case for it.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants