Understanding STRUCTS in BigQuery

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
Search for a command to run...

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
No comments yet. Be the first to comment.
Everything you need to work effectively with nested and repeated data in BigQuery — ARRAY, STRUCT, UNNEST, ARRAY_AGG, and related functions.
I've posted earlier about STRUCTS in BigQuery, here's how I use it from time to time to help me debug and analyze data a bit faster. Since changing filter values for different test cases / observations you are interested about can be a headache (espe...
Here's a useful Dataform concept: pre_operations and post_operations. As the name implies, these represent a set of actions that run before and after the main operation (table, view, or SQL operations

BigQuery has always been a SQL engine for tabular data. Object tables add an interesting twist to that. Instead of rows containing values, an object table gives you one row per file — pointing at da

Query your data lake with warehouse-grade security and performance — without moving a single file.

Ever run a heavy BigQuery SQL query, processed gigabytes of data — and then accidentally closed the tab or forgot to save the results? 😬 Don't re-run it. Your results are still there. BigQuery automa

You can use query parameters in BigQuery hashtag#SQL (now in the console as well!) — but how are they different from variables, and when should you use each? Both parameters and variables act as place

I've previously did a short intro post about ARRAYS in BigQuery, but I do see from time to time people that are just getting started become confused about how are they different from STRUCTS and when should we use them.
Let's clarify this.
STRUCT = a bundle of "columns" inside a single column. STRUCTS fields have a mandatory data type (inferred or declared by you) and an optional name. You're still having one instance of each field, but this field can be an ARRAY of multiple things.
ARRAY = a bundle of "rows", a list inside a single row. They NEED to be of the same type - INT64, STRING, STRUCT etc. They cannot have another ARRAY directly under them, but you can get around that with an ARRAY(STRUCT([array_inside_struct])
You can still combine the two as you want, nest them in multiple layers, as long as you don't have an ARRAY directly under another ARRAY.
Both of them you can compare but you cannot order by or group by.
For STRUCT, you can totally ORDER BY or GROUP BY one of the fields in the STRUCT (as long as it's not a STRUCT or ARRAY itself).
When to use each? Let's look an example.
STRUCT = a bundle of fields that relate to the same "thing" - say your current address - city, street name, postal_code etc. Helps with a cleaner, more intuitive schema. TYPE = RECORD, MODE = NULLABLE
ARRAY = a list of things (0, 1 or more) that are related to this observation, for example a list of instruments a person plays on. TYPE = your_data_type, MODE = REPEATED
ARRAY of STRUCTS = you have a list of "things" that you know multiple things about and want to keep the together i.e. certifications => (name, from_date).
Would also be good to store all them addresses a person ever had. TYPE = RECORD, MODE = REPEATED
Found it useful? Check out to my Analytics newsletter at notjustsql.com.
Enjoyed this? Here are some related articles you might find useful: