Removes <size> elements from the end of <arr> while preserving the order of the remaining elements.
TRIM_ARRAY(<arr>, <size>)
| Parameter | Description |
|---|---|
<arr> | The input ARRAY<T>. T can be a numeric, boolean, string, date/time, IP, or complex type. |
<size> | A non-negative BIGINT specifying how many elements to remove from the end. It cannot exceed the cardinality of <arr>. |
Returns an ARRAY<T> containing the first cardinality(<arr>) - <size> elements of <arr>.
<size> is 0, returns the input array unchanged.<size> equals the array cardinality, returns an empty array.NULL, returns NULL.NULL elements inside the array are preserved.<size> is negative or exceeds the array cardinality, returns an error.Remove two elements from the end:
SELECT trim_array([1, 2, 3, 4], 2);
[1, 2]
A size of zero leaves the array unchanged:
SELECT trim_array(['a', 'b', 'c'], 0);
["a", "b", "c"]
Remove all elements:
SELECT trim_array([1, 2, 3], 3);
[]
NULL elements and nested arrays are supported:
SELECT trim_array([[1, NULL], [2, 3], [4, 5]], 1);
[[1, null], [2, 3]]
A NULL argument produces NULL:
SELECT trim_array(CAST(NULL AS ARRAY<INT>), 0);
NULL
A size larger than the array cardinality produces an error:
SELECT trim_array([1, 2, 3], 4);
ERROR 1105 (HY000): size must not exceed array cardinality 3: 4
ARRAY, TRIM, TRIM_ARRAY