pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Martin Kalcher <martin.kalcher@aboutsource.net>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Thomas Munro <thomas.munro@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: Proposal to introduce a shuffle function to intarray extension
Date: Mon, 18 Jul 2022 09:47:30 +0200
Message-ID: <f36445ec-aad9-0531-39f8-ca3a3bb4860e@aboutsource.net> (raw)
In-Reply-To: <469954.1658100033@sss.pgh.pa.us>
References: <9d160a44-7675-51e8-60cf-6d64b76db831@aboutsource.net>
	<CA+hUKGJFYY6s4nxDRz+PTLNYeUkZJJn_7+iMJoaQeRp5hKz0aQ@mail.gmail.com>
	<b8f44b53-02d6-0be4-42df-cb6f737e6267@aboutsource.net>
	<4073858.1658027909@sss.pgh.pa.us>
	<4077925.1658029046@sss.pgh.pa.us>
	<CA+hUKGLcjrJ9Q2OZAKHN-SQyq_m5QDv8d3WKHYq5HW8Lyozu4w@mail.gmail.com>
	<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>

Am 18.07.22 um 01:20 schrieb Tom Lane:
> (Having said that, even if we were going to implement it with that
> definition, I should think that it'd be easiest to do so on the
> array-of-Datums representation produced by deconstruct_array.
> That way you don't need to do different things for different element
> types.)

Thank you Tom, here is a patch utilising deconstruct_array(). If we 
agree, that this is the way to go, i would like to add array_sample() 
(good name?), some test cases, and documentation.

One more question. How do i pick a Oid for the functions?

Martin

Attachments:

0001-introduce-array_shuffle.patch (text/x-patch, 2.6K) download From baec08168357098287342c92672ef97361a91371 Mon Sep 17 00:00:00 2001 From: Martin Kalcher <martin.kalcher@aboutsource.net> Date: Sun, 17 Jul 2022 18:06:04 +0200 Subject: [PATCH] introduce array_shuffle() --- src/backend/utils/adt/arrayfuncs.c | 61 ++++++++++++++++++++++++++++++ src/include/catalog/pg_proc.dat | 3 ++ 2 files changed, 64 insertions(+) diff --git a/src/backend/utils/adt/arrayfuncs.c b/src/backend/utils/adt/arrayfuncs.c index fb167f226a..e185c1ef74 100644 --- a/src/backend/utils/adt/arrayfuncs.c +++ b/src/backend/utils/adt/arrayfuncs.c @@ -6872,3 +6872,64 @@ trim_array(PG_FUNCTION_ARGS) PG_RETURN_DATUM(result); } + +Datum +array_shuffle(PG_FUNCTION_ARGS) +{ + ArrayType *array, + *result; + int16 elmlen; + bool elmbyval; + char elmalign; + Oid elmtyp; + TypeCacheEntry *typentry; + Datum *elms, + elm; + bool *nuls, + nul; + int nelms, + i, + j; + + array = PG_GETARG_ARRAYTYPE_P(0); + + if (ArrayGetNItems(ARR_NDIM(array), ARR_DIMS(array)) < 2) + PG_RETURN_ARRAYTYPE_P(array); + + elmtyp = ARR_ELEMTYPE(array); + + typentry = (TypeCacheEntry *) fcinfo->flinfo->fn_extra; + + if (typentry == NULL || typentry->type_id != elmtyp) + { + typentry = lookup_type_cache(elmtyp, 0); + fcinfo->flinfo->fn_extra = (void *) typentry; + } + elmlen = typentry->typlen; + elmbyval = typentry->typbyval; + elmalign = typentry->typalign; + + deconstruct_array(array, elmtyp, elmlen, elmbyval, elmalign, + &elms, &nuls, &nelms); + + for (i = nelms - 1; i > 0; i--) + { + j = random() % (i + 1); + elm = elms[i]; + nul = nuls[i]; + elms[i] = elms[j]; + nuls[i] = nuls[j]; + elms[j] = elm; + nuls[j] = nul; + } + + result = construct_md_array(elms, nuls, + ARR_NDIM(array), ARR_DIMS(array), ARR_LBOUND(array), + elmtyp, elmlen, elmbyval, elmalign); + + pfree(elms); + pfree(nuls); + PG_FREE_IF_COPY(array, 0); + + PG_RETURN_ARRAYTYPE_P(result); +} diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 2e41f4d9e8..56aff551d3 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -1681,6 +1681,9 @@ proname => 'arraycontjoinsel', provolatile => 's', prorettype => 'float8', proargtypes => 'internal oid internal int2 internal', prosrc => 'arraycontjoinsel' }, +{ oid => '7777', descr => 'shuffle array', + proname => 'array_shuffle', proisstrict => 'f', prorettype => 'anyarray', + proargtypes => 'anyarray', prosrc => 'array_shuffle' }, { oid => '764', descr => 'large object import', proname => 'lo_import', provolatile => 'v', proparallel => 'u', -- 2.37.1

view thread (25+ messages)  latest in thread

Message-ID: <f36445ec-aad9-0531-39f8-ca3a3bb4860e@aboutsource.net>
Permalink:  ../f36445ec-aad9-0531-39f8-ca3a3bb4860e@aboutsource.net/
Also on:    postgresql.org/message-id/f36445ec-aad9-0531-39f8-ca3a3bb4860e@aboutsource.net

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: martin.kalcher@aboutsource.net, tgl@sss.pgh.pa.us, thomas.munro@gmail.com, pgsql-hackers@lists.postgresql.org
  Subject: Re: Proposal to introduce a shuffle function to intarray extension
  In-Reply-To: <f36445ec-aad9-0531-39f8-ca3a3bb4860e@aboutsource.net>
  References: <9d160a44-7675-51e8-60cf-6d64b76db831@aboutsource.net> <CA+hUKGJFYY6s4nxDRz+PTLNYeUkZJJn_7+iMJoaQeRp5hKz0aQ@mail.gmail.com> <b8f44b53-02d6-0be4-42df-cb6f737e6267@aboutsource.net> <4073858.1658027909@sss.pgh.pa.us> <4077925.1658029046@sss.pgh.pa.us> <CA+hUKGLcjrJ9Q2OZAKHN-SQyq_m5QDv8d3WKHYq5HW8Lyozu4w@mail.gmail.com> <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> <f36445ec-aad9-0531-39f8-ca3a3bb4860e@aboutsource.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox