ARRAY_SHUFFLE
Description
Randomly shuffles the order of elements in an array, preserving the elements and their count. Returns NULL if the input array or seed is NULL. SHUFFLE is an alias for this function.
Warning:
Future release behavior
In a future release,
seedmust be a constant. It accepts any BIGINT value, including negative values, and uses all 64 bits. Constant arrays are also shuffled separately for each row, so the same seed may produce a different result from earlier versions. Core 26.1.3 uses the seed from the first row of each batch and a 32-bit random generator. Use a non-negative seed no greater than 4,294,967,295 in that release. The full BIGINT seed behavior and examples below apply to a future release.
Syntax
ARRAY_SHUFFLE(<arr> [, <seed>])
Parameters
| Parameter | Description |
|---|---|
<arr> | ARRAY to shuffle. |
<seed> | Optional BIGINT seed. In Core 26.1.3, use a constant non-negative value no greater than 4,294,967,295 for predictable seed selection. Full BIGINT seeds and required constant expressions apply to a future release. |
Return value
Returns an array of the same type as the input, with elements randomly reordered. Element count and types are preserved. Returns NULL if the array or seed is NULL, and returns an empty array for an empty input array.
In a future release, rows are processed in batches. With a specified seed, each batch starts a random sequence from that seed, and its rows use that sequence in order. A row's permutation can therefore depend on batch boundaries. Specifying a seed does not guarantee identical permutations for identical arrays. Constant arrays are also shuffled separately for each row.
Example
The following examples describe future release behavior. In Core 26.1.3, use a constant non-negative seed within the range above.
Sorting the shuffled array verifies that its elements are preserved. Both negative seeds and constant expressions are valid:
SELECT ARRAY_SORT(ARRAY_SHUFFLE([1, 2, 3, 4], -1)),
ARRAY_SORT(ARRAY_SHUFFLE([1, 2, 3, 4], 1 + 1));
[1, 2, 3, 4] [1, 2, 3, 4]
NULL and empty arrays:
SELECT ARRAY_SHUFFLE(CAST(NULL AS ARRAY<INT>), 1),
ARRAY_SHUFFLE([1, 2, 3], NULL), ARRAY_SHUFFLE([], 1);
NULL NULL []
Using a column as the seed reports an error containing:
SELECT ARRAY_SHUFFLE([1, 2, 3], number) FROM numbers("number" = "2");
The seed of array_shuffle must be a constant