summaryrefslogtreecommitdiff
path: root/src/backend/parser
diff options
context:
space:
mode:
authorTom Lane2013-11-11 15:42:57 +0000
committerTom Lane2013-11-11 15:42:57 +0000
commit648bd05b13b3624e494ae2996c2d2e0241cefe87 (patch)
treefa49fe965c8c425d69c123a49e34bb55d2fcb2bd /src/backend/parser
parent705556a631c5908cd3caa0b973be13d994ff63e7 (diff)
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/backend/parser')
-rw-r--r--src/backend/parser/parse_clause.c5
-rw-r--r--src/backend/parser/parse_relation.c15
2 files changed, 16 insertions, 4 deletions
diff --git a/src/backend/parser/parse_clause.c b/src/backend/parser/parse_clause.c
index 1f6306a7d35..7a1261d0fd2 100644
--- a/src/backend/parser/parse_clause.c
+++ b/src/backend/parser/parse_clause.c
@@ -720,14 +720,15 @@ transformFromClauseItem(ParseState *pstate, Node *n,
* we always push them into the namespace, but mark them as not
* lateral_ok if the jointype is wrong.
*
+ * Notice that we don't require the merged namespace list to be
+ * conflict-free. See the comments for scanNameSpaceForRefname().
+ *
* NB: this coding relies on the fact that list_concat is not
* destructive to its second argument.
*/
lateral_ok = (j->jointype == JOIN_INNER || j->jointype == JOIN_LEFT);
setNamespaceLateralState(l_namespace, true, lateral_ok);
- checkNameSpaceConflicts(pstate, pstate->p_namespace, l_namespace);
-
sv_namespace_length = list_length(pstate->p_namespace);
pstate->p_namespace = list_concat(pstate->p_namespace, l_namespace);
diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c
index 5f469135cbf..0052d21ad62 100644
--- a/src/backend/parser/parse_relation.c
+++ b/src/backend/parser/parse_relation.c
@@ -130,6 +130,18 @@ refnameRangeTblEntry(ParseState *pstate,
* Search the query's table namespace for an RTE matching the
* given unqualified refname. Return the RTE if a unique match, or NULL
* if no match. Raise error if multiple matches.
+ *
+ * Note: it might seem that we shouldn't have to worry about the possibility
+ * of multiple matches; after all, the SQL standard disallows duplicate table
+ * aliases within a given SELECT level. Historically, however, Postgres has
+ * been laxer than that. For example, we allow
+ * 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". However,
+ * if tab3 is a LATERAL subquery, then from within the subquery both "x"es
+ * are visible. Rather than rejecting queries that used to work, we allow
+ * this situation, and complain only if there's actually an ambiguous
+ * reference to "x".
*/
static RangeTblEntry *
scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
@@ -174,8 +186,7 @@ scanNameSpaceForRefname(ParseState *pstate, const char *refname, int location)
/*
* Search the query's table namespace for a relation RTE matching the
* given relation OID. Return the RTE if a unique match, or NULL
- * if no match. Raise error if multiple matches (which shouldn't
- * happen if the namespace was checked correctly when it was created).
+ * if no match. Raise error if multiple matches.
*
* See the comments for refnameRangeTblEntry to understand why this
* acts the way it does.