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

From: Martin Kalcher <martin(dot)kalcher(at)aboutsource(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Robert Haas <robertmhaas(at)gmail(dot)com>
Cc: 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 21:48:55
Message-ID: 0b5dea2b-37fa-6c81-ea9d-0341ab6f87d6@aboutsource.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Am 18.07.22 um 23:03 schrieb Tom Lane:
> I wrote:
>> 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.
>
> Actually, after poking at it for awhile, that's an overstatement.
> It's true that the type system doesn't think N-D arrays are
> arrays-of-arrays, but there are individual functions/operators that do.
> Thanks Robert for pointing out the inconsistent behavior of
array_sample(). That needs to be fixed.

As Tom's investigation showed, there is no consensus in the code if
multi-dimensional arrays are treated as arrays-of-arrays or not. We need
to decide what should be the correct treatment.

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}

If we go with (2) both functions should only operate on the first
dimension and shuffle whole subarrays and keep the dimensions intact.

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

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

I do not feel qualified to make that decision. (2) complicates the code
a bit, but that should not be the main argument here.

Martin

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Håvar Nøvik 2022-07-18 21:49:30 How to handle failed COMMIT
Previous Message Tom Lane 2022-07-18 21:03:01 Re: [PATCH] Introduce array_shuffle() and array_sample()

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2022-07-18 21:56:49 Re: pg_parameter_aclcheck() and trusted extensions
Previous Message Tom Lane 2022-07-18 21:03:01 Re: [PATCH] Introduce array_shuffle() and array_sample()