Adds an element to the end of an array. The function returns a new array containing all elements from the original array plus the newly added element.
array_pushback(ARRAY<T> arr, T element)
arr:ARRAY type, the array to which to add an elementelement:T type, the element to add to the end of the arraySupported types for T:
Return type: ARRAY
Return value meaning:
Usage notes:
Query Examples:
Add an element to the end of a string array:
SELECT array_pushback(['apple', 'banana', 'cherry'], 'date'); +----------------------------------------------------+ | array_pushback(['apple', 'banana', 'cherry'], 'date') | +----------------------------------------------------+ | ["apple", "banana", "cherry", "date"] | +----------------------------------------------------+
Add a null element to the end of an array containing null values:
SELECT array_pushback([1, null, 3], null); +------------------------------------+ | array_pushback([1, null, 3], null) | +------------------------------------+ | [1, null, 3, null] | +------------------------------------+
Add an element to the end of an empty array:
SELECT array_pushback([], 42); +--------------------------+ | array_pushback([], 42) | +--------------------------+ | [42] | +--------------------------+
Add an element to the end of a float array:
SELECT array_pushback([1.1, 2.2, 3.3], 4.4); +------------------------------------------+ | array_pushback([1.1, 2.2, 3.3], 4.4) | +------------------------------------------+ | [1.1, 2.2, 3.3, 4.4] | +------------------------------------------+
NULL arrays return NULL:
SELECT array_pushback(NULL, 1); +--------------------------+ | array_pushback(NULL, 1) | +--------------------------+ | NULL | +--------------------------+
Add an element to the end of an IP address array:
SELECT array_pushback(CAST(['192.168.1.1', '192.168.1.2'] AS ARRAY<IPV4>), CAST('192.168.1.3' AS IPV4)); +----------------------------------------------------------------------------------+ | array_pushback(CAST(['192.168.1.1', '192.168.1.2'] AS ARRAY<IPV4>), CAST('192.168.1.3' AS IPV4)) | +----------------------------------------------------------------------------------+ | ["192.168.1.1", "192.168.1.2", "192.168.1.3"] | +----------------------------------------------------------------------------------+
Add an element to the end of a nested array:
SELECT array_pushback([[1,2], [3,4]], [5,6]); +------------------------------------------+ | array_pushback([[1,2], [3,4]], [5,6]) | +------------------------------------------+ | [[1, 2], [3, 4], [5, 6]] | +------------------------------------------+
Add an element to the end of a MAP array:
SELECT array_pushback([{'a':1}, {'b':2}], {'c':3}); +----------------------------------------------+ | array_pushback([{'a':1}, {'b':2}], {'c':3}) | +----------------------------------------------+ | [{"a":1}, {"b":2}, {"c":3}] | +----------------------------------------------+
Add an element to the end of a STRUCT array:
SELECT array_pushback(array(named_struct('name','Alice','age',20), named_struct('name','Bob','age',30)), named_struct('name','Charlie','age',40)); +-------------------------------------------------------------------------------------------------------------------------------------------+ | array_pushback(array(named_struct('name','Alice','age',20), named_struct('name','Bob','age',30)), named_struct('name','Charlie','age',40)) | +-------------------------------------------------------------------------------------------------------------------------------------------+ | [{"name":"Alice", "age":20}, {"name":"Bob", "age":30}, {"name":"Charlie", "age":40}] | +-------------------------------------------------------------------------------------------------------------------------------------------+
Error with wrong number of parameters:
SELECT array_pushback([1,2,3]); ERROR 1105 (HY000): errCode = 2, detailMessage: Can not found function 'array_pushback' which has 1 arity. Candidate functions are: [array_pushback(Expression, Expression)]
Error when passing non-array types:
SELECT array_pushback('not_an_array', 1); ERROR 1105 (HY000): errCode = 2, detailMessage: Can not find the compatibility function signature: array_pushback(VARCHAR(12), TINYINT)
ARRAY, PUSHBACK, ARRAY_PUSHBACK