Skip to content

fix: saturate LimitPushPastWindows and ROWS frame bounds for huge FOLLOWING - #26674

Open
ninglinLiu wants to merge 1 commit into
apache:mainfrom
ninglinLiu:fix/limit-push-past-windows-saturating-add-26658
Open

ninglinLiu wants to merge 1 commit into
apache:mainfrom
ninglinLiu:fix/limit-push-past-windows-saturating-add-26658

Conversation

@ninglinLiu

Copy link
Copy Markdown

Important

Fork CI setup: For pull requests from a fork to main, enable workflows in your fork's Actions tab before you push.
See the fork CI setup guide.

Which issue does this PR close?

Rationale for this change

A window frame with a huge ROWS ... FOLLOWING bound (e.g. u64::MAX) made LimitPushPastWindows panic in debug builds when growing fetch by lookahead (fetch + ctx.lookahead). In release builds the same addition wraps, so the SortExec TopK fetch can become smaller than the query needs. EnsureRequirements sort pushdown already uses saturating_add for the same kind of sum.

Running the issue's reproduction also hit overflow in calculate_range_rows (idx + n as usize + 1), so the ROWS frame bound math is saturated the same way so the query can finish.

What changes are included in this PR?

  • Use saturating_add in LimitPushPastWindows when combining fetch/lookahead/skip (and when accumulating lookahead).
  • Saturate ROWS Following start/end index arithmetic in window_state::calculate_range_rows.
  • Add a window_limits.slt case for the issue reproduction.

What is the testing strategy for this PR?

  • New sqllogictest in datafusion/sqllogictest/test_files/window_limits.slt covering the issue query.
  • Locally: cargo test --test sqllogictests -- window_limits.

Are there any user-facing changes?

Bug fix only: queries with extremely large ROWS FOLLOWING bounds no longer panic in the optimizer / ROWS frame path. No API change.

…LOWING

Closes apache#26658.

A ROWS FOLLOWING bound of u64::MAX made LimitPushPastWindows panic in
debug (and wrap the TopK fetch in release) when growing fetch by
lookahead. Use saturating_add like EnsureRequirements sort pushdown,
and saturate the matching ROWS range arithmetic in window_state so the
query can still execute.
@github-actions github-actions Bot added logical-expr Logical plan and expressions optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt) labels Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logical-expr Logical plan and expressions optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LimitPushPastWindows panics with "attempt to add with overflow" for a large ROWS ... FOLLOWING bound

1 participant