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 1oCuo2-0001Xr-6k for pgsql-hackers@arkaria.postgresql.org; Sun, 17 Jul 2022 03:18:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oCunz-0006GK-Uj for pgsql-hackers@arkaria.postgresql.org; Sun, 17 Jul 2022 03:18:51 +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 1oCunz-00066f-JO for pgsql-hackers@lists.postgresql.org; Sun, 17 Jul 2022 03:18:51 +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 1oCuns-00055f-My for pgsql-hackers@lists.postgresql.org; Sun, 17 Jul 2022 03:18:50 +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 26H3ITC44073859; Sat, 16 Jul 2022 23:18:29 -0400 From: Tom Lane To: Martin Kalcher cc: Thomas Munro , PostgreSQL Hackers Subject: Re: Proposal to introduce a shuffle function to intarray extension In-reply-to: References: <9d160a44-7675-51e8-60cf-6d64b76db831@aboutsource.net> Comments: In-reply-to Martin Kalcher message dated "Sun, 17 Jul 2022 04:25:27 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4073857.1658027909.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 16 Jul 2022 23:18:29 -0400 Message-ID: <4073858.1658027909@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Martin Kalcher writes: > Am 16.07.22 um 23:56 schrieb Thomas Munro: >> I understand that this is a specialised int[] shuffle, but I wonder if >> someone would ever want to have a general array shuffle, and how that >> would work, in terms of naming convention etc. > - I am not shure about the naming. intarray has generic names like > sort() and uniq() and specialised names like icount(). I guess in cas= e > someone wants to have a general array shuffle it could be accomplishe= d > with function overloading. Or am i wrong here? I suppose this is exactly the point Thomas was wondering about: if we use a generic function name for this, will it cause problems for someone trying to add a generic function later? We can investigate that question with a couple of toy functions: regression=3D# create function foo(int[]) returns text as 'select ''int[] = version''' language sql; CREATE FUNCTION regression=3D# create function foo(anyarray) returns text as 'select ''any= array version''' language sql; CREATE FUNCTION regression=3D# select foo('{1,2,3}'); ERROR: function foo(unknown) is not unique LINE 1: select foo('{1,2,3}'); ^ HINT: Could not choose a best candidate function. You might need to add e= xplicit type casts. OK, that's not too surprising: with an unknown input there's just not anything that the parser can use to disambiguate. But this happens with just about any overloaded name, so I don't think it's a showstopper. regression=3D# select foo('{1,2,3}'::int[]); foo = --------------- int[] version (1 row) regression=3D# select foo('{1,2,3}'::int8[]); foo = ------------------ anyarray version (1 row) Good, that's more or less the minimum functionality we should expect. regression=3D# select foo('{1,2,3}'::int2[]); ERROR: function foo(smallint[]) is not unique LINE 1: select foo('{1,2,3}'::int2[]); ^ HINT: Could not choose a best candidate function. You might need to add e= xplicit type casts. Oh, that's slightly annoying ... regression=3D# select foo('{1,2,3}'::oid[]); foo = ------------------ anyarray version (1 row) regression=3D# select foo('{1,2,3}'::text[]); foo = ------------------ anyarray version (1 row) regression=3D# select foo('{1,2,3}'::float8[]); foo = ------------------ anyarray version (1 row) I couldn't readily find any case that misbehaves except for int2[]. You can force that to work, at least in one direction: regression=3D# select foo('{1,2,3}'::int2[]::int[]); foo = --------------- int[] version (1 row) On the whole, I'd vote for calling it shuffle(), and expecting that we'd also use that name for any future generic version. That's clearly the easiest-to-remember definition, and it doesn't seem like the gotchas are bad enough to justify using separate names. > - I added a second function sample(), because it is a lot faster to take > some elements from an array than to shuffle the whole array and > slice it. This function can be removed if it is not wanted. I have no opinion about whether this one is valuable enough to include in intarray, but I do feel like sample() is a vague name, and easily confused with marginally-related operations like TABLESAMPLE. Can we think of a more on-point name? Something like "random_subset" would be pretty clear, but it's also clunky. It's too late here for me to think of le mot juste... regards, tom lane