Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cjWMd-0006nv-UN for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Mar 2017 19:26:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cjWMd-0002bT-DY for pgsql-bugs@arkaria.postgresql.org; Thu, 02 Mar 2017 19:26:11 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cjWLw-0001Oh-7i for pgsql-bugs@postgresql.org; Thu, 02 Mar 2017 19:25:28 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cjWLt-0003QR-CL for pgsql-bugs@postgresql.org; Thu, 02 Mar 2017 19:25:27 +0000 Received: from pro.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v22JPMo3023123; Thu, 2 Mar 2017 14:25:23 -0500 From: Tom Lane To: "David G. Johnston" cc: "dlw405@gmail.com" , "pgsql-bugs@postgresql.org" Subject: Re: BUG #14573: lateral joins, ambuiguity In-reply-to: References: <20170302015958.25054.33376@wrigleys.postgresql.org> Comments: In-reply-to "David G. Johnston" message dated "Wed, 01 Mar 2017 20:47:32 -0700" Date: Thu, 02 Mar 2017 14:25:22 -0500 Message-ID: <18204.1488482722@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org "David G. Johnston" writes: > On Wed, Mar 1, 2017 at 8:22 PM, David G. Johnston wrote: >> On Wednesday, March 1, 2017, 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