![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Description: Rows returned that should be excluded by WHERE clause |
#3
| |||
| |||
|
|
Description: Rows returned that should be excluded by WHERE clause |
#4
| |||
| |||
|
|
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@c2n2 gill]$ db2 "select 2 as id, max(apn3) from phoenix.client where 2 =1" ID 2 ----------- ------ 2 - 1 record(s) selected. |
#5
| |||
| |||
|
|
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@c2n2 gill]$ db2 "select 2 as id, max(apn3) from phoenix.client wher= e 2 =3D1" |
|
ID 2 ----------- ------ 2 - 1 record(s) selected. |
#6
| |||
| |||
|
|
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) 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? |
#7
| |||
| |||
|
|
I think this demonstrates the problem much better than I could explain in words. The bug is shown in the two SELECT queries with a WHERE clause. Very bizarre. |
#8
| |||||
| |||||
|
|
"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; |
). See below.|
I think the problem can be expressed as [ snip ] 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. [ ... ] |
|
"Gill, Jerry T." <JTGill (AT) west (DOT) com> writes: 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@c2n2 gill]$ db2 "select 2 as id, max(apn3) from phoenix.client where 2 =1" ID 2 ----------- ------ 2 - 1 record(s) selected. In the WHERE case I think there's no question that the above is correct: WHERE is defined to filter rows before application of aggregates, so zero rows arrive at the MAX aggregate, and that means it produces a NULL. |
|
But HAVING is supposed to filter after aggregation, so I think probably there should be no row out in that case. |
|
regards, tom lane |

#9
| |||
| |||
|
|
[various stuff snipped] You say, "WHERE is defined to filter rows before application of aggregates", but I'd _think_ that should be interpreted to apply only to aggregates in the _current_ query (ie. not in sub-queries). |
![]() |
| Thread Tools | |
| Display Modes | |
| |