Skip to content
AnswerWell geo Documentation

Research · 7 min read · Answerwell

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 nums
FROM my_namespace.sales_data
LIMIT 1

Returns 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_array
FROM my_namespace.products
WHERE categories IS NOT NULL
LIMIT 5

range 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.

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).

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”
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

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(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]]).

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

Removals target equality with a given value:

  • array_remove — first match only
  • array_remove_n — first max matches
  • array_remove_all — every match

Replacements use from and to:

  • array_replace — first occurrence
  • array_replace_n — first max occurrences
  • array_replace_all — all occurrences
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 customers
FROM my_namespace.sales_data
GROUP BY department

Use 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.

Common mistakes when writing Basin SQL against arrays:

  1. Zero-based indexing — use 1 for the first element in array_element and array_slice.
  2. Mixing range and generate_series — remember exclusive vs inclusive upper bound.
  3. Expecting array_remove to drop all copies — only the first equal element; use array_remove_all for every match.
  4. Assuming array_union keeps order or duplicates — documentation defines union without duplicates across both inputs.
  5. Ignoring alias typos in upstream docs — prefer the primary array_* name from Array functions; some list_* stub pages in the cluster point at the wrong target (for example list_distinct listed as alias of array_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.

  1. Open Complex types and run the make_array, range, and array_has examples against a table in your namespace to confirm 1-based positions match your expectations.

  2. In Array functions, run the documented SELECT for array_sort, array_slice, and array_remove_n on literal arrays to verify sort order, slice bounds, and partial removal.

  3. Compare range(0, 5) and generate_series(0, 5) in one query and confirm the upper bound is excluded vs included per Complex types and Array functions.

  4. Run the array_agg example from Aggregate functions with GROUP BY on a column you control and inspect that each group returns one array column.

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

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.