Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e4O0Z-00062b-4a for pgsql-bugs@arkaria.postgresql.org; Tue, 17 Oct 2017 09:17:55 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e4O0Y-0003Aw-7e for pgsql-bugs@arkaria.postgresql.org; Tue, 17 Oct 2017 09:17:54 +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_2) (envelope-from ) id 1e4Nzo-0001ty-Qh for pgsql-bugs@postgresql.org; Tue, 17 Oct 2017 09:17:08 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e4Nzl-0004u0-3C for pgsql-bugs@postgresql.org; Tue, 17 Oct 2017 09:17:07 +0000 Received: from [192.168.127.1] (port=47818 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1e4Nzg-000ISb-I3; Tue, 17 Oct 2017 09:17:00 +0000 Received: from [127.0.0.1] (port=50909 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1e4Nzf-000Nc0-C9; Tue, 17 Oct 2017 09:16:59 +0000 From: Andrew Gierth To: Heikki Linnakangas Cc: Tom Lane , Pg Bugs Subject: Re: Improper const-evaluation of HAVING with grouping sets and subquery pullup In-Reply-To: (Heikki Linnakangas's message of "Tue, 17 Oct 2017 10:44:23 +0300") Message-ID: <87tvyyjg2s.fsf@news-spur.riddles.org.uk> References: <7dbdcf5c-b5a6-ef89-4958-da212fe10176@iki.fi> <1197.1507820787@sss.pgh.pa.us> <87efq3lq93.fsf@news-spur.riddles.org.uk> <87a80rli2w.fsf@news-spur.riddles.org.uk> <32319.1508163874@sss.pgh.pa.us> <8760bfku2u.fsf@news-spur.riddles.org.uk> <2431.1508166918@sss.pgh.pa.us> <871sm3ktl7.fsf@news-spur.riddles.org.uk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Tue, 17 Oct 2017 10:16:58 +0100 MIME-Version: 1.0 Content-Type: text/plain 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 >>>>> "Heikki" == Heikki Linnakangas writes: Heikki> Here's another interesting case, without any subqueries: Heikki> postgres=# SELECT g as newalias1, g as newalias3 Heikki> FROM generate_series(1,3) g Heikki> GROUP BY newalias1, ROLLUP(newalias3); Heikki> newalias1 | newalias3 Heikki> -----------+----------- Heikki> 1 | 1 Heikki> 3 | 3 Heikki> 2 | 2 Heikki> 2 | 2 Heikki> 3 | 3 Heikki> 1 | 1 Heikki> (6 rows) Heikki> Why are there no "summary" rows with NULLs, despite the ROLLUP? To my knowledge this is the correct result. (Though neither version of the query is legal per the SQL spec; allowing expressions and aliases in GROUP BY are nonstandard extensions.) Here's why it happens: after substituting for the aliases, you have GROUP BY g, rollup(g) which is equivalent to GROUP BY GROUPING SETS ((g,g), (g)) which is equivalent to GROUP BY GROUPING SETS ((g), (g)) because duplicate terms within a single grouping set are redundant just as they are in GROUP BY. Heikki> If you replace one of the g's with (g+0), you get the expected Heikki> result: Well, in this case the terms in the grouping set are no longer duplicate; the expansion becomes GROUP BY GROUPING SETS ((g,(g+0)), (g)) and therefore the (g+0) expression becomes null for one of the resulting sets. -- Andrew (irc:RhodiumToad) -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs