问题
I have a problem with BigQuery syntax that I'm a bit stuck on. I have an entry in a table that has multiple key value pairs as an array and I would like to update only a single specific string in the values when the key is a certain value.
Here is the entry
[
{
"event_params": [
{
"key": "programType",
"value": {
"string_value": "custom",
"int_value": null,
"float_value": null,
"double_value": null
}
},
{
"key": "firebase_event_origin",
"value": {
"string_value": "app",
"int_value": null,
"float_value": null,
"double_value": null
}
},
{
"key": "firebase_screen_id",
"value": {
"string_value": null,
"int_value": "5",
"float_value": null,
"double_value": null
}
},
{
"key": "programName",
"value": {
"string_value": "overwrite_me",
"int_value": null,
"float_value": null,
"double_value": null
}
}
]
}
]
and I would like to keep everything the same except when "key" = "programName" I want to overwrite the string_value "overwrite_me" with the new string "anonymous". In general, the string_value is some arbitrary string and I just want it overwritten with the same value.
Based on a few answers here and here I have tried the following query (and various permutations of it)
SET
event_params = ARRAY(
SELECT AS STRUCT * REPLACE(
CASE WHEN event_param.key = 'programName' THEN
((SELECT AS STRUCT value.* REPLACE('anonymous' AS string_value)) AS value)
END
)
FROM UNNEST(event_params) as event_param
)
but BigQuqery always gives me syntax errors, specifically "Syntax error: Expected ")" but got keyword AS at [9:73]". I'm not quite sure what I'm doing wrong and I'm quite puzzled.
回答1:
Below is for BigQuery Standard SQL
See corrected version of very yours code
SET
event_params = ARRAY(
SELECT AS STRUCT * REPLACE(
CASE WHEN event_param.key = 'programName' THEN
(SELECT AS STRUCT * REPLACE('anonymous' AS string_value) FROM UNNEST([value]))
ELSE value
END AS value
)
FROM UNNEST(event_params) AS event_param
)
Also, note - you can remove reference to event_param
as in below example
SET
event_params = ARRAY(
SELECT AS STRUCT * REPLACE(
CASE WHEN key = 'programName' THEN
(SELECT AS STRUCT * REPLACE('anonymous' AS string_value) FROM UNNEST([value]))
ELSE value
END AS value
)
FROM UNNEST(event_params)
)
来源:https://stackoverflow.com/questions/59822154/updating-nested-array-in-bigquery-based-on-value-in-another-column