Re: BUG #17502: View based on window functions returns wrong results when queried

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: Daniel Farkaš <daniel(dot)farkas(at)datoris(dot)com>, Magnus Hagander <magnus(at)hagander(dot)net>, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #17502: View based on window functions returns wrong results when queried
Date: 2022-05-30 09:18:44
Message-ID: CAMbWs48yS=f8tj6zstiMd6yfV3U4YpssyB55PXqjcLUyUA5bww@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Mon, May 30, 2022 at 9:12 AM David Rowley <dgrowleyml(at)gmail(dot)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=# 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

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Ilya Anfimov 2022-05-30 10:27:53 Re: BUG #17502: View based on window functions returns wrong results when queried
Previous Message Fabien COELHO 2022-05-30 09:05:48 Re: psql 15beta1 does not print notices on the console until transaction completes