Repository navigation
[branch-55] fix: correct LEAD/LAG IGNORE NULLS evaluation and limit pushdown (#25472) - #27320
Merged
Merged
Conversation
…pache#25472) ## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> - Closes apache#25471. ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. Please explain the problem you are trying to solve in terms of the user-visible behavior, rather than the implementation. For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the implementation. "COUNT(DISTINCT) returns wrong results when the column contains nulls" is the user-visible problem. --> LEAD and negative-offset LAG with IGNORE NULLS can return NULL or a default value even when the requested non-null row exists. This happens when stateful evaluation fails to refill a partially populated lookahead cache, or when window LIMIT pushdown truncates input before the target row. ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here, but it is sometimes worth providing a summary of the individual changes in this PR. --> - Resume scanning after the last cached non-null row until enough candidates are available or the range ends. - Return `LimitEffect::Unknown` for IGNORE NULLS window UDF expressions, since non-null offsets cannot bound the number of required input rows. ## What is the testing strategy for this PR? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code Briefly describe how this PR is tested, and point to the specific tests you added. For example: 'This new feature is covered by the `sqllogictest` cases added in `foo.slt`'. If this PR does not add tests, explain why. For example, if the change is already covered by existing tests, please mention it. You should also check the `codecov` bot reply on this PR to confirm the changed code is exercised. --> Added regression tests in `window.slt` for LEAD/LAG IGNORE NULLS across NULL gaps and batch boundaries, and in `window_limits.slt` for LIMIT pushdown correctness. ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. If there are any breaking changes to public APIs, please add the `api change` label. --> Affected IGNORE NULLS window queries now return the correct results. No public API changes. (cherry picked from commit 84acf49)
9 of 23 tasks
alamb
approved these changes
Nov 12, 2026
alamb
left a comment
Contributor
There was a problem hiding this comment.
Looks good -- thank you. I verified this is a clean cherry pick
Codecov Report❌ Patch coverage is
@@ Coverage Diff @@
## branch-55 #27320 +/- ##
==========================================
Coverage 81.24% 81.24%
==========================================
Files 1110 1110
Lines 389341 389331 -10
Branches 389341 389331 -10
==========================================
- Hits 316324 316318 -6
+ Misses 54446 54440 -6
- Partials 18571 18573 +2
... and 7 files with indirect coverage changes 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
55.3.0(patch) Release (Nov 2026) #26868 (the 55.3.0 patch release tracking issue).LEAD/LAG IGNORE NULLSreturns incorrect results acrossNULLgaps and withLIMIT#25471 onbranch-55.Rationale for this change
On 55.2.0,
LEAD(v, N) IGNORE NULLSwithN >= 2and negative-offsetLAGwithIGNORE NULLSreturn NULL or the default value when a NULL falls inside the lookahead, although the requested non-null row exists. With window LIMIT pushdown enabled, the input can also be cut before the requested row. These are wrong query results, which makes this eligible for backport as a correctness fix.#25472 fixed it on
main. It is not in 55.1.0 or 55.2.0 and first ships in 56.0.0. It was requested for 55.3.0 on #26868.What changes are included in this PR?
git cherry-pick -xof 84acf49 (fix: correctLEAD/LAG IGNORE NULLSevaluation and limit pushdown #25472, by @lyne7-sc). It applied without conflicts and this PR changes nothing else.datafusion/functions-window/src/lead_lag.rs: the stateful evaluation resumes scanning after the last cached non-null row until enough candidates are available or the range ends.datafusion/physical-plan/src/windows/mod.rs:limit_effectreturnsLimitEffect::Unknownfor anIGNORE NULLSwindow UDF expression, since non-null offsets cannot bound the number of input rows needed.window.slt(NULL gaps and batch boundaries) andwindow_limits.slt(LIMIT pushdown).Are these changes tested?
Yes, by the tests that #25472 added. On this branch (Rust 1.95.0) I ran:
cargo test -p datafusion-functions-window: 18 passed.cargo test -p datafusion-sqllogictest --test sqllogictests -- window: all 7 matching files pass, includingwindow.sltandwindow_limits.sltwith the new cases.Are there any user-facing changes?
Affected
IGNORE NULLSwindow queries return the requested row instead of NULL or the default, as onmain. No public API change. A window function withIGNORE NULLSno longer lets window LIMIT pushdown (datafusion.optimizer.enable_window_limits) cut the input, so the plan of such a query can differ.