BigQuery error · Loading and schema
Array cannot have a null element
BigQuery can use arrays containing NULL inside a query, but it can't return or store them. The error appears when a query result, a table write or a load contains an array with a NULL element, usually from ARRAY_AGG over a nullable column. Add IGNORE NULLS to ARRAY_AGG or filter NULLs out before building the array.
The error message
Array cannot have a null element; error in writing field <field>
Causes
ARRAY_AGG over a nullable column
ARRAY_AGG(x) keeps NULL inputs, so one NULL in a group makes the result unwritable. The field name in the message is your alias, or f0_ for an unnamed column.
Array literals and constructors with NULLs
SELECT [1, NULL, 3] AS numbers fails with error in writing field numbers, even though ARRAY_LENGTH([1, NULL, 3]) works inside the query. Arrays built with ARRAY(SELECT ...) over a nullable column, or from expressions such as a CASE that yields NULL, behave the same.
Nested arrays inside a STRUCT
For a struct column, the message names the path, for example error in writing field s.tags; error in writing field s.
Loading data with null array elements
Stored data has the same rule: a repeated field's value can be null or an array, but the array can't contain null values, so JSON like "tags": ["a", null] is rejected when loaded.
How to fix it
1. Use IGNORE NULLS in ARRAY_AGG
The simplest fix for aggregations.
SELECT ARRAY_AGG(x IGNORE NULLS) AS xs
FROM UNNEST([1, NULL, 3]) AS x;2. Filter NULLs in an ARRAY subquery
Works for arrays built from subqueries or rebuilt from an existing array.
SELECT ARRAY(
SELECT x FROM UNNEST([1, NULL, 3]) AS x WHERE x IS NOT NULL
) AS xs;3. Wrap values in a STRUCT to keep the NULLs
A struct whose field is NULL is not a NULL element, so an array of structs can carry missing values when you need to keep the positions.
SELECT ARRAY_AGG(STRUCT(x AS value)) AS xs
FROM UNNEST([1, NULL]) AS x;4. Handle the window-function case in two steps
ARRAY_AGG used with OVER doesn't accept IGNORE NULLS (Analytic function array_agg does not support IGNORE NULLS or RESPECT NULLS). Aggregate structs in the window, then filter in an outer query.
WITH w AS (
SELECT
o,
ARRAY_AGG(STRUCT(x AS v)) OVER (ORDER BY o) AS seen
FROM UNNEST([STRUCT(1 AS o, 10 AS x), (2, NULL), (3, 30)])
)
SELECT o, ARRAY(SELECT s.v FROM UNNEST(seen) AS s WHERE s.v IS NOT NULL) AS seen
FROM w
ORDER BY o;How to prevent it
- Default to
ARRAY_AGG(... IGNORE NULLS)unless you need the NULL positions. - Clean arrays in the producer before loading JSON or Avro with repeated fields.
- To debug, inspect the array with
FORMAT('%T', arr)orTO_JSON_STRING(arr): both return a string, which can contain NULLs.
FAQ
Why does ARRAY_LENGTH work on the same array?
Arrays with NULL elements are allowed during query evaluation. The restriction applies only when the array is written to a result or a table, so ARRAY_LENGTH([1, NULL, 3]) returns 3 without error.
Is a NULL array the same problem?
No. A whole array can be NULL; BigQuery returns it as an empty array [] and stores it as an empty array. Only NULL elements inside an array fail.
Am I charged for the failed query?
On-demand, no: failed queries aren't billed. The error happens when results are written, so under capacity pricing the slot time used up to that point still counts.
Related
SQL reference
More loading and schema
All BigQuery error messages · What 50M daily events say about BigQuery failures
