Skip to content

Commit 8104f78

Browse files
committed
Update aggregate limit optimizer tests
1 parent d611742 commit 8104f78

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

datafusion/optimizer/src/eliminate_limit.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ mod tests {
210210
Sort: test.a ASC NULLS LAST, fetch=3
211211
Limit: skip=0, fetch=2
212212
Aggregate: groupBy=[[test.a]], aggr=[[sum(test.b)]]
213-
TableScan: test
213+
RightSemi Join: test.a = test.a
214+
Limit: skip=0, fetch=2
215+
Aggregate: groupBy=[[test.a]], aggr=[[]]
216+
TableScan: test
217+
TableScan: test
214218
"
215219
)
216220
}

datafusion/optimizer/src/push_down_limit.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -666,21 +666,25 @@ mod test {
666666
}
667667

668668
#[test]
669-
fn limit_doesnt_push_down_aggregation() -> Result<()> {
669+
fn limit_prefilters_aggregation() -> Result<()> {
670670
let table_scan = test_table_scan()?;
671671

672672
let plan = LogicalPlanBuilder::from(table_scan)
673673
.aggregate(vec![col("a")], vec![max(col("b"))])?
674674
.limit(0, Some(1000))?
675675
.build()?;
676676

677-
// Limit should *not* push down aggregate node
677+
// Limit preselects group keys before running the aggregate
678678
assert_optimized_plan_equal!(
679679
plan,
680680
@r"
681681
Limit: skip=0, fetch=1000
682682
Aggregate: groupBy=[[test.a]], aggr=[[max(test.b)]]
683-
TableScan: test
683+
RightSemi Join: test.a = test.a
684+
Limit: skip=0, fetch=1000
685+
Aggregate: groupBy=[[test.a]], aggr=[[]]
686+
TableScan: test
687+
TableScan: test
684688
"
685689
)
686690
}
@@ -758,14 +762,20 @@ mod test {
758762
.limit(0, Some(10))?
759763
.build()?;
760764

761-
// Limit should use deeper LIMIT 1000, but Limit 10 shouldn't push down aggregation
765+
// Limit should use deeper LIMIT 1000 and preselect group keys for the
766+
// aggregate using the outer LIMIT 10.
762767
assert_optimized_plan_equal!(
763768
plan,
764769
@r"
765770
Limit: skip=0, fetch=10
766771
Aggregate: groupBy=[[test.a]], aggr=[[max(test.b)]]
767-
Limit: skip=0, fetch=1000
768-
TableScan: test, fetch=1000
772+
RightSemi Join: test.a = test.a
773+
Limit: skip=0, fetch=10
774+
Aggregate: groupBy=[[test.a]], aggr=[[]]
775+
Limit: skip=0, fetch=1000
776+
TableScan: test, fetch=1000
777+
Limit: skip=0, fetch=1000
778+
TableScan: test, fetch=1000
769779
"
770780
)
771781
}
@@ -869,21 +879,25 @@ mod test {
869879
}
870880

871881
#[test]
872-
fn limit_doesnt_push_down_with_offset_aggregation() -> Result<()> {
882+
fn limit_with_offset_prefilters_aggregation() -> Result<()> {
873883
let table_scan = test_table_scan()?;
874884

875885
let plan = LogicalPlanBuilder::from(table_scan)
876886
.aggregate(vec![col("a")], vec![max(col("b"))])?
877887
.limit(10, Some(1000))?
878888
.build()?;
879889

880-
// Limit should *not* push down aggregate node
890+
// Limit preselects enough group keys to satisfy offset and fetch
881891
assert_optimized_plan_equal!(
882892
plan,
883893
@r"
884894
Limit: skip=10, fetch=1000
885895
Aggregate: groupBy=[[test.a]], aggr=[[max(test.b)]]
886-
TableScan: test
896+
RightSemi Join: test.a = test.a
897+
Limit: skip=0, fetch=1010
898+
Aggregate: groupBy=[[test.a]], aggr=[[]]
899+
TableScan: test
900+
TableScan: test
887901
"
888902
)
889903
}

0 commit comments

Comments
 (0)