STRUCT: Embedded Objects
Concepts covered: Nested Structs
Grouping Related Fields Into One Column
A STRUCT is a named group of fields embedded inside a row. Instead of storing address_line1, address_city, address_state, address_zip as four separate columns, you store them as one address STRUCT with four sub-fields. Accessing them uses dot notation: address.city.
STRUCTs do not add rows. They add structure within a row. The fact table row count stays the same. This is the key difference from ARRAY (which can add logical rows via UNNEST).
| Approach | Columns | Query |
|---|---|---|
| Flat columns | address_line1, address_city, address_state, address_zip | WHERE address_city = 'Seattle' |
| STRUCT column | address STRUCT<line1, city, state, zip> | WHERE address.city = 'Seattle' |
Functionally, both approaches work. The STRUCT version is cleaner when you have many related field groups: shipping_address, billing_address, contact_info. Without STRUCTs, you end up with 12 flat columns that are hard to visually group. With STRUCTs, you have 3 columns, each with sub-fields.
Schema evolution trap in Snowflake: VARIANT columns (Snowflake's semi-structured type) accept any structure but cannot be indexed or used for partition pruning. If you query a VARIANT field frequently, extract it into a typed column for performance.
About this lesson
This section is part of the Nested Data lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.