From 3204d56fd010cd76f68a385d444c695359e708e6 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 25 Apr 2009 16:45:19 +0000 Subject: Fix the handling of sub-SELECTs appearing in the arguments of an outer-level aggregate function. By definition, such a sub-SELECT cannot reference any variables of query levels between itself and the aggregate's semantic level (else the aggregate would've been assigned to that lower level instead). So the correct, most efficient implementation is to treat the sub-SELECT as being a sub-select of that outer query level, not the level the aggregate syntactically appears in. Not doing so also confuses the heck out of our parameter-passing logic, as illustrated in bug report from Daniel Grace. Fortunately, we were already copying the whole Aggref expression up to the outer query level, so all that's needed is to delay SS_process_sublinks processing of the sub-SELECT until control returns to the outer level. This has been broken since we introduced spec-compliant treatment of outer aggregates in 7.4; so patch all the way back. --- src/test/regress/expected/aggregates.out | 10 ++++++++++ src/test/regress/sql/aggregates.sql | 6 ++++++ 2 files changed, 16 insertions(+) (limited to 'src/test') diff --git a/src/test/regress/expected/aggregates.out b/src/test/regress/expected/aggregates.out index 8aed1864034..6313b94770a 100644 --- a/src/test/regress/expected/aggregates.out +++ b/src/test/regress/expected/aggregates.out @@ -157,6 +157,16 @@ group by ten having exists (select 1 from onek b where sum(distinct a.four + b.four) = b.four); ERROR: aggregates not allowed in WHERE clause +-- Test handling of sublinks within outer-level aggregates. +-- Per bug report from Daniel Grace. +select + (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1))) +from tenk1 o; + ?column? +---------- + 9999 +(1 row) + -- -- test for bitwise integer aggregates -- diff --git a/src/test/regress/sql/aggregates.sql b/src/test/regress/sql/aggregates.sql index b6aba0d66b7..a79c4367e45 100644 --- a/src/test/regress/sql/aggregates.sql +++ b/src/test/regress/sql/aggregates.sql @@ -63,6 +63,12 @@ group by ten having exists (select 1 from onek b where sum(distinct a.four + b.four) = b.four); +-- Test handling of sublinks within outer-level aggregates. +-- Per bug report from Daniel Grace. +select + (select max((select i.unique2 from tenk1 i where i.unique1 = o.unique1))) +from tenk1 o; + -- -- test for bitwise integer aggregates -- -- cgit v1.2.3