Re: [PATCH] Introduce array_shuffle() and array_sample()

From: Salek Talangi <salek(dot)talangi(at)googlemail(dot)com>
To: daniel(at)yesql(dot)se
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Martin Kalcher <martin(dot)kalcher(at)aboutsource(dot)net>, Fabien COELHO <coelho(at)cri(dot)ensmp(dot)fr>, Andres Freund <andres(at)anarazel(dot)de>, Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>, Robert Haas <robertmhaas(at)gmail(dot)com>, Andrew Dunstan <andrew(at)dunslane(dot)net>, John Naylor <john(dot)naylor(at)enterprisedb(dot)com>, Thomas Munro <thomas(dot)munro(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: [PATCH] Introduce array_shuffle() and array_sample()
Date: 2023-04-19 12:04:45
Message-ID: CACX8wBfgatVYMkaQ6we8sSVrFQ0P=uvLkGAhhB5gv692=VkKJA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Hi all,

reading this blog post
https://www.depesz.com/2023/04/18/waiting-for-postgresql-16-add-array_sample-and-array_shuffle-functions/
I became aware of the new feature and had a look at it and the commit
https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=888f2ea0a81ff171087bdd1c5c1eeda3b78d73d4

To me the description
/*
* Shuffle array using Fisher-Yates algorithm. Scan the array and swap
* current item (nelm datums starting at ielms) with a randomly chosen
* later item (nelm datums starting at jelms) in each iteration. We can
* stop once we've done n iterations; then first n items are the result.
*/

seems wrong. For n = 1 the returned item could never be the 1st item of the
array (see "randomly chosen later item").
If this really is the case then the result is not really random. But to me
it seems j later can be 0 (making it not really "later"), so this might
only be a documentation issue.

Best regards
Salek Talangi

Am Mi., 19. Apr. 2023 um 13:48 Uhr schrieb Daniel Gustafsson <
daniel(at)yesql(dot)se>:

> > On 7 Apr 2023, at 17:47, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> >
> > Daniel Gustafsson <daniel(at)yesql(dot)se> writes:
> >> Ah, ok, now I see what you mean, thanks! I'll try to fix up the patch
> like
> >> this tomorrow.
> >
> > Since we're running out of time, I took the liberty of fixing and
> > pushing this.
>
> Great, thanks!
>
> --
> Daniel Gustafsson

In response to

Browse pgsql-general by date

  From Date Subject
Next Message gzh 2023-04-19 13:24:09 psql:t_mstr.sql:994: ERROR: function to_char(numeric) does not exist
Previous Message shaurya jain 2023-04-19 11:56:06 Re: Logical replication failed with SSL SYSCALL error

Browse pgsql-hackers by date

  From Date Subject
Next Message Thom Brown 2023-04-19 12:37:30 Remove references to pre-11 versions
Previous Message shaurya jain 2023-04-19 11:56:06 Re: Logical replication failed with SSL SYSCALL error