pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Martin Kalcher <martin.kalcher@aboutsource.net>
Cc: Fabien COELHO <coelho@cri.ensmp.fr>
Cc: Andres Freund <andres@anarazel.de>
Cc: Dean Rasheed <dean.a.rasheed@gmail.com>
Cc: Robert Haas <robertmhaas@gmail.com>
Cc: Andrew Dunstan <andrew@dunslane.net>
Cc: John Naylor <john.naylor@enterprisedb.com>
Cc: Thomas Munro <thomas.munro@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: [PATCH] Introduce array_shuffle() and array_sample()
Date: Thu, 29 Sep 2022 15:33:26 -0400
Message-ID: <344667.1664480006@sss.pgh.pa.us> (raw)
In-Reply-To: <c4b69308-fc69-66d5-7905-7c3268833a50@aboutsource.net>
References: <CAEZATCVOJHxWOG7DvEjkL_Y-h6f-HK-VkFpsdP76pa6y7xsW7Q@mail.gmail.com>
	<459f6008-90bc-bc52-6f11-e68570e03ac0@aboutsource.net>
	<CAEZATCXCFyr_cpkMygqSiiiHXgZWaXSumwL9=Xz5n7wwzBvaEw@mail.gmail.com>
	<583b6409-df52-86d1-8c0a-ce8e74863ad7@aboutsource.net>
	<036ba529-1f25-d7b5-c5ad-e6c852d928df@aboutsource.net>
	<alpine.DEB.2.22.394.2207240929220.1359119@pseudo>
	<f5af7698-d1eb-bdf4-9969-82b18ab4f8cf@aboutsource.net>
	<alpine.DEB.2.22.394.2207242132460.1359119@pseudo>
	<fe7a6a81-5c1b-aaae-922f-cb86be324373@aboutsource.net>
	<8fabf9e7-3422-58b0-93f6-d0976d4679df@aboutsource.net>
	<20220922152320.ukx6teo7wa7cuwkv@awork3.anarazel.de>
	<193caac5-941e-58b8-65e6-351f777ebd25@aboutsource.net>
	<3502301.1664223385@sss.pgh.pa.us>
	<9cd0925c-9839-a136-c21d-87de9e8a3aa4@aboutsource.net>
	<a81b3160-bb4-8b9e-8f3f-ff45c88fb3a3@mines-paristech.fr>
	<3943591.1664374725@sss.pgh.pa.us>
	<c4b69308-fc69-66d5-7905-7c3268833a50@aboutsource.net>

Martin Kalcher <martin.kalcher@aboutsource.net> writes:
> New patch: array_shuffle() and array_sample() use pg_global_prng_state now.

I took a closer look at the patch today.  I find this behavior a bit
surprising:

+SELECT array_dims(array_sample('[-1:2][2:3]={{1,2},{3,NULL},{5,6},{7,8}}'::int[], 3));
+ array_dims  
+-------------
+ [-1:1][2:3]
+(1 row)

I can buy preserving the lower bound in array_shuffle(), but
array_sample() is not preserving the first-dimension indexes of
the array, so ISTM it ought to reset the first lower bound to 1.

Some other comments:

+        Returns <parameter>n</parameter> randomly chosen elements from <parameter>array</parameter> in selection order.

What's "selection order"?  And this probably shouldn't just rely
on the example to describe what happens with multi-D arrays.
Writing "elements" seems somewhere between confusing and wrong.

* Personally I think I'd pass the TypeCacheEntry pointer to array_shuffle_n,
and let it pull out what it needs.  Less duplicate code that way.

* I find array_shuffle_n drastically undercommented, and what comments
it has are pretty misleading, eg

+		/* Swap all elements in item (i) with elements in item (j). */

j is *not* the index of the second item to be swapped.  You could make
it so, and that might be more readable:

		j = (int) pg_prng_uint64_range(&pg_global_prng_state, i, nitem - 1);
		jelms = elms + j * nelm;
		jnuls = nuls + j * nelm;

But if you want the code to stay as it is, this comment needs work.

* I think good style for SQL-callable C functions is to make the arguments
clear at the top:

+array_sample(PG_FUNCTION_ARGS)
+{
+    ArrayType  *array = PG_GETARG_ARRAYTYPE_P(0);
+    int         n = PG_GETARG_INT32(1);
+    ArrayType  *result;
+    ... other declarations as needed ...

We can't quite make normal C declaration style work, but that's a poor
excuse for not making the argument list visible as directly as possible.

* I wouldn't bother with the PG_FREE_IF_COPY calls.  Those are generally
only used in btree comparison functions, in which there's a need to not
leak memory.

* I wonder if we really need these to be proparallel = 'r'.  If we let
a worker process execute them, they will take numbers from the worker's
pg_global_prng_state seeding not the parent process's seeding, but why
is that a problem?  In the prior version where you were tying them
to the parent's drandom() sequence, proparallel = 'r' made sense;
but now I think it's unnecessary.

			regards, tom lane





view thread (51+ messages)  latest in thread

Message-ID: <344667.1664480006@sss.pgh.pa.us>
Permalink:  ../344667.1664480006@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/344667.1664480006@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, martin.kalcher@aboutsource.net, coelho@cri.ensmp.fr, andres@anarazel.de, dean.a.rasheed@gmail.com, robertmhaas@gmail.com, andrew@dunslane.net, john.naylor@enterprisedb.com, thomas.munro@gmail.com, pgsql-hackers@lists.postgresql.org
  Subject: Re: [PATCH] Introduce array_shuffle() and array_sample()
  In-Reply-To: <344667.1664480006@sss.pgh.pa.us>
  References: <CAEZATCVOJHxWOG7DvEjkL_Y-h6f-HK-VkFpsdP76pa6y7xsW7Q@mail.gmail.com> <459f6008-90bc-bc52-6f11-e68570e03ac0@aboutsource.net> <CAEZATCXCFyr_cpkMygqSiiiHXgZWaXSumwL9=Xz5n7wwzBvaEw@mail.gmail.com> <583b6409-df52-86d1-8c0a-ce8e74863ad7@aboutsource.net> <036ba529-1f25-d7b5-c5ad-e6c852d928df@aboutsource.net> <alpine.DEB.2.22.394.2207240929220.1359119@pseudo> <f5af7698-d1eb-bdf4-9969-82b18ab4f8cf@aboutsource.net> <alpine.DEB.2.22.394.2207242132460.1359119@pseudo> <fe7a6a81-5c1b-aaae-922f-cb86be324373@aboutsource.net> <8fabf9e7-3422-58b0-93f6-d0976d4679df@aboutsource.net> <20220922152320.ukx6teo7wa7cuwkv@awork3.anarazel.de> <193caac5-941e-58b8-65e6-351f777ebd25@aboutsource.net> <3502301.1664223385@sss.pgh.pa.us> <9cd0925c-9839-a136-c21d-87de9e8a3aa4@aboutsource.net> <a81b3160-bb4-8b9e-8f3f-ff45c88fb3a3@mines-paristech.fr> <3943591.1664374725@sss.pgh.pa.us> <c4b69308-fc69-66d5-7905-7c3268833a50@aboutsource.net> <344667.1664480006@sss.pgh.pa.us>

* 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