From: Tom Lane <tgl@sss.pgh.pa.us>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: dlw405@gmail.com <dlw405@gmail.com>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: BUG #14573: lateral joins, ambuiguity
Date: Thu, 02 Mar 2017 14:25:22 -0500
Message-ID: <18204.1488482722@sss.pgh.pa.us> (raw)
In-Reply-To: <CAKFQuwZ8YAzhf5C8zDW0AGwZXKOaeCLWx9+0micBDoHxC_ziFw@mail.gmail.com>
References: <20170302015958.25054.33376@wrigleys.postgresql.org>
<CAKFQuwaZkKzO0kgUhmusGULKTnUAbsuAyYD5XXkDpmr15dt6cQ@mail.gmail.com>
<CAKFQuwZ8YAzhf5C8zDW0AGwZXKOaeCLWx9+0micBDoHxC_ziFw@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Wed, Mar 1, 2017 at 8:22 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:
>> On Wednesday, March 1, 2017, <dlw405@gmail.com> wrote:
>>> The LATERAL JOIN has access to all previous columns in the join, but, it
>>> doesn't give an error when there are two columns of the same name.
>>> Instead, it silently selects the first column.
The above statement is demonstrably false, for example
regression=# create table t1 (f1 int, f2 int);
CREATE TABLE
regression=# select * from t1 a cross join t1 b cross join lateral (select f1) ss;
ERROR: column reference "f1" is ambiguous
LINE 1: ...from t1 a cross join t1 b cross join lateral (select f1) ss;
^
David has the correct analysis:
>> IIUC the preference exhibited is an explicit column present on the left
>> side of the join over the implicit relation named column within its own
>> query.
An unqualified name is first sought as a column reference, and only if
that fails altogether do we check whether it could be interpreted as a
whole-row reference to some table.
> I'd say its working as designed (or, at least, its not unique to LATERAL)
> - though no joy on finding where its end-user documented.
It's mentioned here:
https://www.postgresql.org/docs/9.5/static/rowtypes.html#ROWTYPES-USAGE
Note however that simple names are matched to column names before
table names, so this example works only because there is no column
named c in the query's tables.
and a bit further down
Even though .* does nothing in such cases, using it is good style,
since it makes clear that a composite value is intended. In
particular, the parser will consider c in c.* to refer to a table
name or alias, not to a column name, so that there is no
ambiguity; whereas without .*, it is not clear whether c means a
table name or a column name, and in fact the column-name
interpretation will be preferred if there is a column named c.
(Admittedly, that whole section is of pretty recent vintage; but the
behavior it describes is old.)
>>> We are confused on why there was not an ambiguity error thrown on the
>>> property 'owner' during the 2nd lateral join's SELECT statement. Should
>>> there be?
We can't do that because interpreting "foo" as a table reference is not
per SQL standard. If there's a single possible interpretation as a
column, whether it be plain or LATERAL or outer-query, we have to
resolve it that way without complaint, or we will fail to accept
standard-compliant queries.
The whole business of allowing a table name without ".*" decoration is
a PostQUEL-ism that we inherited from Berkeley and never removed; but
it's nonstandard and somewhat deprecated because of the ambiguity.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, david.g.johnston@gmail.com, dlw405@gmail.com
Subject: Re: BUG #14573: lateral joins, ambuiguity
In-Reply-To: <18204.1488482722@sss.pgh.pa.us>
References: <20170302015958.25054.33376@wrigleys.postgresql.org> <CAKFQuwaZkKzO0kgUhmusGULKTnUAbsuAyYD5XXkDpmr15dt6cQ@mail.gmail.com> <CAKFQuwZ8YAzhf5C8zDW0AGwZXKOaeCLWx9+0micBDoHxC_ziFw@mail.gmail.com> <18204.1488482722@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox