Skip to content

Commit e6684a3

Browse files
committed
fix(unparser): substitute a dropped alias only for unqualified columns
A qualified reference like `t.x` names `t`'s own column and cannot refer to a projection alias, so rewriting it whenever a dropped alias happens to share the name changes the query's meaning. Guard the substitution on `col.relation.is_none()`, matching the same check in sql/src/utils.rs.
1 parent cd5cc64 commit e6684a3

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

datafusion/sql/src/unparser/rewrite.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,17 @@ pub(super) fn rewrite_plan_for_sort_on_non_projected_fields(
321321
.clone()
322322
.transform_down(|e| {
323323
Ok(match &e {
324-
Expr::Column(col) => match dropped_aliases.get(col.name()) {
325-
Some(underlying) => Transformed::yes(underlying.clone()),
326-
None => Transformed::no(e),
327-
},
324+
// Only an unqualified column can name a projection
325+
// alias: `t.x` refers to `t`'s own column even when a
326+
// dropped alias happens to share the name.
327+
Expr::Column(col) if col.relation.is_none() => {
328+
match dropped_aliases.get(col.name()) {
329+
Some(underlying) => {
330+
Transformed::yes(underlying.clone())
331+
}
332+
None => Transformed::no(e),
333+
}
334+
}
328335
_ => Transformed::no(e),
329336
})
330337
})

0 commit comments

Comments
 (0)