Skip to main content

Array Functions

This section provides reference information for array functions in Databend. Array functions enable creation, manipulation, searching, and transformation of array data structures.

Array Creation & Construction

FunctionDescriptionExample
ARRAY_CONSTRUCTCreates an array from individual valuesARRAY_CONSTRUCT(1, 2, 3)[1,2,3]
RANGEGenerates an array of sequential numbersRANGE(1, 5)[1,2,3,4]

Array Access & Information

FunctionDescriptionExample
GETGets an element from an array by indexGET([1,2,3], 1)1
ARRAY_GETAlias for GET functionARRAY_GET([1,2,3], 1)1
CONTAINSChecks if an array contains a specific valueCONTAINS([1,2,3], 2)true
ARRAY_CONTAINSChecks if an array contains a specific valueARRAY_CONTAINS([1,2,3], 2)true

Array Modification

FunctionDescriptionExample
ARRAY_APPENDAppends an element to the end of an arrayARRAY_APPEND([1,2], 3)[1,2,3]
ARRAY_PREPENDPrepends an element to the beginning of an arrayARRAY_PREPEND(0, [1,2])[0,1,2]
ARRAY_INSERTInserts an element at a specific positionARRAY_INSERT([1,3], 1, 2)[1,2,3]
ARRAY_REMOVERemoves all occurrences of a specified elementARRAY_REMOVE([1,2,2,3], 2)[1,3]
ARRAY_REMOVE_FIRSTRemoves the first element from an arrayARRAY_REMOVE_FIRST([1,2,3])[2,3]
ARRAY_REMOVE_LASTRemoves the last element from an arrayARRAY_REMOVE_LAST([1,2,3])[1,2]

Array Combination & Manipulation

FunctionDescriptionExample
ARRAY_CONCATConcatenates multiple arraysARRAY_CONCAT([1,2], [3,4])[1,2,3,4]
ARRAY_SLICEExtracts a portion of an arrayARRAY_SLICE([1,2,3,4], 1, 2)[1,2]
SLICEAlias for ARRAY_SLICE functionSLICE([1,2,3,4], 1, 2)[1,2]
ARRAYS_ZIPCombines multiple arrays element-wiseARRAYS_ZIP([1,2], ['a','b'])[(1,'a'),(2,'b')]

Array Set Operations

FunctionDescriptionExample
ARRAY_DISTINCTReturns unique elements from an arrayARRAY_DISTINCT([1,2,2,3])[1,2,3]
ARRAY_UNIQUEAlias for ARRAY_DISTINCT functionARRAY_UNIQUE([1,2,2,3])[1,2,3]
ARRAY_INTERSECTIONReturns common elements between arraysARRAY_INTERSECTION([1,2,3], [2,3,4])[2,3]
ARRAY_EXCEPTReturns elements in first array but not in secondARRAY_EXCEPT([1,2,3], [2,4])[1,3]
ARRAY_OVERLAPChecks if arrays have common elementsARRAY_OVERLAP([1,2,3], [3,4,5])true

Array Processing & Transformation

FunctionDescriptionExample
ARRAY_TRANSFORMApplies a function to each array elementARRAY_TRANSFORM([1,2,3], x -> x * 2)[2,4,6]
ARRAY_FILTERFilters array elements based on a conditionARRAY_FILTER([1,2,3,4], x -> x > 2)[3,4]
ARRAY_REDUCEReduces array to a single value using aggregationARRAY_REDUCE([1,2,3], 0, (acc,x) -> acc + x)6
ARRAY_AGGREGATEAggregates array elements using a functionARRAY_AGGREGATE([1,2,3], 'sum')6

Array Utility Functions

FunctionDescriptionExample
ARRAY_COMPACTRemoves null values from an arrayARRAY_COMPACT([1,null,2,null,3])[1,2,3]
ARRAY_FLATTENFlattens nested arrays into a single arrayARRAY_FLATTEN([[1,2],[3,4]])[1,2,3,4]
ARRAY_REVERSEReverses the order of array elementsARRAY_REVERSE([1,2,3])[3,2,1]
ARRAY_INDEXOFReturns the index of first occurrence of an elementARRAY_INDEXOF([1,2,3,2], 2)1
UNNESTExpands an array into individual rowsUNNEST([1,2,3])1, 2, 3 (as separate rows)