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 1ueEYI-006hzb-Il for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 15:05:11 +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 1ueEXJ-00921R-KO for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 15:04:09 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ueEXJ-00921I-9s for pgsql-hackers@lists.postgresql.org; Tue, 22 Jul 2025 15:04:09 +0000 Received: from siberian.tulip.relay.mailchannels.net ([23.83.218.246]) by makus.postgresql.org with smtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1ueEXH-000DqO-12 for pgsql-hackers@postgresql.org; Tue, 22 Jul 2025 15:04:08 +0000 X-Sender-Id: dreamhost|x-authsender|nico@cryptonector.com Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 3547F7834F8; Tue, 22 Jul 2025 15:04:05 +0000 (UTC) Received: from pdx1-sub0-mail-a288.dreamhost.com (100-120-62-21.trex-nlb.outbound.svc.cluster.local [100.120.62.21]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 87481784536; Tue, 22 Jul 2025 15:04:04 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1753196644; a=rsa-sha256; cv=none; b=hHM+v5FOiiU/8+fRY/eEPW9HBL74NdUDVghCC+1H+RnZ5wsuEn1znbSgBPcqxSwLU6OBuw TDcHcYdVULakqhRQyGzRSXUcQpdRJkcfh4PpuTP4J2CT5XE5umpztaBNrdmknhw7ShRBR9 rAlXLc/AFNZAVQqI0gceFuiR9soAFlIuhubBCXFAUWp+QJO7zHfVsQVWG+KUECOgH8kbNC LAUrMYgwTJk8CZqwpDs42YgIxAhafIGoQ/Z6VnXs8XMorKcM/JCmh2Dxr5NXLe72xl39Ye zoqdT7nNTd6Rl+uRY8x9jLOfx+2zCUJ7PbXDPVtdYtP6opCfs0bOLSneNDKLfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1753196644; 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:dkim-signature; bh=42E/BhrqMWn5EFEZmf5prHphe7mTHCC65lGm6vQMwdY=; b=lrc2MVfNqgcZETz+8JTEa5uOfcp7pCn0PZo47yY9/nulLqkY5c6xxLrwXtFaycUimSReo5 UA6pQrYM7UZG6o5AfSPjoXnGISx2lr70z56xLHqbqvuEsgdzxBlr6BhKt7wPdYPmm2JO0x OKKPR1onDPVeum+boOilc5YPSdtQbgIrDA+qwHVuaCGqsJq9CYkMgbGAVkGZbQ2YHhiWr2 5hENAdYg/bfIrGjg0wE5TQkgSDQycK6yp2DqUiApRiEYs2ucT2p5VywhF6zkQM8Y9HNeS9 4LK/8ipAm2acvuJpTgmLFwZx5Vw7+eMLI6d+gTFzp9m2SJaYNOZxqM8vBnMu4A== ARC-Authentication-Results: i=1; rspamd-7df5bc8bc6-v7dzt; auth=pass smtp.auth=dreamhost smtp.mailfrom=nico@cryptonector.com X-Sender-Id: dreamhost|x-authsender|nico@cryptonector.com X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|nico@cryptonector.com X-MailChannels-Auth-Id: dreamhost X-Whistle-Fumbling: 5236a35d2372b761_1753196644815_3653112734 X-MC-Loop-Signature: 1753196644815:1106843955 X-MC-Ingress-Time: 1753196644815 Received: from pdx1-sub0-mail-a288.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.120.62.21 (trex/7.1.3); Tue, 22 Jul 2025 15:04:04 +0000 Received: from ubby (syn-075-081-095-064.res.spectrum.com [75.81.95.64]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: nico@cryptonector.com) by pdx1-sub0-mail-a288.dreamhost.com (Postfix) with ESMTPSA id 4bmgX34wQ5z2g; Tue, 22 Jul 2025 08:04:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cryptonector.com; s=dreamhost; t=1753196644; bh=42E/BhrqMWn5EFEZmf5prHphe7mTHCC65lGm6vQMwdY=; h=Date:From:To:Cc:Subject:Content-Type:Content-Transfer-Encoding; b=k0+nKj711LV6FZv0R873gH6r90cszmCj73DURwCzCn/oBgcT+SCWDU4/eaJqK/tV4 ZFrl1izlrm02R8stbP7wCxTu1iagXNXc3T5n25JpDLPsKVry7U9ww7F4/9pfsZA0o1 1M1yGmx+fv8l41OnXNysY7hXGEi7V1B0pV+jTOrupxSdswJt+SgCfOZH2O8F89MuTE 4qWP+F2RMWwr0sbc77vuvXLdiyg3kxvOx0RS126tNIBaZsTaXItUAe4T4ZQ2v5CFgk YJa/E3K7/aWMZDZxra+4JYfu79ez6pXR6aH5vp1QmdQ9fdrMPMFX3lVZp06HaGTWzy uYNLGri030tFA== Date: Tue, 22 Jul 2025 10:04:01 -0500 From: Nico Williams To: Merlin Moncure Cc: Pavel Stehule , Vik Fearing , Matheus Alcantara , Pg Hackers , Peter Eisentraut Subject: Re: Proposal: QUALIFY clause Message-ID: References: <6c998e4f-f6f2-43c2-8b67-cfff360ef241@postgresfriends.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Jul 21, 2025 at 11:02:36PM -0600, Merlin Moncure wrote: > On Mon, Jul 21, 2025 at 10:08=E2=80=AFPM Nico Williams > 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 t= o > > the grouping. >=20 > 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 HAVI= NG > lag(1) OVER() IS NULL; >=20 > 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 () WHERE col =3D 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 --=20