BigQuery · Array function
BigQuery ARRAY_LENGTH
ARRAY_LENGTH(arr) returns the number of elements in an array as INT64: 0 for an empty array and NULL for a NULL array. Use it to count items per row, find rows with empty arrays, or compute the offset of the last element.
ARRAY_LENGTH counts every element, duplicates and NULL elements included. It takes exactly one argument: BigQuery arrays are one-dimensional, so there is no dimension parameter as in PostgreSQL's array_length(arr, 1). Snowflake's ARRAY_SIZE and PostgreSQL's cardinality() map to it; neither name exists in BigQuery.
NULL and empty are different values inside a query. ARRAY_LENGTH of a NULL array is NULL, so WHERE ARRAY_LENGTH(tags) = 0 misses NULL arrays. Wrap it in IFNULL(ARRAY_LENGTH(tags), 0) when both should count as empty. Arrays stored in a table are never NULL, because BigQuery writes a NULL array as an empty one, but computed arrays (from a LEFT JOIN, a JSON function on a missing key, a CASE) can be.
For strings, use LENGTH. For a JSON array, convert it first with JSON_QUERY_ARRAY. Because offsets are zero-based, ARRAY_LENGTH(arr) - 1 is the offset of the last element, though ARRAY_LAST reads better.
Syntax
ARRAY_LENGTH(array_expression)Arguments and return value
| Argument | Description |
|---|---|
array_expression | Any ARRAY value. |
Returns: INT64: 0 for an empty array, NULL for a NULL array.
Examples
Items per order, including empty and NULL arrays
WITH orders AS (
SELECT 'A-100' AS order_id, ['keyboard', 'mouse', 'cable'] AS items UNION ALL
SELECT 'A-101', ['monitor'] UNION ALL
SELECT 'A-102', ARRAY<STRING>[] UNION ALL
SELECT 'A-103', CAST(NULL AS ARRAY<STRING>)
)
SELECT
order_id,
ARRAY_LENGTH(items) AS item_count,
IFNULL(ARRAY_LENGTH(items), 0) AS item_count_or_zero
FROM orders
ORDER BY order_id;| order_id | item_count | item_count_or_zero |
|---|---|---|
| A-100 | 3 | 3 |
| A-101 | 1 | 1 |
| A-102 | 0 | 0 |
| A-103 | NULL | 0 |
NULL elements and duplicates are counted
To count distinct values, unnest the array and use COUNT(DISTINCT).
SELECT
ARRAY_LENGTH(['a', NULL, 'c']) AS with_null_element,
(SELECT COUNT(DISTINCT x) FROM UNNEST(['a', 'a', 'c']) AS x) AS distinct_elements;| with_null_element | distinct_elements |
|---|---|
| 3 | 2 |
Length of a JSON array
SELECT ARRAY_LENGTH(
JSON_QUERY_ARRAY(JSON '{"items": [{"sku": "KB-1"}, {"sku": "MS-2"}]}', '$.items')
) AS item_count;| item_count |
|---|
| 2 |
Sessions by number of pages viewed
WITH sessions AS (
SELECT 's1' AS session_id, ['/home', '/pricing', '/signup'] AS pages UNION ALL
SELECT 's2', ['/blog'] UNION ALL
SELECT 's3', ['/docs'] UNION ALL
SELECT 's4', ['/home', '/docs']
)
SELECT ARRAY_LENGTH(pages) AS pages_viewed, COUNT(*) AS sessions
FROM sessions
GROUP BY pages_viewed
ORDER BY pages_viewed;| pages_viewed | sessions |
|---|---|
| 1 | 2 |
| 2 | 1 |
| 3 | 1 |
Common mistakes
- Calling LENGTH on an array. LENGTH accepts only STRING and BYTES and fails with No matching signature for function LENGTH.
- Testing
ARRAY_LENGTH(arr) = 0on computed arrays that can be NULL. UseIFNULL(ARRAY_LENGTH(arr), 0) = 0so NULL arrays count as empty. - Porting
array_length(arr, 1)from PostgreSQL orARRAY_SIZE(arr)from Snowflake. BigQuery's function takes one argument, and ARRAY_SIZE and CARDINALITY don't exist. - Counting distinct values with ARRAY_LENGTH. Duplicates are counted; use
(SELECT COUNT(DISTINCT x) FROM UNNEST(arr) AS x).
Cost and performance
- On a repeated record such as GA4's
event_params,ARRAY_LENGTH(event_params)references the whole record, so on-demand billing counts every field inside it.(SELECT COUNT(key) FROM UNNEST(event_params))returns the same number whenkeyis never NULL and reads only thekeyfield, which a dry run confirms.
FAQ
How do I count the elements in an array in BigQuery?
Use ARRAY_LENGTH(arr). It returns an INT64 and counts duplicates and NULL elements.
Why does ARRAY_LENGTH return NULL instead of 0?
The array itself is NULL, for example a missing JSON key or an unmatched row of a LEFT JOIN. Use IFNULL(ARRAY_LENGTH(arr), 0) when you want 0.
How do I find rows with an empty array?
For a stored array column, WHERE ARRAY_LENGTH(arr) = 0, because stored arrays are never NULL. For computed arrays, include NULL: WHERE IFNULL(ARRAY_LENGTH(arr), 0) = 0.
Related
Errors you may hit
More array functions
All BigQuery array functions · All functions · BigQuery error messages
