@@ -49,7 +49,10 @@ use datafusion_physical_plan::repartition::RepartitionExec;
4949use datafusion_physical_plan:: sorts:: sort:: SortExec ;
5050use datafusion_physical_plan:: sorts:: sort_preserving_merge:: SortPreservingMergeExec ;
5151use datafusion_physical_plan:: tree_node:: PlanContext ;
52- use datafusion_physical_plan:: { ExecutionPlan , ExecutionPlanProperties } ;
52+ use datafusion_physical_plan:: {
53+ ChildrenPropertiesMode , ExecutionPlan , ExecutionPlanProperties ,
54+ ReplaceChildrenOptions ,
55+ } ;
5356
5457/// "Data class" used by sort pushdown (now driven from `EnsureRequirements`)
5558/// to push down [`SortExec`] in the plan. In some cases the total
@@ -979,20 +982,16 @@ fn expr_source_side(
979982 }
980983}
981984
982- /// Handles the custom pushdown of parent-required sorting requirements down to
983- /// the child execution plans, considering whether the input order is maintained.
984- ///
985- /// # Arguments
986- ///
987- /// * `plan` - A reference to an `ExecutionPlan` for which the pushdown will be applied.
988- /// * `parent_required` - The sorting requirements expected by the parent node.
989- /// * `maintains_input_order` - A vector of booleans indicating whether each child
990- /// maintains the input order.
985+ /// Attempts sort pushdown through an operator without a specialized handler.
991986///
992- /// # Returns
987+ /// First, map the requested output sort columns to a child's columns by position,
988+ /// when the operator preserves that child's row positions. This is only a
989+ /// proposed mapping: the operator may change column positions or values.
993990///
994- /// Returns `Ok(Some(Vec<Option<LexRequirement>>))` if the sorting requirements can be
995- /// pushed down, `Ok(None)` if not. On error, returns a `Result::Err`.
991+ /// Then, rebuild the operator with the proposed sorted children and check its
992+ /// output ordering. Return child requirements only if the rebuilt operator
993+ /// satisfies the original output requirement; otherwise return `None` to keep
994+ /// the sort above the node.
996995fn handle_custom_pushdown (
997996 plan : & Arc < dyn ExecutionPlan > ,
998997 parent_required : OrderingRequirements ,
@@ -1007,8 +1006,8 @@ fn handle_custom_pushdown(
10071006
10081007 // Collect all unique column indices used in the parent-required sorting
10091008 // expression:
1010- let requirement = parent_required. into_single ( ) ;
1011- let all_indices: HashSet < usize > = requirement
1009+ let output_requirement = parent_required. into_single ( ) ;
1010+ let all_indices: HashSet < usize > = output_requirement
10121011 . iter ( )
10131012 . flat_map ( |order| {
10141013 collect_columns ( & order. expr )
@@ -1050,12 +1049,11 @@ fn handle_custom_pushdown(
10501049 . sum :: < usize > ( ) ;
10511050 // Transform the parent-required expression for the child schema by
10521051 // adjusting columns:
1053- let updated_parent_req = requirement
1054- . into_iter ( )
1052+ let child_requirement = output_requirement
1053+ . iter ( )
10551054 . map ( |req| {
10561055 let child_schema = plan_children[ maintained_child_idx] . schema ( ) ;
1057- let updated_columns = req
1058- . expr
1056+ let updated_columns = Arc :: clone ( & req. expr )
10591057 . transform_up ( |expr| {
10601058 if let Some ( col) = expr. downcast_ref :: < Column > ( ) {
10611059 let new_index = col. index ( ) - sub_offset;
@@ -1070,22 +1068,74 @@ fn handle_custom_pushdown(
10701068 . data ;
10711069 Ok ( PhysicalSortRequirement :: new ( updated_columns, req. options ) )
10721070 } )
1073- . collect :: < Result < Vec < _ > > > ( ) ?;
1071+ . collect :: < Result < Vec < _ > > > ( ) ;
1072+ let Ok ( child_requirement) = child_requirement else {
1073+ return Ok ( None ) ;
1074+ } ;
10741075
1075- // Prepare the result, populating with the updated requirements for children that maintain order
1076- let result = maintains_input_order
1076+ // Apply the proposed ordering to each child that preserves row positions.
1077+ let child_requirements = maintains_input_order
10771078 . iter ( )
10781079 . map ( |& maintains_order| {
10791080 if maintains_order {
1080- LexRequirement :: new ( updated_parent_req . clone ( ) )
1081+ LexRequirement :: new ( child_requirement . clone ( ) )
10811082 . map ( OrderingRequirements :: new)
10821083 } else {
10831084 None
10841085 }
10851086 } )
1086- . collect ( ) ;
1087+ . collect :: < Vec < _ > > ( ) ;
1088+
1089+ // Build a trial plan with sorted inputs. Replacing its children lets the
1090+ // custom operator recompute its output ordering through the existing API.
1091+ // This checks plan metadata during optimization.
1092+ let mut sorted_children = Vec :: with_capacity ( plan_children. len ( ) ) ;
1093+ for ( child, required) in plan_children. into_iter ( ) . zip ( & child_requirements) {
1094+ let sorted: Arc < dyn ExecutionPlan > = if let Some ( required) = required {
1095+ let ordering = LexOrdering :: from ( required. first ( ) . clone ( ) ) ;
1096+ let schema = child. schema ( ) ;
1097+ // Positional mapping can produce incompatible types, or invalid
1098+ // indices in another order-preserving child. Validate before
1099+ // SortExec::new, which panics if computing its properties fails.
1100+ if ordering. iter ( ) . any ( |sort| {
1101+ collect_columns ( & sort. expr )
1102+ . iter ( )
1103+ . any ( |column| column. index ( ) >= schema. fields ( ) . len ( ) )
1104+ || sort. expr . data_type ( & schema) . is_err ( )
1105+ } ) {
1106+ return Ok ( None ) ;
1107+ }
1108+ if child
1109+ . equivalence_properties ( )
1110+ . extract_common_sort_prefix ( ordering. clone ( ) )
1111+ . is_err ( )
1112+ {
1113+ return Ok ( None ) ;
1114+ }
1115+ Arc :: new (
1116+ SortExec :: new ( ordering, Arc :: clone ( child) )
1117+ . with_preserve_partitioning ( true ) ,
1118+ )
1119+ } else {
1120+ Arc :: clone ( child)
1121+ } ;
1122+ sorted_children. push ( sorted) ;
1123+ }
1124+ let Ok ( candidate_plan) = Arc :: clone ( plan) . replace_children (
1125+ sorted_children,
1126+ ReplaceChildrenOptions :: new ( ChildrenPropertiesMode :: Recompute ) ,
1127+ ) else {
1128+ return Ok ( None ) ;
1129+ } ;
1130+ if !candidate_plan
1131+ . equivalence_properties ( )
1132+ . ordering_satisfy_requirement ( output_requirement)
1133+ . unwrap_or ( false )
1134+ {
1135+ return Ok ( None ) ;
1136+ }
10871137
1088- Ok ( Some ( result ) )
1138+ Ok ( Some ( child_requirements ) )
10891139 } else {
10901140 Ok ( None )
10911141 }
@@ -1366,6 +1416,36 @@ mod tests {
13661416 LexRequirement :: new ( reqs) . unwrap ( )
13671417 }
13681418
1419+ // Exercise the custom-operator fallback directly with ProjectionExec's
1420+ // property derivation. The SQL tests cover dispatch through a custom plan.
1421+ #[ test]
1422+ fn custom_pushdown_accepts_renamed_columns ( ) -> Result < ( ) > {
1423+ let child = child_schema ( ) ;
1424+ let input = Arc :: new ( EmptyExec :: new ( Arc :: clone ( & child) ) ) ;
1425+ let plan: Arc < dyn ExecutionPlan > = Arc :: new ( ProjectionExec :: try_new (
1426+ [
1427+ ( col ( "a" , & child) ?, "first" . to_string ( ) ) ,
1428+ ( col ( "b" , & child) ?, "second" . to_string ( ) ) ,
1429+ ] ,
1430+ input,
1431+ ) ?) ;
1432+ let output = plan. schema ( ) ;
1433+ let required = OrderingRequirements :: new ( lex ( [
1434+ req ( "first" , & output, ASC ) ,
1435+ req ( "second" , & output, DESC ) ,
1436+ ] ) ) ;
1437+ let expected = OrderingRequirements :: new ( lex ( [
1438+ req ( "a" , & child, ASC ) ,
1439+ req ( "b" , & child, DESC ) ,
1440+ ] ) ) ;
1441+
1442+ assert_eq ! (
1443+ handle_custom_pushdown( & plan, required, & [ true ] ) ?,
1444+ Some ( vec![ Some ( expected) ] )
1445+ ) ;
1446+ Ok ( ( ) )
1447+ }
1448+
13691449 #[ test]
13701450 fn remap_single_hard_requirement_through_reordering_projection ( ) {
13711451 let projection = reordering_projection ( ) ;
0 commit comments