pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Matheus Alcantara <matheusssilv97@gmail.com>
To: Vik Fearing <vik@postgresfriends.org>
To: Pg Hackers <pgsql-hackers@postgresql.org>
Cc: Peter Eisentraut <peter@eisentraut.org>
Subject: Re: Proposal: QUALIFY clause
Date: Tue, 22 Jul 2025 08:55:56 -0300
Message-ID: <DBIKCA3XAHPS.QIAI5362CP5M@gmail.com> (raw)
In-Reply-To: <6c998e4f-f6f2-43c2-8b67-cfff360ef241@postgresfriends.org>
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>

On Mon Jul 21, 2025 at 7:11 PM -03, Vik Fearing wrote:
> That is my preferred grammar, thank you.  
>
Thanks for confirming!

> I have not looked at the C code by this can be obtained with a syntax
> transformation. To wit:
> SELECT a, b, c
> FROM tab
> QUALIFY wf() OVER () = ?
>
>
> can be rewritten as:
>
>
> SELECT a, b, c
> FROM (
>      SELECT a, b, c, wf() OVER () = ? AS qc
>      FROM tab
> ) AS q
> WHERE qc
>
>
> and then let the optimizer take over.  The standard does this kind of 
> thing all over the place; I don't know what the postgres project's 
> position on doing things like this are.
>
The current patch supports the following syntaxes:
    SELECT a, b, c
    wf() OVER () as d
    FROM tab
    QUALIFY d = 1

and
    SELECT a, b, c
    wf() OVER ()
    FROM tab
    QUALIFY wf() OVER () = 1

When using the "QUALIFY d = 1" form, I currently rewrite the expression
as "wf() OVER () = 1" by searching the targetlist for the matching
alias, replacing the Var with the corresponding WindowFunc. Then I
append this clause to the topqual, which is later assigned to
WindowAggPath.plan->qual in create_one_window_path().

Besides this approach works I'm almost sure that this is not correct
because searching the window function on targetlist doesen't seems
correct to me. Tom also pointed out that this design could be confusing,
which reinforces the need to rethink it.

This transformation that you've suggested seems a better approach to
handle the QUALIFY clause to me as well. Unless anyone objects, I'll
prepare the next patch version based on that strategy.

Thanks very much for the comments!

--
Matheus Alcantara





view thread (41+ messages)  latest in thread

Message-ID: <DBIKCA3XAHPS.QIAI5362CP5M@gmail.com>
Permalink:  ../DBIKCA3XAHPS.QIAI5362CP5M@gmail.com/
Also on:    postgresql.org/message-id/DBIKCA3XAHPS.QIAI5362CP5M@gmail.com

 · 

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-hackers@postgresql.org
  Cc: matheusssilv97@gmail.com, vik@postgresfriends.org, peter@eisentraut.org
  Subject: Re: Proposal: QUALIFY clause
  In-Reply-To: <DBIKCA3XAHPS.QIAI5362CP5M@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> <DBIKCA3XAHPS.QIAI5362CP5M@gmail.com>

* 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