Solved: BigQuery - Array cannot have a null element error in writing field ids

When trying to aggregate a group into an ARRAY, if there are NULL values in the group, It throws the below error:

Array cannot have a null element; error in writing field ids

Example:

Here's how my table looks like


Query:
SELECT name, ARRAY_AGG(DISTINCT order_id) AS ids
FROM table GROUP BY name

Since some of the order ids are null, it throws the below error message:

Array cannot have a null element; error in writing field ids

Solution:

You want to use the IGNORE NULLS optional clause. What this clause does is to exclude NULL values from the result of the array creation

\n\n

We can perform a query with the ARRAY_AGG() function such as this one:

\n\n
SELECT name, ARRAY_AGG(order_id IGNORE NULLS) as order_ids\nFROM `PROJECT.DATASET.TABLE`\nGROUP BY name\nORDER BY name\n
\n\n

In order to obtain the following results:

\n\n


    \n

Mike-Barn

posted on

Enjoy great content like this and a lot more !

Signup for a free account to write a post / comment / upvote posts. Its simple and takes less than 5 seconds