Skip to content

Commit 5ab14b2

Browse files
committed
fix order by process not copy ambiguous names
1 parent 5abb8cb commit 5ab14b2

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

datafusion/common/src/dfschema.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,11 @@ impl DFSchema {
380380
let finished_with_metadata = finished.with_metadata(metadata);
381381
self.inner = finished_with_metadata.into();
382382
self.field_qualifiers.extend(qualifiers);
383+
// Propagate ambiguous names from the other schema so that names marked
384+
// as ambiguous (e.g. by a JOIN) are not silently dropped when schemas
385+
// are merged for ORDER BY / HAVING resolution.
386+
self.ambiguous_names
387+
.extend(other_schema.ambiguous_names.iter().cloned());
383388
}
384389

385390
/// Get a list of fields for this schema

datafusion/sql/tests/sql_integration.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3938,6 +3938,19 @@ fn order_by_ambiguous_name() {
39383938
);
39393939
}
39403940

3941+
#[test]
3942+
fn order_by_ambiguous_name_via_subquery() {
3943+
// `age` is not in the SELECT list; ORDER BY falls back to the FROM schema,
3944+
// which is a subquery over a JOIN — `age` must still be flagged ambiguous.
3945+
let sql = "SELECT id FROM (SELECT * FROM person a JOIN person b USING (id)) sub ORDER BY age";
3946+
let err = logical_plan(sql).unwrap_err().strip_backtrace();
3947+
3948+
assert_snapshot!(
3949+
err,
3950+
@"Schema error: Ambiguous reference to unqualified field age"
3951+
);
3952+
}
3953+
39413954
#[test]
39423955
fn group_by_ambiguous_name() {
39433956
let sql = "select max(id) from person a join person b using (id) group by age";

0 commit comments

Comments
 (0)