Skip to content
Advertisement

How do I use BigQuery DML to transform some fields of a struct nested within an array, within a struct, within an array?

I think this is a more complex version of the question in Update values in struct arrays in BigQuery.

I’m trying to update some of the fields in a struct, where the struct is heavily nested. I’m having trouble creating the SQL to do it. Here’s my table schema:

This is what the schema looks like in the BigQuery GUI after I create the table:

BigQuery table schema from GUI

Here’s the data I’ve inserted:

My goal is to transform the “search.query” and “metadata.value” fields. For example, uppercasing them, leaving every other column (and every other struct field) in the row unchanged.

I’m looking for a solution involving either manually specifying each column in the SQL, or preferably, one where I can only mention the columns/fields I want to transform in the SQL, omitting all other columns/fields. This is a minimal example. The table I’m working on in production has hundreds of columns and fields.

For example, that row, when transformed this way, would change from:

to:

Advertisement

Answer

preferably, one where I can only mention the columns/fields I want to transform in the SQL …

Use below approach – it does exactly what you wish – ONLY those fields that are to be updated are in use, all other (tens or hundreds …) are preserved as is

if applied to sample data in your question – result is

enter image description here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement