@@ -519,6 +519,28 @@ 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+ }
522544 LogicalPlan :: Sort { expr, input, .. } => {
523545 let ( names, _) = sort_to_column_names ( expr, input) ;
524546
@@ -606,26 +628,15 @@ fn extract_column_name(expr: &Expr) -> Option<String> {
606628 }
607629}
608630
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- }
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 ( ) ) ;
620636 }
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- }
637+ } ) ;
638+
639+ alias_to_column
629640}
630641
631642fn sort_to_column_names ( sort_exprs : & Vec < Expr > , input : & LogicalPlan ) -> ( Vec < String > , bool ) {
@@ -642,7 +653,7 @@ fn sort_to_column_names(sort_exprs: &Vec<Expr>, input: &LogicalPlan) -> (Vec<Str
642653 }
643654 match expr. as_ref ( ) {
644655 Expr :: Column ( c) => {
645- res. push ( get_original_name ( & c. name , input ) ) ;
656+ res. push ( c. name . clone ( ) ) ;
646657 }
647658 _ => {
648659 return ( Vec :: new ( ) , true ) ;
@@ -755,6 +766,39 @@ impl PlanRewriter for ChooseIndex<'_> {
755766
756767 fn enter_node ( & mut self , n : & LogicalPlan , context : & Self :: Context ) -> Option < Self :: Context > {
757768 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+ }
758802 LogicalPlan :: Limit { n, .. } => Some ( context. update_limit ( Some ( * n) ) ) ,
759803 LogicalPlan :: Skip { n, .. } => {
760804 if let Some ( limit) = context. limit {
0 commit comments