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 1udykw-0037JE-0r for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Jul 2025 22:13:10 +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 1udyjv-003FbF-VI for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Jul 2025 22:12:08 +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 1udyjv-003Fb7-KK for pgsql-hackers@lists.postgresql.org; Mon, 21 Jul 2025 22:12:07 +0000 Received: from relay6-d.mail.gandi.net ([2001:4b98:dc4:8::226]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1udyjs-00070o-0L for pgsql-hackers@postgresql.org; Mon, 21 Jul 2025 22:12:07 +0000 Received: by mail.gandi.net (Postfix) with ESMTPSA id 1D0F8431F5; Mon, 21 Jul 2025 22:11:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=postgresfriends.org; s=gm1; t=1753135920; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BDPxtMwBrTbnlXIlD2HjuOpNyh3X+VIKgMh4ot0GjCc=; b=M1X8gF4Yz1mQhQYMOBYbDjDYX7PK0zoLWwxG1KW6MhRE/T00z+CQftB29/Gp95GuoB++xm fiXLwBRo/d2o4UHEyub7LElwsGJEtyAaxDUglgdO918ldgL8asKhC0fxGOLYPFNp5LMm4i JpUUIy0dtMJLXRF2zhqWS02t3MOYZ1hQMSKJcoouGh5HZIF1xi0isq83EV23kk11bQ/o+j cAr+lCyBKkvIJfWvuSbzXJl+h63BLDL7uKy48Fzp0VUmF3ccvRXorknoKeMMC88oUUY9Rg VcP2k1BerwqXi/akp1Tc+tN6at1Ddk/VdokM5FNjNSqTmvXXBKQdWLHIUf7lVQ== Message-ID: <6c998e4f-f6f2-43c2-8b67-cfff360ef241@postgresfriends.org> Date: Tue, 22 Jul 2025 00:11:59 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Proposal: QUALIFY clause To: Matheus Alcantara , Pg Hackers Cc: Peter Eisentraut References: Content-Language: en-US From: Vik Fearing In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-GND-State: clean X-GND-Score: -100 X-GND-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdefgdejfedvfecutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfitefpfffkpdcuggftfghnshhusghstghrihgsvgenuceurghilhhouhhtmecufedtudenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhepkfffgggfuffvvehfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpeggihhkucfhvggrrhhinhhguceovhhikhesphhoshhtghhrvghsfhhrihgvnhgushdrohhrgheqnecuggftrfgrthhtvghrnhepuddtveefleegvddutdejhffhleehhfevheeilefhgeettdeuhfdtffegvdfgteffnecukfhppedvrgdtvdemkeegvdekmeduudgvtdemvdejtddumegrgegvudemgedvfhegmegrlegrkeemkegvieeknecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehinhgvthepvdgrtddvmeekgedvkeemudduvgdtmedvjedtudemrgegvgdumeegvdhfgeemrgelrgekmeekvgeikedphhgvlhhopeglkffrggeimedvrgdtvdemkeegvdekmeduudgvtdemvdejtddumegrgegvudemgedvfhegmegrlegrkeemkegvieekngdpmhgrihhlfhhrohhmpehvihhksehpohhsthhgrhgvshhfrhhivghnughsrdhorhhgpdhnsggprhgtphhtthhopeefpdhrtghpthhtohepmhgrthhhvghushhsshhilhhvleejsehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqhhgrtghkvghrshesphhoshhtghhrvghsqhhlrdhorhhgp dhrtghpthhtohepphgvthgvrhesvghishgvnhhtrhgruhhtrdhorhhg X-GND-Sasl: vik@postgresfriends.org List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 21/07/2025 23:29, Matheus Alcantara wrote: > On Mon Jul 21, 2025 at 5:23 PM -03, Vik Fearing wrote: >> On 21/07/2025 14:47, Matheus Alcantara wrote: >>> Hi all, >>> >>> I'm sending a proof-of-concept patch to add support for the QUALIFY >>> clause in Postgres. This feature allows filtering rows after window >>> functions are computed, using a syntax similar to the WHERE or HAVING >>> clauses. >> >> I took a very brief look at this, and I think your grammar is wrong. >> The QUALIFY clause should go after the WINDOW clause, just like >> FROM/WHERE and GROUP BY/HAVING. >> >> >> That is what I am proposing to the standards committee, and I already >> have some buy-in for that. >> > Thank you for the brief review and for the comments! > > I'm not sure if I fully understand but please see the new attached > version. 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 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. -- Vik Fearing