Array Functions


Array Aggregation

arrayAgg

Description:

Concatenates the input values into an array. Optionally removes duplicates when isDistinct is set to true. This function is commonly used to aggregate multiple rows into a single array grouped by a dimension.

Signature:

ARRAY_AGG(dataColumn, 'dataType' [, isDistinct])

Arguments:

  • dataColumn - The input column or expression to aggregate. Can be a scalar or an array type.

  • 'dataType' - The element type of the resulting array. Must be a string literal (e.g., 'STRING', 'INT', 'LONG', 'DOUBLE').

  • isDistinct (optional) - Boolean flag to include only distinct elements. Defaults to false.

Returns:

An array of the specified dataType containing all aggregated values.

Example:

  1. Aggregate scalar values into an array

Results:

  1. Aggregate array values across rows

Results:

Notes:

  • When the input column is an array, all sub-arrays are flattened before aggregation.

  • When isDistinct is true, duplicate elements are removed from the final array.

  • The order of elements in the output array is not guaranteed.

  • Supports both scalar and array input types for numeric and string data.


LISTAGG

Description:

Concatenates the input values into a single string, with an optional delimiter.

Similar to ARRAY_AGG, but produces a string instead of an array.

LISTAGG is useful for generating comma-separated lists or other delimited strings from multiple rows.

Signature:

Arguments:

  • dataColumn — The input column or expression to concatenate.

  • delimiter (optional) — A string used to separate values in the output. Defaults to ','.

  • isDistinct (optional) — Boolean flag to include only distinct elements. Defaults to false.

Returns:

A single concatenated STRING containing all values (optionally distinct), separated by the given delimiter.

Examples:

  1. Concatenate names with commas

Result:

  1. Concatenate array input

Result:

Notes:

  • If the input column is an array, all sub-arrays are flattened before concatenation.

  • When isDistinct is true, duplicate values are removed before joining.

  • The output order of elements is not guaranteed.

  • The delimiter argument is optional; default is a comma ','.


sumArrayLong

Description:

Computes the sum of all elements in an array of LONG (or integer-compatible) values across all input rows.

This function is useful for aggregating numeric arrays, such as metrics or counters, into a single scalar value.

Signature:

Arguments:

  • arrayColumn — Input column containing arrays of numeric (LONG or INT) values.

Returns:

A LONG representing the sum of all elements across all arrays in the group.

Examples:

  1. Sum elements of arrays across rows

Input:

Result:

  1. With GROUP BY

Result:

Notes:

  • NULL and empty arrays are ignored.

  • All numeric values are coerced to LONG before summation.

  • If any element is non-numeric, the query will fail.


sumArrayDouble

Description:

Computes the sum of all elements in an array of DOUBLE (floating-point) values across all input rows.

This is the double-precision variant of sumArrayLong, and is typically used for aggregating numeric arrays with decimal values, such as scores, probabilities, or weights.

Signature:

Arguments:

  • arrayColumn — Input column containing arrays of numeric (DOUBLE or FLOAT) values.

Returns:

A DOUBLE representing the sum of all elements across all arrays in the group.

Examples:

  1. Sum elements of arrays across rows

Input:

Result:

  1. With GROUP BY

Notes:

  • NULL and empty arrays are ignored.

  • All numeric elements are coerced to DOUBLE before summation.

  • Use sumArrayLong for integer data to avoid type conversion overhead.


Array Reversal

arrayReverseInt

Description: Reverses the order of elements in an integer array. Syntax: arrayReverseInt(int_array) Example:

arrayReverseString

Description: Reverses the order of elements in a string array. Syntax: arrayReverseString(string_array) Example:


Array Sorting

arraySortInt

Description: Sorts an integer array in ascending order. Syntax: arraySortInt(int_array) Example:

arraySortString

Description: Sorts a string array lexicographically. Syntax: arraySortString(string_array) Example:


Array Index Operations

arrayIndexOfInt

Description: Returns the first 0-based index of an integer value. Syntax: arrayIndexOfInt(int_array, value) Example:

arrayIndexOfString

Description: Returns the first 0-based index of a string value. Syntax: arrayIndexOfString(string_array, value) Example:

arrayIndexesOfInt

Description: Returns all indices of an integer value. Syntax: arrayIndexesOfInt(int_array, value) Example:

arrayIndexesOfLong

Description: Returns all indices of a long value. Syntax: arrayIndexesOfLong(long_array, value) Example:

arrayIndexesOfFloat

Description: Returns all indices of a float value. Syntax: arrayIndexesOfFloat(float_array, value) Example:

arrayIndexesOfDouble

Description: Returns all indices of a double value. Syntax: arrayIndexesOfDouble(double_array, value) Example:

arrayIndexesOfString

Description: Returns all indices of a string value. Syntax: arrayIndexesOfString(string_array, value) Example:


Array Intersection

intersectIndices

Description: Returns common indices between two sorted integer arrays. Syntax: intersectIndices(array1, array2) Example:


arraysOverlap

Description:

Returns true if the two input arrays have at least one element in common, and false otherwise.

This function is useful for checking whether two arrays share any overlapping values — for example, to test whether a user’s assigned tags intersect with a set of filter tags.

Syntax:

arraysOverlap(array1, array2)

Example:


Array Contains

arrayContainsInt

Description: Checks if an integer array contains a value. Syntax: arrayContainsInt(int_array, value) Example:

arrayContainsString

Description: Checks if a string array contains a value. Syntax: arrayContainsString(string_array, value) Example:


Array Slicing

arraySliceInt

Description: Extracts a subarray (start inclusive, end exclusive). Syntax: arraySliceInt(int_array, start, end) Example:

arraySliceString

Description: Extracts a string subarray. Syntax: arraySliceString(string_array, start, end) Example:


Array Distinct

arrayDistinctInt

Description: Removes duplicate integers. Syntax: arrayDistinctInt(int_array) Example:

arrayDistinctString

Description: Removes duplicate strings. Syntax: arrayDistinctString(string_array) Example:


Array Remove

arrayRemoveInt

Description: Removes the first occurrence of an integer. Syntax: arrayRemoveInt(int_array, value) Example:

arrayRemoveString

Description: Removes the first occurrence of a string. Syntax: arrayRemoveString(string_array, value) Example:


Array Union

arrayUnionInt

Description: Combines two integer arrays with unique values. Syntax: arrayUnionInt(array1, array2) Example:

arrayUnionString

Description: Combines two string arrays with unique values. Syntax: arrayUnionString(array1, array2) Example:


Array Concatenation

arrayConcatInt

Description: Concatenates two integer arrays. Syntax: arrayConcatInt(array1, array2) Example:

arrayConcatLong

Description: Concatenates two long arrays. Syntax: arrayConcatLong(array1, array2) Example:

arrayConcatFloat

Description: Concatenates two float arrays. Syntax: arrayConcatFloat(array1, array2) Example:

arrayConcatDouble

Description: Concatenates two double arrays. Syntax: arrayConcatDouble(array1, array2) Example:

arrayConcatString

Description: Concatenates two string arrays. Syntax: arrayConcatString(array1, array2) Example:


Array Element Access

arrayElementAtInt

Description: Returns the 1-indexed integer element. Syntax: arrayElementAtInt(array, index) Example:

arrayElementAtLong

Description: Returns the 1-indexed long element. Syntax: arrayElementAtLong(array, index) Example:

arrayElementAtFloat

Description: Returns the 1-indexed float element. Syntax: arrayElementAtFloat(array, index) Example:

arrayElementAtDouble

Description: Returns the 1-indexed double element. Syntax: arrayElementAtDouble(array, index) Example:

arrayElementAtString

Description: Returns the 1-indexed string element. Syntax: arrayElementAtString(array, index) Example:


Array Summation

arraySumInt

Description: Sums all integers in an array. Syntax: arraySumInt(int_array) Example:

arraySumLong

Description: Sums all longs in an array. Syntax: arraySumLong(long_array) Example:


Array Construction

arrayValueConstructor

Description: Constructs an array from elements. Syntax: array(element1, element2, ...) Example:


Array Generation

generateIntArray

Description: Generates an integer sequence. Syntax: generateIntArray(start, end, increment) Example:

generateLongArray

Description: Generates a long sequence. Syntax: generateLongArray(start, end, increment) Example:

generateFloatArray

Description: Generates a float sequence. Syntax: generateFloatArray(start, end, increment) Example:

generateDoubleArray

Description: Generates a double sequence. Syntax: generateDoubleArray(start, end, increment) Example:


String Conversion

arrayToString (2-argument)

Description: Joins elements with a delimiter. Syntax: arrayToString(string_array, delimiter) Example:

arrayToString (3-argument)

Description: Joins elements with null replacement. Syntax: arrayToString(string_array, delimiter, nullString) Example:

Last updated

Was this helpful?