First n items of array if array.size() < n

#1

Hey folks,
I’m simply trying to grab the first n elements of an array field like this:

SELECT
attributes[0:3] AS attributes
FROM `bucket`
USE KEYS["id"]

If there are less than ‘n’ (in this case 3) items in the array nothing is returned. I’m getting around this by doing the following:

IFMISSING(attributes[0:3], attributes) AS attributes

Is there a better way to do this?

Thanks

#2
SELECT
attributes[0: LEAST(3, ARRAY_LENGHTH(attributes))] AS attributes
FROM `bucket`
USE KEYS["id"];

OR

SELECT  (SELECT RAW a 
                     FROM d.attributes AS a 
                      LIMIT 3) AS attributes
FROM `bucket` AS d 
USE KEYS["id"];

OR

SELECT 
CASE ARRAY_LENGTH(attributes) > 3 THEN attributes[0:3] ELSE attributes END AS attributes
FROM   `bucket` USE KEYS["id"];