@@ -595,14 +595,17 @@ pub fn get_required_group_by_exprs_indices(
595595pub fn get_required_sort_exprs_indices (
596596 schema : & DFSchema ,
597597 sort_expr_names : & [ String ] ,
598- ) -> Option < Vec < usize > > {
598+ ) -> Vec < usize > {
599599 let dependencies = schema. functional_dependencies ( ) ;
600600 let field_names = schema. field_names ( ) ;
601601
602602 let mut known_field_indices = HashSet :: new ( ) ;
603603 let mut required_sort_expr_indices = Vec :: new ( ) ;
604604
605605 for ( sort_expr_idx, sort_expr_name) in sort_expr_names. iter ( ) . enumerate ( ) {
606+ // If the sort expression doesn't correspond to a known schema field
607+ // (e.g. a computed expression), we can't reason about it via functional
608+ // dependencies, so conservatively keep it.
606609 let Some ( field_idx) = field_names
607610 . iter ( )
608611 . position ( |field_name| field_name == sort_expr_name)
@@ -611,6 +614,10 @@ pub fn get_required_sort_exprs_indices(
611614 continue ;
612615 } ;
613616
617+ // A sort expression is removable if its value is functionally determined
618+ // by fields that already appear earlier in the sort order: if the earlier
619+ // fields are fixed, this one's value is fixed too, so it adds no ordering
620+ // information.
614621 let removable = dependencies. deps . iter ( ) . any ( |dependency| {
615622 dependency. target_indices . contains ( & field_idx)
616623 && dependency
@@ -627,7 +634,7 @@ pub fn get_required_sort_exprs_indices(
627634 required_sort_expr_indices. push ( sort_expr_idx) ;
628635 }
629636
630- Some ( required_sort_expr_indices)
637+ required_sort_expr_indices
631638}
632639
633640/// Updates entries inside the `entries` vector with their corresponding
0 commit comments