Re: [BUGS] Improper const-evaluation of HAVING with grouping sets and subquery pullup - Mailing list pgsql-bugs

From Andrew Gierth
Subject Re: [BUGS] Improper const-evaluation of HAVING with grouping sets and subquery pullup
Date
Msg-id 87tvyyjg2s.fsf@news-spur.riddles.org.uk
Whole thread Raw
In response to Re: [BUGS] Improper const-evaluation of HAVING with grouping sets andsubquery pullup  (Heikki Linnakangas <hlinnaka@iki.fi>)
Responses Re: [BUGS] Improper const-evaluation of HAVING with grouping sets and subquery pullup  (Andrew Gierth <andrew@tao11.riddles.org.uk>)
Re: [BUGS] Improper const-evaluation of HAVING with grouping sets and subquery pullup  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-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
newalias3Heikki>FROM generate_series(1,3) gHeikki> GROUP BY newalias1, ROLLUP(newalias3);Heikki>  newalias1 |
newalias3Heikki>-----------+-----------Heikki>          1 |         1Heikki>          3 |         3Heikki>          2 |
       2Heikki>          2 |         2Heikki>          3 |         3Heikki>          1 |         1Heikki> (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 expectedHeikki> 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

pgsql-bugs by date:

Previous
From: Heikki Linnakangas
Date:
Subject: Re: [BUGS] Improper const-evaluation of HAVING with grouping sets andsubquery pullup
Next
From: Andrew Gierth
Date:
Subject: Re: [BUGS] Improper const-evaluation of HAVING with grouping sets and subquery pullup