Explain Codes LogoExplain Codes Logo

Cannot access field in Big Query with type ARRAY>

sql
prompt-engineering
array-manipulation
bigquery
Nikita BarsukovbyNikita Barsukov·Jan 2, 2025
TLDR

Whenever there's an array of structs in BigQuery terrorizing your peace, UNNEST() is your superhero. It expands the array and makes dot notation your magic wand:

SELECT hit.hitNumber, hit.time, hit.hour FROM your_table, UNNEST(hits) AS hit

Just like that, UNNEST() turns your scary hits array into an orderly table-like line, enabling extraction of hitNumber, time, and hour.

Mastering arrays and structs manipulation

Delving into array and struct manipulation in BigQuery is like going on an adventure. Here's your backpack of knowledge:

Flattening arrays into simple rows

Consider UNNEST() your flattening hammer. It crushes your array into a set of rows. You can then directly reference your aliased struct fields like a boss:

SELECT h.hitNumber, h.time, h.hour -- get'em hits FROM `your_dataset.your_table`, UNNEST(hits) AS h -- Hammer time!

Managing nested arrays

Arrays can be nasty if they contain nested arrays. Multiple unnesting operations might be the way, but beware of cartesian joins- they could blow your result sets out of proportion.

SELECT parent.id, child.value FROM `your_dataset.your_table`, UNNEST(parentArray) AS parent, -- Unnest inception UNNEST(parent.childArray) AS child -- another one!

Null handling during array expansion

Unnesting sometimes could be ruthless, it omits rows with null arrays. Apply the LEFT JOIN CPR to revive those parent rows:

SELECT parent.id, IFNULL(child.value, "Default Value") -- If null, then it's a "Default value" party! FROM `your_dataset.your_table` parent LEFT JOIN UNNEST(parent.childArray) AS child ON TRUE

Efficient array aggregation

Post-unnesting, to reassemble results back into an array, the ARRAY_AGG() function is the glue you need. Just keep your GROUP BY clauses on check:

SELECT parent.id, ARRAY_AGG(child.value) as childValues FROM `your_dataset.your_table` parent LEFT JOIN UNNEST(parent.childArray) AS child ON TRUE GROUP BY parent.id

Consider this knowledge backpack your survival kit as you navigate the wilderness of advanced array and struct scenarios in BigQuery.

Pro tips and common pitfalls in arrays and structs world

When dealing with arrays of structs, the landscape is rugged. Here's your navigation map:

Being tactical in joining unnested arrays

When you need to join arrays from multiple tables, unnest with care and join tactically to prevent unintended values making a party:

SELECT a.id, b.value FROM `your_dataset.table_a`, UNNEST(array_a) AS a -- Unnest: check! JOIN `your_dataset.table_b`, UNNEST(array_b) AS b -- Another unnest: double check! ON a.id = b.id -- Join condition: This ain't no random party!

Handling arrays with complex structs

With arrays housing multiple struct fields, you can cherry-pick which fields to access by naming them after the dot notation:

SELECT hit.hitNumber as number, hit.page.title as pageTitle -- Selector mode activated! FROM `your_dataset.your_table`, UNNEST(hits) AS hit -- Our good ol' Unnest

Keeping an eye on performance

The SELECT statement is your bouncer, allowing only relevant fields to the party. This reduces data overload and boosts performance:

SELECT hit.hitNumber, hit.page.pagePath -- Only the important ones, please! FROM `your_dataset.your_table`, UNNEST(hits) AS hit -- Unnesting, as usual

Navigating through complex nested data in BigQuery with these pro tips is like sailing through a storm with a map and compass!