From: Nico Williams <nico@cryptonector.com>
To: Merlin Moncure <mmoncure@gmail.com>
Cc: Pavel Stehule <pavel.stehule@gmail.com>
Cc: Vik Fearing <vik@postgresfriends.org>
Cc: Matheus Alcantara <matheusssilv97@gmail.com>
Cc: Pg Hackers <pgsql-hackers@postgresql.org>
Cc: Peter Eisentraut <peter@eisentraut.org>
Subject: Re: Proposal: QUALIFY clause
Date: Tue, 22 Jul 2025 10:04:01 -0500
Message-ID: <aH+oYUO0NrAvTd9P@ubby> (raw)
In-Reply-To: <CAHyXU0zxy1-W4YH9KhtBr07eWN-8xJJLUPdpoHNQ0t16Ve7F-w@mail.gmail.com>
References: <CAFY6G8euMWRjTEdRV9M=RL2_EJNDm5UNeHat9U1FGGwzfjGSfw@mail.gmail.com>
<a042f530-90a4-46fc-8595-43053fcfc06e@postgresfriends.org>
<DBI1WKCGAPN4.321XNB6R2YKPT@gmail.com>
<6c998e4f-f6f2-43c2-8b67-cfff360ef241@postgresfriends.org>
<CAFj8pRCNZ8vLca8XK8s-Xha7PDmfw0r_JXnhb8Z87On4O=qctQ@mail.gmail.com>
<CAHyXU0wnmAC2aSJXE8mWarydvQ_gY9OEFrpRNZ9OQ3De3vXZ2A@mail.gmail.com>
<aH8OuSXcoZTL0Vg6@ubby>
<CAHyXU0zxy1-W4YH9KhtBr07eWN-8xJJLUPdpoHNQ0t16Ve7F-w@mail.gmail.com>
On Mon, Jul 21, 2025 at 11:02:36PM -0600, Merlin Moncure wrote:
> On Mon, Jul 21, 2025 at 10:08 PM Nico Williams <nico@cryptonector.com>
> wrote:
> > I would have a HAVING clause that comes _before_ GROUP BY apply to
> > window functions and a second one that comes _after_ GROUP BY apply to
> > the grouping.
>
> I don't know...consider:
> #1 SELECT lag(1) OVER(), max(v) FROM (SELECT 1 AS v) q HAVING true ;
> ...
> #2 SELECT lag(1) OVER(), max(v) FROM (SELECT 1 AS v) q HAVING true HAVING
> lag(1) OVER() IS NULL;
>
> What does the HAVING clause apply to in #1? I think you might be in
> trouble with the standard here. 2nd clause doesn't feel right in #2. The
> basic problem is that HAVING does more than just 'syntax sugar subquery /
> WHERE' and it just can't be hijacked to do something else IMO.
#2 would be a syntax error because the second HAVING did not come after
a GROUP BY. #1 would not be a syntax error only because of the use of
window functions before the HAVING.
> Syntax simplifying
> SELECT * FROM (<window function query>) WHERE col = x
Yes. I'd rather that than QUALIFY. QUALIFY only makes sense because so
many other RDBMSes have it and it's likely to get standardized.
Nico
--
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-hackers@postgresql.org
Cc: nico@cryptonector.com, mmoncure@gmail.com, pavel.stehule@gmail.com, vik@postgresfriends.org, matheusssilv97@gmail.com, peter@eisentraut.org
Subject: Re: Proposal: QUALIFY clause
In-Reply-To: <aH+oYUO0NrAvTd9P@ubby>
References: <CAFY6G8euMWRjTEdRV9M=RL2_EJNDm5UNeHat9U1FGGwzfjGSfw@mail.gmail.com> <a042f530-90a4-46fc-8595-43053fcfc06e@postgresfriends.org> <DBI1WKCGAPN4.321XNB6R2YKPT@gmail.com> <6c998e4f-f6f2-43c2-8b67-cfff360ef241@postgresfriends.org> <CAFj8pRCNZ8vLca8XK8s-Xha7PDmfw0r_JXnhb8Z87On4O=qctQ@mail.gmail.com> <CAHyXU0wnmAC2aSJXE8mWarydvQ_gY9OEFrpRNZ9OQ3De3vXZ2A@mail.gmail.com> <aH8OuSXcoZTL0Vg6@ubby> <CAHyXU0zxy1-W4YH9KhtBr07eWN-8xJJLUPdpoHNQ0t16Ve7F-w@mail.gmail.com> <aH+oYUO0NrAvTd9P@ubby>
* 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