Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ndoyG-0000Bi-0J for pgsql-general@arkaria.postgresql.org; Mon, 11 Apr 2022 08:00:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ndoyD-0005TD-OU for pgsql-general@arkaria.postgresql.org; Mon, 11 Apr 2022 08:00:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ndoyD-0005T3-Bk for pgsql-general@lists.postgresql.org; Mon, 11 Apr 2022 08:00:21 +0000 Received: from smtp-zimbra.meteo.fr ([137.129.16.22] helo=celebrian1.meteo.fr) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ndoyB-00075W-1Y for pgsql-general@postgresql.org; Mon, 11 Apr 2022 08:00:20 +0000 Received: from localhost (localhost [127.0.0.1]) by celebrian1.meteo.fr (Postfix) with ESMTP id 8A0ED1C209A; Mon, 11 Apr 2022 10:00:16 +0200 (CEST) Received: from celebrian1.meteo.fr ([127.0.0.1]) by localhost (celebrian1.meteo.fr [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 6ST1rO0RfmVz; Mon, 11 Apr 2022 10:00:16 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by celebrian1.meteo.fr (Postfix) with ESMTP id 6D91F1C20A3; Mon, 11 Apr 2022 10:00:16 +0200 (CEST) X-Virus-Scanned: amavisd-new at celebrian1.meteo.fr Received: from celebrian1.meteo.fr ([127.0.0.1]) by localhost (celebrian1.meteo.fr [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id UorlrMz4Z9n7; Mon, 11 Apr 2022 10:00:16 +0200 (CEST) Received: from zumba5.meteo.fr (zumba5 [172.31.9.201]) by celebrian1.meteo.fr (Postfix) with ESMTP id 4D5CC1C209A; Mon, 11 Apr 2022 10:00:16 +0200 (CEST) Date: Mon, 11 Apr 2022 10:00:15 +0200 (CEST) From: PALAYRET Jacques To: "David G. Johnston" Cc: pgsql-general Message-ID: <1512389835.55848519.1649664015407.JavaMail.zimbra@meteo.fr> In-Reply-To: References: <472671960.55252257.1649406546838.JavaMail.zimbra@meteo.fr> Subject: Re: PostgreSQL : error hint for LATERAL join MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_2733dc95-c1e0-412c-b9d5-5022fd2e8790" X-Originating-IP: [172.31.9.204] X-Mailer: Zimbra 8.7.11_GA_3822 (ZimbraWebClient - FF91 (Win)/8.7.11_GA_3822) Thread-Topic: PostgreSQL : error hint for LATERAL join Thread-Index: kd3xRrRkIr18ZrAc79hOjCZbw0D9vg== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=_2733dc95-c1e0-412c-b9d5-5022fd2e8790 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello,=20 Your point of view is interesting. May I discuss it ?=20 One answered to me on my question about " bug (ou not) in CTE Common Table = Expressions or Subqueries in the FROM Clause " :=20 " Its doesn=E2=80=99t have to seem logical to you, but this is how it is de= fined to work and thus the observed behavior is not a bug. "=20 I thought about it and OK, even if regretted that the PostgreSQL language s= ometimes ( rarely ) doesn't really make sense for me (given my logic).=20 So, are standards the rule or not ?=20 For me, one of the two following things should be true : either the hint (i= n case of a lateral error) is incomplete or the possibility of " cross join= lateral " should be removed.=20 Of course, the idea of CROSS join doesn't make think about LATERAL idea.=20 But, is there a difference between a CROSS join and a INNER join with the "= ON TRUE " clause ?=20 Note : an inner join is a cross join with a clause ON (logical condition) a= nd OK, of course, an INNER join has not always " ON TRUE " clause, but it c= an.=20 I think you are right to write " Because the lateral takes precedence ".=20 LATERAL " takes precedence " over CROSS in the same way as INNER JOIN ... a= nd INNER JOIN ON TRUE=20 With LATERAL, joins are no more independant relations, neither CROSS or INN= ER.=20 SELECT *=20 FROM (=20 VALUES ('a'),('b')=20 ) t (c1)=20 JOIN LATERAL (=20 VALUES ('b', c1), ('c',c1 || '*')=20 ) u(d1) ON true=20 ;=20 c1 | d1 | column2=20 ----+----+---------=20 a | b | a=20 a | c | a*=20 b | b | b=20 b | c | b*=20 (4 lignes)=20 Regards=20 De: "David G. Johnston" =20 =C3=80: "PALAYRET Jacques" =20 Cc: "pgsql-general" =20 Envoy=C3=A9: Vendredi 8 Avril 2022 15:36:34=20 Objet: Re: PostgreSQL : error hint for LATERAL join=20 On Fri, Apr 8, 2022 at 1:29 AM PALAYRET Jacques < [ mailto:jacques.palayret= @meteo.fr | jacques.palayret@meteo.fr ] > wrote:=20 For a LATERAL join, I think the hint (in case of error) is incomplete :=20 " D=C3=89TAIL : The combining JOIN type must be INNER or LEFT for a LATERAL= reference. "=20 to be replaced by :=20 " D=C3=89TAIL : The combining JOIN type must be CROSS, INNER or LEFT for a = LATERAL reference. "=20 Note : it depends on what is needed=20 Regardless of whether the syntax works or not, the conceptual idea that a l= ateral is also somehow a cross join is just wrong. A cross join, by definit= ion, takes two independent relations and performs a cartesian product betwe= en them. A lateral join, by definition, takes a row from the left side of t= he join, evaluates the right side using one or more columns from that rows,= then produces an output row for each row produced by the right side (copyi= ng the left) - the inner/outer marker indicating what to do when the right = side produces zero rows.=20 If you use a non-trivial demonstration query (i.e., one that doesn't try to= multiply 1x1) this becomes more clear:=20 postgres=3D# SELECT *=20 FROM (=20 VALUES ('a'),('b')=20 ) t (c1)=20 CROSS JOIN LATERAL (=20 VALUES ('b', c1), ('c',c1 || '*')=20 ) u(d1)=20 ;=20 c1 | d1 | column2=20 ----+----+---------=20 a | b | a=20 a | c | a*=20 b | b | b=20 b | c | b*=20 (4 rows)=20 The presence of the cross join is misleading (if anything the error message= is sound advice and the behavior shown is wrong, but likely standard's man= dated). If it were a true cross join the relation u produced 4 unique rows = and the relation t produced 2, thus the output should have 8 rows. It only = has four. Because the lateral takes precedence here and only matches a subs= et of the right-side output rows with the left side.=20 David J.=20 --=_2733dc95-c1e0-412c-b9d5-5022fd2e8790 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Hello,

Your point of view is interesting. May I discuss it ?
One = answered to me on my question about " bug (ou not) in CTE Common Table Expr= essions or Subqueries in the FROM Clause " :
"  Its doesn=E2=80=99t= have to seem logical to you, but this is how it is defined to work and thu= s the observed behavior is not a bug. "
I thought about it and OK, even = if regretted that the PostgreSQL language sometimes (rarely) doesn't really make sense for me (given my logic).

So= , are standards the rule or not ?
For me, one of the two following thing= s should be true : either the hint (in case of a lateral error) is incomple= te or the possibility of " cross join lateral " should be removed.

O= f course, the idea of CROSS join doesn't make think about LATERAL idea.
= But, is there a difference between a CROSS join and a INNER join with the "= ON TRUE " clause ?
Note : an inn= er join is a cross join with a clause ON (logical condition) and OK, of cou= rse, an INNER join has not always " ON TRUE " clause, but it can.
=

I think you are right to write " Because the lateral takes precede= nce ".
LATERAL " takes precedence " over CROSS in the same way as INNER = JOIN ... and INNER JOIN ON TRUE
= With LATERAL, joins are no more independant relations, neither CROSS or INN= ER.

= SELECT *
FROM (
VALUES ('a'),('b')
) t (c1)
 JOIN LATERAL = (
VALUES ('b', c1), ('c',c1 || '*')
) u(d1) ON true
;
 c1 = | d1 | column2
----+----+---------
 a  | b  | a
&nb= sp;a  | c  | a*
 b  | b  | b
 b  |= c  | b*
(4 lignes)

Regards

De: "David G. Johnston" = <david.g.johnston@gmail.com>
=C3=80: "PALAYRET Jacques" <= ;jacques.palayret@meteo.fr>
Cc: "pgsql-general" <pgsql-gene= ral@postgresql.org>
Envoy=C3=A9: Vendredi 8 Avril 2022 15:36:3= 4
Objet: Re: PostgreSQL : error hint for LATERAL join
On Fri, Apr 8, 2022 at 1:29= AM PALAYRET Jacques <jacques.palayret@meteo.fr= > wrote:
For a LATERAL join, I think the hint (in case of error) is incomple= te :

" D=C3=89TAIL : The combining JOIN type must be INNE= R or LEFT for a LATERAL reference. "
 to be replaced by :
" D=C3= =89TAIL : The combining JOIN type must be CROSS, INNER or LEFT for a LATERA= L reference. "

Note : it depends on what is needed


Regardless of whether the syntax works or not, the conceptual idea th= at a lateral is also somehow a cross join is just wrong.  A cross join= , by definition, takes two independent relations and performs a cartesian p= roduct between them.  A lateral join, by definition, takes a row from = the left side of the join, evaluates the right side using one or more = columns from that rows, then produces an output row for each row produced b= y the right side (copying the left) - the inner/outer  marker indicati= ng what to do when the right side produces zero rows.

If you use a non-trivial demonst= ration query (i.e., one that doesn't try to multiply 1x1) this becomes more= clear:
=
pos= tgres=3D# SELECT *
FROM (
VALUES ('a'),('b')
) t (c1)
 CRO= SS JOIN LATERAL (
VALUES ('b', c1), ('c',c1 || '*')
) u(d1)
;
&= nbsp;c1 | d1 | column2
----+----+---------
 a  | b  | = a
 a  | c  | a*
 b  | b  | b
 b=  | c  | b*
(4 rows)

The presence of the cross join is misleading (if= anything the error message is sound advice and the behavior shown is wrong= , but likely standard's mandated).  If it were a true cross join the r= elation u produced 4 unique rows and the relation t produced 2, thus the ou= tput should have 8 rows.  It only has four.  Because the lateral = takes precedence here and only matches a subset of the right-side output ro= ws with the left side.

David J.

--=_2733dc95-c1e0-412c-b9d5-5022fd2e8790--