Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 04e6ee4

Browse files
committed
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.
1 parent 8e41c62 commit 04e6ee4

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

src/backend/parser/parse_clause.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -721,14 +721,15 @@ transformFromClauseItem(ParseState *pstate, Node *n,
721721
* we always push them into the namespace, but mark them as not
722722
* lateral_ok if the jointype is wrong.
723723
*
724+
* Notice that we don't require the merged namespace list to be
725+
* conflict-free. See the comments for scanNameSpaceForRefname().
726+
*
724727
* NB: this coding relies on the fact that list_concat is not
725728
* destructive to its second argument.
726729
*/
727730
lateral_ok = (j->jointype == JOIN_INNER || j->jointype == JOIN_LEFT);
728731
setNamespaceLateralState(l_namespace, true, lateral_ok);
729732

730-
checkNameSpaceConflicts(pstate, pstate->p_namespace, l_namespace);
731-
732733
sv_namespace_length = list_length(pstate->p_namespace);
733734
pstate->p_namespace = list_concat(pstate->p_namespace, l_namespace);
734735

src/backend/parser/parse_relation.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ refnameRangeTblEntry(ParseState *pstate,
131131
* Search the query's table namespace for an RTE matching the
132132
* given unqualified refname. Return the RTE if a unique match, or NULL
133133
* if no match. Raise error if multiple matches.
134+
*
135+
* Note: it might seem that we shouldn't have to worry about the possibility
136+
* of multiple matches; after all, the SQL standard disallows duplicate table
137+
* aliases within a given SELECT level. Historically, however, Postgres has
138+
* been laxer than that. For example, we allow
139+
* SELECT ... FROM tab1 x CROSS JOIN (tab2 x CROSS JOIN tab3 y) z
140+
* on the grounds that the aliased join (z) hides the aliases within it,
141+
* therefore there is no conflict between the two RTEs named "x". However,
142+
* if tab3 is a LATERAL subquery, then from within the subquery both "x"es
143+
* are visible. Rather than rejecting queries that used to work, we allow
144+
* this situation, and complain only if there's actually an ambiguous
145+
* reference to "x".
134146
*/
135147
static RangeTblEntry *
136148
scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
@@ -175,8 +187,7 @@ scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
175187
/*
176188
* Search the query's table namespace for a relation RTE matching the
177189
* given relation OID. Return the RTE if a unique match, or NULL
178-
* if no match. Raise error if multiple matches (which shouldn't
179-
* happen if the namespace was checked correctly when it was created).
190+
* if no match. Raise error if multiple matches.
180191
*
181192
* See the comments for refnameRangeTblEntry to understand why this
182193
* acts the way it does.

src/test/regress/expected/join.out

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3092,6 +3092,24 @@ SELECT * FROM
30923092
(5 rows)
30933093

30943094
rollback;
3095+
-- bug #8444: we've historically allowed duplicate aliases within aliased JOINs
3096+
select * from
3097+
int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = f1; -- error
3098+
ERROR: column reference "f1" is ambiguous
3099+
LINE 2: ..._tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = f1;
3100+
^
3101+
select * from
3102+
int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = y.f1; -- error
3103+
ERROR: invalid reference to FROM-clause entry for table "y"
3104+
LINE 2: ...bl x join (int4_tbl x cross join int4_tbl y) j on q1 = y.f1;
3105+
^
3106+
HINT: There is an entry for table "y", but it cannot be referenced from this part of the query.
3107+
select * from
3108+
int8_tbl x join (int4_tbl x cross join int4_tbl y(ff)) j on q1 = f1; -- ok
3109+
q1 | q2 | f1 | ff
3110+
----+----+----+----
3111+
(0 rows)
3112+
30953113
--
30963114
-- Test LATERAL
30973115
--
@@ -3946,6 +3964,12 @@ ERROR: invalid reference to FROM-clause entry for table "a"
39463964
LINE 1: ...m int4_tbl a full join lateral generate_series(0, a.f1) g on...
39473965
^
39483966
DETAIL: The combining JOIN type must be INNER or LEFT for a LATERAL reference.
3967+
-- check we complain about ambiguous table references
3968+
select * from
3969+
int8_tbl x cross join (int4_tbl x cross join lateral (select x.f1) ss);
3970+
ERROR: table reference "x" is ambiguous
3971+
LINE 2: ...cross join (int4_tbl x cross join lateral (select x.f1) ss);
3972+
^
39493973
-- LATERAL can be used to put an aggregate into the FROM clause of its query
39503974
select 1 from tenk1 a, lateral (select max(a.unique1) from int4_tbl b) ss;
39513975
ERROR: aggregate functions are not allowed in FROM clause of their own query level

src/test/regress/sql/join.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,15 @@ SELECT * FROM
890890

891891
rollback;
892892

893+
-- bug #8444: we've historically allowed duplicate aliases within aliased JOINs
894+
895+
select * from
896+
int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = f1; -- error
897+
select * from
898+
int8_tbl x join (int4_tbl x cross join int4_tbl y) j on q1 = y.f1; -- error
899+
select * from
900+
int8_tbl x join (int4_tbl x cross join int4_tbl y(ff)) j on q1 = f1; -- ok
901+
893902
--
894903
-- Test LATERAL
895904
--
@@ -1077,5 +1086,8 @@ select f1,g from int4_tbl a cross join (select a.f1 as g) ss;
10771086
-- SQL:2008 says the left table is in scope but illegal to access here
10781087
select f1,g from int4_tbl a right join lateral generate_series(0, a.f1) g on true;
10791088
select f1,g from int4_tbl a full join lateral generate_series(0, a.f1) g on true;
1089+
-- check we complain about ambiguous table references
1090+
select * from
1091+
int8_tbl x cross join (int4_tbl x cross join lateral (select x.f1) ss);
10801092
-- LATERAL can be used to put an aggregate into the FROM clause of its query
10811093
select 1 from tenk1 a, lateral (select max(a.unique1) from int4_tbl b) ss;

0 commit comments

Comments
 (0)