Bug 4906 -- Left join of subselect incorrect

Started by Mathieu Fenniakover 16 years ago4 messagesbugs
Jump to latest
#1Mathieu Fenniak
mathieu@fenniak.net

Hi all,

After running the attached setup.sql.gz SQL script on a PostgreSQL
8.4.0 database, the following two queries which should be logically
identical return different results. As far as I can tell from the
query analysis, the LEFT JOIN on query A is happening after
"ee.projectid = pc.projectid" is filtered; therefore the rows where
projectid is NULL are not visible in query A. The issue does not
occur in PostgreSQL 8.3.6.

My apologies for the large test setup; I attempted build up the same
test case, but was unable to reproduce the issue. I had to tear down
my database as much as I could while maintaining the issue.

Query A:
select *
FROM expense ex
JOIN expenseentry ee ON ex.id = ee.expenseid
LEFT JOIN (
SELECT projectclient.projectid, projectclient.clientid,
projectclient.billingpercentage
FROM projectclient
WHERE projectclient.projectid IN (
SELECT project.id
FROM project
WHERE project.clientbillingallocationmethod <> 2)
) pc ON ee.projectid = pc.projectid

Query B:
select *
FROM expense ex
JOIN expenseentry ee ON ex.id = ee.expenseid
LEFT JOIN (
SELECT projectclient.projectid, projectclient.clientid,
projectclient.billingpercentage
FROM projectclient
INNER JOIN project ON (projectclient.projectid = project.id)
WHERE project.clientbillingallocationmethod <> 2
) pc ON ee.projectid = pc.projectid

Attachments:

setup.sql.gzapplication/x-gzip; name=setup.sql.gz; x-unix-mode=0700Download
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Mathieu Fenniak (#1)
Re: Bug 4906 -- Left join of subselect incorrect

Mathieu Fenniak <mathieu@fenniak.net> writes:

After running the attached setup.sql.gz SQL script on a PostgreSQL
8.4.0 database, the following two queries which should be logically
identical return different results. As far as I can tell from the
query analysis, the LEFT JOIN on query A is happening after
"ee.projectid = pc.projectid" is filtered; therefore the rows where
projectid is NULL are not visible in query A.

Yeah, it seems to be confused about whether it can interchange the
order of the semijoin and left join. I'll take a look.

regards, tom lane

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Mathieu Fenniak (#1)
Re: Bug 4906 -- Left join of subselect incorrect

Mathieu Fenniak <mathieu@fenniak.net> writes:

After running the attached setup.sql.gz SQL script on a PostgreSQL
8.4.0 database, the following two queries which should be logically
identical return different results. As far as I can tell from the
query analysis, the LEFT JOIN on query A is happening after
"ee.projectid = pc.projectid" is filtered; therefore the rows where
projectid is NULL are not visible in query A.

This should fix it. Thanks for the report!

regards, tom lane

#4Mathieu Fenniak
mathieu@fenniak.net
In reply to: Tom Lane (#3)
Re: Bug 4906 -- Left join of subselect incorrect

On 20-Jul-09, at 8:05 PM, Tom Lane wrote:

This should fix it. Thanks for the report!

regards, tom lane

Just tested this patch, and it works perfectly. Thank-you.

Mathieu