BigQuery · Array function
BigQuery OFFSET and SAFE_OFFSET
arr[OFFSET(n)] returns the array element at zero-based position n and fails if n is out of range; arr[SAFE_OFFSET(n)] returns NULL instead. ORDINAL(n) and SAFE_ORDINAL(n) do the same with one-based positions.
BigQuery reads array elements with a subscript and a position keyword. OFFSET counts from 0 and ORDINAL counts from 1, so arr[OFFSET(0)] and arr[ORDINAL(1)] both return the first element. A bare index, arr[0], behaves like OFFSET.
OFFSET, ORDINAL and the bare index raise Array index N is out of bounds when the position doesn't exist, and one short array anywhere in the data fails the whole query. The SAFE_ variants return NULL instead, which is what you want for data of uneven shape, such as the parts of SPLIT or optional values. Negative positions are always out of range; there is no Python-style arr[-1].
The position can be any INT64 expression, including a column or ARRAY_LENGTH(arr) - 1. Combined with UNNEST's WITH OFFSET, it reads a second array at the same position, which is how you zip two parallel arrays.
Two other OFFSETs are unrelated: UNNEST(arr) WITH OFFSET adds a position column (see UNNEST), and LIMIT 10 OFFSET 20 skips rows. JSON values have their own subscript, json_col[0], which returns SQL NULL for a missing position without any SAFE keyword.
Syntax
array_expression[OFFSET(zero_based_position)]
array_expression[SAFE_OFFSET(zero_based_position)]
array_expression[ORDINAL(one_based_position)]
array_expression[SAFE_ORDINAL(one_based_position)]
array_expression[zero_based_position]Arguments and return value
| Argument | Description |
|---|---|
array_expression | The ARRAY to read from. |
position | An INT64 expression: a literal, a column or a calculation. FLOAT64 values are rejected. |
Returns: The element type T of ARRAY<T>. SAFE_OFFSET and SAFE_ORDINAL return NULL when the position is out of range.
Examples
Zero-based and one-based access
SELECT
['bronze', 'silver', 'gold'][OFFSET(0)] AS by_offset,
['bronze', 'silver', 'gold'][ORDINAL(1)] AS by_ordinal,
['bronze', 'silver', 'gold'][2] AS bare_index,
['bronze', 'silver', 'gold'][SAFE_OFFSET(3)] AS out_of_range;| by_offset | by_ordinal | bare_index | out_of_range |
|---|---|---|---|
| bronze | bronze | gold | NULL |
Read SPLIT results safely
With OFFSET(1), the second row would fail the query, because SPLIT returns a one-element array for it.
WITH signups AS (
SELECT 'ana@acme.io' AS email UNION ALL
SELECT 'not-an-email'
)
SELECT email, SPLIT(email, '@')[SAFE_OFFSET(1)] AS domain
FROM signups
ORDER BY email;| domain | |
|---|---|
| ana@acme.io | acme.io |
| not-an-email | NULL |
Negative positions and the last element
SAFE_OFFSET(-1) and SAFE_ORDINAL(0) are out of range, so they return NULL. Compute the last position from ARRAY_LENGTH instead.
SELECT
['a', 'b', 'c'][SAFE_OFFSET(-1)] AS negative_index,
['a', 'b', 'c'][SAFE_ORDINAL(0)] AS ordinal_zero,
['a', 'b', 'c'][SAFE_OFFSET(ARRAY_LENGTH(['a', 'b', 'c']) - 1)] AS last_element;| negative_index | ordinal_zero | last_element |
|---|---|---|
| NULL | NULL | c |
Pair two arrays by position
WITH latency AS (
SELECT ['p50', 'p90', 'p99'] AS labels, [120, 340, 910] AS values_ms
)
SELECT label, values_ms[SAFE_OFFSET(pos)] AS value_ms
FROM latency
CROSS JOIN UNNEST(labels) AS label WITH OFFSET AS pos
ORDER BY pos;| label | value_ms |
|---|---|
| p50 | 120 |
| p90 | 340 |
| p99 | 910 |
Common mistakes
- Mixing up zero- and one-based positions.
arr[1]is the second element, while PostgreSQL users expect the first. Writearr[ORDINAL(1)]when you think in one-based terms. - Using OFFSET on data of varying length, such as SPLIT results. The first short array stops the query; SAFE_OFFSET returns NULL for that row.
- Trying negative positions.
arr[OFFSET(-1)]fails with "Array index -1 is out of bounds (underflow)"; use ARRAY_LAST. - Reading every SAFE_OFFSET NULL as a missing position. Inside a query an array can hold NULL elements, so NULL can also be the element itself.
- Using SAFE keywords on structs. The struct subscript (
s[OFFSET(0)]) supports OFFSET and ORDINAL but not SAFE_OFFSET or SAFE_ORDINAL.
FAQ
What is the difference between OFFSET and ORDINAL in BigQuery?
OFFSET positions start at 0 and ORDINAL positions start at 1, so arr[OFFSET(0)] and arr[ORDINAL(1)] return the same element. Both raise an error when the position doesn't exist.
How do I fix "Array index is out of bounds" in BigQuery?
Replace OFFSET with SAFE_OFFSET (or ORDINAL with SAFE_ORDINAL) so missing positions return NULL instead of failing the query. See Array index out of bounds for other causes.
How do I get the last element of an array?
Use ARRAY_LAST, or arr[SAFE_OFFSET(ARRAY_LENGTH(arr) - 1)] if the array can be empty.
What does WITH OFFSET do in UNNEST?
It adds a column with each element's zero-based position when you unnest an array. It works well with the subscript: unnest one array WITH OFFSET AS pos and read another with other[SAFE_OFFSET(pos)].
Related
Errors you may hit
More array functions
All BigQuery array functions · All functions · BigQuery error messages
