Updating nested array in BigQuery based on value in another column

被刻印的时光 ゝ 提交于 2021-02-17 02:16:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!