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

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, Martin Kalcher <martin(dot)kalcher(at)aboutsource(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: 2022-07-19 13:05:23
Message-ID: CA+TgmoYzjnOvu4FXhM5Mk1iBKdsh37-u5ga7T8YLT1sW=_8MUw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Mon, Jul 18, 2022 at 6:43 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Um ... why is "the order in which the elements were chosen" a concept
> we want to expose? ISTM sample() is a black box in which notionally
> the decisions could all be made at once.

I agree with that. But I also think it's fine for the elements to be
returned in a shuffled order rather than the original order.

> > I really think this function needs to grow an algorithm argument that can
> > be used to specify stuff like ordering, replacement/without-replacement,
> > etc...just some enums separated by commas that can be added to the call.
>
> I think you might run out of gold paint somewhere around here. I'm
> still not totally convinced we should bother with the sample() function
> at all, let alone that it needs algorithm variants. At some point we
> say to the user "here's a PL, write what you want for yourself".

I don't know what gold paint has to do with anything here, but I agree
that David's proposal seems to be moving the goalposts a very long
way.

The thing is, as Martin points out, these functions already exist in a
bunch of other systems. For one example I've used myself, see
https://underscorejs.org/

I probably wouldn't have called a function to put a list into a random
order "shuffle" in a vacuum, but it seems to be common nomenclature
these days. I believe that if you don't make reference to Fisher-Yates
in the documentation, they kick you out of the cool programmers club.

--
Robert Haas
EDB: http://www.enterprisedb.com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message ghoostinger 2022-07-19 13:16:39 Out Of Memory
Previous Message Ray O'Donnell 2022-07-19 12:10:13 Re: restore question

Browse pgsql-hackers by date

  From Date Subject
Next Message Japin Li 2022-07-19 13:08:53 Re: Memory leak fix in psql
Previous Message Michael Paquier 2022-07-19 12:51:13 Re: errdetail/errhint style