Skip to content

Commit 80ec0e7

Browse files
committed
Skip the pushdown when fetch plus lookahead overflows; test lead and FOLLOWING start bounds
1 parent d1d0e5c commit 80ec0e7

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

‎datafusion/physical-optimizer/src/limit_pushdown_past_window.rs‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,16 @@ fn apply_limit(
194194
ctx.lookahead = 0;
195195
return Some(Transformed::no(Arc::clone(node)));
196196
};
197-
// Saturate like EnsureRequirements sort pushdown: a huge window
198-
// FOLLOWING bound must not wrap the TopK fetch in release builds.
197+
// A huge window FOLLOWING bound can make `fetch + lookahead` overflow.
198+
// Such a fetch could not drop any rows, and a `TopK` with a huge fetch
199+
// keeps every row in memory without spilling, so skip the pushdown.
200+
let Some(fetch) = fetch.checked_add(ctx.lookahead) else {
201+
ctx.lookahead = 0;
202+
return Some(Transformed::no(Arc::clone(node)));
203+
};
199204
let fetch = match node.fetch() {
200-
None => fetch.saturating_add(ctx.lookahead),
201-
Some(existing) => cmp::min(existing, fetch.saturating_add(ctx.lookahead)),
205+
None => fetch,
206+
Some(existing) => cmp::min(existing, fetch),
202207
};
203208
Some(Transformed::complete(node.with_fetch(Some(fetch)).unwrap()))
204209
}

‎datafusion/sqllogictest/test_files/window_limits.slt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,5 +874,21 @@ FROM t_huge_following ORDER BY a LIMIT 2;
874874
1 15
875875
2 14
876876

877+
# A `lead` offset on top of a huge frame saturates the lookahead
878+
query II
879+
SELECT a, lead(x, 9223372036854775807) OVER (ORDER BY a ROWS BETWEEN CURRENT ROW AND 18446744073709551615 FOLLOWING) s
880+
FROM t_huge_following ORDER BY a LIMIT 2;
881+
----
882+
1 NULL
883+
2 NULL
884+
885+
# A huge FOLLOWING start bound must not overflow the ROWS frame range
886+
query II
887+
SELECT a, sum(x) OVER (ORDER BY a ROWS BETWEEN 18446744073709551615 FOLLOWING AND 18446744073709551615 FOLLOWING) s
888+
FROM t_huge_following ORDER BY a LIMIT 2;
889+
----
890+
1 NULL
891+
2 NULL
892+
877893
statement ok
878894
DROP TABLE t_huge_following;

0 commit comments

Comments
 (0)