BUG #3308: Nested outer join gives wrong results

From: "George Sakkis" <george(dot)sakkis(at)gmail(dot)com>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #3308: Nested outer join gives wrong results
Date: 2007-05-25 20:24:40
Message-ID: 200705252024.l4PKOern055836@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs


The following bug has been logged online:

Bug reference: 3308
Logged by: George Sakkis
Email address: george(dot)sakkis(at)gmail(dot)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 ?

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2007-05-25 21:21:52 Re: BUG #3308: Nested outer join gives wrong results
Previous Message Tom Lane 2007-05-25 14:38:51 Re: BUG #3307: Software error when accessed