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
ARRAYBuilds an array from expressionsARRAY(1, 2, 3)[1,2,3]
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_GENERATE_RANGEGenerates a sequence with optional stepARRAY_GENERATE_RANGE(0, 6, 2)[0,2,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_SIZEReturns array length (alias: ARRAY_LENGTH)ARRAY_SIZE([1,2,3])3
ARRAY_COUNTCounts non-NULL entriesARRAY_COUNT([1,NULL,2])2
ARRAY_ANYReturns the first non-NULL valueARRAY_ANY([NULL,'a','b'])'a'

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_SORTSorts values; variants control order/nullsARRAY_SORT([3,1,2])[1,2,3]

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 Aggregations & Statistics

FunctionDescriptionExample
ARRAY_SUMSum of numeric valuesARRAY_SUM([1,2,3])6
ARRAY_AVGAverage of numeric valuesARRAY_AVG([1,2,3])2
ARRAY_MEDIANMedian of numeric valuesARRAY_MEDIAN([1,3,2])2
ARRAY_MINMinimum valueARRAY_MIN([3,1,2])1
ARRAY_MAXMaximum valueARRAY_MAX([3,1,2])3
ARRAY_STDDEV_POPPopulation standard deviation (alias: ARRAY_STD)ARRAY_STDDEV_POP([1,2,3])
ARRAY_STDDEV_SAMPSample standard deviation (alias: ARRAY_STDDEV)ARRAY_STDDEV_SAMP([1,2,3])
ARRAY_KURTOSISExcess kurtosis of valuesARRAY_KURTOSIS([1,2,3,4])
ARRAY_SKEWNESSSkewness of valuesARRAY_SKEWNESS([1,2,3,4])
ARRAY_APPROX_COUNT_DISTINCTApproximate distinct countARRAY_APPROX_COUNT_DISTINCT([1,1,2])2

Array Formatting

FunctionDescriptionExample
ARRAY_TO_STRINGJoins array elements into a stringARRAY_TO_STRING(['a','b'], ',')'a,b'

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)
Try Databend Cloud for FREE

Multimodal, object-storage-native warehouse for BI, vectors, search, and geo.

Snowflake-compatible SQL with automatic scaling.

Sign up and get $200 in credits.

Try it today