Skip to content

Commit 41d9edc

Browse files
cool9850311claude
andcommitted
test: cover the nested shape Absolute could invent a limit in
Review asked about a nested window below a sort the rule had already given a fetch to. The test that landed earlier used the smallest shape that reaches the arm -- one window, no limit anywhere -- so the nested shape the review described had no test of its own. This adds it: the limit is spent on the outer sort, so the inner window reaches `grow_limit` with nothing in hand and the inner sort has no `LIMIT` above it at all. The arm now has three tests. The two that assert a full sort are not redundant, because the shapes differ in when they became reachable. Reverting only the `Absolute` fix turns both red. Also reverting `Transformed::yes` to `Transformed::complete`, which is what `main` does, leaves the nested test green and the single-window test red: `Stop` kept the walk out of everything below an applied sort, so the nested shape is reachable only once the walk continues, while the single-window shape needs no nesting and is reachable on `main` as it stands. That is the split behind the one-line "pre-existing" in the `Absolute` commit: it holds for the shape that commit tested, not for the nested one review raised. Co-Authored-By: Claude Opus 5 <[email protected]>
1 parent 01dd163 commit 41d9edc

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,22 @@ mod tests {
443443
})
444444
}
445445

446+
/// Build `GlobalLimitExec(100) → BoundedWindowAggExec(row_number) → SortExec
447+
/// → BoundedWindowAggExec(at_least_200) → SortExec → PlaceholderRowExec`.
448+
///
449+
/// The outer sort consumes the limit, so the inner window is reached with
450+
/// nothing in hand and the inner sort has no limit above it at all. Only this
451+
/// PR makes the shape reachable: before it, applying a fetch ended the walk,
452+
/// so nothing below the outer sort was ever visited.
453+
fn build_nested_absolute_plan() -> datafusion_common::Result<Arc<dyn ExecutionPlan>> {
454+
let input: Arc<dyn ExecutionPlan> = Arc::new(PlaceholderRowExec::new(schema()));
455+
let udwf = Arc::new(WindowUDF::from(AtLeast200(RowNumber::new())));
456+
let inner = sort_then_window(&udwf, "at_least_200", input)?;
457+
let outer = sort_then_window(&row_number_udwf(), "row_number", inner)?;
458+
459+
Ok(Arc::new(GlobalLimitExec::new(outer, 0, Some(100))))
460+
}
461+
446462
/// With no limit in the plan there is nothing to raise, so the sort must stay a
447463
/// full sort. Turning it into a TopK here would drop rows the query asked for.
448464
#[test]
@@ -469,4 +485,21 @@ mod tests {
469485
PlaceholderRowExec
470486
"#);
471487
}
488+
489+
/// The nested shape from review: the limit is spent on the outer sort, so the
490+
/// inner window's `Absolute(200)` has nothing to raise. It must not become a
491+
/// fetch on the inner sort, which no `LIMIT` sits above.
492+
#[test]
493+
fn absolute_limit_effect_does_not_invent_a_limit_below_an_applied_sort() {
494+
let plan = build_nested_absolute_plan().unwrap();
495+
let optimized = optimize(plan);
496+
assert_snapshot!(plan_str(optimized.as_ref()), @r#"
497+
GlobalLimitExec: skip=0, fetch=100
498+
BoundedWindowAggExec: wdw=[row_number: Field { "row_number": UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
499+
SortExec: TopK(fetch=100), expr=[a@0 ASC], preserve_partitioning=[true], sort_prefix=[a@0 ASC]
500+
BoundedWindowAggExec: wdw=[at_least_200: Field { "at_least_200": UInt64 }, frame: ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW], mode=[Sorted]
501+
SortExec: expr=[a@0 ASC], preserve_partitioning=[true]
502+
PlaceholderRowExec
503+
"#);
504+
}
472505
}

0 commit comments

Comments
 (0)