Re: BUG #1528: Rows returned that should be excluded by WHERE clause

From: "Gill, Jerry T(dot)" <JTGill(at)west(dot)com>
To: <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #1528: Rows returned that should be excluded by WHERE clause
Date: 2005-03-08 16:41:53
Message-ID: 9D87A98A6510F24C817257895EF4282A01AA8E7B@omacex08.corp.westworlds.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Just an interesting side note here, this behavior is identical to DB2. I am not sure if that makes it correct or not, but here is an example.

[gill(at)c2n2 gill]$ db2 "select 2 as id, max(apn3) from phoenix.client where 2 =1"

ID 2
----------- ------
2 -

1 record(s) selected.

-jgill

-----Original Message-----
From: pgsql-bugs-owner(at)postgresql(dot)org
[mailto:pgsql-bugs-owner(at)postgresql(dot)org]On Behalf Of Tom Lane
Sent: Tuesday, March 08, 2005 2:07 AM
To: Peter Wright
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: [BUGS] BUG #1528: Rows returned that should be excluded by
WHERE clause

"Peter Wright" <pete(at)flooble(dot)net> writes:
> Description: Rows returned that should be excluded by WHERE clause

Interesting point. The view and union don't seem to be the issue;
I think the problem can be expressed as

regression=# select 2 as id, max(b) from t2 having 2 = 1;
id | max
----+-----
2 |
(1 row)

Now, if this were a WHERE clause, I think the answer would be right:

regression=# select 2 as id, max(b) from t2 where 2 = 1;
id | max
----+-----
2 |
(1 row)

but since it's HAVING I think this is probably wrong. Looking at the
EXPLAIN output

regression=# explain select 2 as id, max(b) from t2 having 2 = 1;
QUERY PLAN
----------------------------------------------------------------
Aggregate (cost=3.68..3.68 rows=1 width=2)
-> Result (cost=0.00..3.14 rows=214 width=2)
One-Time Filter: false
-> Seq Scan on t2 (cost=0.00..3.14 rows=214 width=2)
(4 rows)

the issue is clearly that the known-false HAVING clause is pushed down
inside the aggregation, as though it were WHERE. The existing code
pushes down HAVING to WHERE if the clause contains no aggregates, but
evidently this is too simplistic. What are the correct conditions for
pushing down HAVING clauses to WHERE?

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2005-03-08 17:14:35 Re: BUG #1528: Rows returned that should be excluded by WHERE clause
Previous Message Tom Lane 2005-03-08 15:32:46 Re: BUG #1531: rotated log truncation broken