@@ -27,8 +27,6 @@ use datafusion_common::config::ConfigOptions;
2727use datafusion_common:: error:: Result ;
2828use datafusion_common:: tree_node:: { Transformed , TreeNodeRecursion } ;
2929use datafusion_common:: utils:: combine_limit;
30- use datafusion_datasource:: file_scan_config:: { FileScanConfig , FileScanConfigBuilder } ;
31- use datafusion_datasource:: source:: DataSourceExec ;
3230use datafusion_physical_plan:: coalesce_partitions:: CoalescePartitionsExec ;
3331use datafusion_physical_plan:: limit:: { GlobalLimitExec , LocalLimitExec } ;
3432use datafusion_physical_plan:: sorts:: sort_preserving_merge:: SortPreservingMergeExec ;
@@ -52,7 +50,7 @@ pub struct GlobalRequirements {
5250 fetch : Option < usize > ,
5351 skip : usize ,
5452 satisfied : bool ,
55- order_sensitive : bool ,
53+ preserve_order : bool ,
5654}
5755
5856impl LimitPushdown {
@@ -72,7 +70,7 @@ impl PhysicalOptimizerRule for LimitPushdown {
7270 fetch : None ,
7371 skip : 0 ,
7472 satisfied : false ,
75- order_sensitive : false ,
73+ preserve_order : false ,
7674 } ;
7775 pushdown_limits ( plan, global_state)
7876 }
@@ -116,7 +114,7 @@ impl LimitExec {
116114 }
117115 }
118116
119- fn order_sensitive ( & self ) -> bool {
117+ fn preserve_order ( & self ) -> bool {
120118 match self {
121119 Self :: Global ( global) => global. required_ordering ( ) . is_some ( ) ,
122120 Self :: Local ( local) => local. required_ordering ( ) . is_some ( ) ,
@@ -156,7 +154,7 @@ pub fn pushdown_limit_helper(
156154 ) ;
157155 global_state. skip = skip;
158156 global_state. fetch = fetch;
159- global_state. order_sensitive = limit_exec. order_sensitive ( ) ;
157+ global_state. preserve_order = limit_exec. preserve_order ( ) ;
160158
161159 // Now the global state has the most recent information, we can remove
162160 // the `LimitExec` plan. We will decide later if we should add it again
@@ -253,21 +251,19 @@ pub fn pushdown_limit_helper(
253251 let maybe_fetchable = pushdown_plan. with_fetch ( skip_and_fetch) ;
254252 if global_state. satisfied {
255253 if let Some ( plan_with_fetch) = maybe_fetchable {
256- let plan_with_preserve_order = ensure_preserve_order_if_needed (
257- plan_with_fetch,
258- global_state. order_sensitive ,
259- ) ;
254+ let plan_with_preserve_order = plan_with_fetch
255+ . with_preserve_order ( global_state. preserve_order )
256+ . unwrap_or ( plan_with_fetch) ;
260257 Ok ( ( Transformed :: yes ( plan_with_preserve_order) , global_state) )
261258 } else {
262259 Ok ( ( Transformed :: no ( pushdown_plan) , global_state) )
263260 }
264261 } else {
265262 global_state. satisfied = true ;
266263 pushdown_plan = if let Some ( plan_with_fetch) = maybe_fetchable {
267- let plan_with_preserve_order = ensure_preserve_order_if_needed (
268- plan_with_fetch,
269- global_state. order_sensitive ,
270- ) ;
264+ let plan_with_preserve_order = plan_with_fetch
265+ . with_preserve_order ( global_state. preserve_order )
266+ . unwrap_or ( plan_with_fetch) ;
271267
272268 if global_skip > 0 {
273269 add_global_limit (
@@ -362,37 +358,4 @@ fn add_global_limit(
362358 Arc :: new ( GlobalLimitExec :: new ( pushdown_plan, skip, fetch) ) as _
363359}
364360
365- /// Helper function to handle DataSourceExec preserve_order setting
366- fn ensure_preserve_order_if_needed (
367- plan : Arc < dyn ExecutionPlan > ,
368- order_sensitive : bool ,
369- ) -> Arc < dyn ExecutionPlan > {
370- if !order_sensitive {
371- return plan;
372- }
373-
374- let Some ( data_source_exec) = plan. as_any ( ) . downcast_ref :: < DataSourceExec > ( ) else {
375- return plan;
376- } ;
377-
378- let Some ( file_scan_config) = data_source_exec
379- . data_source ( )
380- . as_any ( )
381- . downcast_ref :: < FileScanConfig > ( )
382- else {
383- return plan;
384- } ;
385-
386- if file_scan_config. preserve_order {
387- return plan;
388- }
389-
390- let new_config = FileScanConfigBuilder :: from ( file_scan_config. clone ( ) )
391- . with_preserve_order ( true )
392- . build ( ) ;
393-
394- let new_data_source_exec = DataSourceExec :: new ( Arc :: new ( new_config) ) ;
395- Arc :: new ( new_data_source_exec) as Arc < dyn ExecutionPlan >
396- }
397-
398361// See tests in datafusion/core/tests/physical_optimizer
0 commit comments