You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 0db5244
Browse filesBrowse the repository at this point in the historyBrowse files
fix: correct LEAD/LAG IGNORE NULLS evaluation and limit pushdown (apache#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
`Closesapache#123` indicates that this PR will close issue apache#123.
-->
- Closesapache#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.
# Non-null offsets cannot be used as physical row counts for LIMIT pushdown.
116
+
statement ok
117
+
SET datafusion.optimizer.enable_window_limits = false;
118
+
119
+
query III
120
+
SELECT id,
121
+
LEAD(v) IGNORE NULLS OVER w,
122
+
LAG(v, -1, -1) IGNORE NULLS OVER w
123
+
FROM (VALUES (1, 10), (2, NULL), (3, NULL), (4, 40)) AS t(id, v)
124
+
WINDOW w AS (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
125
+
ORDER BY id
126
+
LIMIT 1;
127
+
----
128
+
1 40 40
129
+
130
+
statement ok
131
+
SET datafusion.optimizer.enable_window_limits = true;
132
+
133
+
query III
134
+
SELECT id,
135
+
LEAD(v) IGNORE NULLS OVER w,
136
+
LAG(v, -1, -1) IGNORE NULLS OVER w
137
+
FROM (VALUES (1, 10), (2, NULL), (3, NULL), (4, 40)) AS t(id, v)
138
+
WINDOW w AS (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
139
+
ORDER BY id
140
+
LIMIT 1;
141
+
----
142
+
1 40 40
143
+
144
+
# Negative-offset LAG must also block pushdown when no LEAD is present.
145
+
query II
146
+
SELECT id, LAG(v, -1) IGNORE NULLS OVER (
147
+
ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
148
+
)
149
+
FROM (VALUES (1, 10), (2, NULL), (3, NULL), (4, 40)) AS t(id, v)
150
+
ORDER BY id
151
+
LIMIT 1;
152
+
----
153
+
1 40
154
+
155
+
# Negative-offset LAG needs lookahead even without IGNORE NULLS.
156
+
query II
157
+
SELECT id, LAG(v, -1) OVER (
158
+
ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
159
+
)
160
+
FROM (VALUES (1, 10), (2, 20), (3, 30), (4, 40)) AS t(id, v)
161
+
ORDER BY id
162
+
LIMIT 1;
163
+
----
164
+
1 20
165
+
166
+
# Causal IGNORE NULLS functions should retain TopK below the window.
167
+
query IIIIIII
168
+
SELECT id,
169
+
LAG(v) IGNORE NULLS OVER w,
170
+
LEAD(v, -1) IGNORE NULLS OVER w,
171
+
LEAD(v, 0) IGNORE NULLS OVER w,
172
+
FIRST_VALUE(v) IGNORE NULLS OVER w,
173
+
LAST_VALUE(v) IGNORE NULLS OVER w,
174
+
NTH_VALUE(v, 2) IGNORE NULLS OVER w
175
+
FROM (VALUES (1, 10), (2, NULL), (3, 30), (4, 40)) AS t(id, v)
176
+
WINDOW w AS (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
177
+
ORDER BY id
178
+
LIMIT 3;
179
+
----
180
+
1 NULL NULL 10 10 10 NULL
181
+
2 10 10 NULL 10 10 NULL
182
+
3 10 10 30 10 30 30
183
+
184
+
query TT
185
+
EXPLAIN
186
+
SELECT id,
187
+
LAG(v) IGNORE NULLS OVER w,
188
+
LEAD(v, -1) IGNORE NULLS OVER w,
189
+
LEAD(v, 0) IGNORE NULLS OVER w,
190
+
FIRST_VALUE(v) IGNORE NULLS OVER w,
191
+
LAST_VALUE(v) IGNORE NULLS OVER w,
192
+
NTH_VALUE(v, 2) IGNORE NULLS OVER w
193
+
FROM (VALUES (1, 10), (2, NULL), (3, 30), (4, 40)) AS t(id, v)
194
+
WINDOW w AS (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
195
+
ORDER BY id
196
+
LIMIT 3;
197
+
----
198
+
logical_plan
199
+
01)Sort: t.id ASC NULLS LAST, fetch=3
200
+
02)--Projection: t.id, lag(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, lead(t.v,Int64(-1)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, lead(t.v,Int64(0)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, first_value(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, last_value(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, nth_value(t.v,Int64(2)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
201
+
03)----WindowAggr: windowExpr=[[lag(t.v)IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, lead(t.v, Int64(-1))IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, lead(t.v, Int64(0))IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, first_value(t.v)IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, last_value(t.v)IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, nth_value(t.v, Int64(2))IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
202
+
04)------SubqueryAlias: t
203
+
05)--------Projection: column1 AS id, column2 AS v
01)ProjectionExec: expr=[id@0 as id, lag(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as lag(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, lead(t.v,Int64(-1)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@3 as lead(t.v,Int64(-1)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, lead(t.v,Int64(0)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@4 as lead(t.v,Int64(0)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, first_value(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@5 as first_value(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, last_value(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@6 as last_value(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, nth_value(t.v,Int64(2)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@7 as nth_value(t.v,Int64(2)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]
207
+
02)--GlobalLimitExec: skip=0, fetch=3
208
+
03)----BoundedWindowAggExec: wdw=[lag(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "lag(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable Int64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, lead(t.v,Int64(-1)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "lead(t.v,Int64(-1)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable Int64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, lead(t.v,Int64(0)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "lead(t.v,Int64(0)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable Int64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, first_value(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "first_value(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable Int64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, last_value(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "last_value(t.v) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable Int64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, nth_value(t.v,Int64(2)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "nth_value(t.v,Int64(2)) IGNORE NULLS ORDER BY [t.id ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable Int64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
209
+
04)------ProjectionExec: expr=[column1@0 as id, column2@1 as v]
0 commit comments