Skip to content

Apache DataFusion SQL Query Engine

fix: push a limit past every window in a plan, not just the first (#26773)

Cheng-Yi Shihf058e2d5367f
diff --git a/datafusion/physical-optimizer/src/limit_pushdown_past_window.rs b/datafusion/physical-optimizer/src/limit_pushdown_past_window.rs
index 763f5d79c3..a63267fd32 100644
--- a/datafusion/physical-optimizer/src/limit_pushdown_past_window.rs
+++ b/datafusion/physical-optimizer/src/limit_pushdown_past_window.rs
@@ -23,8 +23,9 @@ impl LimitPushPastWindows {
}
}
-#[derive(Eq, PartialEq)]
+#[derive(Eq, PartialEq, Default)]
enum Phase {
+ #[default]
FindOrGrow,
Apply,
}
@@ -33,16 +34,23 @@ enum Phase {
struct TraverseState {
pub limit: Option<usize>,
pub lookahead: usize,
+ pub phase: Phase,
}
impl TraverseState {
+ /// Forget the chain we were walking: no limit in hand, nothing to grow by,
+ /// and back to looking for the next limit.
+ pub fn reset(&mut self) {
+ *self = Self::default();
+ }
+
+ /// Start a chain from a limit we just found, abandoning whatever chain we
+ /// were walking: that one never found a sort to apply to, and this limit is
+ /// the one that governs everything below here.
pub fn reset_limit(&mut self, limit: Option<usize>) {
self.limit = limit;
self.lookahead = 0;
- }
-
- pub fn max_lookahead(&mut self, new_val: usize) {
- self.lookahead = self.lookahead.max(new_val);
+ self.phase = Phase::FindOrGrow;
}
}
@@ -56,7 +64,6 @@ impl PhysicalOptimizerRule for LimitPushPastWindows {
return Ok(original);
}
let mut ctx = TraverseState::default();
- let mut phase = Phase::FindOrGrow;
let result = original.transform_down(|node| {
// helper closure to DRY out most the early return cases
let reset = |node,
@@ -64,8 +71,7 @@ impl PhysicalOptimizerRule for LimitPushPastWindows {
-> datafusion_common::Result<
Transformed<Arc<dyn ExecutionPlan>>,
> {
- ctx.limit = None;
- ctx.lookahead = 0;
+ ctx.reset();
Ok(Transformed::no(node))
};
@@ -74,14 +80,20 @@ impl PhysicalOptimizerRule for LimitPushPastWindows {
return reset(node, &mut ctx);
}
- // grab the latest limit we see
- if phase == Phase::FindOrGrow && get_limit(&node, &mut ctx) {
+ // Grab the latest limit we see. The phase only has to hold us back at
+ // a `SortPreservingMergeExec`, which is both a limit source and an
+ // apply target; a plain limit node is never an apply target, so
+ // reading it is always safe -- and it is more current than the chain
+ // we were on, which has not found a sort to apply to.
+ if (ctx.phase == Phase::FindOrGrow || !node.is::<SortPreservingMergeExec>())
+ && get_limit(&node, &mut ctx)
+ {
return Ok(Transformed::no(node));
}
// grow the limit if we hit a window function
if let LimitBehavior::Window(lookahead) = node.limit_behavior() {
- phase = Phase::Apply;
+ ctx.phase = Phase::Apply;
if !grow_limit(lookahead, &mut ctx) {
return reset(node, &mut ctx);
}
@@ -89,7 +101,7 @@ impl PhysicalOptimizerRule for LimitPushPastWindows {
}
// Apply the limit if we hit a sortpreservingmerge node
- if phase == Phase::Apply
+ if ctx.phase == Phase::Apply
&& let Some(out) = apply_limit(&node, &mut ctx)
{
return Ok(out);
@@ -123,14 +135,23 @@ fn grow_limit(lookahead: Option<WindowLookahead>, ctx: &mut TraverseState) -> bo
let Some(lookahead) = lookahead else {
return false;
};
- if let Some(min_rows) = lookahead.min_rows {
- ctx.limit = Some(ctx.limit.unwrap_or(0).max(min_rows));
+ // Only raise a limit we actually have. With nothing in hand there is no
+ // limit to raise, and inventing one truncates a sort that has no `LIMIT`
+ // above it at all.
+ if let (Some(min_rows), Some(cur)) = (lookahead.min_rows, ctx.limit) {
+ ctx.limit = Some(cur.max(min_rows));
}
- ctx.max_lookahead(lookahead.frame_end);
+ // A window reads ahead of every row the windows above it need, so stacked
+ // windows add their lookaheads. Expressions within one window share theirs,
+ // which `WindowLookahead` has already taken the maximum over.
+ //
// Frame bounds come straight from the query and can be as large as
// `u64::MAX`, so saturate instead of overflowing. A saturated lookahead
// makes the `checked_add` in `apply_limit` fail, which skips the pushdown.
- ctx.max_lookahead(ctx.lookahead.saturating_add(lookahead.relative));
+ ctx.lookahead = ctx
+ .lookahead
+ .saturating_add(lookahead.frame_end)
+ .saturating_add(lookahead.relative);
true
}
@@ -141,23 +162,28 @@ fn apply_limit(
if !node.is::<SortExec>() && !node.is::<SortPreservingMergeExec>() {
return None;
}
- let latest = ctx.limit.take();
- let Some(fetch) = latest else {
- ctx.limit = None;
- ctx.lookahead = 0;
- return Some(Transformed::no(Arc::clone(node)));
- };
+ // Taking the state both reads the chain we were walking and forgets it.
+ let TraverseState {
+ limit, lookahead, ..
+ } = std::mem::take(ctx);
// A fetch that overflows can't drop any rows, so skip the pushdown rather
// than turning the sort into a TopK that holds every row and can't spill.
- let Some(fetch) = fetch.checked_add(ctx.lookahead) else {
- ctx.lookahead = 0;
+ let fetch = limit
+ .and_then(|limit| limit.checked_add(lookahead))
+ .map(|fetch| node.fetch().map_or(fetch, |existing| existing.min(fetch)));
+ // A `SortPreservingMergeExec` emits its input's first rows in order, so its
+ // fetch bounds every partition below it too: start the next chain from it.
+ // This sits before the early return below on purpose -- with no limit in hand
+ // the merge is left alone, but its own fetch is still a bound to carry down.
+ if node.is::<SortPreservingMergeExec>() {
+ ctx.reset_limit(fetch.or_else(|| node.fetch()));
+ }
+ let Some(fetch) = fetch else {
return Some(Transformed::no(Arc::clone(node)));
};
- let fetch = match node.fetch() {
- None => fetch,
- Some(existing) => existing.min(fetch),
- };
- Some(Transformed::complete(node.with_fetch(Some(fetch)).unwrap()))
+ // Carry on past this sort rather than stopping: the subtree below may hold
+ // another independent limit over another window.
+ Some(Transformed::yes(node.with_fetch(Some(fetch)).unwrap()))
}
fn get_limit(node: &Arc<dyn ExecutionPlan>, ctx: &mut TraverseState) -> bool {
@@ -177,11 +203,16 @@ fn get_limit(node: &Arc<dyn ExecutionPlan>, ctx: &mut TraverseState) -> bool {
#[cfg(test)]
mod tests {
use super::*;
- use arrow::datatypes::{DataType, Field, Schema};
+ use arrow::datatypes::{DataType, Field, FieldRef, Schema};
use datafusion_common::ScalarValue;
use datafusion_expr::WindowFrame;
- use datafusion_expr::{WindowFrameBound, WindowFrameUnits};
- use datafusion_functions_window::row_number::row_number_udwf;
+ use datafusion_expr::function::{PartitionEvaluatorArgs, WindowUDFFieldArgs};
+ use datafusion_expr::{
+ LimitEffect, PartitionEvaluator, Signature, WindowFrameBound, WindowFrameUnits,
+ WindowUDF, WindowUDFImpl,
+ };
+ use datafusion_functions_window::row_number::{RowNumber, row_number_udwf};
+ use datafusion_physical_expr::PhysicalExpr;
use datafusion_physical_expr::expressions::col;
use datafusion_physical_expr::window::StandardWindowExpr;
use datafusion_physical_expr_common::sort_expr::{LexOrdering, PhysicalSortExpr};
@@ -202,13 +233,16 @@ mod tests {
Arc::new(Schema::new(vec![Field::new("a", DataType::Int64, false)]))
}
- /// Build: LocalLimitExec or GlobalLimitExec → BoundedWindowAggExec(row_number) → SortExec
- fn build_window_plan(
- use_local_limit: bool,
+ /// Wrap `input` in `SortExec -> BoundedWindowAggExec(udwf)`, both ordered by `a`.
+ /// The frame ends at the current row, so the window adds no lookahead of its own
+ /// and the fetch a test asserts on comes only from what the rule carried down.
+ fn sort_then_window(
+ udwf: &Arc<WindowUDF>,
+ name: &str,
+ input: Arc<dyn ExecutionPlan>,
+ input_order_mode: InputOrderMode,
) -> datafusion_common::Result<Arc<dyn ExecutionPlan>> {
- let s = schema();
- let input: Arc<dyn ExecutionPlan> =
- Arc::new(PlaceholderRowExec::new(Arc::clone(&s)));
+ let s = input.schema();
let ordering =
LexOrdering::new(vec![PhysicalSortExpr::new_default(col("a", &s)?).asc()])
@@ -219,13 +253,7 @@ mod tests {
);
let window_expr = Arc::new(StandardWindowExpr::new(
- create_udwf_window_expr(
- &row_number_udwf(),
- &[],
- &s,
- "row_number".to_string(),
- false,
- )?,
+ create_udwf_window_expr(udwf, &[], &s, name.to_string(), false)?,
&[],
ordering.as_ref(),
Arc::new(WindowFrame::new_bounds(
@@ -235,20 +263,31 @@ mod tests {
)),
));
- let window: Arc<dyn ExecutionPlan> = Arc::new(BoundedWindowAggExec::try_new(
+ Ok(Arc::new(BoundedWindowAggExec::try_new(
vec![window_expr],
sort,
- InputOrderMode::Sorted,
+ input_order_mode,
true,
- )?);
+ )?))
+ }
- let limit: Arc<dyn ExecutionPlan> = if use_local_limit {
+ /// Build: LocalLimitExec or GlobalLimitExec → BoundedWindowAggExec(row_number) → SortExec
+ fn build_window_plan(
+ use_local_limit: bool,
+ ) -> datafusion_common::Result<Arc<dyn ExecutionPlan>> {
+ let input: Arc<dyn ExecutionPlan> = Arc::new(PlaceholderRowExec::new(schema()));
+ let window = sort_then_window(
+ &row_number_udwf(),
+ "row_number",
+ input,
+ InputOrderMode::Sorted,
+ )?;
+
+ Ok(if use_local_limit {
Arc::new(LocalLimitExec::new(window, 100))
} else {
Arc::new(GlobalLimitExec::new(window, 0, Some(100)))
- };
-
- Ok(limit)
+ })
}
fn optimize(plan: Arc<dyn ExecutionPlan>) -> Arc<dyn ExecutionPlan> {
@@ -283,4 +322,143 @@ mod tests {
PlaceholderRowExec
"#);
}
+
+ /// A window whose input is not grouped by partition key reads ahead by an
+ /// amount no frame bound describes, so the rule has to leave the sort below it
+ /// alone. This is the plan from [`global_limit_pushes_past_window`] with only
+ /// the input order mode changed.
+ #[test]
+ fn interleaved_partitions_block_the_pushdown() {
+ let input: Arc<dyn ExecutionPlan> = Arc::new(PlaceholderRowExec::new(schema()));
+ let window = sort_then_window(
+ &row_number_udwf(),
+ "row_number",
+ input,
+ InputOrderMode::Linear,
+ )
+ .unwrap();
+ let plan: Arc<dyn ExecutionPlan> =
+ Arc::new(GlobalLimitExec::new(window, 0, Some(100)));
+ let optimized = optimize(plan);
+ assert_snapshot!(plan_str(optimized.as_ref()), @r#"
+ GlobalLimitExec: skip=0, fetch=100
+ BoundedWindowAggExec: wdw=[row_number: Field { "row_number": UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Linear]
+ SortExec: expr=[a@0 ASC], preserve_partitioning=[true]
+ PlaceholderRowExec
+ "#);
+ }
+
+ /// A `row_number` wrapper that claims it always needs at least 200 input rows.
+ /// No in-tree window function returns [`LimitEffect::Absolute`], so wrapping one
+ /// is the only way to reach that arm of `grow_limit`.
+ #[derive(Debug, PartialEq, Eq, Hash)]
+ struct AtLeast200(RowNumber);
+
+ impl WindowUDFImpl for AtLeast200 {
+ fn name(&self) -> &str {
+ "at_least_200"
+ }
+
+ fn signature(&self) -> &Signature {
+ self.0.signature()
+ }
+
+ fn partition_evaluator(
+ &self,
+ args: PartitionEvaluatorArgs,
+ ) -> datafusion_common::Result<Box<dyn PartitionEvaluator>> {
+ self.0.partition_evaluator(args)
+ }
+
+ fn field(&self, args: WindowUDFFieldArgs) -> datafusion_common::Result<FieldRef> {
+ self.0.field(args)
+ }
+
+ fn limit_effect(&self, _args: &[Arc<dyn PhysicalExpr>]) -> LimitEffect {
+ LimitEffect::Absolute(200)
+ }
+ }
+
+ /// Build `[GlobalLimitExec(100) →] BoundedWindowAggExec(at_least_200) → SortExec
+ /// → PlaceholderRowExec`. Without the limit there is no fetch anywhere in the plan.
+ fn build_absolute_plan(
+ with_limit: bool,
+ ) -> datafusion_common::Result<Arc<dyn ExecutionPlan>> {
+ let input: Arc<dyn ExecutionPlan> = Arc::new(PlaceholderRowExec::new(schema()));
+ let udwf = Arc::new(WindowUDF::from(AtLeast200(RowNumber::new())));
+ let window =
+ sort_then_window(&udwf, "at_least_200", input, InputOrderMode::Sorted)?;
+
+ Ok(if with_limit {
+ Arc::new(GlobalLimitExec::new(window, 0, Some(100)))
+ } else {
+ window
+ })
+ }
+
+ /// Build `GlobalLimitExec(100) → BoundedWindowAggExec(row_number) → SortExec
+ /// → BoundedWindowAggExec(at_least_200) → SortExec → PlaceholderRowExec`.
+ ///
+ /// The outer sort consumes the limit, so the inner window is reached with
+ /// nothing in hand and the inner sort has no limit above it at all. Only this
+ /// PR makes the shape reachable: before it, applying a fetch ended the walk,
+ /// so nothing below the outer sort was ever visited.
+ fn build_nested_absolute_plan() -> datafusion_common::Result<Arc<dyn ExecutionPlan>> {
+ let input: Arc<dyn ExecutionPlan> = Arc::new(PlaceholderRowExec::new(schema()));
+ let udwf = Arc::new(WindowUDF::from(AtLeast200(RowNumber::new())));
+ let inner =
+ sort_then_window(&udwf, "at_least_200", input, InputOrderMode::Sorted)?;
+ let outer = sort_then_window(
+ &row_number_udwf(),
+ "row_number",
+ inner,
+ InputOrderMode::Sorted,
+ )?;
+
+ Ok(Arc::new(GlobalLimitExec::new(outer, 0, Some(100))))
+ }
+
+ /// With no limit in the plan there is nothing to raise, so the sort must stay a
+ /// full sort. Turning it into a TopK here would drop rows the query asked for.
+ #[test]
+ fn absolute_limit_effect_does_not_invent_a_limit() {
+ let plan = build_absolute_plan(false).unwrap();
+ let optimized = optimize(plan);
+ assert_snapshot!(plan_str(optimized.as_ref()), @r#"
+ BoundedWindowAggExec: wdw=[at_least_200: Field { "at_least_200": UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+ SortExec: expr=[a@0 ASC], preserve_partitioning=[true]
+ PlaceholderRowExec
+ "#);
+ }
+
+ /// A limit that is in the plan is still raised to the window's floor: 100 rows
+ /// are asked for, the window needs 200, so the sort fetches 200.
+ #[test]
+ fn absolute_limit_effect_raises_an_existing_limit() {
+ let plan = build_absolute_plan(true).unwrap();
+ let optimized = optimize(plan);
+ assert_snapshot!(plan_str(optimized.as_ref()), @r#"
+ GlobalLimitExec: skip=0, fetch=100
+ BoundedWindowAggExec: wdw=[at_least_200: Field { "at_least_200": UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+ SortExec: TopK(fetch=200), expr=[a@0 ASC], preserve_partitioning=[true]
+ PlaceholderRowExec
+ "#);
+ }
+
+ /// The nested variant: the limit is spent on the outer sort, so the inner
+ /// window's `Absolute(200)` has nothing to raise. It must not become a fetch
+ /// on the inner sort, which no `LIMIT` sits above.
+ #[test]
+ fn absolute_limit_effect_does_not_invent_a_limit_below_an_applied_sort() {
+ let plan = build_nested_absolute_plan().unwrap();
+ let optimized = optimize(plan);
+ assert_snapshot!(plan_str(optimized.as_ref()), @r#"
+ GlobalLimitExec: skip=0, fetch=100
+ BoundedWindowAggExec: wdw=[row_number: Field { "row_number": UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+ SortExec: TopK(fetch=100), expr=[a@0 ASC], preserve_partitioning=[true], sort_prefix=[a@0 ASC]
+ BoundedWindowAggExec: wdw=[at_least_200: Field { "at_least_200": UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+ SortExec: expr=[a@0 ASC], preserve_partitioning=[true]
+ PlaceholderRowExec
+ "#);
+ }
}
diff --git a/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs b/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs
index 5c09fa2261..b2372cc182 100644
--- a/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs
+++ b/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs
@@ -542,6 +542,14 @@ impl ExecutionPlan for BoundedWindowAggExec {
}
fn limit_behavior(&self) -> LimitBehavior {
+ // A frame's end bound counts rows within a partition, so reading it as a
+ // row count of the input only holds when each partition is a contiguous
+ // run of it -- which is what `Sorted` means. In the other modes a
+ // partition's rows are interleaved with other partitions', and how far
+ // ahead this node reads is not known here.
+ if self.input_order_mode != InputOrderMode::Sorted {
+ return LimitBehavior::Window(None);
+ }
LimitBehavior::Window(window_lookahead(&self.window_expr))
}
diff --git a/datafusion/sqllogictest/test_files/window.slt b/datafusion/sqllogictest/test_files/window.slt
index 297899be8b..221439a4cb 100644
--- a/datafusion/sqllogictest/test_files/window.slt
+++ b/datafusion/sqllogictest/test_files/window.slt
@@ -1331,7 +1331,7 @@ physical_plan
02)--GlobalLimitExec: skip=0, fetch=5
03)----BoundedWindowAggExec: wdw=[sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING: Field { "sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN 5 PRECEDING AND 1 FOLLOWING], mode=[Sorted]
04)------BoundedWindowAggExec: wdw=[sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING: Field { "sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING], mode=[Sorted]
-05)--------SortExec: TopK(fetch=10), expr=[c9@0 DESC], preserve_partitioning=[false]
+05)--------SortExec: TopK(fetch=11), expr=[c9@0 DESC], preserve_partitioning=[false]
06)----------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c9], file_type=csv, has_header=true
query III
@@ -1374,7 +1374,7 @@ physical_plan
02)--GlobalLimitExec: skip=0, fetch=5
03)----BoundedWindowAggExec: wdw=[sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING: Field { "sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN 5 PRECEDING AND 1 FOLLOWING], mode=[Sorted]
04)------BoundedWindowAggExec: wdw=[sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING: Field { "sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING], mode=[Sorted]
-05)--------SortExec: TopK(fetch=10), expr=[c9@0 DESC], preserve_partitioning=[false]
+05)--------SortExec: TopK(fetch=11), expr=[c9@0 DESC], preserve_partitioning=[false]
06)----------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c9], file_type=csv, has_header=true
# ensure limit pushdown can handle bigger preceding instead of following
@@ -1434,7 +1434,7 @@ physical_plan
02)--GlobalLimitExec: skip=5, fetch=5
03)----BoundedWindowAggExec: wdw=[sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING: Field { "sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN 5 PRECEDING AND 1 FOLLOWING], mode=[Sorted]
04)------BoundedWindowAggExec: wdw=[sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING: Field { "sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING], mode=[Sorted]
-05)--------SortExec: TopK(fetch=15), expr=[c9@0 DESC], preserve_partitioning=[false]
+05)--------SortExec: TopK(fetch=16), expr=[c9@0 DESC], preserve_partitioning=[false]
06)----------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c9], file_type=csv, has_header=true
query III
@@ -1729,7 +1729,7 @@ physical_plan
02)--GlobalLimitExec: skip=0, fetch=5
03)----BoundedWindowAggExec: wdw=[sum(aggregate_test_100.c9) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING: Field { "sum(aggregate_test_100.c9) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING], mode=[Sorted]
04)------BoundedWindowAggExec: wdw=[sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c1 ASC NULLS LAST, aggregate_test_100.c9 DESC NULLS FIRST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING: Field { "sum(aggregate_test_100.c9) ORDER BY [aggregate_test_100.c1 ASC NULLS LAST, aggregate_test_100.c9 DESC NULLS FIRST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING], mode=[Sorted]
-05)--------SortExec: TopK(fetch=10), expr=[c1@0 ASC NULLS LAST, c9@1 DESC], preserve_partitioning=[false]
+05)--------SortExec: TopK(fetch=15), expr=[c1@0 ASC NULLS LAST, c9@1 DESC], preserve_partitioning=[false]
06)----------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c1, c9], file_type=csv, has_header=true
@@ -1773,7 +1773,7 @@ physical_plan
02)--GlobalLimitExec: skip=0, fetch=5
03)----BoundedWindowAggExec: wdw=[sum(aggregate_test_100.c9) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c9 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING: Field { "sum(aggregate_test_100.c9) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c9 ASC NULLS LAST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN 5 PRECEDING AND 1 FOLLOWING], mode=[Sorted]
04)------BoundedWindowAggExec: wdw=[sum(aggregate_test_100.c9) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING: Field { "sum(aggregate_test_100.c9) PARTITION BY [aggregate_test_100.c1] ORDER BY [aggregate_test_100.c9 DESC NULLS FIRST] ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN 1 PRECEDING AND 5 FOLLOWING], mode=[Sorted]
-05)--------SortExec: TopK(fetch=10), expr=[c1@0 ASC NULLS LAST, c9@1 DESC], preserve_partitioning=[false]
+05)--------SortExec: TopK(fetch=11), expr=[c1@0 ASC NULLS LAST, c9@1 DESC], preserve_partitioning=[false]
06)----------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c1, c9], file_type=csv, has_header=true
query III
diff --git a/datafusion/sqllogictest/test_files/window_limits.slt b/datafusion/sqllogictest/test_files/window_limits.slt
index 32fdf93d4a..04a19ffaa8 100644
--- a/datafusion/sqllogictest/test_files/window_limits.slt
+++ b/datafusion/sqllogictest/test_files/window_limits.slt
@@ -897,3 +897,588 @@ physical_plan
statement ok
DROP TABLE huge_frame_t;
+
+# Every window in a plan should get the limit pushed past it, not just the first
+# one. See https://github.com/apache/datafusion/issues/26659
+#
+# Both branches of this union have their own window and their own limit, so both
+# SortExecs should become a TopK. `rowsort` is required here: UnionExec yields
+# its branches in a nondeterministic order.
+statement ok
+set datafusion.optimizer.enable_window_limits = false;
+
+query II rowsort
+SELECT * FROM (SELECT empno, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) s FROM employees ORDER BY empno LIMIT 2)
+UNION ALL
+SELECT * FROM (SELECT empno, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) s FROM employees ORDER BY empno LIMIT 3);
+----
+102 3276123488
+102 3276123488
+299 3304897863
+299 3304897863
+363 5170205535
+
+statement ok
+set datafusion.optimizer.enable_window_limits = true;
+
+query II rowsort
+SELECT * FROM (SELECT empno, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) s FROM employees ORDER BY empno LIMIT 2)
+UNION ALL
+SELECT * FROM (SELECT empno, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) s FROM employees ORDER BY empno LIMIT 3);
+----
+102 3276123488
+102 3276123488
+299 3304897863
+299 3304897863
+363 5170205535
+
+query TT
+EXPLAIN
+SELECT * FROM (SELECT empno, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) s FROM employees ORDER BY empno LIMIT 2)
+UNION ALL
+SELECT * FROM (SELECT empno, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) s FROM employees ORDER BY empno LIMIT 3);
+----
+logical_plan
+01)Union
+02)--Sort: employees.empno ASC NULLS LAST, fetch=2
+03)----Projection: employees.empno, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS s
+04)------WindowAggr: windowExpr=[[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+05)--------TableScan: employees projection=[empno, salary]
+06)--Sort: employees.empno ASC NULLS LAST, fetch=3
+07)----Projection: employees.empno, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS s
+08)------WindowAggr: windowExpr=[[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+09)--------TableScan: employees projection=[empno, salary]
+physical_plan
+01)UnionExec
+02)--ProjectionExec: expr=[empno@0 as empno, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as s]
+03)----GlobalLimitExec: skip=0, fetch=2
+04)------BoundedWindowAggExec: wdw=[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+05)--------SortExec: TopK(fetch=2), expr=[empno@0 ASC NULLS LAST], preserve_partitioning=[false]
+06)----------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100_with_dates.csv]]}, projection=[empno, salary], file_type=csv, has_header=true
+07)--ProjectionExec: expr=[empno@0 as empno, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as s]
+08)----GlobalLimitExec: skip=0, fetch=3
+09)------BoundedWindowAggExec: wdw=[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+10)--------SortExec: TopK(fetch=3), expr=[empno@0 ASC NULLS LAST], preserve_partitioning=[false]
+11)----------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100_with_dates.csv]]}, projection=[empno, salary], file_type=csv, has_header=true
+
+# A second, independent window+limit further down the same chain should get a
+# TopK too. Three things about this query are load bearing, so do not simplify
+# it: the two levels must order by different columns (otherwise EnforceSorting
+# drops the inner SortExec), the outer SELECT must reference `s1` (otherwise
+# projection pushdown removes the inner window), and both levels need their own
+# LIMIT.
+statement ok
+set datafusion.optimizer.enable_window_limits = false;
+
+query IIII
+SELECT empno, salary, s1, sum(empno) OVER (ORDER BY salary ROWS UNBOUNDED PRECEDING) AS s2
+FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s1 FROM employees ORDER BY empno LIMIT 5)
+ORDER BY salary LIMIT 2;
+----
+299 28774375 3304897863 299
+417 557517119 5727722654 716
+
+statement ok
+set datafusion.optimizer.enable_window_limits = true;
+
+query IIII
+SELECT empno, salary, s1, sum(empno) OVER (ORDER BY salary ROWS UNBOUNDED PRECEDING) AS s2
+FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s1 FROM employees ORDER BY empno LIMIT 5)
+ORDER BY salary LIMIT 2;
+----
+299 28774375 3304897863 299
+417 557517119 5727722654 716
+
+query TT
+EXPLAIN
+SELECT empno, salary, s1, sum(empno) OVER (ORDER BY salary ROWS UNBOUNDED PRECEDING) AS s2
+FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s1 FROM employees ORDER BY empno LIMIT 5)
+ORDER BY salary LIMIT 2;
+----
+logical_plan
+01)Sort: employees.salary ASC NULLS LAST, fetch=2
+02)--Projection: employees.empno, employees.salary, s1, sum(employees.empno) ORDER BY [employees.salary ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS s2
+03)----WindowAggr: windowExpr=[[sum(CAST(employees.empno AS Int64)) ORDER BY [employees.salary ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+04)------Sort: employees.empno ASC NULLS LAST, fetch=5
+05)--------Projection: employees.empno, employees.salary, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS s1
+06)----------WindowAggr: windowExpr=[[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+07)------------TableScan: employees projection=[empno, salary]
+physical_plan
+01)ProjectionExec: expr=[empno@0 as empno, salary@1 as salary, s1@2 as s1, sum(employees.empno) ORDER BY [employees.salary ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@3 as s2]
+02)--GlobalLimitExec: skip=0, fetch=2
+03)----BoundedWindowAggExec: wdw=[sum(employees.empno) ORDER BY [employees.salary ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "sum(employees.empno) ORDER BY [employees.salary ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable Int64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+04)------SortExec: TopK(fetch=2), expr=[salary@1 ASC NULLS LAST], preserve_partitioning=[false]
+05)--------ProjectionExec: expr=[empno@0 as empno, salary@1 as salary, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as s1]
+06)----------GlobalLimitExec: skip=0, fetch=5
+07)------------BoundedWindowAggExec: wdw=[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+08)--------------SortExec: TopK(fetch=5), expr=[empno@0 ASC NULLS LAST], preserve_partitioning=[false]
+09)----------------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100_with_dates.csv]]}, projection=[empno, salary], file_type=csv, has_header=true
+
+# A window the rule cannot grow past -- here a RANGE frame, which is not
+# statically evaluatable -- must not poison the rest of the traversal. The limit
+# below it is independent and still belongs in the sort under the inner window.
+#
+# The next two queries differ only in the outer ORDER BY, which decides what the
+# walk meets after the window it could not grow past. If the stale phase survives,
+# both leave the inner SortExec a full sort; the second is the stricter of the two:
+# * ORDER BY salary leaves an outer SortExec in place, so either clear can be the
+# one that forgets the stale phase.
+# * ORDER BY empno matches the inner ordering, so EnforceSorting elides the outer
+# SortExec and the next node down is the inner limit itself. With no sort in
+# between, the clear in the early-return path is the only one that runs.
+statement ok
+set datafusion.optimizer.enable_window_limits = false;
+
+query IIII
+SELECT empno, salary, s1, sum(empno) OVER (ORDER BY salary) AS s2
+FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s1 FROM employees ORDER BY empno LIMIT 5)
+ORDER BY salary LIMIT 2;
+----
+299 28774375 3304897863 299
+417 557517119 5727722654 716
+
+statement ok
+set datafusion.optimizer.enable_window_limits = true;
+
+query IIII
+SELECT empno, salary, s1, sum(empno) OVER (ORDER BY salary) AS s2
+FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s1 FROM employees ORDER BY empno LIMIT 5)
+ORDER BY salary LIMIT 2;
+----
+299 28774375 3304897863 299
+417 557517119 5727722654 716
+
+query TT
+EXPLAIN
+SELECT empno, salary, s1, sum(empno) OVER (ORDER BY salary) AS s2
+FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s1 FROM employees ORDER BY empno LIMIT 5)
+ORDER BY salary LIMIT 2;
+----
+logical_plan
+01)Sort: employees.salary ASC NULLS LAST, fetch=2
+02)--Projection: employees.empno, employees.salary, s1, sum(employees.empno) ORDER BY [employees.salary ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS s2
+03)----WindowAggr: windowExpr=[[sum(CAST(employees.empno AS Int64)) ORDER BY [employees.salary ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+04)------Sort: employees.empno ASC NULLS LAST, fetch=5
+05)--------Projection: employees.empno, employees.salary, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS s1
+06)----------WindowAggr: windowExpr=[[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+07)------------TableScan: employees projection=[empno, salary]
+physical_plan
+01)ProjectionExec: expr=[empno@0 as empno, salary@1 as salary, s1@2 as s1, sum(employees.empno) ORDER BY [employees.salary ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@3 as s2]
+02)--GlobalLimitExec: skip=0, fetch=2
+03)----BoundedWindowAggExec: wdw=[sum(employees.empno) ORDER BY [employees.salary ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "sum(employees.empno) ORDER BY [employees.salary ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable Int64 }, frame: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+04)------SortExec: expr=[salary@1 ASC NULLS LAST], preserve_partitioning=[false]
+05)--------ProjectionExec: expr=[empno@0 as empno, salary@1 as salary, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as s1]
+06)----------GlobalLimitExec: skip=0, fetch=5
+07)------------BoundedWindowAggExec: wdw=[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+08)--------------SortExec: TopK(fetch=5), expr=[empno@0 ASC NULLS LAST], preserve_partitioning=[false]
+09)----------------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100_with_dates.csv]]}, projection=[empno, salary], file_type=csv, has_header=true
+
+statement ok
+set datafusion.optimizer.enable_window_limits = false;
+
+query III
+SELECT empno, s1, sum(empno) OVER (ORDER BY empno) AS s2
+FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s1 FROM employees ORDER BY empno LIMIT 5)
+ORDER BY empno LIMIT 2;
+----
+102 3276123488 102
+299 3304897863 401
+
+statement ok
+set datafusion.optimizer.enable_window_limits = true;
+
+query III
+SELECT empno, s1, sum(empno) OVER (ORDER BY empno) AS s2
+FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s1 FROM employees ORDER BY empno LIMIT 5)
+ORDER BY empno LIMIT 2;
+----
+102 3276123488 102
+299 3304897863 401
+
+query TT
+EXPLAIN
+SELECT empno, s1, sum(empno) OVER (ORDER BY empno) AS s2
+FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s1 FROM employees ORDER BY empno LIMIT 5)
+ORDER BY empno LIMIT 2;
+----
+logical_plan
+01)Sort: employees.empno ASC NULLS LAST, fetch=2
+02)--Projection: employees.empno, s1, sum(employees.empno) ORDER BY [employees.empno ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS s2
+03)----WindowAggr: windowExpr=[[sum(CAST(employees.empno AS Int64)) ORDER BY [employees.empno ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+04)------Sort: employees.empno ASC NULLS LAST, fetch=5
+05)--------Projection: employees.empno, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS s1
+06)----------WindowAggr: windowExpr=[[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+07)------------TableScan: employees projection=[empno, salary]
+physical_plan
+01)ProjectionExec: expr=[empno@0 as empno, s1@1 as s1, sum(employees.empno) ORDER BY [employees.empno ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as s2]
+02)--GlobalLimitExec: skip=0, fetch=2
+03)----BoundedWindowAggExec: wdw=[sum(employees.empno) ORDER BY [employees.empno ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "sum(employees.empno) ORDER BY [employees.empno ASC NULLS LAST] RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable Int64 }, frame: RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+04)------ProjectionExec: expr=[empno@0 as empno, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as s1]
+05)--------GlobalLimitExec: skip=0, fetch=5
+06)----------BoundedWindowAggExec: wdw=[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+07)------------SortExec: TopK(fetch=5), expr=[empno@0 ASC NULLS LAST], preserve_partitioning=[false]
+08)--------------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100_with_dates.csv]]}, projection=[empno, salary], file_type=csv, has_header=true
+
+# A window the rule *can* grow past must not swallow the limit below it either.
+# Growing leaves the walk in `Phase::Apply`, and the guard on `get_limit` used to
+# skip every limit while in that phase -- so the inner `GlobalLimitExec` below was
+# never read, the chain was dropped at it instead, and the inner window's sort was
+# left a full sort although a `LIMIT 5` sat directly above it.
+#
+# This is the previous query with the outer frame changed from RANGE to ROWS, and
+# the change of frame is what makes it fail: a RANGE frame cannot be grown past, so
+# it resets the state on the way down and the inner limit is seen afterwards. A ROWS
+# frame can, so nothing resets. Three other properties are load-bearing: both levels
+# must order by the same column, or `EnforceSorting` leaves an outer SortExec in
+# between and `apply_limit` resets there; the outer SELECT must reference `s1`, or
+# projection pushdown removes the inner window; and both levels need their own LIMIT.
+
+statement ok
+set datafusion.optimizer.enable_window_limits = false;
+
+query III
+SELECT empno, s1, sum(empno) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s2
+FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s1 FROM employees ORDER BY empno LIMIT 5)
+ORDER BY empno LIMIT 2;
+----
+102 3276123488 102
+299 3304897863 401
+
+statement ok
+set datafusion.optimizer.enable_window_limits = true;
+
+query III
+SELECT empno, s1, sum(empno) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s2
+FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s1 FROM employees ORDER BY empno LIMIT 5)
+ORDER BY empno LIMIT 2;
+----
+102 3276123488 102
+299 3304897863 401
+
+query TT
+EXPLAIN
+SELECT empno, s1, sum(empno) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s2
+FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS UNBOUNDED PRECEDING) AS s1 FROM employees ORDER BY empno LIMIT 5)
+ORDER BY empno LIMIT 2;
+----
+logical_plan
+01)Sort: employees.empno ASC NULLS LAST, fetch=2
+02)--Projection: employees.empno, s1, sum(employees.empno) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS s2
+03)----WindowAggr: windowExpr=[[sum(CAST(employees.empno AS Int64)) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+04)------Sort: employees.empno ASC NULLS LAST, fetch=5
+05)--------Projection: employees.empno, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS s1
+06)----------WindowAggr: windowExpr=[[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+07)------------TableScan: employees projection=[empno, salary]
+physical_plan
+01)ProjectionExec: expr=[empno@0 as empno, s1@1 as s1, sum(employees.empno) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as s2]
+02)--GlobalLimitExec: skip=0, fetch=2
+03)----BoundedWindowAggExec: wdw=[sum(employees.empno) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "sum(employees.empno) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable Int64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+04)------ProjectionExec: expr=[empno@0 as empno, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@2 as s1]
+05)--------GlobalLimitExec: skip=0, fetch=5
+06)----------BoundedWindowAggExec: wdw=[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+07)------------SortExec: TopK(fetch=5), expr=[empno@0 ASC NULLS LAST], preserve_partitioning=[false]
+08)--------------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100_with_dates.csv]]}, projection=[empno, salary], file_type=csv, has_header=true
+
+# Stacked windows add their lookaheads, they do not take the max. An outer window
+# reads the inner window's output column, so every row the outer window reads
+# ahead is a row the inner window must have computed correctly -- which needs the
+# inner window's own lookahead on top. Taking the max leaves the inner sort a row
+# short and the last row of the result is computed over a truncated frame.
+#
+# The outer `LIMIT 3` here is spent on the SortExec that orders by `salary`, and
+# the walk then starts a new chain at the inner `LIMIT 3`, so the shape is only
+# reachable when applying a fetch does not end the walk.
+# Three properties are load-bearing: `s2` must read `s1`, or
+# the max is enough and the case does not fail; the two frames must end at
+# different offsets (1 and 2), or the max and the sum agree; and the outer
+# window's frame must end at the current row so it adds no lookahead of its own.
+
+statement ok
+set datafusion.optimizer.enable_window_limits = false;
+
+query IIII
+SELECT empno, salary, s2, sum(empno) OVER (ORDER BY salary ROWS UNBOUNDED PRECEDING) AS s3
+FROM (
+ SELECT empno, salary, sum(s1) OVER (ORDER BY empno ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s2
+ FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) AS s1 FROM employees)
+ ORDER BY empno LIMIT 3
+)
+ORDER BY salary LIMIT 3;
+----
+299 28774375 8936059064 299
+363 1865307672 15119054465 662
+102 3276123488 7621804701 764
+
+statement ok
+set datafusion.optimizer.enable_window_limits = true;
+
+query IIII
+SELECT empno, salary, s2, sum(empno) OVER (ORDER BY salary ROWS UNBOUNDED PRECEDING) AS s3
+FROM (
+ SELECT empno, salary, sum(s1) OVER (ORDER BY empno ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s2
+ FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) AS s1 FROM employees)
+ ORDER BY empno LIMIT 3
+)
+ORDER BY salary LIMIT 3;
+----
+299 28774375 8936059064 299
+363 1865307672 15119054465 662
+102 3276123488 7621804701 764
+
+query TT
+EXPLAIN
+SELECT empno, salary, s2, sum(empno) OVER (ORDER BY salary ROWS UNBOUNDED PRECEDING) AS s3
+FROM (
+ SELECT empno, salary, sum(s1) OVER (ORDER BY empno ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s2
+ FROM (SELECT empno, salary, sum(salary) OVER (ORDER BY empno ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) AS s1 FROM employees)
+ ORDER BY empno LIMIT 3
+)
+ORDER BY salary LIMIT 3;
+----
+logical_plan
+01)Sort: employees.salary ASC NULLS LAST, fetch=3
+02)--Projection: employees.empno, employees.salary, s2, sum(employees.empno) ORDER BY [employees.salary ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW AS s3
+03)----WindowAggr: windowExpr=[[sum(CAST(employees.empno AS Int64)) ORDER BY [employees.salary ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW]]
+04)------Sort: employees.empno ASC NULLS LAST, fetch=3
+05)--------Projection: employees.empno, employees.salary, sum(s1) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING AS s2
+06)----------WindowAggr: windowExpr=[[sum(s1) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING]]
+07)------------Projection: employees.empno, employees.salary, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING AS s1
+08)--------------WindowAggr: windowExpr=[[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING]]
+09)----------------TableScan: employees projection=[empno, salary]
+physical_plan
+01)ProjectionExec: expr=[empno@0 as empno, salary@1 as salary, s2@2 as s2, sum(employees.empno) ORDER BY [employees.salary ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW@3 as s3]
+02)--GlobalLimitExec: skip=0, fetch=3
+03)----BoundedWindowAggExec: wdw=[sum(employees.empno) ORDER BY [employees.salary ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: Field { "sum(employees.empno) ORDER BY [employees.salary ASC NULLS LAST] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW": nullable Int64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
+04)------SortExec: TopK(fetch=3), expr=[salary@1 ASC NULLS LAST], preserve_partitioning=[false]
+05)--------ProjectionExec: expr=[empno@0 as empno, salary@1 as salary, sum(s1) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING@3 as s2]
+06)----------GlobalLimitExec: skip=0, fetch=3
+07)------------BoundedWindowAggExec: wdw=[sum(s1) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING: Field { "sum(s1) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING], mode=[Sorted]
+08)--------------ProjectionExec: expr=[empno@0 as empno, salary@1 as salary, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING@2 as s1]
+09)----------------BoundedWindowAggExec: wdw=[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING: Field { "sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING], mode=[Sorted]
+10)------------------SortExec: TopK(fetch=6), expr=[empno@0 ASC NULLS LAST], preserve_partitioning=[false]
+11)--------------------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100_with_dates.csv]]}, projection=[empno, salary], file_type=csv, has_header=true
+
+# The same arithmetic with nothing in between: both windows sit on one chain
+# above a single sort. `main` already gets this shape wrong, because its early
+# stop only kept the walk out of the subtree below a sort it had given a fetch
+# to -- it never stopped the walk from passing two windows in a row.
+
+statement ok
+set datafusion.optimizer.enable_window_limits = false;
+
+query II
+SELECT empno, sum(s1) OVER (ORDER BY empno ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s2
+FROM (SELECT empno, sum(salary) OVER (ORDER BY empno ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) AS s1 FROM employees)
+ORDER BY empno LIMIT 3;
+----
+102 7621804701
+299 8936059064
+363 15119054465
+
+statement ok
+set datafusion.optimizer.enable_window_limits = true;
+
+query II
+SELECT empno, sum(s1) OVER (ORDER BY empno ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s2
+FROM (SELECT empno, sum(salary) OVER (ORDER BY empno ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) AS s1 FROM employees)
+ORDER BY empno LIMIT 3;
+----
+102 7621804701
+299 8936059064
+363 15119054465
+
+query TT
+EXPLAIN
+SELECT empno, sum(s1) OVER (ORDER BY empno ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s2
+FROM (SELECT empno, sum(salary) OVER (ORDER BY empno ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING) AS s1 FROM employees)
+ORDER BY empno LIMIT 3;
+----
+logical_plan
+01)Sort: employees.empno ASC NULLS LAST, fetch=3
+02)--Projection: employees.empno, sum(s1) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING AS s2
+03)----WindowAggr: windowExpr=[[sum(s1) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING]]
+04)------Projection: employees.empno, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING AS s1
+05)--------WindowAggr: windowExpr=[[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING]]
+06)----------TableScan: employees projection=[empno, salary]
+physical_plan
+01)ProjectionExec: expr=[empno@0 as empno, sum(s1) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING@2 as s2]
+02)--GlobalLimitExec: skip=0, fetch=3
+03)----BoundedWindowAggExec: wdw=[sum(s1) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING: Field { "sum(s1) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING], mode=[Sorted]
+04)------ProjectionExec: expr=[empno@0 as empno, sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING@2 as s1]
+05)--------BoundedWindowAggExec: wdw=[sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING: Field { "sum(employees.salary) ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN CURRENT ROW AND 2 FOLLOWING], mode=[Sorted]
+06)----------SortExec: TopK(fetch=6), expr=[empno@0 ASC NULLS LAST], preserve_partitioning=[false]
+07)------------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100_with_dates.csv]]}, projection=[empno, salary], file_type=csv, has_header=true
+
+# A `SortPreservingMergeExec` emits its input's first rows in order, so its fetch
+# bounds every partition below it as well -- the same reading `limit_pushdown` has
+# of `LimitBehavior::Merge`, where the limit stays above and each child gets a copy.
+# `apply_limit` took the traversal state, which is right for a `SortExec` because a
+# sort reorders its input, but it threw that bound away at a merge: a second window
+# below the merge kept a full sort although the merge's fetch bounded it.
+#
+# A `PARTITION BY` window is what puts a merge in the middle, so the inner window
+# here partitions and the outer one does not; without it there is no merge and the
+# shape degenerates into the stacked-window case above. Three other properties are
+# load-bearing: the outer SELECT must read `s1`, or projection pushdown drops the
+# inner window entirely; both levels must order by the same two columns, or
+# `EnforceSorting` puts another per-partition SortExec between the merge and the
+# inner window and the chain the merge starts is reset there, never reaching the
+# inner window's sort; and the outer frame must end after the current row, or it
+# adds no lookahead, the merge's fetch is just the limit, and the test no longer
+# tells `5 + 1` apart from `5`.
+#
+# The fetch the merge is given is `5 + 1`, and that 6 is what starts the next chain,
+# so the sort under the inner window gets `6 + 1`.
+
+statement ok
+set datafusion.optimizer.enable_window_limits = false;
+
+query TIII
+SELECT depname, empno, s1,
+ sum(s1) OVER (ORDER BY depname, empno ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s2
+FROM (
+ SELECT depname, empno,
+ sum(salary) OVER (PARTITION BY depname ORDER BY empno
+ ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s1
+ FROM employees
+)
+ORDER BY depname, empno LIMIT 5;
+----
+a 102 5141431160 11022181173
+a 363 5880750013 10041486965
+a 829 4160736952 5060807172
+a 2555 900070220 5372396992
+a 2809 4472326772 8621826796
+
+statement ok
+set datafusion.optimizer.enable_window_limits = true;
+
+query TIII
+SELECT depname, empno, s1,
+ sum(s1) OVER (ORDER BY depname, empno ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s2
+FROM (
+ SELECT depname, empno,
+ sum(salary) OVER (PARTITION BY depname ORDER BY empno
+ ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s1
+ FROM employees
+)
+ORDER BY depname, empno LIMIT 5;
+----
+a 102 5141431160 11022181173
+a 363 5880750013 10041486965
+a 829 4160736952 5060807172
+a 2555 900070220 5372396992
+a 2809 4472326772 8621826796
+
+query TT
+EXPLAIN
+SELECT depname, empno, s1,
+ sum(s1) OVER (ORDER BY depname, empno ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s2
+FROM (
+ SELECT depname, empno,
+ sum(salary) OVER (PARTITION BY depname ORDER BY empno
+ ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s1
+ FROM employees
+)
+ORDER BY depname, empno LIMIT 5;
+----
+logical_plan
+01)Sort: employees.depname ASC NULLS LAST, employees.empno ASC NULLS LAST, fetch=5
+02)--Projection: employees.depname, employees.empno, s1, sum(s1) ORDER BY [employees.depname ASC NULLS LAST, employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING AS s2
+03)----WindowAggr: windowExpr=[[sum(s1) ORDER BY [employees.depname ASC NULLS LAST, employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING]]
+04)------Projection: employees.depname, employees.empno, sum(employees.salary) PARTITION BY [employees.depname] ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING AS s1
+05)--------WindowAggr: windowExpr=[[sum(employees.salary) PARTITION BY [employees.depname] ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING]]
+06)----------TableScan: employees projection=[depname, empno, salary]
+physical_plan
+01)ProjectionExec: expr=[depname@0 as depname, empno@1 as empno, s1@2 as s1, sum(s1) ORDER BY [employees.depname ASC NULLS LAST, employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING@3 as s2]
+02)--GlobalLimitExec: skip=0, fetch=5
+03)----BoundedWindowAggExec: wdw=[sum(s1) ORDER BY [employees.depname ASC NULLS LAST, employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING: Field { "sum(s1) ORDER BY [employees.depname ASC NULLS LAST, employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING], mode=[Sorted]
+04)------SortPreservingMergeExec: [depname@0 ASC NULLS LAST, empno@1 ASC NULLS LAST], fetch=6
+05)--------ProjectionExec: expr=[depname@0 as depname, empno@1 as empno, sum(employees.salary) PARTITION BY [employees.depname] ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING@3 as s1]
+06)----------BoundedWindowAggExec: wdw=[sum(employees.salary) PARTITION BY [employees.depname] ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING: Field { "sum(employees.salary) PARTITION BY [employees.depname] ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING], mode=[Sorted]
+07)------------SortExec: TopK(fetch=7), expr=[depname@0 ASC NULLS LAST, empno@1 ASC NULLS LAST], preserve_partitioning=[true], sort_prefix=[depname@0 ASC NULLS LAST, empno@1 ASC NULLS LAST]
+08)--------------RepartitionExec: partitioning=Hash([depname@0], 4), input_partitions=1, maintains_sort_order=true
+09)----------------SortExec: TopK(fetch=7), expr=[depname@0 ASC NULLS LAST, empno@1 ASC NULLS LAST], preserve_partitioning=[true]
+10)------------------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100_with_dates.csv]]}, projection=[depname, empno, salary], file_type=csv, has_header=true
+
+# The same shape with the limit moved inside the subquery, so nothing above the
+# outer window hands the walk a limit at all. The merge's *own* fetch is still a
+# bound on everything below it, and starting the next chain from it is the only
+# thing that gets a fetch to the sort under the inner window: here the sort gets
+# `5 + 1` from a limit the rule never had in hand.
+#
+# This is not the previous case written twice. Replacing
+# `fetch.or_else(|| node.fetch())` with `fetch` leaves the previous case green and
+# turns this one red, because there `fetch` is `Some(6)` and here it is `None`.
+# The results differ too: the last row's `s2` has no following row to read, since
+# the `LIMIT 5` is applied before the outer window rather than after it.
+
+statement ok
+set datafusion.optimizer.enable_window_limits = false;
+
+query TIII
+SELECT depname, empno, s1,
+ sum(s1) OVER (ORDER BY depname, empno ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s2
+FROM (
+ SELECT depname, empno,
+ sum(salary) OVER (PARTITION BY depname ORDER BY empno
+ ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s1
+ FROM employees
+ ORDER BY depname, empno LIMIT 5
+);
+----
+a 102 5141431160 11022181173
+a 363 5880750013 10041486965
+a 829 4160736952 5060807172
+a 2555 900070220 5372396992
+a 2809 4472326772 4472326772
+
+statement ok
+set datafusion.optimizer.enable_window_limits = true;
+
+query TIII
+SELECT depname, empno, s1,
+ sum(s1) OVER (ORDER BY depname, empno ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s2
+FROM (
+ SELECT depname, empno,
+ sum(salary) OVER (PARTITION BY depname ORDER BY empno
+ ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s1
+ FROM employees
+ ORDER BY depname, empno LIMIT 5
+);
+----
+a 102 5141431160 11022181173
+a 363 5880750013 10041486965
+a 829 4160736952 5060807172
+a 2555 900070220 5372396992
+a 2809 4472326772 4472326772
+
+query TT
+EXPLAIN
+SELECT depname, empno, s1,
+ sum(s1) OVER (ORDER BY depname, empno ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s2
+FROM (
+ SELECT depname, empno,
+ sum(salary) OVER (PARTITION BY depname ORDER BY empno
+ ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING) AS s1
+ FROM employees
+ ORDER BY depname, empno LIMIT 5
+);
+----
+logical_plan
+01)Projection: employees.depname, employees.empno, s1, sum(s1) ORDER BY [employees.depname ASC NULLS LAST, employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING AS s2
+02)--WindowAggr: windowExpr=[[sum(s1) ORDER BY [employees.depname ASC NULLS LAST, employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING]]
+03)----Sort: employees.depname ASC NULLS LAST, employees.empno ASC NULLS LAST, fetch=5
+04)------Projection: employees.depname, employees.empno, sum(employees.salary) PARTITION BY [employees.depname] ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING AS s1
+05)--------WindowAggr: windowExpr=[[sum(employees.salary) PARTITION BY [employees.depname] ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING]]
+06)----------TableScan: employees projection=[depname, empno, salary]
+physical_plan
+01)ProjectionExec: expr=[depname@0 as depname, empno@1 as empno, s1@2 as s1, sum(s1) ORDER BY [employees.depname ASC NULLS LAST, employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING@3 as s2]
+02)--BoundedWindowAggExec: wdw=[sum(s1) ORDER BY [employees.depname ASC NULLS LAST, employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING: Field { "sum(s1) ORDER BY [employees.depname ASC NULLS LAST, employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING], mode=[Sorted]
+03)----SortPreservingMergeExec: [depname@0 ASC NULLS LAST, empno@1 ASC NULLS LAST], fetch=5
+04)------ProjectionExec: expr=[depname@0 as depname, empno@1 as empno, sum(employees.salary) PARTITION BY [employees.depname] ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING@3 as s1]
+05)--------BoundedWindowAggExec: wdw=[sum(employees.salary) PARTITION BY [employees.depname] ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING: Field { "sum(employees.salary) PARTITION BY [employees.depname] ORDER BY [employees.empno ASC NULLS LAST] ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING": nullable UInt64 }, frame: ROWS BETWEEN CURRENT ROW AND 1 FOLLOWING], mode=[Sorted]
+06)----------SortExec: TopK(fetch=6), expr=[depname@0 ASC NULLS LAST, empno@1 ASC NULLS LAST], preserve_partitioning=[true], sort_prefix=[depname@0 ASC NULLS LAST, empno@1 ASC NULLS LAST]
+07)------------RepartitionExec: partitioning=Hash([depname@0], 4), input_partitions=1, maintains_sort_order=true
+08)--------------SortExec: TopK(fetch=6), expr=[depname@0 ASC NULLS LAST, empno@1 ASC NULLS LAST], preserve_partitioning=[true]
+09)----------------DataSourceExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100_with_dates.csv]]}, projection=[depname, empno, salary], file_type=csv, has_header=true