Re: Proposal: QUALIFY clause

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Vik Fearing <vik(at)postgresfriends(dot)org>
Cc: Matheus Alcantara <matheusssilv97(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>, Peter Eisentraut <peter(at)eisentraut(dot)org>
Subject: Re: Proposal: QUALIFY clause
Date: 2025-07-21 22:56:15
Message-ID: 661034.1753138575@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Vik Fearing <vik(at)postgresfriends(dot)org> writes:
> That is my preferred grammar, thank you. 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

That answers another question I was going to raise. Matheus's
opening example was

SELECT depname,
empno,
salary,
RANK() OVER (PARTITION BY depname ORDER BY salary DESC) AS rnk
FROM empsalary
QUALIFY rnk = 1;

which implies that the QUALIFY clause sees the SELECT output columns,
and hence that it can't use any values not emitted by the SELECT list.
Your transformation implies that it sees the same namespace as the
SELECT list, which seems like a much better and less confusing
definition to me.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2025-07-21 23:02:52 Re: Verify predefined LWLocks tranches have entries in wait_event_names.txt
Previous Message Michael Paquier 2025-07-21 22:54:37 Re: Support for 8-byte TOAST values (aka the TOAST infinite loop problem)