ARRAY_REPEAT
Function
ARRAY_REPEAT
is used to generate an array of a specified length, where all elements have the given value.
Syntax
ARRAY_REPEAT(element, count)
Parameters
-
element
: Any storage type supported in anARRAY
. -
count
: Integer type, specifies the length of the returned array.
Return Value
- Returns an array of type
ARRAY<T>
, whereT
is the type ofelement
.- The array contains
count
copies of the sameelement
.
- The array contains
Usage Notes
- If
count = 0
orNULL
, returns an empty array. - If
element
isNULL
, all elements in the array areNULL
. - This function has the same functionality as
ARRAY_WITH_CONSTANT
, but the parameter order is reversed.
Examples
-
Simple example
SELECT ARRAY_REPEAT('hello', 3);
+---------------------------------+
| ARRAY_REPEAT('hello', 3) |
+---------------------------------+
| ["hello", "hello", "hello"] |
+---------------------------------+ -
Special cases
SELECT ARRAY_REPEAT('hello', 0);
+---------------------------------+
| ARRAY_REPEAT('hello', 0) |
+---------------------------------+
| [] |
+---------------------------------+
SELECT ARRAY_REPEAT('hello', NULL);
+------------------------------------+
| ARRAY_REPEAT('hello', NULL) |
+------------------------------------+
| [] |
+------------------------------------+
SELECT ARRAY_REPEAT(NULL, 2);
+------------------------------+
| ARRAY_REPEAT(NULL, 2) |
+------------------------------+
| [null, null] |
+------------------------------+
SELECT ARRAY_REPEAT(NULL, NULL);
+---------------------------------+
| ARRAY_REPEAT(NULL, NULL) |
+---------------------------------+
| [] |
+---------------------------------+
-- Returns error: INVALID_ARGUMENT
SELECT ARRAY_REPEAT('hello', -1);