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 1nWb5Z-0007mF-Lc for pgsql-general@arkaria.postgresql.org; Tue, 22 Mar 2022 09:46:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nWb5Y-0003sQ-I5 for pgsql-general@arkaria.postgresql.org; Tue, 22 Mar 2022 09:46:04 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nWb5Y-0003sC-2Z for pgsql-general@lists.postgresql.org; Tue, 22 Mar 2022 09:46:04 +0000 Received: from smtp-zimbra.meteo.fr ([137.129.16.22] helo=celebrian2.meteo.fr) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nWb5T-0004wO-93 for pgsql-general@postgresql.org; Tue, 22 Mar 2022 09:46:02 +0000 Received: from localhost (localhost [127.0.0.1]) by celebrian2.meteo.fr (Postfix) with ESMTP id 98D9D48070 for ; Tue, 22 Mar 2022 10:45:56 +0100 (CET) Received: from celebrian2.meteo.fr ([127.0.0.1]) by localhost (celebrian2.meteo.fr [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id sgW-elWV9aY7 for ; Tue, 22 Mar 2022 10:45:56 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by celebrian2.meteo.fr (Postfix) with ESMTP id 78C5648074 for ; Tue, 22 Mar 2022 10:45:56 +0100 (CET) X-Virus-Scanned: amavisd-new at celebrian2.meteo.fr Received: from celebrian2.meteo.fr ([127.0.0.1]) by localhost (celebrian2.meteo.fr [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id YRXhx3SNpY2I for ; Tue, 22 Mar 2022 10:45:56 +0100 (CET) Received: from zumba5.meteo.fr (zumba5 [172.31.9.201]) by celebrian2.meteo.fr (Postfix) with ESMTP id 4688948070 for ; Tue, 22 Mar 2022 10:45:56 +0100 (CET) Date: Tue, 22 Mar 2022 10:45:55 +0100 (CET) From: PALAYRET Jacques To: pgsql-general@postgresql.org Message-ID: <1370619684.47054020.1647942355596.JavaMail.zimbra@meteo.fr> Subject: PostgreSQL : bug (ou not) in CTE Common Table Expressions or Subqueries in the FROM Clause MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_54480aee-c58b-434d-a05f-35799a631aed" X-Originating-IP: [172.31.9.204] X-Mailer: Zimbra 8.7.11_GA_3822 (ZimbraWebClient - FF91 (Win)/8.7.11_GA_3822) Thread-Index: ONQ8V7e4ERMAEKYJnI2KbxhebgbhXw== Thread-Topic: PostgreSQL : bug (ou not) in CTE Common Table Expressions or Subqueries in the FROM Clause List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=_54480aee-c58b-434d-a05f-35799a631aed Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hello,=20 # Let's consider a table defined as follows :=20 CREATE TABLE weather_stations(=20 id integer,=20 name varchar(30),=20 elev integer=20 ) ;=20 # After loading, the content :=20 id | name | elev=20 ----------+----------------------+------=20 31069001 | TOULOUSE-BLAGNAC | 151=20 31006001 | ALBIAC AGGLOMERATION | 289=20 31010001 | LUCHON-ANTIGNAC | 599=20 50003001 | AGON-COUTAINVILLE | 2=20 50195001 | GATHEMO | 330=20 (5 lignes)=20 ### With CTE :=20 # I'm suprised by the following result, the behavior of PostgreSQL ; is tha= t a bug ? :=20 =3D Statement 1 : =3D=20 WITH elev_Tlse_Blagnac AS (=20 SELECT elev FROM weather_stations WHERE id=3D31069001=20 )=20 SELECT id, name, elev FROM weather_stations=20 WHERE elev > (=20 SELECT elev FROM elev_Tlse_Blagnac WHERE id BETWEEN 31000000 and 31999999= =20 ) ;=20 id | name | elev=20 ----------+----------------------+------=20 31006001 | ALBIAC AGGLOMERATION | 289=20 31010001 | LUCHON-ANTIGNAC | 599=20 (2 lignes)=20 # According to me, the previous result is an error, because the parentheses= are not taken into account.=20 The column id is not part of elev_Tlse_Blagnac.=20 # The same result as following, which is of course OK :=20 =3D Statement 2 : =3D=20 WITH elev_Tlse_Blagnac AS (=20 SELECT elev FROM weather_stations WHERE id=3D31069001=20 )=20 SELECT id, name, elev FROM weather_stations=20 WHERE elev > (=20 SELECT elev FROM elev_Tlse_Blagnac=20 )=20 AND id BETWEEN 31000000 and 31999999=20 ;=20 id | name | elev=20 ----------+----------------------+------=20 31006001 | ALBIAC AGGLOMERATION | 289=20 31010001 | LUCHON-ANTIGNAC | 599=20 (2 lignes)=20 ### Same weird behavior with subquery in FROM clause :=20 # NOT OK (according to me), because the parentheses are not taken into acco= unt :=20 =3D Statement 3 : =3D=20 SELECT id, name, elev FROM weather_stations=20 WHERE elev > (=20 SELECT elev FROM (SELECT elev FROM weather_stations WHERE id=3D31069001) el= ev_Tlse_Blagnac WHERE id BETWEEN 31000000 and 31999999=20 ) ;=20 id | name | elev=20 ----------+----------------------+------=20 31006001 | ALBIAC AGGLOMERATION | 289=20 31010001 | LUCHON-ANTIGNAC | 599=20 (2 lignes)=20 # OK, the parentheses are taken into account because there is no confusion = with the column id (elev_Tlse_Blagnac has a column named id) :=20 =3D Statement 4 : =3D=20 SELECT id, name, elev FROM weather_stations WHERE elev > (=20 SELECT elev FROM (SELECT * FROM weather_stations WHERE id=3D31069001) elev_= Tlse_Blagnac WHERE id BETWEEN 31000000 and 31999999=20 ) ;=20 id | name | elev=20 ----------+----------------------+------=20 31006001 | ALBIAC AGGLOMERATION | 289=20 31010001 | LUCHON-ANTIGNAC | 599=20 50195001 | GATHEMO | 330=20 (3 lignes)=20 # OK (of course) :=20 =3D Statement 5 : =3D=20 SELECT id, name, elev FROM weather_stations WHERE elev > (=20 SELECT elev FROM (SELECT * FROM weather_stations WHERE id=3D31069001) elev_= Tlse_Blagnac=20 )=20 AND id BETWEEN 31000000 and 31999999=20 ;=20 id | name | elev=20 ----------+----------------------+------=20 31006001 | ALBIAC AGGLOMERATION | 289=20 31010001 | LUCHON-ANTIGNAC | 599=20 (2 lignes)=20 Is that a PostgreSQL bug or not, statement 1 or statement 3 (yes according = to me) ?=20 Regards=20 ----- M=C3=A9t=C3=A9o-France -----=20 PALAYRET JACQUES=20 DCSC/GDC=20 jacques.palayret@meteo.fr=20 Fixe : +33 561078319=20 --=_54480aee-c58b-434d-a05f-35799a631aed Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
Hello,

# Let's consider a table defined as follows :
CREATE T= ABLE weather_stations(
id integer,
name varchar(30),
elev integer<= br>) ;

# After loading, the content :
    id =    |         name &n= bsp;       | elev
----------+-------------= ---------+------
 31069001 | TOULOUSE-BLAGNAC   &nbs= p; |  151
 31006001 | ALBIAC AGGLOMERATION |  289
&nbs= p;31010001 | LUCHON-ANTIGNAC      |  599
&= nbsp;50003001 | AGON-COUTAINVILLE    |    2 50195001 | GATHEMO        &= nbsp;     |  330
(5 lignes)

### With CTE= :
# I'm suprised by the following result, the behavior of  PostgreSQL ; is that a bug ? :
=3D State= ment 1 : =3D
WITH elev_Tlse_Blagnac AS (
SELECT elev FROM weather_sta= tions WHERE id=3D31069001
)
SELECT id, name, elev FROM weather_statio= ns
WHERE elev > (
SELECT elev FROM elev_Tlse_Blagnac WHERE id BETW= EEN 31000000 and 31999999
) ;
    id   = |         name   &n= bsp;     | elev
----------+----------------------+--= ----
 31006001 | ALBIAC AGGLOMERATION |  289
 31010001= | LUCHON-ANTIGNAC      |  599
(2 lignes)<= br># According to me, the previous result is an error, because the parenthe= ses are not taken into account.
The column id is not part of elev_Tlse_B= lagnac.


# The same re= sult as following, which is of course OK :
=3D Statement 2 : =3D
WITH= elev_Tlse_Blagnac AS (
SELECT elev FROM weather_stations WHERE id=3D310= 69001
)
SELECT id, name, elev FROM weather_stations
WHERE elev >= ; (
SELECT elev FROM elev_Tlse_Blagnac
)
AND id BETWEEN 31000000 a= nd 31999999
;
    id    |  &n= bsp;      name      =    | elev
----------+----------------------+------
 31= 006001 | ALBIAC AGGLOMERATION |  289
 31010001 | LUCHON-ANTIGN= AC      |  599
(2 lignes)

### Same weird behavior with subquery= in FROM clause :
# NOT OK (according to me), because the parentheses ar= e not taken into account :
=3D Statement 3 : =3D
SELECT id, name, ele= v FROM weather_stations
WHERE elev > (
SELECT elev FROM (SELECT el= ev FROM weather_stations WHERE id=3D31069001) elev_Tlse_Blagnac WHERE id BE= TWEEN 31000000 and 31999999
) ;
    id  &nbs= p; |         name   =       | elev
----------+----------------------+= ------
 31006001 | ALBIAC AGGLOMERATION |  289
 310100= 01 | LUCHON-ANTIGNAC      |  599
(2 lignes= )

# OK, the parentheses are taken into account because there is no c= onfusion with the column id (elev_Tlse_Blagnac has a column named id) :
= =3D Statement 4 : =3D
SELECT id, name, elev FROM weather_stations WHERE = elev > (
SELECT elev FROM (SELECT * FROM weather_stations WHERE id=3D= 31069001) elev_Tlse_Blagnac WHERE id BETWEEN 31000000 and 31999999
) ;    id    |     &n= bsp;   name         | ele= v
----------+----------------------+------
 31006001 | ALBIAC AG= GLOMERATION |  289
 31010001 | LUCHON-ANTIGNAC  &nbs= p;   |  599
 50195001 | GATHEMO   &nb= sp;          |  330
(3= lignes)

# OK (of course) :
=3D Statement 5 : =3D
SELECT id, n= ame, elev FROM weather_stations WHERE elev > (
SELECT elev FROM (SELE= CT * FROM weather_stations WHERE id=3D31069001) elev_Tlse_Blagnac
)
A= ND id BETWEEN 31000000 and 31999999
;
    id &nbs= p;  |         name  =        | elev
----------+-----------------= -----+------
 31006001 | ALBIAC AGGLOMERATION |  289
 = 31010001 | LUCHON-ANTIGNAC      |  599
(2 = lignes)


Is that a PostgreSQL bug or not, statement 1 or statemen= t 3 (yes according to me) ?


Regards
----= - M=C3=A9t=C3=A9o-France -----
PALAYRET JACQUES
DCSC/GDC
jacques.= palayret@meteo.fr
Fixe : +33 561078319
--=_54480aee-c58b-434d-a05f-35799a631aed--