BUG #3308: Nested outer join gives wrong results - Mailing list pgsql-bugs

From George Sakkis
Subject BUG #3308: Nested outer join gives wrong results
Date
Msg-id 200705252024.l4PKOern055836@wwwmaster.postgresql.org
Whole thread Raw
Responses Re: BUG #3308: Nested outer join gives wrong results  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
The following bug has been logged online:

Bug reference:      3308
Logged by:          George Sakkis
Email address:      george.sakkis@gmail.com
PostgreSQL version: 8.2.3
Operating system:   Linux
Description:        Nested outer join gives wrong results
Details:

I have the following two subqueries, both returning a single column q_id;
also the result set of A is a strict superset of B:

Query A
-------
SELECT query.id as q_id
FROM ranker, run, query
WHERE ranker.id = 72 AND
      run.id = ranker.run_id AND
      query.set_id = run.set_id

(966 rows)

Query B
-------
SELECT serp_result.q_id
FROM serp_result LEFT OUTER JOIN editor_rating using (q_id,norm_url)
WHERE serp_result.ranker_id = 72 AND
      serp_result.rank <= 1
      AND editor_rating.grade is null

(251 rows)


Now, the left outer join of A and B should be equal to A in this case since
A is a superset of B. If I save A in a temp table and use this for the join,
that's indeed the result:

SELECT query.id as q_id INTO TEMP t1
FROM ranker, run, query
WHERE ranker.id = 72 AND
      run.id = ranker.run_id AND
      query.set_id = run.set_id

SELECT *
FROM t1
LEFT JOIN (
  SELECT serp_result.q_id
  FROM serp_result LEFT OUTER JOIN editor_rating using (q_id,norm_url)
  WHERE serp_result.ranker_id = 72 AND serp_result.rank <= 1 AND
editor_rating.grade is null
) AS t2 USING (q_id)
;

(966 rows)

Likewise if I save B into a temp table and join with A:

SELECT serp_result.q_id into TEMP t2
FROM serp_result LEFT OUTER JOIN editor_rating using (q_id,norm_url)
WHERE serp_result.ranker_id = 72 AND serp_result.rank <= 1 AND
editor_rating.grade is null

SELECT *
FROM (
  SELECT query.id as q_id
  FROM ranker, run, query
  WHERE ranker.id = 72 AND
        run.id = ranker.run_id AND
        query.set_id = run.set_id
) AS t1
LEFT JOIN t2 USING (q_id)

(966 rows)

If I don't use temp tables though, the result is equal to B instead:

SELECT *
FROM (
  SELECT query.id as q_id
  FROM ranker, run, query
  WHERE ranker.id = 72 AND
        run.id = ranker.run_id AND
        query.set_id = run.set_id
) AS t1
LEFT JOIN (
  SELECT serp_result.q_id
  FROM serp_result LEFT OUTER JOIN editor_rating using (q_id,norm_url)
  WHERE serp_result.ranker_id = 72 AND
        serp_result.rank <= 1
        AND editor_rating.grade is null
) AS t2 USING (q_id)
;

(251 rows)

What gives ?

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #3307: Software error when accessed
Next
From: Tom Lane
Date:
Subject: Re: BUG #3308: Nested outer join gives wrong results