summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2013-11-11 10:43:00 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2013-11-11 10:43:00 -0500
commit04e6ee40206fa61dc856bf2840ce6bb198d5200c (patch)
tree1b02114159fbb2e889fbaa370dc13d118d159ae4 /src/test
parent8e41c621a625f154e96f40ce688a036520cb59aa (diff)
downloadpostgresql-04e6ee40206fa61dc856bf2840ce6bb198d5200c.tar.gz
Re-allow duplicate aliases within aliased JOINs.
Although the SQL spec forbids duplicate table aliases, historically we've allowed queries like SELECT ... FROM tab1 x CROSS JOIN (tab2 x CROSS JOIN tab3 y) z on the grounds that the aliased join (z) hides the aliases within it, therefore there is no conflict between the two RTEs named "x". The LATERAL patch broke this, on the misguided basis that "x" could be ambiguous if tab3 were a LATERAL subquery. To avoid breaking existing queries, it's better to allow this situation and complain only if tab3 actually does contain an ambiguous reference. We need only remove the check that was throwing an error, because the column lookup code is already prepared to handle ambiguous references. Per bug #8444.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/regress/expected/join.out24
-rw-r--r--src/test/regress/sql/join.sql12
2 files changed, 36 insertions, 0 deletions
diff --git a/src/test/regress/expected/join.out b/src/test/regress/expected/join.out
index c94ac614af..7c3c9aced2 100644
--- a/src/test/regress/expected/join.out
+++ b/src/test/regress/expected/join.out
@@ -3092,6 +3092,24 @@ SELECT * FROM
(5 rows)
rollback;
+-- bug #8444: we've historically allowed duplicate aliases within aliased JOINs
+select * from
+ int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = f1; -- error
+ERROR: column reference "f1" is ambiguous
+LINE 2: ..._tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = f1;
+ ^
+select * from
+ int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = y.f1; -- error
+ERROR: invalid reference to FROM-clause entry for table "y"
+LINE 2: ...bl x join (int4_tbl x cross join int4_tbl y) j on q1 = y.f1;
+ ^
+HINT: There is an entry for table "y", but it cannot be referenced from this part of the query.
+select * from
+ int8_tbl x join (int4_tbl x cross join int4_tbl y(ff)) j on q1 = f1; -- ok
+ q1 | q2 | f1 | ff
+----+----+----+----
+(0 rows)
+
--
-- Test LATERAL
--
@@ -3946,6 +3964,12 @@ ERROR: invalid reference to FROM-clause entry for table "a"
LINE 1: ...m int4_tbl a full join lateral generate_series(0, a.f1) g on...
^
DETAIL: The combining JOIN type must be INNER or LEFT for a LATERAL reference.
+-- check we complain about ambiguous table references
+select * from
+ int8_tbl x cross join (int4_tbl x cross join lateral (select x.f1) ss);
+ERROR: table reference "x" is ambiguous
+LINE 2: ...cross join (int4_tbl x cross join lateral (select x.f1) ss);
+ ^
-- LATERAL can be used to put an aggregate into the FROM clause of its query
select 1 from tenk1 a, lateral (select max(a.unique1) from int4_tbl b) ss;
ERROR: aggregate functions are not allowed in FROM clause of their own query level
diff --git a/src/test/regress/sql/join.sql b/src/test/regress/sql/join.sql
index 351400f2da..07ad270863 100644
--- a/src/test/regress/sql/join.sql
+++ b/src/test/regress/sql/join.sql
@@ -890,6 +890,15 @@ SELECT * FROM
rollback;
+-- bug #8444: we've historically allowed duplicate aliases within aliased JOINs
+
+select * from
+ int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = f1; -- error
+select * from
+ int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = y.f1; -- error
+select * from
+ int8_tbl x join (int4_tbl x cross join int4_tbl y(ff)) j on q1 = f1; -- ok
+
--
-- Test LATERAL
--
@@ -1077,5 +1086,8 @@ select f1,g from int4_tbl a cross join (select a.f1 as g) ss;
-- SQL:2008 says the left table is in scope but illegal to access here
select f1,g from int4_tbl a right join lateral generate_series(0, a.f1) g on true;
select f1,g from int4_tbl a full join lateral generate_series(0, a.f1) g on true;
+-- check we complain about ambiguous table references
+select * from
+ int8_tbl x cross join (int4_tbl x cross join lateral (select x.f1) ss);
-- LATERAL can be used to put an aggregate into the FROM clause of its query
select 1 from tenk1 a, lateral (select max(a.unique1) from int4_tbl b) ss;