pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedWeird behaviour of ROLLUP/GROUPING
7+ messages / 3 participants
[nested] [flat]
Commits from this thread:
2019-01-17 e74d8c5085 409230a721 624046abe2 91448e7dcc 174fab9934 d16d453870 Postpone aggregate checks until after collation is assigned.
* Weird behaviour of ROLLUP/GROUPING
@ 2019-01-16 12:51 Guillaume Lelarge <guillaume@lelarge.info>
0 siblings, 2 replies; 7+ messages in thread
From: Guillaume Lelarge @ 2019-01-16 12:51 UTC (permalink / raw)
To: pgsql-general
Hi,
One of my customers found something quite weird on his 9.6 cluster. Here is
a quick demo showing the issue:
-- quick demo table
CREATE TABLE t1 (a integer, b timestamp, c integer);
-- a working query
SELECT
CASE grouping(a) WHEN 1 THEN 'some text' ELSE a::text END AS new_a,
CASE WHEN grouping(to_char(b, 'MMYYYY')) = 1
THEN 'some date'
ELSE to_char(b, 'MMYYYY') END AS new_b,
sum(c)
FROM t1
GROUP BY ROLLUP(a, to_char(b,'MMYYYY'));
-- the non-working query
SELECT
CASE grouping(a) WHEN 1 THEN 'some text' ELSE a::text END AS new_a,
CASE grouping(to_char(b, 'MMYYYY'))
WHEN 1 THEN 'some date'
ELSE to_char(b, 'MMYYYY') END AS new_b,
sum(c)
FROM t1
GROUP BY ROLLUP(a, to_char(b,'MMYYYY'));
ERROR: arguments to GROUPING must be grouping expressions of the
associated query level
LINE 3: CASE grouping(to_char(b, 'MMYYYY')) WHEN 1 THEN 'some date' ...
^
AFAICT, both queries should behave the same, though their actual behaviours
are quite opposite. Working fine for the first, erroring out on the second.
Does anyone has any idea what's going on here?
BTW, it shows the same issue on 11.1 (actually HEAD on REL_11_STABLE).
Thanks.
Regards.
--
Guillaume.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Weird behaviour of ROLLUP/GROUPING
@ 2019-01-16 13:15 Pavel Stehule <pavel.stehule@gmail.com>
parent: Guillaume Lelarge <guillaume@lelarge.info>
1 sibling, 0 replies; 7+ messages in thread
From: Pavel Stehule @ 2019-01-16 13:15 UTC (permalink / raw)
To: Guillaume Lelarge <guillaume@lelarge.info>; +Cc: pgsql-general
st 16. 1. 2019 v 13:51 odesílatel Guillaume Lelarge <guillaume@lelarge.info>
napsal:
> Hi,
>
> One of my customers found something quite weird on his 9.6 cluster. Here
> is a quick demo showing the issue:
>
> -- quick demo table
> CREATE TABLE t1 (a integer, b timestamp, c integer);
>
> -- a working query
> SELECT
> CASE grouping(a) WHEN 1 THEN 'some text' ELSE a::text END AS new_a,
> CASE WHEN grouping(to_char(b, 'MMYYYY')) = 1
> THEN 'some date'
> ELSE to_char(b, 'MMYYYY') END AS new_b,
> sum(c)
> FROM t1
> GROUP BY ROLLUP(a, to_char(b,'MMYYYY'));
>
> -- the non-working query
> SELECT
> CASE grouping(a) WHEN 1 THEN 'some text' ELSE a::text END AS new_a,
> CASE grouping(to_char(b, 'MMYYYY'))
> WHEN 1 THEN 'some date'
> ELSE to_char(b, 'MMYYYY') END AS new_b,
> sum(c)
> FROM t1
> GROUP BY ROLLUP(a, to_char(b,'MMYYYY'));
> ERROR: arguments to GROUPING must be grouping expressions of the
> associated query level
> LINE 3: CASE grouping(to_char(b, 'MMYYYY')) WHEN 1 THEN 'some date' ...
> ^
>
> AFAICT, both queries should behave the same, though their actual
> behaviours are quite opposite. Working fine for the first, erroring out on
> the second.
>
> Does anyone has any idea what's going on here?
>
> BTW, it shows the same issue on 11.1 (actually HEAD on REL_11_STABLE).
>
looks like PostgreSQL bug - it cannot to work with subqueries correctly
Pavel
>
> Thanks.
>
> Regards.
>
>
> --
> Guillaume.
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Weird behaviour of ROLLUP/GROUPING
@ 2019-01-16 15:55 Andrew Gierth <andrew@tao11.riddles.org.uk>
parent: Guillaume Lelarge <guillaume@lelarge.info>
1 sibling, 1 reply; 7+ messages in thread
From: Andrew Gierth @ 2019-01-16 15:55 UTC (permalink / raw)
To: Guillaume Lelarge <guillaume@lelarge.info>; +Cc: pgsql-general
>>>>> "Guillaume" == Guillaume Lelarge <guillaume@lelarge.info> writes:
Guillaume> CASE grouping(to_char(b, 'MMYYYY'))
Guillaume> ERROR: arguments to GROUPING must be grouping expressions of the
Guillaume> associated query level
Guillaume> LINE 3: CASE grouping(to_char(b, 'MMYYYY')) WHEN 1 THEN 'some date' ...
Guillaume> AFAICT, both queries should behave the same, though their
Guillaume> actual behaviours are quite opposite. Working fine for the
Guillaume> first, erroring out on the second.
Guillaume> Does anyone has any idea what's going on here?
Not yet. But I will find out, since it seems to be a bug.
--
Andrew (irc:RhodiumToad)
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Weird behaviour of ROLLUP/GROUPING
@ 2019-01-16 16:40 Andrew Gierth <andrew@tao11.riddles.org.uk>
parent: Andrew Gierth <andrew@tao11.riddles.org.uk>
0 siblings, 1 reply; 7+ messages in thread
From: Andrew Gierth @ 2019-01-16 16:40 UTC (permalink / raw)
To: Guillaume Lelarge <guillaume@lelarge.info>; +Cc: pgsql-general
>>>>> "Andrew" == Andrew Gierth <andrew@tao11.riddles.org.uk> writes:
>>>>> "Guillaume" == Guillaume Lelarge <guillaume@lelarge.info> writes:
Guillaume> CASE grouping(to_char(b, 'MMYYYY'))
Guillaume> ERROR: arguments to GROUPING must be grouping expressions of the
Guillaume> associated query level
Guillaume> LINE 3: CASE grouping(to_char(b, 'MMYYYY')) WHEN 1 THEN 'some date' ...
Guillaume> AFAICT, both queries should behave the same, though their
Guillaume> actual behaviours are quite opposite. Working fine for the
Guillaume> first, erroring out on the second.
Guillaume> Does anyone has any idea what's going on here?
Andrew> Not yet. But I will find out, since it seems to be a bug.
It is definitely a bug, to do with assignment of collations. It
specifically happens when you use GROUPING which contains any
subexpression of a collatable type, inside a CASE expr WHEN clause,
since that assigns collations to the expression much earlier in parsing
than the rest of the query, so the code that validates GROUPING ends up
trying to compare an expression which has had collations assigned to it
to one which has not, and so it thinks they differ.
I will see about fixing this, somehow.
--
Andrew (irc:RhodiumToad)
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Weird behaviour of ROLLUP/GROUPING
@ 2019-01-16 17:05 Guillaume Lelarge <guillaume@lelarge.info>
parent: Andrew Gierth <andrew@tao11.riddles.org.uk>
0 siblings, 1 reply; 7+ messages in thread
From: Guillaume Lelarge @ 2019-01-16 17:05 UTC (permalink / raw)
To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: pgsql-general
Le mer. 16 janv. 2019 à 17:40, Andrew Gierth <andrew@tao11.riddles.org.uk>
a écrit :
> >>>>> "Andrew" == Andrew Gierth <andrew@tao11.riddles.org.uk> writes:
> >>>>> "Guillaume" == Guillaume Lelarge <guillaume@lelarge.info> writes:
>
> Guillaume> CASE grouping(to_char(b, 'MMYYYY'))
>
> Guillaume> ERROR: arguments to GROUPING must be grouping expressions of
> the
> Guillaume> associated query level
> Guillaume> LINE 3: CASE grouping(to_char(b, 'MMYYYY')) WHEN 1 THEN 'some
> date' ...
>
> Guillaume> AFAICT, both queries should behave the same, though their
> Guillaume> actual behaviours are quite opposite. Working fine for the
> Guillaume> first, erroring out on the second.
>
> Guillaume> Does anyone has any idea what's going on here?
>
> Andrew> Not yet. But I will find out, since it seems to be a bug.
>
> It is definitely a bug, to do with assignment of collations. It
> specifically happens when you use GROUPING which contains any
> subexpression of a collatable type, inside a CASE expr WHEN clause,
> since that assigns collations to the expression much earlier in parsing
> than the rest of the query, so the code that validates GROUPING ends up
> trying to compare an expression which has had collations assigned to it
> to one which has not, and so it thinks they differ.
>
> I will see about fixing this, somehow.
>
>
Thanks a lot.
--
Guillaume.
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Weird behaviour of ROLLUP/GROUPING
@ 2019-01-17 07:27 Andrew Gierth <andrew@tao11.riddles.org.uk>
parent: Guillaume Lelarge <guillaume@lelarge.info>
0 siblings, 1 reply; 7+ messages in thread
From: Andrew Gierth @ 2019-01-17 07:27 UTC (permalink / raw)
To: Guillaume Lelarge <guillaume@lelarge.info>; +Cc: pgsql-general
>>>>> "Guillaume" == Guillaume Lelarge <guillaume@lelarge.info> writes:
>> I will see about fixing this, somehow.
Guillaume> Thanks a lot.
I've committed a fix (to all supported branches, since this bug actually
precedes the addition of GROUPING SETS and can be triggered with a
simple GROUP BY if you try hard enough). The regression test says it
works now, but it'd be good if you could try it again on REL_11_STABLE
(at commit e74d8c5085 or later) to check that it fixes your case.
--
Andrew (irc:RhodiumToad)
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Weird behaviour of ROLLUP/GROUPING
@ 2019-01-17 08:23 Guillaume Lelarge <guillaume@lelarge.info>
parent: Andrew Gierth <andrew@tao11.riddles.org.uk>
0 siblings, 0 replies; 7+ messages in thread
From: Guillaume Lelarge @ 2019-01-17 08:23 UTC (permalink / raw)
To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: pgsql-general
Le jeu. 17 janv. 2019 à 08:27, Andrew Gierth <andrew@tao11.riddles.org.uk>
a écrit :
> >>>>> "Guillaume" == Guillaume Lelarge <guillaume@lelarge.info> writes:
>
> >> I will see about fixing this, somehow.
>
> Guillaume> Thanks a lot.
>
> I've committed a fix (to all supported branches, since this bug actually
> precedes the addition of GROUPING SETS and can be triggered with a
> simple GROUP BY if you try hard enough). The regression test says it
> works now, but it'd be good if you could try it again on REL_11_STABLE
> (at commit e74d8c5085 or later) to check that it fixes your case.
>
>
I checked on REL9_6_STABLE and REL_11_STABLE, and it works great. Thank a
lot for the quick fix!
--
Guillaume.
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2019-01-17 08:23 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-01-16 12:51 Weird behaviour of ROLLUP/GROUPING Guillaume Lelarge <guillaume@lelarge.info>
2019-01-16 13:15 ` Pavel Stehule <pavel.stehule@gmail.com>
2019-01-16 15:55 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2019-01-16 16:40 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2019-01-16 17:05 ` Guillaume Lelarge <guillaume@lelarge.info>
2019-01-17 07:27 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2019-01-17 08:23 ` Guillaume Lelarge <guillaume@lelarge.info>
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