@@ -391,11 +391,34 @@ impl Unnest {
391391}
392392
393393/// Alias expression
394- #[ derive( Clone , PartialEq , Eq , PartialOrd , Hash , Debug ) ]
394+ #[ derive( Clone , PartialEq , Eq , Debug ) ]
395395pub struct Alias {
396396 pub expr : Box < Expr > ,
397397 pub relation : Option < TableReference > ,
398398 pub name : String ,
399+ pub metadata : Option < std:: collections:: HashMap < String , String > > ,
400+ }
401+
402+ impl Hash for Alias {
403+ fn hash < H : Hasher > ( & self , state : & mut H ) {
404+ self . expr . hash ( state) ;
405+ self . relation . hash ( state) ;
406+ self . name . hash ( state) ;
407+ }
408+ }
409+
410+ impl PartialOrd for Alias {
411+ fn partial_cmp ( & self , other : & Self ) -> Option < std:: cmp:: Ordering > {
412+ let cmp = self . expr . partial_cmp ( & other. expr ) ;
413+ let Some ( std:: cmp:: Ordering :: Equal ) = cmp else {
414+ return cmp;
415+ } ;
416+ let cmp = self . relation . partial_cmp ( & other. relation ) ;
417+ let Some ( std:: cmp:: Ordering :: Equal ) = cmp else {
418+ return cmp;
419+ } ;
420+ self . name . partial_cmp ( & other. name )
421+ }
399422}
400423
401424impl Alias {
@@ -409,8 +432,17 @@ impl Alias {
409432 expr : Box :: new ( expr) ,
410433 relation : relation. map ( |r| r. into ( ) ) ,
411434 name : name. into ( ) ,
435+ metadata : None ,
412436 }
413437 }
438+
439+ pub fn with_metadata (
440+ mut self ,
441+ metadata : Option < std:: collections:: HashMap < String , String > > ,
442+ ) -> Self {
443+ self . metadata = metadata;
444+ self
445+ }
414446}
415447
416448/// Binary expression
@@ -1278,6 +1310,27 @@ impl Expr {
12781310 Expr :: Alias ( Alias :: new ( self , None :: < & str > , name. into ( ) ) )
12791311 }
12801312
1313+ /// Return `self AS name` alias expression with metadata
1314+ ///
1315+ /// The metadata will be attached to the Arrow Schema field when the expression
1316+ /// is converted to a field via `Expr.to_field()`.
1317+ ///
1318+ /// # Example
1319+ /// ```
1320+ /// # use datafusion_expr::col;
1321+ /// use std::collections::HashMap;
1322+ /// let metadata = HashMap::from([("key".to_string(), "value".to_string())]);
1323+ /// let expr = col("foo").alias_with_metadata("bar", Some(metadata));
1324+ /// ```
1325+ ///
1326+ pub fn alias_with_metadata (
1327+ self ,
1328+ name : impl Into < String > ,
1329+ metadata : Option < std:: collections:: HashMap < String , String > > ,
1330+ ) -> Expr {
1331+ Expr :: Alias ( Alias :: new ( self , None :: < & str > , name. into ( ) ) . with_metadata ( metadata) )
1332+ }
1333+
12811334 /// Return `self AS name` alias expression with a specific qualifier
12821335 pub fn alias_qualified (
12831336 self ,
@@ -1287,6 +1340,28 @@ impl Expr {
12871340 Expr :: Alias ( Alias :: new ( self , relation, name. into ( ) ) )
12881341 }
12891342
1343+ /// Return `self AS name` alias expression with a specific qualifier and metadata
1344+ ///
1345+ /// The metadata will be attached to the Arrow Schema field when the expression
1346+ /// is converted to a field via `Expr.to_field()`.
1347+ ///
1348+ /// # Example
1349+ /// ```
1350+ /// # use datafusion_expr::col;
1351+ /// use std::collections::HashMap;
1352+ /// let metadata = HashMap::from([("key".to_string(), "value".to_string())]);
1353+ /// let expr = col("foo").alias_qualified_with_metadata(Some("tbl"), "bar", Some(metadata));
1354+ /// ```
1355+ ///
1356+ pub fn alias_qualified_with_metadata (
1357+ self ,
1358+ relation : Option < impl Into < TableReference > > ,
1359+ name : impl Into < String > ,
1360+ metadata : Option < std:: collections:: HashMap < String , String > > ,
1361+ ) -> Expr {
1362+ Expr :: Alias ( Alias :: new ( self , relation, name. into ( ) ) . with_metadata ( metadata) )
1363+ }
1364+
12901365 /// Remove an alias from an expression if one exists.
12911366 ///
12921367 /// If the expression is not an alias, the expression is returned unchanged.
@@ -1738,11 +1813,13 @@ impl NormalizeEq for Expr {
17381813 expr : self_expr,
17391814 relation : self_relation,
17401815 name : self_name,
1816+ ..
17411817 } ) ,
17421818 Expr :: Alias ( Alias {
17431819 expr : other_expr,
17441820 relation : other_relation,
17451821 name : other_name,
1822+ ..
17461823 } ) ,
17471824 ) => {
17481825 self_name == other_name
@@ -2088,6 +2165,7 @@ impl HashNode for Expr {
20882165 expr : _expr,
20892166 relation,
20902167 name,
2168+ ..
20912169 } ) => {
20922170 relation. hash ( state) ;
20932171 name. hash ( state) ;
0 commit comments