@@ -5516,3 +5516,89 @@ DROP TABLE t1;
55165516
55175517statement ok
55185518DROP TABLE t2;
5519+
5520+ ####
5521+ # Ambiguous unqualified column references through a subquery alias wrapping JOINs.
5522+ # When two or more JOIN inputs share a column name the outer query must use
5523+ # the qualified form (alias.column); bare unqualified references must be rejected.
5524+ ####
5525+
5526+ statement ok
5527+ CREATE TABLE t_left(id INT, age INT, name VARCHAR) AS VALUES
5528+ (1, 10, 'alice'),
5529+ (2, 20, 'bob'),
5530+ (3, 30, 'carol');
5531+
5532+ statement ok
5533+ CREATE TABLE t_right(id INT, age INT, score INT) AS VALUES
5534+ (1, 10, 100),
5535+ (2, 20, 200),
5536+ (4, 40, 400);
5537+
5538+ statement ok
5539+ CREATE TABLE t_extra(id INT, dept VARCHAR) AS VALUES
5540+ (1, 'eng'),
5541+ (2, 'sales'),
5542+ (5, 'hr');
5543+
5544+ # 2-way join: qualified references to columns shared by both sides work fine
5545+ query III rowsort
5546+ SELECT sub.id, sub.age, sub.score
5547+ FROM (SELECT t_left.id, t_left.age, t_right.score
5548+ FROM t_left JOIN t_right ON t_left.id = t_right.id) AS sub;
5549+ ----
5550+ 1 10 100
5551+ 2 20 200
5552+
5553+ # 2-way join: unqualified "id" is ambiguous (both sides expose it)
5554+ query error DataFusion error: Schema error: Ambiguous reference to unqualified field id
5555+ SELECT sub.id FROM (SELECT * FROM t_left JOIN t_right ON t_left.id = t_right.id) AS sub WHERE id = 1;
5556+
5557+ # 2-way join: unqualified "age" is ambiguous (both sides expose it)
5558+ query error DataFusion error: Schema error: Ambiguous reference to unqualified field age
5559+ SELECT sub.age FROM (SELECT * FROM t_left JOIN t_right ON t_left.id = t_right.id) AS sub WHERE age > 5;
5560+
5561+ # 3-way join: qualified references still work when all three tables share "id"
5562+ query IIIT rowsort
5563+ SELECT sub.id, sub.age, sub.score, sub.dept
5564+ FROM (SELECT t_left.id, t_left.age, t_right.score, t_extra.dept
5565+ FROM t_left
5566+ JOIN t_right ON t_left.id = t_right.id
5567+ JOIN t_extra ON t_left.id = t_extra.id) AS sub;
5568+ ----
5569+ 1 10 100 eng
5570+ 2 20 200 sales
5571+
5572+ # 3-way join: unqualified "id" is ambiguous (present in all three tables)
5573+ query error DataFusion error: Schema error: Ambiguous reference to unqualified field id
5574+ SELECT sub.id FROM (SELECT * FROM t_left
5575+ JOIN t_right ON t_left.id = t_right.id
5576+ JOIN t_extra ON t_left.id = t_extra.id) AS sub
5577+ WHERE id = 1;
5578+
5579+ # 3-way join: unqualified "age" is ambiguous (shared by t_left and t_right)
5580+ query error DataFusion error: Schema error: Ambiguous reference to unqualified field age
5581+ SELECT sub.age FROM (SELECT * FROM t_left
5582+ JOIN t_right ON t_left.id = t_right.id
5583+ JOIN t_extra ON t_left.id = t_extra.id) AS sub
5584+ WHERE age > 5;
5585+
5586+ # 3-way join: unambiguous columns (unique to one table) need no qualifier
5587+ query IT rowsort
5588+ SELECT sub.score, sub.dept
5589+ FROM (SELECT t_left.id, t_left.age, t_right.score, t_extra.dept
5590+ FROM t_left
5591+ JOIN t_right ON t_left.id = t_right.id
5592+ JOIN t_extra ON t_left.id = t_extra.id) AS sub;
5593+ ----
5594+ 100 eng
5595+ 200 sales
5596+
5597+ statement ok
5598+ DROP TABLE t_left;
5599+
5600+ statement ok
5601+ DROP TABLE t_right;
5602+
5603+ statement ok
5604+ DROP TABLE t_extra;
0 commit comments