OUTER JOIN and DISJUNCTION

Related to
  ACTION SimonR: explain how much of disjuction can be done with
  optionals, nested or otherwise.  Point to references, problems with
  LEFT OUTER JOIN in the literature.

In my experiments, I haven't found DISJUNCTION that I couldn't
implement with OUTER JOINs.

first create a holds table (MySQL syntax):
CREATE TABLE holds (id INTEGER NOT NULL AUTO_INCREMENT, 
                    s VARCHAR(80), p VARCHAR(80), o VARCHAR(80), 
                    PRIMARY KEY(id));
INSERT INTO holds (id) VALUES (0);
INSERT INTO holds (s, p, o) VALUES ('a1', 'foaf:mbox', 'mbox1');
INSERT INTO holds (s, p, o) VALUES ('a1', 'foaf:name', 'bob smith');
INSERT INTO holds (s, p, o) VALUES ('a2', 'foaf:mbox', 'mbox2');
INSERT INTO holds (s, p, o) VALUES ('a2', 'foaf:givenName', 'bob');
INSERT INTO holds (s, p, o) VALUES ('a2', 'foaf:family', 'smith');
-- extra data that we won't select:
INSERT INTO holds (s, p, o) VALUES ('a1', 'foaf:knows', 'a2');
INSERT INTO holds (s, p, o) VALUES ('a1', 'foaf:bloodType', 'A-');
INSERT INTO holds (s, p, o) VALUES ('a2', 'foaf:bloodType', 'A+');


Unground Disjunction, symetric paths (doesn't require OUTER JOINs):
  (($a foaf:name $name)
   UNION ($a foaf:givenName $given))
triple store SQL query:
  SELECT h1.s AS a, 
         (h1.p="foaf:name" ) AS _L1, 
         (h1.p="foaf:givenName") AS _R1,
         h1.o AS _L1_name_R1_gvien
    FROM holds AS h1
   WHERE ((h1.p="foaf:name")
       OR (h1.p="foaf:givenName"))
+------+------+------+-------------------+
| a    | _L1  | _R1  | _L1_name_R1_gvien |
+------+------+------+-------------------+
| a1   |    1 |    0 | bob smith         |
| a2   |    0 |    1 | bob               |
+------+------+------+-------------------+


Ground Disjunction, assymetric paths:
  ($a foaf:mbox $mbox)
  (($a foaf:name $name)
   UNION (($a foaf:givenName $given)
          ($a foaf:family $family)))
triple store SQL query:
  SELECT h1.s AS a, 
         h1.o AS mbox, 
         (    h2.s=h1.s AND h2.p="foaf:name" 
           AND h3.id=0) AS _L1, 
         (    h2.s=h1.s AND h2.p="foaf:givenName" 
           AND h3.s=h1.s AND h3.p="foaf:givenName") AS _R1,
         h2.o AS _L1_name_R1_gvien, 
         h3.o AS family
    FROM holds AS h1
         LEFT OUTER JOIN holds AS h2 ON 1
         LEFT OUTER JOIN holds AS h3 ON 1
   WHERE h1.p="foaf:mbox"
     AND ((    h2.s=h1.s AND h2.p="foaf:name" 
           AND h3.id=0)
       OR (    h2.s=h1.s AND h2.p="foaf:givenName" 
           AND h3.s=h1.s AND h3.p="foaf:givenName"))
+------+-------+------+------+-------------------+--------+
| a    | mbox  | _L1  | _R1  | _L1_name_R1_gvien | family |
+------+-------+------+------+-------------------+--------+
| a2   | mbox2 |    0 |    1 | bob               | bob    |
| a1   | mbox1 |    1 |    0 | bob smith         | NULL   |
+------+-------+------+------+-------------------+--------+


Unground Disjunction, assymetric paths:
  (($a foaf:name $name)
   UNION (($a foaf:givenName $given)
          ($a foaf:family $family)))
triple store SQL query:
  SELECT h1.s AS a, 
         (    h1.p="foaf:name" 
           AND h2.id=0) AS _L1, 
         (    h1.p="foaf:givenName" 
           AND h2.s=h1.s AND h2.p="foaf:givenName") AS _R1,
         h1.o AS _L1_name_R1_gvien, 
         h2.o AS family
    FROM holds AS h1
         LEFT OUTER JOIN holds AS h2 ON 1
   WHERE ((    h1.p="foaf:name" 
           AND h2.id=0)
       OR (    h1.p="foaf:givenName" 
           AND h2.s=h1.s AND h2.p="foaf:givenName"))
+------+------+------+-------------------+--------+
| a    | _L1  | _R1  | _L1_name_R1_gvien | family |
+------+------+------+-------------------+--------+
| a2   |    0 |    1 | bob               | bob    |
| a1   |    1 |    0 | bob smith         | NULL   |
+------+------+------+-------------------+--------+
-- 
-eric

office: +1.617.258.5741 NE43-344, MIT, Cambridge, MA 02144 USA
cell:   +1.857.222.5741

(eric@w3.org)
Feel free to forward this message to any list for any purpose other than
email address distribution.

Received on Monday, 15 November 2004 11:31:44 UTC