Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1udzQn-003LzC-D0 for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Jul 2025 22:56:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1udzQm-003V8o-Ai for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Jul 2025 22:56:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1udzQm-003V8g-1a for pgsql-hackers@lists.postgresql.org; Mon, 21 Jul 2025 22:56:24 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1udzQi-0007L0-2o for pgsql-hackers@postgresql.org; Mon, 21 Jul 2025 22:56:24 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 56LMuFW1661035; Mon, 21 Jul 2025 18:56:15 -0400 From: Tom Lane To: Vik Fearing cc: Matheus Alcantara , Pg Hackers , Peter Eisentraut Subject: Re: Proposal: QUALIFY clause In-reply-to: <6c998e4f-f6f2-43c2-8b67-cfff360ef241@postgresfriends.org> References: <6c998e4f-f6f2-43c2-8b67-cfff360ef241@postgresfriends.org> Comments: In-reply-to Vik Fearing message dated "Tue, 22 Jul 2025 00:11:59 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <661033.1753138575.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Mon, 21 Jul 2025 18:56:15 -0400 Message-ID: <661034.1753138575@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Vik Fearing 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