ARRAY_WITH_CONSTANT
Function
ARRAY_WITH_CONSTANT
is used to generate an array of a specified length, where all elements have the given value.
Syntax
ARRAY_WITH_CONSTANT(count, element)
Parameters
-
count
: Integer type, specifies the length of the returned array. -
element
: Any storage type supported in anARRAY
.
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_REPEAT
, but the parameter order is reversed. - Can be combined with other array functions to construct more complex data.
Examples
-
Simple example
SELECT ARRAY_WITH_CONSTANT(3, 'hello');
+---------------------------------+
| ARRAY_WITH_CONSTANT(3, 'hello') |
+---------------------------------+
| ["hello", "hello", "hello"] |
+---------------------------------+ -
Special cases
SELECT ARRAY_WITH_CONSTANT(0, 'hello');
+---------------------------------+
| ARRAY_WITH_CONSTANT(0, 'hello') |
+---------------------------------+
| [] |
+---------------------------------+
SELECT ARRAY_WITH_CONSTANT(NULL, 'hello');
+------------------------------------+
| ARRAY_WITH_CONSTANT(NULL, 'hello') |
+------------------------------------+
| [] |
+------------------------------------+
SELECT ARRAY_WITH_CONSTANT(2, NULL);
+------------------------------+
| ARRAY_WITH_CONSTANT(2, NULL) |
+------------------------------+
| [null, null] |
+------------------------------+
SELECT ARRAY_WITH_CONSTANT(NULL, NULL);
+---------------------------------+
| ARRAY_WITH_CONSTANT(NULL, NULL) |
+---------------------------------+
| [] |
+---------------------------------+
-- Returns error: INVALID_ARGUMENT
SELECT ARRAY_WITH_CONSTANT(-1, 'hello');