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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Martin Kalcher <martin(dot)kalcher(at)aboutsource(dot)net>
Cc: Robert Haas <robertmhaas(at)gmail(dot)com>, 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-18 22:18:40
Message-ID: 840271.1658182720@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Martin Kalcher <martin(dot)kalcher(at)aboutsource(dot)net> writes:
> If we go with (1) array_shuffle() and array_sample() should shuffle each
> element individually and always return a one-dimensional array.

> select array_shuffle('{{1,2},{3,4},{5,6}}');
> -----------
> {1,4,3,5,6,2}

> select array_sample('{{1,2},{3,4},{5,6}}', 3);
> ----------
> {1,4,3}

Independently of the dimensionality question --- I'd imagined that
array_sample would select a random subset of the array elements
but keep their order intact. If you want the behavior shown
above, you can do array_shuffle(array_sample(...)). But if we
randomize it, and that's not what the user wanted, she has no
recourse.

Now, if you're convinced that the set of people wanting
sampling-without-shuffling is the empty set, then making everybody
else call two functions is a loser. But I'm not convinced.
At the least, I'd like to see the argument made why nobody
would want that.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message David G. Johnston 2022-07-18 22:32:00 Re: [PATCH] Introduce array_shuffle() and array_sample()
Previous Message Adrian Klaver 2022-07-18 22:09:09 Re: postgis

Browse pgsql-hackers by date

  From Date Subject
Next Message David G. Johnston 2022-07-18 22:32:00 Re: [PATCH] Introduce array_shuffle() and array_sample()
Previous Message David G. Johnston 2022-07-18 22:17:11 Re: BUG #17434: CREATE/DROP DATABASE can be executed in the same transaction with other commands