@@ -438,28 +438,6 @@ impl PlanRewriter for CollectConstraints {
438438 c : & Self :: Context ,
439439 ) -> Result < LogicalPlan , DataFusionError > {
440440 match & n {
441- LogicalPlan :: Projection {
442- expr,
443- input,
444- schema,
445- } => {
446- let mut alias_to_column = HashMap :: new ( ) ;
447- expr. iter ( ) . for_each ( |e| {
448- if let Expr :: Alias ( box Expr :: Column ( c) , alias) = e {
449- alias_to_column. insert ( alias. clone ( ) , c. clone ( ) ) ;
450- }
451- } ) ;
452-
453- self . constraints . iter_mut ( ) . for_each ( |c| {
454- c. sort_on . iter_mut ( ) . for_each ( |sort_columns| {
455- sort_columns. sort_on . iter_mut ( ) . for_each ( |sort_column| {
456- if let Some ( column) = alias_to_column. get ( sort_column) {
457- * sort_column = column. name . clone ( ) ;
458- }
459- } ) ;
460- } ) ;
461- } ) ;
462- }
463441 LogicalPlan :: TableScan {
464442 projection,
465443 filters,
@@ -541,6 +519,28 @@ impl PlanRewriter for CollectConstraints {
541519 order_col_names : current_context. order_col_names . clone ( ) ,
542520 } )
543521 }
522+ LogicalPlan :: Projection { expr, .. } => {
523+ let alias_to_column = get_alias_to_column ( expr) ;
524+
525+ if let Some ( order_col_names) = & current_context. order_col_names {
526+ let names: Vec < String > = order_col_names
527+ . iter ( )
528+ . map ( |k| {
529+ alias_to_column
530+ . get ( k)
531+ . map_or_else ( || k. clone ( ) , |v| v. name . clone ( ) )
532+ } )
533+ . collect ( ) ;
534+
535+ if !names. is_empty ( ) {
536+ return Some ( current_context. update_order_col_names ( names) ) ;
537+ } else {
538+ return None ;
539+ }
540+ }
541+
542+ None
543+ }
544544 LogicalPlan :: Sort { expr, input, .. } => {
545545 let ( names, _) = sort_to_column_names ( expr, input) ;
546546
@@ -628,6 +628,17 @@ fn extract_column_name(expr: &Expr) -> Option<String> {
628628 }
629629}
630630
631+ fn get_alias_to_column ( expr : & Vec < Expr > ) -> HashMap < String , logical_plan:: Column > {
632+ let mut alias_to_column = HashMap :: new ( ) ;
633+ expr. iter ( ) . for_each ( |e| {
634+ if let Expr :: Alias ( box Expr :: Column ( c) , alias) = e {
635+ alias_to_column. insert ( alias. clone ( ) , c. clone ( ) ) ;
636+ }
637+ } ) ;
638+
639+ alias_to_column
640+ }
641+
631642///Try to get original column namse from if underlined projection or aggregates contains columns aliases
632643fn get_original_name ( may_be_alias : & String , input : & LogicalPlan ) -> String {
633644 fn get_name ( exprs : & Vec < Expr > , may_be_alias : & String ) -> String {
@@ -664,7 +675,8 @@ fn sort_to_column_names(sort_exprs: &Vec<Expr>, input: &LogicalPlan) -> (Vec<Str
664675 }
665676 match expr. as_ref ( ) {
666677 Expr :: Column ( c) => {
667- res. push ( get_original_name ( & c. name , input) ) ;
678+ // res.push(get_original_name(&c.name, input));
679+ res. push ( c. name . clone ( ) ) ;
668680 }
669681 _ => {
670682 return ( Vec :: new ( ) , true ) ;
@@ -778,30 +790,26 @@ impl PlanRewriter for ChooseIndex<'_> {
778790 fn enter_node ( & mut self , n : & LogicalPlan , context : & Self :: Context ) -> Option < Self :: Context > {
779791 match n {
780792 LogicalPlan :: Projection { expr, .. } => {
781- let mut alias_to_column = HashMap :: new ( ) ;
782- expr. iter ( ) . for_each ( |e| {
783- if let Expr :: Alias ( box Expr :: Column ( c) , alias) = e {
784- alias_to_column. insert ( alias. clone ( ) , c. clone ( ) ) ;
785- }
786- } ) ;
793+ let alias_to_column = get_alias_to_column ( expr) ;
787794
788- let names: Vec < String > = context
789- . sort
790- . clone ( )
791- . unwrap_or_default ( )
792- . iter ( )
793- . map ( |k| {
794- alias_to_column
795- . get ( k)
796- . map_or_else ( || k. clone ( ) , |v| v. name . clone ( ) )
797- } )
798- . collect ( ) ;
795+ if let Some ( sort) = & context. sort {
796+ let names: Vec < String > = sort
797+ . clone ( )
798+ . iter ( )
799+ . map ( |k| {
800+ alias_to_column
801+ . get ( k)
802+ . map_or_else ( || k. clone ( ) , |v| v. name . clone ( ) )
803+ } )
804+ . collect ( ) ;
799805
800- if !names. is_empty ( ) {
801- Some ( context. update_sort ( names, context. sort_is_asc ) )
802- } else {
803- None
806+ if !names. is_empty ( ) {
807+ return Some ( context. update_sort ( names, context. sort_is_asc ) ) ;
808+ } else {
809+ return None ;
810+ }
804811 }
812+ None
805813 }
806814 LogicalPlan :: Limit { n, .. } => Some ( context. update_limit ( Some ( * n) ) ) ,
807815 LogicalPlan :: Skip { n, .. } => {
0 commit comments