pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Pg Bugs <pgsql-bugs@postgresql.org>
Subject: Re: Improper const-evaluation of HAVING with grouping sets and subquery pullup
Date: Tue, 17 Oct 2017 10:16:58 +0100
Message-ID: <87tvyyjg2s.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <aefc657e-edb2-64d5-6df1-a0828f6e9104@iki.fi>
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>
	<aefc657e-edb2-64d5-6df1-a0828f6e9104@iki.fi>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

>>>>> "Heikki" == Heikki Linnakangas <hlinnaka@iki.fi> 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



view thread (21+ messages)  latest in thread

Message-ID: <87tvyyjg2s.fsf@news-spur.riddles.org.uk>
Permalink:  ../87tvyyjg2s.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87tvyyjg2s.fsf@news-spur.riddles.org.uk

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: andrew@tao11.riddles.org.uk, hlinnaka@iki.fi, tgl@sss.pgh.pa.us
  Subject: Re: Improper const-evaluation of HAVING with grouping sets and subquery pullup
  In-Reply-To: <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> <aefc657e-edb2-64d5-6df1-a0828f6e9104@iki.fi> <87tvyyjg2s.fsf@news-spur.riddles.org.uk>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox