Skip to content

Apache DataFusion SQL Query Engine

Add relation to alias expr in schema display (#14311)

phisn1920771d8019
diff --git a/datafusion/expr/src/expr.rs b/datafusion/expr/src/expr.rs
index b8e495ee7a..7070761f63 100644
--- a/datafusion/expr/src/expr.rs
+++ b/datafusion/expr/src/expr.rs
@@ -2281,6 +2281,11 @@ impl Display for SchemaDisplay<'_> {
Ok(())
}
// Expr is not shown since it is aliased
+ Expr::Alias(Alias {
+ name,
+ relation: Some(relation),
+ ..
+ }) => write!(f, "{relation}.{name}"),
Expr::Alias(Alias { name, .. }) => write!(f, "{name}"),
Expr::Between(Between {
expr,
@@ -2769,10 +2774,10 @@ fn fmt_function(
/// The name of the column (field) that this `Expr` will produce in the physical plan.
/// The difference from [Expr::schema_name] is that top-level columns are unqualified.
pub fn physical_name(expr: &Expr) -> Result<String> {
- if let Expr::Column(col) = expr {
- Ok(col.name.clone())
- } else {
- Ok(expr.schema_name().to_string())
+ match expr {
+ Expr::Column(col) => Ok(col.name.clone()),
+ Expr::Alias(alias) => Ok(alias.name.clone()),
+ _ => Ok(expr.schema_name().to_string()),
}
}
@@ -3023,6 +3028,30 @@ mod test {
)
}
+ #[test]
+ fn test_schema_display_alias_with_relation() {
+ assert_eq!(
+ format!(
+ "{}",
+ SchemaDisplay(
+ &lit(1).alias_qualified("table_name".into(), "column_name")
+ )
+ ),
+ "table_name.column_name"
+ );
+ }
+
+ #[test]
+ fn test_schema_display_alias_without_relation() {
+ assert_eq!(
+ format!(
+ "{}",
+ SchemaDisplay(&lit(1).alias_qualified(None::<&str>, "column_name"))
+ ),
+ "column_name"
+ );
+ }
+
fn wildcard_options(
opt_ilike: Option<IlikeSelectItem>,
opt_exclude: Option<ExcludeSelectItem>,