Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oDXVN-0005Kn-1o for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Jul 2022 20:38:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oDXVL-00069c-Vc for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Jul 2022 20:38:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oDXSK-0007Xa-Lp for pgsql-hackers@lists.postgresql.org; Mon, 18 Jul 2022 20:35:04 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oDXSE-0003gU-AC for pgsql-hackers@lists.postgresql.org; Mon, 18 Jul 2022 20:35:03 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 26IKYoPi809172; Mon, 18 Jul 2022 16:34:50 -0400 From: Tom Lane To: Robert Haas cc: Martin Kalcher , John Naylor , Thomas Munro , PostgreSQL Hackers Subject: Re: [PATCH] Introduce array_shuffle() and array_sample() In-reply-to: References: <9d160a44-7675-51e8-60cf-6d64b76db831@aboutsource.net> <4073858.1658027909@sss.pgh.pa.us> <4077925.1658029046@sss.pgh.pa.us> <5ef15c6f-e108-53af-3d82-587fac394f1a@aboutsource.net> <463181.1658097987@sss.pgh.pa.us> <5b7a1ee9-f942-2502-3a0c-e9fc12511139@aboutsource.net> <469954.1658100033@sss.pgh.pa.us> <687490.1658152096@sss.pgh.pa.us> <1708d95a-629e-5ac5-deab-d20a8d0b2b0e@aboutsource.net> Comments: In-reply-to Robert Haas message dated "Mon, 18 Jul 2022 16:27:30 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <809170.1658176490.1@sss.pgh.pa.us> Date: Mon, 18 Jul 2022 16:34:50 -0400 Message-ID: <809171.1658176490@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Robert Haas writes: > On Mon, Jul 18, 2022 at 3:03 PM Martin Kalcher > 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