Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update a nested record in BigQuery using DML syntax?

I've got the following BigQuery schema, and I'm trying to update the event_dim.date field:

enter image description here

I tried the following query using standard SQL and the new BigQuery DML:

UPDATE `sara-bigquery.examples.app_events_20170113`
SET event_dim.date = '20170113'
WHERE true

But got this error:

Error: Cannot access field date on a value with type ARRAY<STRUCT<name STRING, params ARRAY<STRUCT<key STRING, 
value STRUCT<string_value STRING, int_value INT64, float_value FLOAT64, ...>>>, timestamp_micros INT64, ...>> at [2:15]

I'm able to select the nested field with this query:

 SELECT x.date FROM `sara-bigquery.examples.app_events_20170113`,
 UNNEST(event_dim) x

But can't figure out the correct UPDATE syntax.

like image 512
Sara Avatar asked Jan 13 '17 15:01

Sara


People also ask

How do you update records in BigQuery?

The BigQuery data manipulation language (DML) enables you to update, insert, and delete data from your BigQuery tables. You can execute DML statements just as you would a SELECT statement, with the following conditions: You must use standard SQL. To enable standard SQL, see Switching SQL dialects.

How do you overwrite data in BigQuery?

To append to or overwrite a table using query results, specify a destination table and set the write disposition to either: Append to table — Appends the query results to an existing table. Overwrite table — Overwrites an existing table with the same name using the query results.

How do you query nested objects in BigQuery?

BigQuery automatically flattens nested fields when querying. To query a column with nested data, each field must be identified in the context of the column that contains it. For example: customer.id refers to the id field in the customer column.

How do you overwrite a partition in BigQuery?

You can always over-write a partitioned table in BQ using the postfix of YYYYMMDD in the output table name of your query, along with using WRITE_TRUNCATE as your write disposition (i.e. to truncate whatever is existing in that partition and write new results).


Video Answer


1 Answers

That query failed because event_dim is an array of structs. This should do the trick:

UPDATE `sara-bigquery.examples.app_events_20170113`
SET event_dim = ARRAY(
  SELECT AS STRUCT * REPLACE('20170113' AS date) FROM UNNEST(event_dim)
)
WHERE true

Check out the docs on how arrays are handled in Standard SQL for more details.

like image 159
Sara Avatar answered Sep 19 '22 01:09

Sara