pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Evan Simpson <evan@tokenexchange.com>
To: pgsql-sql@postgreSQL.org
Subject: PROPOSAL: Statement for one-sided joins
Date: Tue, 10 Aug 1999 12:19:56 -0500
Message-ID: <008d01bee354$91a76fa0$8d84b2d8@token.hapenney.com> (raw)

I'm just getting started with PostgreSQL, and I love it; I hope to
eventually use it in all of my projects.  The only thing holding me back is
the lack of a good way to perform left outer joins.  I scanned all of the
mailing lists, and it seems that the issue has come up several times, and
people have shown interest, but there has been no visible progress.

In my experience, when I code a one-sided join I'm generally thinking "for
selected objects from this class, fetch attributes plus related data from
other classes".  Based on my vague impression that PostgreSQL converts some
queries internally into nested loops, I suggest the following new statement
(partially stolen from InterBase's stored procedure language):

FOR ... [WHERE ...] [GROUP BY ...] [HAVING ...] DO <statement>

This would convert directly into a nested loop around the <statement>, and
would replace any empty SELECT result within <statement> with a single row
in which "local" object attributes are NULL.  The current object(s) from the
FOR ... DO would be accessible inside <statement>.  Then one could write the
SQL92:

SELECT p.name, c.name FROM parents p LEFT JOIN children c ON c.parent = p.id

as

FOR parents p DO SELECT p.name, c.name FROM children c WHERE c.parent = p.id

More complex constructions could involve nested FOR ... DO's, in which case
the inner FOR ... DO's would each invoke their <statement> at least once,
with NULL objects if necessary.  A list of all widgets, exploded into parts
and sub-parts if possible, could be written:

FOR widgets w DO
  FOR parts p1, widgets wp1 WHERE p1.widget = w.id and p1.part = wp1.id DO
    SELECT w.name, wp1.name, wp2.name FROM parts p2, widgets wp2
      WHERE p2.widget = p1.part and p2.part = wp2.id

Does this look more or less complicated to implement and use than the SQL92
LEFT JOIN?  Is it too non-standard to live?  Too ambiguous or narrow?  I'd
implement it myself, but I'm light-years away from being able to contribute
anything but bug reports and ideas right now.

Thanks,
Evan Simpson




Message-ID: <008d01bee354$91a76fa0$8d84b2d8@token.hapenney.com>
Permalink:  ../008d01bee354$91a76fa0$8d84b2d8@token.hapenney.com/
Also on:    postgresql.org/message-id/008d01bee354$91a76fa0$8d84b2d8@token.hapenney.com

 · 

reply

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-sql@postgresql.org
  Cc: evan@tokenexchange.com
  Subject: Re: PROPOSAL: Statement for one-sided joins
  In-Reply-To: <008d01bee354$91a76fa0$8d84b2d8@token.hapenney.com>
  References: <008d01bee354$91a76fa0$8d84b2d8@token.hapenney.com>

* 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