@@ -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