Creates an array. The function accepts zero or more parameters and returns an array containing all input elements.
array([element1, element2, ...])
element1, element2, ...:Any type, elements to be included in the array. Supports zero or more parameters.Supported element types:
Return type: ARRAY
Return value meaning:
Usage notes:
Query Examples:
Create an array with multiple elements:
SELECT array(1, 2, 3, 4, 5); +----------------------+ | array(1, 2, 3, 4, 5) | +----------------------+ | [1, 2, 3, 4, 5] | +----------------------+
Create an array with elements of different types:
SELECT array(1, 'hello', 3.14, true); +----------------------------------+ | array(1, 'hello', 3.14, true) | +----------------------------------+ | ["1", "hello", "3.14", "true"] | +----------------------------------+
Create an empty array:
SELECT array(); +----------+ | array() | +----------+ | [] | +----------+
Create an array with null elements:
SELECT array(1, null, 3, null, 5); +--------------------------------+ | array(1, null, 3, null, 5) | +--------------------------------+ | [1, null, 3, null, 5] | +--------------------------------+
Create an array containing arrays:
SELECT array([1,2], [3,4], [5,6]); +----------------------------+ | array([1,2], [3,4], [5,6]) | +----------------------------+ | [[1, 2], [3, 4], [5, 6]] | +----------------------------+
Create an array containing maps:
SELECT array({'a':1}, {'b':2}, {'c':3}); +----------------------------------+ | array({'a':1}, {'b':2}, {'c':3}) | +----------------------------------+ | [{"a":1}, {"b":2}, {"c":3}] | +----------------------------------+
Create an array containing structs:
SELECT array(named_struct('name','Alice','age',20), named_struct('name','Bob','age',30)); +-----------------------------------------------------------------------------------+ | array(named_struct('name','Alice','age',20), named_struct('name','Bob','age',30)) | +-----------------------------------------------------------------------------------+ | [{"name":"Alice", "age":20}, {"name":"Bob", "age":30}] | +-----------------------------------------------------------------------------------+
Mixing complex types with basic types will cause an error:
SELECT array([1,2], 'hello'); ERROR 1105 (HY000): errCode = 2, detailMessage = can not cast from origin type ARRAY<TINYINT> to target type=TEXT
Mixing different complex types will cause an error:
SELECT array([1,2], named_struct('name','Alice','age',20)); ERROR 1105 (HY000): errCode = 2, detailMessage = Can not find the compatibility function signature: array(ARRAY<TINYINT>, STRUCT<name:VARCHAR(5),age:TINYINT>)
ARRAY