@@ -3,8 +3,11 @@ use crate::queryplanner::planning::WorkerExec;
33use crate :: queryplanner:: query_executor:: ClusterSendExec ;
44use crate :: queryplanner:: tail_limit:: TailLimitExec ;
55use crate :: queryplanner:: topk:: AggregateTopKExec ;
6+ use datafusion:: config:: ConfigOptions ;
67use datafusion:: error:: DataFusionError ;
78use datafusion:: physical_expr:: LexOrdering ;
9+ use datafusion:: physical_optimizer:: limit_pushdown:: LimitPushdown ;
10+ use datafusion:: physical_optimizer:: PhysicalOptimizerRule as _;
811use datafusion:: physical_plan:: aggregates:: { AggregateExec , AggregateMode } ;
912use datafusion:: physical_plan:: coalesce_partitions:: CoalescePartitionsExec ;
1013use datafusion:: physical_plan:: limit:: GlobalLimitExec ;
@@ -192,10 +195,13 @@ pub fn ensure_partition_merge_with_acceptable_parent(
192195 }
193196}
194197
195- ///Add `GlobalLimitExec` behind worker node if this node has `limit` property set
196- ///Should be executed after all optimizations which can move `Worker` node or change it input
198+ /// Add `GlobalLimitExec` behind worker node if this node has `limit` property set and applies DF
199+ /// `LimitPushdown` optimizer. Should be executed after all optimizations which can move `Worker`
200+ /// node or change its input. `config` is ignored -- we pass it to DF's `LimitPushdown` optimizer,
201+ /// which also ignores it (as of DF 46.0.1).
197202pub fn add_limit_to_workers (
198203 p : Arc < dyn ExecutionPlan > ,
204+ config : & ConfigOptions ,
199205) -> Result < Arc < dyn ExecutionPlan > , DataFusionError > {
200206 let limit_and_reverse;
201207 let input;
@@ -217,6 +223,7 @@ pub fn add_limit_to_workers(
217223 p. with_new_children ( vec ! [ limit] )
218224 } else {
219225 let limit = Arc :: new ( GlobalLimitExec :: new ( input. clone ( ) , 0 , Some ( limit) ) ) ;
220- p. with_new_children ( vec ! [ limit] )
226+ let limit_optimized = LimitPushdown :: new ( ) . optimize ( limit, config) ?;
227+ p. with_new_children ( vec ! [ limit_optimized] )
221228 }
222229}
0 commit comments