Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aSYfL-0004Kt-QJ for pgsql-bugs@arkaria.postgresql.org; Sun, 07 Feb 2016 23:22:51 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aSYfK-0003hH-Q4 for pgsql-bugs@arkaria.postgresql.org; Sun, 07 Feb 2016 23:22:50 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1aSYei-000310-0I for pgsql-bugs@postgresql.org; Sun, 07 Feb 2016 23:22:12 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1aSYea-0003g5-Ca for pgsql-bugs@postgresql.org; Sun, 07 Feb 2016 23:22:10 +0000 Received: from localhost ([127.0.0.1]:23260 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.84 (FreeBSD)) (envelope-from ) id 1aSYeT-000Ken-DK; Sun, 07 Feb 2016 23:21:57 +0000 From: Andrew Gierth To: Dean Rasheed Cc: Andres Freund , wrb , Haribabu Kommi , "pgsql-bugs\@postgresql.org" Subject: Re: BUG #13863: Select from views gives wrong results In-Reply-To: (Dean Rasheed's message of "Mon, 18 Jan 2016 20:06:54 +0000") Message-ID: <87si14w1tz.fsf@news-spur.riddles.org.uk> References: <20160113183558.12989.56904@wrigleys.postgresql.org> <82C006C0-07A7-401B-9BDA-0BABD9025099@autistici.org> <20160116131202.GP10941@awork2.anarazel.de> <87egdfuk2n.fsf@news-spur.riddles.org.uk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (berkeley-unix) Date: Sun, 07 Feb 2016 23:21:57 +0000 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Pg-Spam-Score: -2.1 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --=-=-= Content-Type: text/plain I think for now the following patch is best. This preserves the ability to push degenerate HAVING clauses through an explicit GROUP BY () as well as an implicit one, but prevents pushdown in all other cases. Includes comment and regression test. -- Andrew (irc:RhodiumToad) --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=havingfix.patch Content-Description: fix HAVING pushdown with grouping sets diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index a3cc274..4f8e4c0 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -653,13 +653,19 @@ subquery_planner(PlannerGlobal *glob, Query *parse, * In some cases we may want to transfer a HAVING clause into WHERE. We * cannot do so if the HAVING clause contains aggregates (obviously) or * volatile functions (since a HAVING clause is supposed to be executed - * only once per group). Also, it may be that the clause is so expensive - * to execute that we're better off doing it only once per group, despite - * the loss of selectivity. This is hard to estimate short of doing the - * entire planning process twice, so we use a heuristic: clauses - * containing subplans are left in HAVING. Otherwise, we move or copy the - * HAVING clause into WHERE, in hopes of eliminating tuples before - * aggregation instead of after. + * only once per group). We also can't do this if there are any nonempty + * grouping sets; moving such a clause into WHERE would potentially change + * the results, if any referenced column isn't present in all the grouping + * sets. (If there are only empty grouping sets, then the HAVING clause + * must be degenerate as discussed below.) + * + * Also, it may be that the clause is so expensive to execute that we're + * better off doing it only once per group, despite the loss of + * selectivity. This is hard to estimate short of doing the entire + * planning process twice, so we use a heuristic: clauses containing + * subplans are left in HAVING. Otherwise, we move or copy the HAVING + * clause into WHERE, in hopes of eliminating tuples before aggregation + * instead of after. * * If the query has explicit grouping then we can simply move such a * clause into WHERE; any group that fails the clause will not be in the @@ -679,7 +685,8 @@ subquery_planner(PlannerGlobal *glob, Query *parse, { Node *havingclause = (Node *) lfirst(l); - if (contain_agg_clause(havingclause) || + if ((parse->groupClause && parse->groupingSets) || + contain_agg_clause(havingclause) || contain_volatile_functions(havingclause) || contain_subplans(havingclause)) { diff --git a/src/test/regress/expected/groupingsets.out b/src/test/regress/expected/groupingsets.out index b0b8c4b..42425a5 100644 --- a/src/test/regress/expected/groupingsets.out +++ b/src/test/regress/expected/groupingsets.out @@ -607,6 +607,60 @@ having exists (select 1 from onek b where sum(distinct a.four) = b.four); 9 | 3 (25 rows) +-- Pushdown of HAVING issues +select a,count(*) from gstest2 group by rollup(a) order by a; + a | count +---+------- + 1 | 8 + 2 | 1 + | 9 +(3 rows) + +select a,count(*) from gstest2 group by rollup(a) having a is distinct from 1 order by a; + a | count +---+------- + 2 | 1 + | 9 +(2 rows) + +explain (costs off) + select a,count(*) from gstest2 group by rollup(a) having a is distinct from 1 order by a; + QUERY PLAN +---------------------------------- + GroupAggregate + Group Key: a + Group Key: () + Filter: (a IS DISTINCT FROM 1) + -> Sort + Sort Key: a + -> Seq Scan on gstest2 +(7 rows) + +select v.c, (select count(*) from gstest2 group by () having v.c) + from (values (false),(true)) v(c) order by v.c; + c | count +---+------- + f | + t | 9 +(2 rows) + +explain (costs off) + select v.c, (select count(*) from gstest2 group by () having v.c) + from (values (false),(true)) v(c) order by v.c; + QUERY PLAN +----------------------------------------------------------- + Sort + Sort Key: "*VALUES*".column1 + -> Values Scan on "*VALUES*" + SubPlan 1 + -> Aggregate + Group Key: () + Filter: "*VALUES*".column1 + -> Result + One-Time Filter: "*VALUES*".column1 + -> Seq Scan on gstest2 +(10 rows) + -- HAVING with GROUPING queries select ten, grouping(ten) from onek group by grouping sets(ten) having grouping(ten) >= 0 diff --git a/src/test/regress/sql/groupingsets.sql b/src/test/regress/sql/groupingsets.sql index bff85d0..0b3b1c4 100644 --- a/src/test/regress/sql/groupingsets.sql +++ b/src/test/regress/sql/groupingsets.sql @@ -183,6 +183,18 @@ select ten, sum(distinct four) from onek a group by grouping sets((ten,four),(ten)) having exists (select 1 from onek b where sum(distinct a.four) = b.four); +-- Pushdown of HAVING issues +select a,count(*) from gstest2 group by rollup(a) order by a; +select a,count(*) from gstest2 group by rollup(a) having a is distinct from 1 order by a; +explain (costs off) + select a,count(*) from gstest2 group by rollup(a) having a is distinct from 1 order by a; + +select v.c, (select count(*) from gstest2 group by () having v.c) + from (values (false),(true)) v(c) order by v.c; +explain (costs off) + select v.c, (select count(*) from gstest2 group by () having v.c) + from (values (false),(true)) v(c) order by v.c; + -- HAVING with GROUPING queries select ten, grouping(ten) from onek group by grouping sets(ten) having grouping(ten) >= 0 --=-=-= Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --=-=-=--