The STRUCT function in BigQuery creates a structured data type that groups multiple fields of different types into a single value, similar to a row or object. You use it to build nested and repeated data within a table, which is essential for working with BigQuery’s hierarchical data model.
What Is a STRUCT in BigQuery?
A STRUCT is a container that holds an ordered set of fields, each with a name and a data type. Think of it as a record or an object in programming languages. For example, a STRUCT can hold a person’s name (STRING), age (INT64), and address (another STRUCT). STRUCTs allow you to store related data together without creating separate tables or using JSON strings.
In BigQuery, STRUCTs are used in table schemas to define nested columns. When you query a STRUCT column, you access its fields using dot notation, like column.field. STRUCTs can also be repeated, creating an array of STRUCTs, which is how BigQuery handles one-to-many relationships.
How to Create a STRUCT
You can create a STRUCT using the STRUCT function or the shorthand AS STRUCT syntax. The basic syntax is:
STRUCT(expr1 [AS field_name1], expr2 [AS field_name2], ...)
Here’s a simple example:
SELECT STRUCT(1 AS id, 'Alice' AS name) AS person;
This returns a single column person of type STRUCT with fields id and name. You can also use the AS STRUCT shorthand with a row of values:
SELECT (1, 'Alice') AS person;
But the explicit STRUCT function is clearer and allows you to name fields. You can nest STRUCTs inside other STRUCTs:
SELECT STRUCT(1 AS id, 'Alice' AS name, STRUCT('NY' AS state, 10001 AS zip) AS address) AS person;
Querying STRUCT Fields
To access a field inside a STRUCT, use dot notation. For example, if you have a table people with a STRUCT column person, you can query:
SELECT person.name, person.address.zip FROM people;
You can also use STRUCTs in comparisons. Two STRUCTs are equal if they have the same field names and values. For example:
SELECT STRUCT(1 AS a, 'x' AS b) = STRUCT(1 AS a, 'x' AS b) AS is_equal;
This returns true.
STRUCT vs ARRAY in BigQuery
STRUCT and ARRAY are often confused but serve different purposes. An ARRAY is an ordered list of values of the same type, while a STRUCT is a single record with named fields of possibly different types. You can combine them: an ARRAY of STRUCTs is common for storing lists of objects.
For example, a table might have a column orders which is an ARRAY of STRUCTs, each STRUCT containing order_id and amount. To query this, you use UNNEST to flatten the array:
SELECT o.order_id, o.amount
FROM customers, UNNEST(customers.orders) AS o;
This is a fundamental pattern in BigQuery for working with nested repeated data.
Common Use Cases for STRUCT
- Nested columns in tables: Instead of flattening data into multiple columns, you can group related fields into a STRUCT.
- Passing multiple values to a function: Some BigQuery functions accept STRUCTs as arguments, like
TO_JSON. - Creating complex data types in queries: You can construct STRUCTs on the fly to return structured results from subqueries.
- Modeling hierarchical data: For example, a
userSTRUCT containingprofileandsettingsSTRUCTs.
Constructing STRUCTs from Existing Columns
You often need to create a STRUCT from multiple columns in a table. For instance, given a table with columns first_name and last_name, you can combine them:
SELECT STRUCT(first_name AS first, last_name AS last) AS full_name FROM people;
You can also use the ROW function as an alias, but STRUCT is more explicit. When you create a STRUCT from a SELECT list, the field names default to the column names. For example:
SELECT (SELECT AS STRUCT * FROM people LIMIT 1) AS person;
This creates a STRUCT with all columns from the people table.
Performance and Cost Considerations
Using STRUCTs can improve query performance by reducing joins and denormalizing data. Since BigQuery is columnar, querying a few fields from a STRUCT is efficient because only those columns are scanned. However, be cautious: if you store large STRUCTs with many fields, and you only need one field, BigQuery still scans the entire STRUCT column? Actually, BigQuery’s columnar storage treats each STRUCT field as a separate column internally, so querying a single field only scans that sub-column. This is a major advantage.
Costs are based on the amount of data processed. Nested STRUCTs can increase the logical size of a table, but if you query only specific fields, the cost is lower than querying the whole table. To minimize costs, avoid selecting entire STRUCTs when you only need a few fields.
Frequently Asked Questions
Can I update a field inside a STRUCT?
BigQuery does not support updating a single field of a STRUCT directly. You must replace the entire STRUCT value. For example, to change the name field, you would rewrite the whole STRUCT with the new value.
How do I create an array of STRUCTs?
Use the ARRAY function with STRUCTs, or use the ARRAY_AGG function to aggregate STRUCTs. For example: SELECT ARRAY_AGG(STRUCT(x AS a, y AS b)) FROM table.
What is the difference between STRUCT and RECORD?
In BigQuery, RECORD is the legacy name for STRUCT. They are the same thing; the modern term is STRUCT.
Can I compare two STRUCTs?
Yes, you can compare STRUCTs for equality using = or !=. The comparison is done field by field, and the field order must match.
How do I access a field from a STRUCT in a subquery?
If you have a subquery that returns a STRUCT, you can access its fields directly in the outer query using dot notation. For example: SELECT (SELECT STRUCT(1 AS a, 2 AS b)).a returns 1.
Related
- Rockwell 20V Max Brushless Drill Driver Review
- Select All Windows Command: Full Guide
- Counter Object Python: How It Works & When to Use It
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Oracle PL/SQL String Functions: A Practical GuideOracle PL/SQL string functions are built-in routines for measuring, searching, extracting, combining, and changing text. Common choices include…
- Check If an Excel File Is Corrupted: Signs and FixesTo check whether an Excel file is corrupted, make a copy and try opening it in Excel; if…
- Round Pedestal Table: Pros, Cons & Buying GuideA round pedestal table is a dining or occasional table supported by a single central column (the pedestal)…
- Concat Excel Multiple Cells: Formulas, Spaces, and TipsTo concat (combine) multiple cells in Excel, use =CONCAT(A2:C2) to join the values in A2 through C2 without…
- Excel Function to Extract Part of a String: ExamplesExcel has several functions for extracting part of a string: use LEFT for characters at the start, RIGHT…
- Excel Filter Shortcut Key: Apply, Toggle, and Use FiltersIn Excel for Microsoft 365 on Windows, press Ctrl+Shift+L to turn AutoFilter on or off for the selected…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.