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.

[EMAIL PROTECTED] 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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of Tom Lane
Sent: Tuesday, March 08, 2005 2:07 AM
To: Peter Wright
Cc: pgsql-bugs@postgresql.org
Subject: Re: [BUGS] BUG #1528: Rows returned that should be excluded by
WHERE clause 


"Peter Wright" <[EMAIL PROTECTED]> 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


---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
      subscribe-nomail command to [EMAIL PROTECTED] so that your
      message can get through to the mailing list cleanly

Reply via email to