presto-main/src/main/java/io/prestosql/operator/scalar/ArrayShuffleFunction.java/ArrayShuffleFunction/shuffle
class ArrayShuffleFunction:    @TypeParameter("E")
    @SqlType("array(E)")
    public Block shuffle(
            @TypeParameter("E") Type type,
            @SqlType("array(E)") Block block)
    {
        int length = block.getPositionCount();
        if (positions.length < length) {
            positions = new int[length];
        }
        for (int i = 0; i < length; i++) {
            positions[i] = i;
        }

        // Fisher-Yates shuffle
        // Randomly swap a pair of positions
        for (int i = length - 1; i > 0; i--) {
            int index = ThreadLocalRandom.current().nextInt(i + 1);
            int swap = positions[i];
            positions[i] = positions[index];
            positions[index] = swap;
        }

        if (pageBuilder.isFull()) {
            pageBuilder.reset();
        }

        BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);

        for (int i = 0; i < length; i++) {
            type.appendTo(block, positions[i], blockBuilder);
        }
        pageBuilder.declarePositions(length);

        return blockBuilder.getRegion(blockBuilder.getPositionCount() - length, length);
    }