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 1nvbYC-000417-RR for pgsql-bugs@arkaria.postgresql.org; Mon, 30 May 2022 09:19:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nvbYB-0004fT-Ko for pgsql-bugs@arkaria.postgresql.org; Mon, 30 May 2022 09:18:59 +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 1nvbYB-0004ex-B4 for pgsql-bugs@lists.postgresql.org; Mon, 30 May 2022 09:18:59 +0000 Received: from mail-io1-xd29.google.com ([2607:f8b0:4864:20::d29]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nvbY9-0003Mi-PJ for pgsql-bugs@lists.postgresql.org; Mon, 30 May 2022 09:18:58 +0000 Received: by mail-io1-xd29.google.com with SMTP id b4so10726453iog.11 for ; Mon, 30 May 2022 02:18:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=tczHqXvXzhAcaOn9ytLF0fjbSVRnfHciPAjyexIsCiM=; b=GRcuAgKOv7eeGTiX/OsdUNpcpCVcp7Yp8Ewqc58JqBLpmR1c8PMuwHmzU3TDL3YK7s oiceoSFUX7YZhNSdeY04e8iwx05shAWYmnexiZSwgm9yBAKZ2WZ6Jfiyr6wuQYVa/QCK mPPChLklveeUvq30/6jpZLkMvJ/s/qjF6q6KcfrplV0n6LfwncIRRRq0+A4ogtqndaSu Srygq43x1EZMMk81Qwx/tEakkSLvPM0KJ63f+twv1bTgZXJg8siLif9XGypQEg3ghqQo V8FwpLA9jq7rPJ5P/WPBtI1/h03MJrGNxMQ3BoYt83RImKpjEueRTFut+OXJ+ib9ZIh7 LgwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=tczHqXvXzhAcaOn9ytLF0fjbSVRnfHciPAjyexIsCiM=; b=WJjF//SeEGmAR2uZRfT0xd8BlK5tMKa1rC3H8qMArx2TAV1jhI1h/sXzPjWb8mgEgr ARqtr3Ql2cRqxf6lGc5s1Dt2trtRwRApQK9rR5/JCEGWyqEciDLV1uEfdBIiJwu80GZY 9FMfcHgIXaclgWt7oTGXNEZWnAOZFUfNAipx2PiqgzXLVU3D+fr5mpr1I1q7E4kGZBCM 24GMtrgyLcRG4yT630F6uTmN9ZUscqpZSGJy0niRVZPKUBvOOJBQzddf8oLNHOwvwzwE LjbPXKa2njjXGYT20CxpmevhQEmzUFgglgD1fnwsDQolU0StcKEZh4nHqQ/0VTh5lkam p6Tw== X-Gm-Message-State: AOAM532hJdDK5vo17G/js2/NjwgNSERbZTR8wBMgLrcojOpyV6QcJlDE 4BySJ3r5JwMKSZrWQIqRqyVTJtRGsOQRtpFEBEb/Bf18urk= X-Google-Smtp-Source: ABdhPJzHsVhtpdQfEOWTFRXJnhMjVhXKWmB1XQS7XDrNE7+KIG0hcHq4Ma4ItM8pShaTaVvBfkT/W2cR4b/tp8r87T4= X-Received: by 2002:a05:6602:14c7:b0:665:49cc:4f43 with SMTP id b7-20020a05660214c700b0066549cc4f43mr15181263iow.156.1653902335885; Mon, 30 May 2022 02:18:55 -0700 (PDT) MIME-Version: 1.0 References: <17502-281a7aaacfaa872a@postgresql.org> In-Reply-To: From: Richard Guo Date: Mon, 30 May 2022 17:18:44 +0800 Message-ID: Subject: Re: BUG #17502: View based on window functions returns wrong results when queried To: David Rowley Cc: =?UTF-8?Q?Daniel_Farka=C5=A1?= , Magnus Hagander , PostgreSQL mailing lists Content-Type: multipart/alternative; boundary="0000000000003948cd05e0372514" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000003948cd05e0372514 Content-Type: text/plain; charset="UTF-8" On Mon, May 30, 2022 at 9:12 AM David Rowley wrote: > The following is also pretty strange. Why should adding the SRF column > to the ORDER BY change the number of output rows? > > postgres=# select distinct on (a) a,b, generate_Series(1,2) from ab > order by a,b,3 desc; > a | b | generate_series > ---+---+----------------- > 1 | 1 | 2 > 2 | 1 | 2 > (2 rows) > > > postgres=# select distinct on (a) a,b, generate_Series(1,2) from ab > order by a,b; > a | b | generate_series > ---+---+----------------- > 1 | 1 | 1 > 1 | 1 | 2 > 2 | 1 | 1 > 2 | 1 | 2 > (4 rows) > This is indeed weird. This seems depends on at which plan level we add ProjectSetPath. For the first query we insert ProjectSetPath at the scan/join level, because the SRF is included in the ORDER BY, which makes the SRF appear in the scan/join target. For the second query the SRF is postponed to after the Sort. Is this a bug we should fix? Thanks Richard --0000000000003948cd05e0372514 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Mon, May 30, 2022 at 9:12 AM David Row= ley <dgrowleyml@gmail.com>= ; wrote:
The following is also pretty strange. Why should adding the SRF column
to the ORDER BY change the number of output rows?

postgres=3D# select distinct on (a) a,b, generate_Series(1,2) from ab
order by a,b,3 desc;
=C2=A0a | b | generate_series
---+---+-----------------
=C2=A01 | 1 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A02
=C2=A02 | 1 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A02
(2 rows)


postgres=3D# select distinct on (a) a,b, generate_Series(1,2) from ab
order by a,b;
=C2=A0a | b | generate_series
---+---+-----------------
=C2=A01 | 1 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A01
=C2=A01 | 1 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A02
=C2=A02 | 1 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A01
=C2=A02 | 1 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A02
(4 rows)

This is indeed weird. This see= ms depends on at which plan level we add
ProjectSetPath. For the first q= uery we insert ProjectSetPath at the
scan/join level, because the SRF is= included in the ORDER BY, which
makes the SRF appear=C2=A0in the scan/j= oin target. For the second query the
SRF is postponed to after the Sort.=

Is this a bug we should fix?

Thanks
Richard
--0000000000003948cd05e0372514--