Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pMkds-0008L7-L9 for pgsql-bugs@arkaria.postgresql.org; Tue, 31 Jan 2023 07:01:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pMkdr-0003LL-Ee for pgsql-bugs@arkaria.postgresql.org; Tue, 31 Jan 2023 07:01:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pMkdr-0003L7-4c for pgsql-bugs@lists.postgresql.org; Tue, 31 Jan 2023 07:01:19 +0000 Received: from mail-yb1-xb31.google.com ([2607:f8b0:4864:20::b31]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pMkdp-0003d7-SA for pgsql-bugs@lists.postgresql.org; Tue, 31 Jan 2023 07:01:18 +0000 Received: by mail-yb1-xb31.google.com with SMTP id x4so16981960ybp.1 for ; Mon, 30 Jan 2023 23:01:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=PfmVuVZjJrDPO+TTZ+PmICA/2sKBNs7EI0CQGKyAUDs=; b=owlyzTZ9ionCBQuUT/E5vOXeC9Si5AnQCf2L2uB4x8PaDv0s/FgVRyKQBSJ6FUrZr8 l7cvSA2l35y1koqcUv0FCp5WFRbFeVRz9OjtM0chLj8dGe2FaZcUW06EJnU5fqq38qXF lIo3PCImSzwcsAXDyae/J2WzuDm2R1FRbpXzqs9VCdYDwyezUY5kl5sAnCL0TNd5JRAg fJV97jW1eO+qBEgaVATSGGYpNLK9yXBk+Es0eGTUPcXlMz9YUAv+Twm/ckkZNi7LfR2O LknCydwrlNXl/7ioVBC6nJI5CHXYhC7X5VxxwhTZ8WsbIEBummFExm3Fp/x9XEFfMbXS vdmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PfmVuVZjJrDPO+TTZ+PmICA/2sKBNs7EI0CQGKyAUDs=; b=HERkxueFae9NHyZYrUsiUH2JLPfd95PgMFy8WFSz+Ux05DFv5AqHiG+Lq2GRUScH3G FC312CFWkvBbdz0VBvttScaCKrwbXb0cZs0TyvuA25tWRFRzjWDYA9eONJ0aDZANGtMT kNkW0U19k91R/pHO8oo5NHoPTCmgFCi918iaiTwUhqLnA9XYqFpjhm29t8xANI1W25sZ hdomXfESHdu0xVSYZMUsHRAeHbXMXByEdsQ4pat+zgT4Hxr1NYnEgtsrJtHmmgJ8+6xo cdS5S+RMlfmaW/l76rkW69TxLkHQDvFclcfnBiHaasK7RB2zsApetWOmhzCU3KZG3pJt LiBg== X-Gm-Message-State: AFqh2koExRpWLYdvxcoZSZbMPUwL+3mhMbFoQhWuWVp9QMxtBeLuECWh ocE73mdzRSzh7gbHp+QLa5dI6LqKbqjp9anCig4= X-Google-Smtp-Source: AMrXdXv2QU6qrJx7mr7+0/WAR6zf7hiFTpBqPPuf7Bjv3gXYE0vwjCka/4tNE9t4FHNrDyHVbJC3kRRncFJdd5qdFDU= X-Received: by 2002:a25:9c03:0:b0:7f4:40f8:f23a with SMTP id c3-20020a259c03000000b007f440f8f23amr4691732ybo.506.1675148476016; Mon, 30 Jan 2023 23:01:16 -0800 (PST) MIME-Version: 1.0 References: <17502-281a7aaacfaa872a@postgresql.org> <965990.1675119213@sss.pgh.pa.us> In-Reply-To: <965990.1675119213@sss.pgh.pa.us> From: Richard Guo Date: Tue, 31 Jan 2023 15:01:04 +0800 Message-ID: Subject: Re: BUG #17502: View based on window functions returns wrong results when queried To: Tom Lane Cc: David Rowley , =?UTF-8?Q?Daniel_Farka=C5=A1?= , Magnus Hagander , PostgreSQL mailing lists Content-Type: multipart/alternative; boundary="000000000000dbed3e05f389e51b" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000dbed3e05f389e51b Content-Type: text/plain; charset="UTF-8" On Tue, Jan 31, 2023 at 6:53 AM Tom Lane wrote: > This bug seems to have slipped off the radar screen, but it's still > a bug. I continue to believe that the best fix is to disallow SRFs > in window definitions, and present the trivial patch to do so. This patch fixes the original issue reported by Daniel. Another issue discussed in this thread is the weirdness when there are SRFs in the targetlist of a query with DISTINCT ON clause, as described by David. create table ab (a int, b int); insert into ab values(1,1),(1,2),(2,1),(2,2); # select distinct on (a) a, b, generate_series(1,2) g from ab order by a, b; a | b | g ---+---+--- 1 | 1 | 1 1 | 1 | 2 2 | 1 | 1 2 | 1 | 2 (4 rows) # select distinct on (a) a, b, g from ab, lateral generate_series(1,2) as g order by a, b; a | b | g ---+---+--- 1 | 1 | 1 2 | 1 | 1 (2 rows) According to the doc these two queries are supposed to be 'almost exactly the same'. So it's weird to see they give different number of output rows. Should we also fix this issue? If so it seems we need some changes about postponing SRFs in make_sort_input_target(). Thanks Richard --000000000000dbed3e05f389e51b Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Tue, Jan 31, 2023 at 6:53 AM Tom Lane = <tgl@sss.pgh.pa.us> wrote:
This bug seems to have slipped off the radar screen, but it's still
a bug.=C2=A0 I continue to believe that the best fix is to disallow SRFs in window definitions, and present the trivial patch to do so.
=
=C2=A0
This patch fixes the original issue reported by Danie= l.=C2=A0 Another issue
discussed in this thread is the weirdness when th= ere are SRFs in the
targetlist of a query with DISTINCT ON clause, as de= scribed by David.

create table ab (a int, b int);
insert into ab = values(1,1),(1,2),(2,1),(2,2);

# select distinct on (a) a, b, genera= te_series(1,2) g from ab order by a, b;
=C2=A0a | b | g
---+---+---=C2=A01 | 1 | 1
=C2=A01 | 1 | 2
=C2=A02 | 1 | 1
=C2=A02 | 1 | 2<= br>(4 rows)

# select distinct on (a) a, b, g from ab, lateral genera= te_series(1,2) as g order by a, b;
=C2=A0a | b | g
---+---+---
=C2= =A01 | 1 | 1
=C2=A02 | 1 | 1
(2 rows)

According to the doc the= se two queries are supposed to be 'almost
exactly the same'.=C2= =A0 So it's weird to see they give different number of
output rows.<= br>
Should we also fix this issue?=C2=A0 If so it seems we need some cha= nges
about postponing SRFs in make_sort_input_target().

ThanksRichard
--000000000000dbed3e05f389e51b--