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 1ueBbI-0069GF-NA for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 11:56:05 +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 1ueBbH-0076N3-Hh for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 11:56:03 +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 1ueBbH-0076Ms-8D for pgsql-hackers@lists.postgresql.org; Tue, 22 Jul 2025 11:56:03 +0000 Received: from mail-pg1-x52b.google.com ([2607:f8b0:4864:20::52b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ueBbF-000CKa-1m for pgsql-hackers@postgresql.org; Tue, 22 Jul 2025 11:56:02 +0000 Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-b3bcb168fd5so4511155a12.3 for ; Tue, 22 Jul 2025 04:56:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753185360; x=1753790160; darn=postgresql.org; h=in-reply-to:references:subject:cc:to:from:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=qtcZ6U9DR9dH0V7EC15/Ow82YNY4EEiokDMlKslNfro=; b=Ecvwy/lHmz3zx9rPsva3hHTREclEhLz/3d72454LwDzv6OQpRkHiSQxJZBTD7wT1FR eQfUDvPYB6hdlmfyoInb83TJkS4rvwcsgweZrPJjmoT14IVhhuOcGryo7dVvP65ReXRh Iu/b8lbbpGBlCLDZM3a6ezlA7yVkib6p4E8fsc9mgjv07EiIKSXucNnNEr1conBqXrfC GRUPCmb8TmAFRtdmyvPLsMJ6Z+p013VpKM26A6F23d41GSvbEJGm7nuuJ6sVlNFUiNq7 7OtetMPVzXib0Cw+QamBnRGrgsvyOwq2JfI8Bg9glIIkfDD412MCECrQ3zyf2REE8rMl wTug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753185360; x=1753790160; h=in-reply-to:references:subject:cc:to:from:message-id:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=qtcZ6U9DR9dH0V7EC15/Ow82YNY4EEiokDMlKslNfro=; b=fz6hK3Rl0Kdjonm/woAWC5rO1p630GesrffOAnzC9PlHkfTcHtTKTFsg7chy9c02tZ dVwi6HZVw5OWaHumrAI3oi9LGQQG3mc0KLGFXmh7pQihStADUkbdEPOO+torNCXBD0GR bX6USHTTHp2pUhe0iJ0JqyUr5udDZs4btkiElF08HydVZovKDh1hVZuakfLfaNRFRj9l Jjt3yVtjBtSHSHMuz6vnfS98QFyusTqd6tIvUpXCuk5LVSy/ww1MAp+a/OHl8wiUGzzD EUU0dwp2OzCfGEGsDZ8+jYag3fydRr4lbsjCIQRVrOkY54y5WAHjDUVpsben8q3GTwvS DSVA== X-Forwarded-Encrypted: i=1; AJvYcCV122/IKEl1BGteY308BiNIkZaEUHrMSzTwSl4Jj99Dukzxse1pKhGkDU8WT8vFfAPyVDzk5SxH6o2hV10J@postgresql.org X-Gm-Message-State: AOJu0YxRspIyWYkd4TK/pOaCfAfk5P52tQzrsw+RIUI5mQNMUEjG9Zng X27yCXotwk6Oa6p8DoSYquSvpvkLAf/pZKk+2G1Hu7skDBr8oKxhSCisgUJy+g== X-Gm-Gg: ASbGncs/XdjxFRFLNm8nIRpLkRFmrQH9szaN+6f7I2SX9iSR10q4Ay8ta46zFJ/HEhN Zsje6iSeWHGmPz1hthPsWJ/47AB9YIsnqV6vCIpqwDlqzFFuMnAjzfC7Guz+VnYdg10hqcPxDE1 QgFAC1HPpto1tZDMqN39VG6myEYhW+GktiMxkfRR9dVytDlC40IhSoX1H1X0EFV+XXldc2gNvOb 7HbvHltNVGQSnJXJiwU1RnbfqfyI2DZX7qzkMcft+T4JnG3FETJAxHZcgy30spAT5gZpX1U6a1V MiF3Sbjw0dxxR2f/iDRTPYugsHECQMr64Hko7kzBNfbwV1juqBVeELmIglrWHtoqo/FIZQkDUgL a7cEw6PTo+n/07gDLG8xWyG8mIEGQS96mxRPCGPqSLG4U8Q== X-Google-Smtp-Source: AGHT+IEMssfT40RXXVTSWLuu8LTyGV9LoemhURO5iFOg8MAkzloEs44h0Gb9VDhC1iUs/2i8Q/NALw== X-Received: by 2002:a17:90b:3909:b0:311:e305:4e97 with SMTP id 98e67ed59e1d1-31c9f4a6b52mr31525444a91.19.1753185359783; Tue, 22 Jul 2025 04:55:59 -0700 (PDT) Received: from localhost ([2804:14d:328a:a59c:c4f9:8c97:bf93:1b27]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-31cc3f474a0sm7824238a91.44.2025.07.22.04.55.58 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 22 Jul 2025 04:55:59 -0700 (PDT) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 22 Jul 2025 08:55:56 -0300 Message-Id: From: "Matheus Alcantara" To: "Vik Fearing" , "Pg Hackers" Cc: "Peter Eisentraut" Subject: Re: Proposal: QUALIFY clause X-Mailer: aerc 0.20.1 References: <6c998e4f-f6f2-43c2-8b67-cfff360ef241@postgresfriends.org> In-Reply-To: <6c998e4f-f6f2-43c2-8b67-cfff360ef241@postgresfriends.org> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon Jul 21, 2025 at 7:11 PM -03, Vik Fearing wrote: > That is my preferred grammar, thank you.=C2=A0=20 > 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 () =3D ? > > > can be rewritten as: > > > SELECT a, b, c > FROM ( > =C2=A0 =C2=A0 SELECT a, b, c, wf() OVER () =3D ? AS qc > =C2=A0 =C2=A0 FROM tab > ) AS q > WHERE qc > > > and then let the optimizer take over.=C2=A0 The standard does this kind o= f=20 > thing all over the place; I don't know what the postgres project's=20 > 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 =3D 1 and SELECT a, b, c wf() OVER () FROM tab QUALIFY wf() OVER () =3D 1 When using the "QUALIFY d =3D 1" form, I currently rewrite the expression as "wf() OVER () =3D 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