pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Join condition parsing puzzle
3+ messages / 2 participants
[nested] [flat]

* Join condition parsing puzzle
@ 2018-08-23 18:17  Mark Jeffcoat <jeffcoat@alumni.rice.edu>
  0 siblings, 1 reply; 3+ messages in thread

From: Mark Jeffcoat @ 2018-08-23 18:17 UTC (permalink / raw)
  To: pgsql-general

I'm looking at a query generated by SQLAlchemy. It works; Postgres is
perfectly happy to run it, and it gives answers that make sense to the
guy who wrote it. But I don't understand why it works.

Stripped way down ...

CREATE VIEW relation_a (id_c, id_v)
AS VALUES (1, 20),  (2, 21), (3, 22);

CREATE VIEW relation_b (id_c, id_v, id_p)
AS VALUES (1, 20, 300), (2, 21, 301);

CREATE VIEW relation_c (id_p)
AS VALUES (301);

SELECT *
FROM relation_a
LEFT JOIN relation_b
JOIN relation_c
ON (relation_c.id_p = relation_b.id_p)
ON (relation_a.id_c = relation_b.id_c AND relation_a.id_v = relation_b.id_v);


I would have claimed before seeing this example that it wasn't even
grammatical; I thought the only legal place to write the ON clause was
immediately after the JOIN. Apparently not.

How should I read this query?  I'd appreciate any help understanding this.

-- 
Mark Jeffcoat
Austin, TX




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Join condition parsing puzzle
@ 2018-08-23 21:51  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Mark Jeffcoat <jeffcoat@alumni.rice.edu>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2018-08-23 21:51 UTC (permalink / raw)
  To: Mark Jeffcoat <jeffcoat@alumni.rice.edu>; +Cc: pgsql-general

Mark Jeffcoat <jeffcoat@alumni.rice.edu> writes:
> SELECT *
> FROM relation_a
> LEFT JOIN relation_b
> JOIN relation_c
> ON (relation_c.id_p = relation_b.id_p)
> ON (relation_a.id_c = relation_b.id_c AND relation_a.id_v = relation_b.id_v);

> I would have claimed before seeing this example that it wasn't even
> grammatical; I thought the only legal place to write the ON clause was
> immediately after the JOIN. Apparently not.

> How should I read this query?  I'd appreciate any help understanding this.

You read it as

SELECT *
FROM
  relation_a
  LEFT JOIN (relation_b
             JOIN relation_c
             ON (relation_c.id_p = relation_b.id_p))
  ON (relation_a.id_c = relation_b.id_c AND relation_a.id_v = relation_b.id_v);

There's no other valid way to parenthesize it, so that's what
the parser does.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Join condition parsing puzzle
@ 2018-08-25 00:43  Mark Jeffcoat <jeffcoat@alumni.rice.edu>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Mark Jeffcoat @ 2018-08-25 00:43 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general

On Thu, Aug 23, 2018 at 4:51 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> Mark Jeffcoat <jeffcoat@alumni.rice.edu> writes:

> > I would have claimed before seeing this example that it wasn't even
> > grammatical; I thought the only legal place to write the ON clause was
> > immediately after the JOIN. Apparently not.
>
> You read it as
>
> SELECT *
> FROM
>   relation_a
>   LEFT JOIN (relation_b
>              JOIN relation_c
>              ON (relation_c.id_p = relation_b.id_p))
>   ON (relation_a.id_c = relation_b.id_c AND relation_a.id_v = relation_b.id_v);
>
> There's no other valid way to parenthesize it, so that's what
> the parser does.



Thank you very much for your help, Tom. In retrospect, I see I'd
over-generalized the rule that sub-selects in the from clause require
an alias.

Clear now.

-- 
Mark Jeffcoat
Austin, TX




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2018-08-25 00:43 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-08-23 18:17 Join condition parsing puzzle Mark Jeffcoat <jeffcoat@alumni.rice.edu>
2018-08-23 21:51 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-08-25 00:43   ` Mark Jeffcoat <jeffcoat@alumni.rice.edu>

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