I've got the following BigQuery schema, and I'm trying to update the event_dim.date
field:
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.
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.
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With