@@ -28,7 +28,7 @@ use crate::Operator;
2828use crate :: { aggregate_function, ExprSchemable } ;
2929use arrow:: datatypes:: DataType ;
3030use datafusion_common:: tree_node:: { Transformed , TreeNode } ;
31- use datafusion_common:: { internal_err, DFSchema } ;
31+ use datafusion_common:: { internal_err, DFSchema , OwnedTableReference } ;
3232use datafusion_common:: { plan_err, Column , DataFusionError , Result , ScalarValue } ;
3333use std:: collections:: HashSet ;
3434use std:: fmt;
@@ -172,7 +172,7 @@ pub enum Expr {
172172 /// plan into physical plan.
173173 Wildcard ,
174174 /// Represents a reference to all available fields in a specific schema.
175- ///
175+ ///
176176 /// This expr has to be resolved to a list of columns before translating logical
177177 /// plan into physical plan.
178178 QualifiedWildcard { qualifier : String } ,
@@ -191,13 +191,20 @@ pub enum Expr {
191191#[ derive( Clone , PartialEq , Eq , Hash , Debug ) ]
192192pub struct Alias {
193193 pub expr : Box < Expr > ,
194+ pub relation : Option < OwnedTableReference > ,
194195 pub name : String ,
195196}
196197
197198impl Alias {
198- pub fn new ( expr : Expr , name : impl Into < String > ) -> Self {
199+ /// Create an alias with an optional schema/field qualifier.
200+ pub fn new (
201+ expr : Expr ,
202+ relation : Option < impl Into < OwnedTableReference > > ,
203+ name : impl Into < String > ,
204+ ) -> Self {
199205 Self {
200206 expr : Box :: new ( expr) ,
207+ relation : relation. map ( |r| r. into ( ) ) ,
201208 name : name. into ( ) ,
202209 }
203210 }
@@ -849,7 +856,27 @@ impl Expr {
849856 asc,
850857 nulls_first,
851858 } ) => Expr :: Sort ( Sort :: new ( Box :: new ( expr. alias ( name) ) , asc, nulls_first) ) ,
852- _ => Expr :: Alias ( Alias :: new ( self , name. into ( ) ) ) ,
859+ _ => Expr :: Alias ( Alias :: new ( self , None :: < & str > , name. into ( ) ) ) ,
860+ }
861+ }
862+
863+ /// Return `self AS name` alias expression with a specific qualifier
864+ pub fn alias_qualified (
865+ self ,
866+ relation : Option < impl Into < OwnedTableReference > > ,
867+ name : impl Into < String > ,
868+ ) -> Expr {
869+ match self {
870+ Expr :: Sort ( Sort {
871+ expr,
872+ asc,
873+ nulls_first,
874+ } ) => Expr :: Sort ( Sort :: new (
875+ Box :: new ( expr. alias_qualified ( relation, name) ) ,
876+ asc,
877+ nulls_first,
878+ ) ) ,
879+ _ => Expr :: Alias ( Alias :: new ( self , relation, name. into ( ) ) ) ,
853880 }
854881 }
855882
0 commit comments