Skip to content

Commit 0d67b48

Browse files
committed
Do not double alias Exprs
1 parent dd0fd88 commit 0d67b48

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

datafusion/expr/src/expr.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,19 @@ impl Alias {
412412
name: name.into(),
413413
}
414414
}
415+
416+
/// Return true if this alias represents the unaliased `name`
417+
pub fn eq_name(&self, name: &str) -> bool {
418+
self.eq_qualified_name(None, name)
419+
}
420+
421+
pub fn eq_qualified_name(
422+
&self,
423+
relation: Option<&TableReference>,
424+
name: &str,
425+
) -> bool {
426+
self.relation.as_ref() == relation && self.name == name
427+
}
415428
}
416429

417430
/// Binary expression
@@ -1270,6 +1283,8 @@ impl Expr {
12701283

12711284
/// Ensure `expr` has the name as `original_name` by adding an
12721285
/// alias if necessary.
1286+
///
1287+
// TODO: remove? and deprecate??
12731288
pub fn alias_if_changed(self, original_name: String) -> Result<Expr> {
12741289
let new_name = self.name_for_alias()?;
12751290
if new_name == original_name {
@@ -1281,7 +1296,12 @@ impl Expr {
12811296

12821297
/// Return `self AS name` alias expression
12831298
pub fn alias(self, name: impl Into<String>) -> Expr {
1284-
Expr::Alias(Alias::new(self, None::<&str>, name.into()))
1299+
let name = name.into();
1300+
// don't re-alias if already aliased to the same name
1301+
if matches!(&self, Expr::Alias(alias) if alias.eq_name(&name)) {
1302+
return self;
1303+
}
1304+
Expr::Alias(Alias::new(self, None::<&str>, name))
12851305
}
12861306

12871307
/// Return `self AS name` alias expression with a specific qualifier
@@ -1290,7 +1310,14 @@ impl Expr {
12901310
relation: Option<impl Into<TableReference>>,
12911311
name: impl Into<String>,
12921312
) -> Expr {
1293-
Expr::Alias(Alias::new(self, relation, name.into()))
1313+
let relation = relation.map(|r| r.into());
1314+
let name = name.into();
1315+
// don't re-alias if already aliased to the same name
1316+
if matches!(&self, Expr::Alias(alias) if alias.eq_qualified_name(relation.as_ref(), &name))
1317+
{
1318+
return self;
1319+
}
1320+
Expr::Alias(Alias::new(self, relation, name))
12941321
}
12951322

12961323
/// Remove an alias from an expression if one exists.
@@ -2831,7 +2858,6 @@ pub fn physical_name(expr: &Expr) -> Result<String> {
28312858
_ => Ok(expr.schema_name().to_string()),
28322859
}
28332860
}
2834-
28352861
#[cfg(test)]
28362862
mod test {
28372863
use crate::expr_fn::col;

datafusion/sqllogictest/test_files/aggregate.slt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6640,3 +6640,16 @@ SELECT a, median(b), arrow_typeof(median(b)) FROM group_median_all_nulls GROUP B
66406640
----
66416641
group0 NULL Int32
66426642
group1 NULL Int32
6643+
6644+
# should not be a double alias
6645+
query TT
6646+
explain SELECT count(*) order by count(*);
6647+
----
6648+
logical_plan
6649+
01)Sort: count(*) ASC NULLS LAST
6650+
02)--Projection: count(Int64(1)) AS count(*)
6651+
03)----Aggregate: groupBy=[[]], aggr=[[count(Int64(1))]]
6652+
04)------EmptyRelation
6653+
physical_plan
6654+
01)ProjectionExec: expr=[1 as count(*)]
6655+
02)--PlaceholderRowExec

0 commit comments

Comments
 (0)