@@ -519,28 +519,6 @@ impl PlanRewriter for CollectConstraints {
519519 order_col_names : current_context. order_col_names . clone ( ) ,
520520 } )
521521 }
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- }
544522 LogicalPlan :: Sort { expr, input, .. } => {
545523 let ( names, _) = sort_to_column_names ( expr, input) ;
546524
@@ -628,15 +606,26 @@ fn extract_column_name(expr: &Expr) -> Option<String> {
628606 }
629607}
630608
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 ( ) ) ;
609+ ///Try to get original column namse from if underlined projection or aggregates contains columns aliases
610+ fn get_original_name ( may_be_alias : & String , input : & LogicalPlan ) -> String {
611+ fn get_name ( exprs : & Vec < Expr > , may_be_alias : & String ) -> String {
612+ let expr = exprs. iter ( ) . find ( |& expr| match expr {
613+ Expr :: Alias ( _, name) => name == may_be_alias,
614+ _ => false ,
615+ } ) ;
616+ if let Some ( expr) = expr {
617+ if let Some ( original_name) = extract_column_name ( expr) {
618+ return original_name;
619+ }
636620 }
637- } ) ;
638-
639- alias_to_column
621+ may_be_alias. clone ( )
622+ }
623+ match input {
624+ LogicalPlan :: Projection { expr, .. } => get_name ( expr, may_be_alias) ,
625+ LogicalPlan :: Filter { input, .. } => get_original_name ( may_be_alias, input) ,
626+ LogicalPlan :: Aggregate { group_expr, .. } => get_name ( group_expr, may_be_alias) ,
627+ _ => may_be_alias. clone ( ) ,
628+ }
640629}
641630
642631fn sort_to_column_names ( sort_exprs : & Vec < Expr > , input : & LogicalPlan ) -> ( Vec < String > , bool ) {
@@ -653,7 +642,7 @@ fn sort_to_column_names(sort_exprs: &Vec<Expr>, input: &LogicalPlan) -> (Vec<Str
653642 }
654643 match expr. as_ref ( ) {
655644 Expr :: Column ( c) => {
656- res. push ( c. name . clone ( ) ) ;
645+ res. push ( get_original_name ( & c. name , input ) ) ;
657646 }
658647 _ => {
659648 return ( Vec :: new ( ) , true ) ;
@@ -766,39 +755,6 @@ impl PlanRewriter for ChooseIndex<'_> {
766755
767756 fn enter_node ( & mut self , n : & LogicalPlan , context : & Self :: Context ) -> Option < Self :: Context > {
768757 match n {
769- LogicalPlan :: Projection { expr, .. } => {
770- let alias_to_column = get_alias_to_column ( expr) ;
771-
772- let new_single_value_filtered_cols = context
773- . single_value_filtered_cols
774- . iter ( )
775- . map ( |name| {
776- alias_to_column
777- . get ( name)
778- . map_or_else ( || name. clone ( ) , |col| col. name . clone ( ) )
779- } )
780- . collect ( ) ;
781-
782- let mut new_context =
783- context. update_single_value_filtered_cols ( new_single_value_filtered_cols) ;
784-
785- if let Some ( sort) = & new_context. sort {
786- let names: Vec < String > = sort
787- . iter ( )
788- . map ( |k| {
789- alias_to_column
790- . get ( k)
791- . map_or_else ( || k. clone ( ) , |col| col. name . clone ( ) )
792- } )
793- . collect ( ) ;
794-
795- if !names. is_empty ( ) {
796- new_context = new_context. update_sort ( names, context. sort_is_asc ) ;
797- }
798- }
799-
800- Some ( new_context)
801- }
802758 LogicalPlan :: Limit { n, .. } => Some ( context. update_limit ( Some ( * n) ) ) ,
803759 LogicalPlan :: Skip { n, .. } => {
804760 if let Some ( limit) = context. limit {
0 commit comments