SQL Array Functions: Creation, Membership, Transform, and array_agg
How Basin SQL array columns work, which scalar functions to use for lists, and when to use array_agg in GROUP BY.
This page summarises Cloudflare Basin SQL documentation on array (list) columns and the scalar and aggregate functions that create, inspect, and change them. The scalar catalogue lives under Array functions; column behaviour and worked examples appear in Complex types. Grouping uses array_agg from Aggregate functions.
Array columns: ordered lists and 1-based index
Section titled “Array columns: ordered lists and 1-based index”Array columns hold ordered lists of values. Indexing is 1-based, not zero-based like many programming languages.
Array columns contain ordered lists of values. Array indexing is 1-based.
— Cloudflare, Complex types
To read position 2, use array_element(arr, 2) (or bracket notation equivalent to it in Basin SQL). array_slice also uses 1-based begin and end, and negative bounds count from the end of the array.
Most functions accept an array expression built from constants, columns, nested calls, or operators. Many names have list_* aliases (list_append, list_concat, and so on) that behave the same as the array_* forms—see the alias tables in Array functions.
Creating arrays: make_array, string_to_array, range, and generate_series
Section titled “Creating arrays: make_array, string_to_array, range, and generate_series”How to build an array from literals or columns
Section titled “How to build an array from literals or columns”make_array(expression1[, ..., expression_n]) (alias make_list) builds an Arrow array from expressions:
SELECT make_array(1, 2, 3) AS numsFROM my_namespace.sales_dataLIMIT 1Returns an Arrow array using the specified input expressions.
— Cloudflare, Array functions
The same pattern appears in Complex types.
How to split a delimited string into an array
Section titled “How to split a delimited string into an array”string_to_array(categories, ',') splits text on a delimiter—typical for tag or category columns stored as strings:
SELECT string_to_array(categories, ',') AS cat_arrayFROM my_namespace.productsWHERE categories IS NOT NULLLIMIT 5range vs generate_series: exclusive vs inclusive end
Section titled “range vs generate_series: exclusive vs inclusive end”These two are easy to confuse because range lists generate_series as an alias in the scalar reference, while Complex types documents different semantics:
| Function | Stop bound | Example result |
|---|---|---|
range(start, stop[, step]) |
Exclusive — start <= x < end |
range(0, 5) → [0, 1, 2, 3, 4] |
generate_series(start, stop[, step]) |
Inclusive | generate_series(1, 5) → [1, 2, 3, 4, 5] |
The range start..end contains all values with
start <= x < end. It is empty if start >= end.— Cloudflare, Array functions
Similar to the range function, but it includes the upper bound.
— Cloudflare, Array functions
For date ranges, range requires three non-NULL arguments (start, end, step); partial forms that work for numbers are not allowed for dates—see the examples in Array functions. Step cannot be 0 for either generator.
Size, shape, and emptiness
Section titled “Size, shape, and emptiness”array_length, cardinality, array_ndims, and array_dims
Section titled “array_length, cardinality, array_ndims, and array_dims”array_length(array[, dimension])returns length along a dimension (default behaviour in examples uses a flat list).cardinality(array)returns the total element count (including nested structure flattened for counting in the scalar examples).
Complex types notes that cardinality is described there as an alias for array_length for simple cases; the scalar reference treats cardinality as total elements across nested data—match your use case to the doc you are following.
array_ndims counts dimensions; array_dims returns an array describing each dimension size (for example [[1,2,3],[4,5,6]] → [2, 3] in the scalar docs).
empty(): boolean vs 0/1
Section titled “empty(): boolean vs 0/1”Complex types describes empty(arr) as true when the array has zero elements. The scalar reference states:
Returns 1 for an empty array or 0 for a non-empty array.
— Cloudflare, Array functions
Treat the return type as whatever your query context expects, and confirm with a one-row SELECT if the distinction matters for WHERE or joins.
Membership and search: array_has, array_position, array_positions
Section titled “Membership and search: array_has, array_position, array_positions”Check one value or compare two arrays
Section titled “Check one value or compare two arrays”| Function | Meaning |
|---|---|
array_has(array, element) |
true if the array contains the element (aliases array_contains, list_has) |
array_has_all(array, sub-array) |
true if every element of sub-array appears in array |
array_has_any(array, sub-array) |
true if any element appears in both |
Find index of a value
Section titled “Find index of a value”array_position(array, element[, index]) returns the first occurrence (1-based). Complex types adds that it returns 0 if not found.
array_positions(array, element) returns all positions as an array (e.g. duplicates of 2 → [2, 3]).
Order and deduplication: array_sort, array_distinct, array_reverse, flatten
Section titled “Order and deduplication: array_sort, array_distinct, array_reverse, flatten”array_sort(array[, desc, nulls_first]) sorts ascending by default; desc and nulls_first follow ASC/DESC and NULLS FIRST/NULLS LAST style flags.
array_distinct removes duplicate values while preserving a single copy of each distinct value.
array_reverse reverses element order.
Converts an array of arrays to a flat array … Applies to any depth of nested arrays … Does not change arrays that are already flat.
— Cloudflare, Array functions
Complex types describes flatten as flattening nested arrays by one level in its example; for deeply nested data, rely on the scalar definition above.
Transform without changing type: slice, concat, append, remove, replace
Section titled “Transform without changing type: slice, concat, append, remove, replace”array_slice and resizing
Section titled “array_slice and resizing”array_slice(array, begin, end[, stride]) returns a sub-array; begin/end are inclusive and 1-based; stride defaults to 1.
array_resize(array, size, value) sets length to size, filling new slots with value or empty if value is omitted.
array_repeat(element, count) repeats a scalar or nest repeats (e.g. array_repeat([1, 2], 2) → [[1, 2], [1, 2]]).
End and front edits
Section titled “End and front edits”| Function | Effect |
|---|---|
array_append / array_push_back |
Add one element at end |
array_prepend / array_push_front |
Add one element at front |
array_concat(a1, a2, …) |
Concatenate arrays in order |
array_pop_back / array_pop_front |
Drop last or first element |
Value-level remove and replace
Section titled “Value-level remove and replace”Removals target equality with a given value:
array_remove— first match onlyarray_remove_n— firstmaxmatchesarray_remove_all— every match
Replacements use from and to:
array_replace— first occurrencearray_replace_n— firstmaxoccurrencesarray_replace_all— all occurrences
Set operations on two arrays
Section titled “Set operations on two arrays”| Function | Result |
|---|---|
array_intersect |
Elements in both arrays |
array_union |
All elements from both, without duplicates |
array_except |
Elements in the first array not in the second |
Extrema and strings: array_max, array_min, array_to_string
Section titled “Extrema and strings: array_max, array_min, array_to_string”Complex types documents array_max and array_min over numeric arrays, and array_any_value for the first non-NULL element.
array_to_string(array, delimiter) converts each element to text and joins with the delimiter (aliases array_join, list_to_string). Nested arrays are flattened to a single delimited string in the scalar example on Array functions.
array_agg: collect GROUP BY values into one array
Section titled “array_agg: collect GROUP BY values into one array”For analytics pipelines, array_agg is an aggregate (not a scalar) that rolls up rows:
Collects values from a group into an array.
— Cloudflare, Aggregate functions
SELECT department, array_agg(customer_id) AS customersFROM my_namespace.sales_dataGROUP BY departmentUse it when you need one row per group with a list of member values (for example all customer IDs in a department), not when you only need scalar edits on an existing array column.
Deprecated names and common mistakes
Section titled “Deprecated names and common mistakes”Common mistakes when writing Basin SQL against arrays:
- Zero-based indexing — use
1for the first element inarray_elementandarray_slice. - Mixing
rangeandgenerate_series— remember exclusive vs inclusive upper bound. - Expecting
array_removeto drop all copies — only the first equal element; usearray_remove_allfor every match. - Assuming
array_unionkeeps order or duplicates — documentation defines union without duplicates across both inputs. - Ignoring alias typos in upstream docs — prefer the primary
array_*name from Array functions; somelist_*stub pages in the cluster point at the wrong target (for examplelist_distinctlisted as alias ofarray_dims).
regexp_match in Scalar functions returns the first regex match as an array—related to type shape but not the general array toolkit above.
How to check this on your site
Section titled “How to check this on your site”-
Open Complex types and run the
make_array,range, andarray_hasexamples against a table in your namespace to confirm 1-based positions match your expectations. -
In Array functions, run the documented
SELECTforarray_sort,array_slice, andarray_remove_non literal arrays to verify sort order, slice bounds, and partial removal. -
Compare
range(0, 5)andgenerate_series(0, 5)in one query and confirm the upper bound is excluded vs included per Complex types and Array functions. -
Run the
array_aggexample from Aggregate functions withGROUP BYon a column you control and inspect that each group returns one array column. -
You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”If something on this page does not match the official Basin SQL docs or behaves differently in your environment, email yimasynthetic@gmail.com with the page URL and the exact sentence or example you are questioning.