Skip to content

Commit 72be1cb

Browse files
committed
Improve unalias_nested
UPSTREAM NOTE: This was attempted to be fixed with apache#15008 but was closed This is the tracking issue on DataFusion: apache#14895
1 parent 8825151 commit 72be1cb

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

datafusion/expr/src/expr.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ impl Alias {
498498
self
499499
}
500500

501-
/// Return true if this alias represents the unaliased `name`
501+
/// Return true if this alias represents the unaliased `name`
502502
pub fn eq_name(&self, name: &str) -> bool {
503503
self.eq_qualified_name(None, name)
504504
}
@@ -1399,7 +1399,14 @@ impl Expr {
13991399

14001400
/// Return `self AS name` alias expression
14011401
pub fn alias(self, name: impl Into<String>) -> Expr {
1402-
Expr::Alias(Alias::new(self, None::<&str>, name.into()))
1402+
// Don't nest aliases
1403+
let Expr::Alias(mut alias) = self else {
1404+
return Expr::Alias(Alias::new(self, None::<&str>, name.into()));
1405+
};
1406+
1407+
alias.relation = None;
1408+
alias.name = name.into();
1409+
Expr::Alias(alias)
14031410
}
14041411

14051412
/// Return `self AS name` alias expression with metadata
@@ -1420,7 +1427,16 @@ impl Expr {
14201427
name: impl Into<String>,
14211428
metadata: Option<std::collections::HashMap<String, String>>,
14221429
) -> Expr {
1423-
Expr::Alias(Alias::new(self, None::<&str>, name.into()).with_metadata(metadata))
1430+
// Don't nest aliases
1431+
let Expr::Alias(mut alias) = self else {
1432+
return Expr::Alias(
1433+
Alias::new(self, None::<&str>, name.into()).with_metadata(metadata),
1434+
);
1435+
};
1436+
1437+
alias.relation = None;
1438+
alias.name = name.into();
1439+
Expr::Alias(alias.with_metadata(metadata))
14241440
}
14251441

14261442
/// Return `self AS name` alias expression with a specific qualifier
@@ -1458,7 +1474,14 @@ impl Expr {
14581474
name: impl Into<String>,
14591475
metadata: Option<std::collections::HashMap<String, String>>,
14601476
) -> Expr {
1461-
Expr::Alias(Alias::new(self, relation, name.into()).with_metadata(metadata))
1477+
// Don't nest aliases
1478+
let Expr::Alias(mut alias) = self else {
1479+
return Expr::Alias(Alias::new(self, relation, name).with_metadata(metadata));
1480+
};
1481+
1482+
alias.relation = relation.map(|r| r.into());
1483+
alias.name = name.into();
1484+
Expr::Alias(alias.with_metadata(metadata))
14621485
}
14631486

14641487
/// Remove an alias from an expression if one exists.

0 commit comments

Comments
 (0)