Re: Custom shuffle function stopped working in 9.6

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Custom shuffle function stopped working in 9.6
Date: 2017-02-11 16:51:02
Message-ID: CAFj8pRAbcVUrkQM_5zsOLj=szKtQUuG3J0t=nAEsNuWhfxTOVQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi

2017-02-11 17:37 GMT+01:00 Alexander Farber <alexander(dot)farber(at)gmail(dot)com>:

> Good evening,
>
> after switching to 9.6.2 from 9.5.3 the following custom function has
> stopped working:
>
> CREATE OR REPLACE FUNCTION words_shuffle(in_array varchar[])
> RETURNS varchar[] AS
> $func$
> SELECT array_agg(letters.x) FROM
> (SELECT UNNEST(in_array) x ORDER BY RANDOM()) letters;
> $func$ LANGUAGE sql STABLE;
>
> In 9.5.3 it was shuffling characters:
>
> words=> select words_shuffle(ARRAY['a','b','c','d','e','f']);
> words_shuffle
> ---------------
> {c,d,b,a,e,f}
> (1 row)
>
> But in 9.6.2 it has stopped doing so:
> words=> select words_shuffle(ARRAY['a','b','c','d','e','f']);
> words_shuffle
> ---------------
> {a,b,c,d,e,f}
> (1 row)
>
> Any suggestions for a better shuffling function please?
>

CREATE OR REPLACE FUNCTION words_shuffle(in_array varchar[])
RETURNS varchar[] AS
$func$
SELECT array_agg(letters.x) FROM
(SELECT x FROM UNNEST(in_array) x ORDER BY RANDOM()) letters;
$func$ LANGUAGE sql STABLE;

there was some optimisations for faster expression evaluation - probably
this is one effect of this optimisation.

generally SRF function should not be used in target list - now when we have
LATERAL join, it is not necessary

Regards

Pavel

>
> Regards
> Alex
>
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alexander Farber 2017-02-11 17:17:10 Re: Custom shuffle function stopped working in 9.6
Previous Message Alexander Farber 2017-02-11 16:37:27 Custom shuffle function stopped working in 9.6