Re: [PATCH] Introduce array_shuffle() and array_sample() - Mailing list pgsql-hackers

From Tom Lane
Subject Re: [PATCH] Introduce array_shuffle() and array_sample()
Date
Msg-id 809171.1658176490@sss.pgh.pa.us
Whole thread Raw
In response to Re: [PATCH] Introduce array_shuffle() and array_sample()  (Robert Haas <robertmhaas@gmail.com>)
Responses Re: [PATCH] Introduce array_shuffle() and array_sample()
List pgsql-hackers
Robert Haas <robertmhaas@gmail.com> writes:
> On Mon, Jul 18, 2022 at 3:03 PM Martin Kalcher
> <martin.kalcher@aboutsource.net> wrote:
>> array_shuffle(anyarray) -> anyarray
>> array_sample(anyarray, integer) -> anyarray

> I think it's questionable whether the behavior of array_shuffle() is
> correct for a multi-dimensional array. The implemented behavior is to
> keep the dimensions as they were, but permute the elements across all
> levels at random. But there are at least two other behaviors that seem
> potentially defensible: (1) always return a 1-dimensional array, (2)
> shuffle the sub-arrays at the top-level without the possibility of
> moving elements within or between sub-arrays. What behavior we decide
> is best here should be documented.

Martin had originally proposed (2), which I rejected on the grounds
that we don't treat multi-dimensional arrays as arrays-of-arrays for
any other purpose.  Maybe we should have, but that ship sailed decades
ago, and I doubt that shuffle() is the place to start changing it.

I could get behind your option (1) though, to make it clearer that
the input array's dimensionality is not of interest.  Especially since,
as you say, (1) is pretty much the only sensible choice for array_sample.

> I also think you should add test cases involving multi-dimensional
> arrays, as well as arrays with non-default bounds. e.g. trying
> shuffling or sampling some values like
> '[8:10][-6:-5]={{1,2},{3,4},{5,6}}'::int[]

This'd only matter if we decide not to ignore the input's dimensionality.

            regards, tom lane



pgsql-hackers by date:

Previous
From: Robert Haas
Date:
Subject: Re: pg15b2: large objects lost on upgrade
Next
From: Jacob Champion
Date:
Subject: Re: [Commitfest 2022-07] Begins Now