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 1ueI8D-007QYf-JM for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 18:54:30 +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 1ueI8C-00Bx5s-DY for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 18:54:28 +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 1ueI8C-00Bx5k-4G for pgsql-hackers@lists.postgresql.org; Tue, 22 Jul 2025 18:54:28 +0000 Received: from mail-pf1-x436.google.com ([2607:f8b0:4864:20::436]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ueI88-000HDx-36 for pgsql-hackers@postgresql.org; Tue, 22 Jul 2025 18:54:28 +0000 Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-74b54cead6cso3805286b3a.1 for ; Tue, 22 Jul 2025 11:54:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753210464; x=1753815264; darn=postgresql.org; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=1nZWzW+TOq8GrYtinVv5r/V14XpypmT11SAoRNIEn4U=; b=Yyr/SKMNbYOg9E2oe3FLyXu1+p8wDM8dKBJzVWP68aOdxKK5ZSqrI7VYZ+iXZuKCsv e8WynysDP+ayZum4J/y7n2ydhUVbDWLY8LVT3xpYx6ZpPqeE0QFCbnR4PxPBoVFMelGs xBMD2uhp4IwiLWumM1Zky3tppmvOohmGjyUs56tmXYnldIeYfWK4xJwvFpJkAT62iBbs 2S25BaZrhPwuUiZ8XyKMD3i8sHVnJ2BcEvRIS6KqWNmRDmwJHsN3yEnfVu74fnPrBzhc DaHtF5J0hgPZsdrHHFweZIxvaO4ujMJS53rNfaKYgCsJQDndDj6imd3i+TvxkxxcKbHT G3rA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753210464; x=1753815264; h=in-reply-to:references:to:from:subject:cc:message-id:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=1nZWzW+TOq8GrYtinVv5r/V14XpypmT11SAoRNIEn4U=; b=nfHTTrH79yqnOc9eeWiWCCsE9DDEk8Llax5f2Fp/W9Czd3DcyDNMgqsiZ64YoSxOle MWH+fZZWA/2l59RSFZEEoPzJwMvNL2WQqMaUYCamKGuP5BDppMT22AlGnuAfmcCM6k0k e7/GBLQkhyx04VzwdcWc+03YX+yBoy53J7ttgaqmCG7p7l+EDZjqcGdWpkZiESCZINk2 s06pZ2I1HuAYt7KaJdvKnkRHQ9uB/CxETk0TQkG2ItnujoQiSz1JVLPSV3KTbiRPwRaU ILOKF8VHC7A6Yp6QyM4vzWeofURoO4Fy+/su7XhOIsjLHfM0isYTc9y29U36/ofRrmQ3 QhiQ== X-Forwarded-Encrypted: i=1; AJvYcCVM+lRK/hDSp/e1HbLyrN7g4ozG0yc6i2D6FFs55A+IKi2YbHpmik3qGgr9KZznjnmzE8wPDgQiVe2NsnQR@postgresql.org X-Gm-Message-State: AOJu0Yyt3cRBOaaZiXdoO7gtjtUcYkv5Z3mwqhK/AxdbbXwSXzx+yA5m IjfSaodpkUoZNLGfgtMBTDYiMwxPlMGJKZiGLgdSem+tMSJXwi7KQbD3 X-Gm-Gg: ASbGncvh2ged1J+UIlU5AxwsgjEokj4c7liGk5/xuOmtbEjJ3Zt0BD1MQ4npo4gx+xn lrl8qww7FqDjr4H88/WiM4mxTVrH3ovvrLAJ56pC9PbFwxgyDrSXagQSzX90bXMM802rW8cavlP efaL7NvlKnaWJAZlu87++ibAS9PeKHTWDTovEH3UEXDhfW+jYFBqcMKiJnMH2xdxKmJr5hB13Rp p715K2HkAMWUjV+46h5uk3zy6yWEsapcxWGfqVVVsffE+IOGEGnWM+0woCaGlZAztD6aBgvrSQT lK/yQ/dVhGbmnssGL8B0MWki4UcYMqWxafNuiQ033OC2mzqokxQ6F/3OyTbawH696DKZEQlHAPC TpYWJeCqXCGG7+ybZVI9XHASTRkwyyqBfz2jBxOzAQq4aGQ== X-Google-Smtp-Source: AGHT+IFcFMEEoCygyBJCh0skHfRu3aGsFxFPxZZc0b6c0pmy+BPF8fg73TPoETdym+44WFOmNntHUg== X-Received: by 2002:a05:6a20:9389:b0:234:3932:2958 with SMTP id adf61e73a8af0-2381245729dmr37807054637.20.1753210463511; Tue, 22 Jul 2025 11:54:23 -0700 (PDT) Received: from localhost ([2804:14d:328a:a59c:c4f9:8c97:bf93:1b27]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-759cbf5a13csm7960945b3a.162.2025.07.22.11.54.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 22 Jul 2025 11:54:22 -0700 (PDT) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Tue, 22 Jul 2025 15:54:19 -0300 Message-Id: Cc: "Vik Fearing" , "Pg Hackers" , "Peter Eisentraut" Subject: Re: Proposal: QUALIFY clause From: "Matheus Alcantara" To: "Marcos Pegoraro" X-Mailer: aerc 0.20.1 References: <6c998e4f-f6f2-43c2-8b67-cfff360ef241@postgresfriends.org> In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue Jul 22, 2025 at 3:11 PM -03, Marcos Pegoraro wrote: > Em ter., 22 de jul. de 2025 =C3=A0s 08:56, Matheus Alcantara < > matheusssilv97@gmail.com> escreveu: > >> The current patch supports the following syntaxes: >> SELECT a, b, c >> wf() OVER () as d >> FROM tab >> QUALIFY d =3D 1 >> When using the "QUALIFY d =3D 1" form, I currently rewrite the expressio= n >> as "wf() OVER () =3D 1" by searching the targetlist for the matching >> alias, replacing the Var with the corresponding WindowFunc. >> > > Not related to $subject but that way you did, > that replacement alias for wf expression, is cool. > With that would it be possible to have where replacements too ? Maybe. > > select a+b as ab from t where ab =3D 5 > Do you mean instead of reference the "ab" using a Var it replaces with a OpExpr of "a+b"? I think that it would be possible, but the current implementation is not fully correct, it only works for OpExpr's. Using AND/OR operators does not work and I think that to make it fully correct is more complex, so I think that rewriting the query to use a subquery as Vik suggested on [1] fixes this issue and also makes it easier to understand. (this happens on transformQualifyClause() if you want to take a look) [1] https://www.postgresql.org/message-id/6c998e4f-f6f2-43c2-8b67-cfff360ef= 241%40postgresfriends.org -- Matheus Alcantara