BigQuery error · Data and type conversion
Array index is out of bounds
You read an array position that doesn't exist, for example element 3 of a 2-element array, typically SPLIT(...)[OFFSET(2)] on a string with fewer parts than expected. BigQuery fails the query instead of returning NULL. Use SAFE_OFFSET or SAFE_ORDINAL, which return NULL for missing positions.
The error message
Array index <n> is out of bounds (overflow)
Causes
SPLIT results with fewer parts than expected
SPLIT('a-b', '-')[OFFSET(2)] fails with Array index 2 is out of bounds (overflow). One malformed value (a missing delimiter, an empty string) in millions of rows is enough.
Empty arrays
[OFFSET(0)] on an empty array, such as ARRAY_AGG(...) over no matching rows or a repeated field with no entries, fails the same way.
Off-by-one between OFFSET and ORDINAL
OFFSET counts from 0 and ORDINAL from 1; a bare [n] is OFFSET. ORDINAL(0) or a negative offset fails with Array index 0 is out of bounds (underflow) or Array index -1 is out of bounds (underflow).
How to fix it
1. Use SAFE_OFFSET or SAFE_ORDINAL
Missing positions return NULL instead of failing the query.
WITH skus AS (
SELECT 'SHOE-RED-42' AS sku UNION ALL
SELECT 'HAT-BLUE' UNION ALL
SELECT 'GIFTCARD'
)
SELECT
sku,
SPLIT(sku, '-')[SAFE_OFFSET(1)] AS color,
SPLIT(sku, '-')[SAFE_OFFSET(2)] AS size
FROM skus;2. Take the first or last element safely
ARRAY_FIRST and ARRAY_LAST read the ends of an array, but they also fail on an empty array. Use [SAFE_OFFSET(0)], or [SAFE_ORDINAL(ARRAY_LENGTH(arr))] for the last element, when arrays may be empty.
SELECT
ARRAY_LAST([10, 20, 30]) AS last_value,
ARRAY<INT64>[][SAFE_OFFSET(0)] AS first_of_empty;3. Find the rows with short arrays
Check lengths to fix the data instead of hiding it.
WITH skus AS (
SELECT 'SHOE-RED-42' AS sku UNION ALL
SELECT 'GIFTCARD'
)
SELECT sku, ARRAY_LENGTH(SPLIT(sku, '-')) AS parts
FROM skus
WHERE ARRAY_LENGTH(SPLIT(sku, '-')) < 3;How to prevent it
- Default to
SAFE_OFFSETwhen parsing strings from external systems; keepOFFSETonly where a missing element should stop the pipeline. - For delimited strings with a variable number of parts, consider
REGEXP_EXTRACT, which returns NULL when there is no match. - A failed query isn't billed on-demand, but it can fail late in a long scan; under capacity pricing those slots are still used.
FAQ
What is the difference between OFFSET and ORDINAL?
OFFSET(n) is zero-based and ORDINAL(n) is one-based, so arr[OFFSET(0)] and arr[ORDINAL(1)] are the same element. Both fail on missing positions; their SAFE_ versions return NULL.
How do other databases handle a missing index?
PostgreSQL returns NULL for an out-of-range subscript, and Snowflake returns NULL too, which is why queries ported from them fail in BigQuery. SAFE_OFFSET gives the same NULL behavior.
What does (overflow) versus (underflow) mean?
Overflow means the index is past the end of the array; underflow means it is below the first position, such as a negative offset or ORDINAL(0).
Related
SQL reference
More data and type conversion
All BigQuery error messages · What 50M daily events say about BigQuery failures
