BigQuery选择期望双嵌套列

时间:2018-10-22 08:56:15

标签: google-bigquery

我正在尝试从BigQuery表中删除列,并且已按照此处所述的说明进行操作:

https://cloud.google.com/bigquery/docs/manually-changing-schemas#deleting_a_column_from_a_table_schema

这不直接起作用,因为我要删除的列在结构中嵌套了两次。以下SO问题是相关的,但没有一个可以解决这种确切情况。

  1. 单个嵌套字段: BigQuery select * except nested column

  2. 双重嵌套字段(解决方案具有枚举架构中的所有字段,这对我来说是无用的,因为我的架构很大): BigQuery: select * replace from multiple nested column

我已经尝试调整上述解决方案,但我想我已经很接近了,但还不能完全起作用。

这将删除该字段,但仅返回嵌套字段,而不返回整个表(对于我要删除的示例a.b.field_name。请参见最后的示例架构)

SELECT AS STRUCT * EXCEPT(a), a.* REPLACE (
    (SELECT AS STRUCT a.b.* EXCEPT (field_name)) AS b
)
FROM `table`

下次尝试给我一个错误:Scalar subquery produced more than one element

WITH a_tmp AS (
    SELECT AS STRUCT a.* REPLACE (
        (SELECT AS STRUCT a.b.* EXCEPT (field_name)) AS b
    ) 
    FROM `table`
)

SELECT * REPLACE (
    (SELECT AS STRUCT a.* FROM a_tmp) AS a
)
FROM `table`

是否有解决此问题的通用方法?还是我被迫在第二个链接中使用枚举的解决方案?

示例架构:

[
    {
        "name": "a",
        "type": "RECORD",
        "fields": [
            {
                "name": "b",
                "type": "RECORD"
                "fields": [
                    {
                        "name": "field_name",
                        "type": "STRING"
                    },
                    {
                        "name": "other_field_name".
                        "type": "STRING"
                    }
                ]
            },
        ]
    }
]

我希望最终架构是相同的,但没有field_name

1 个答案:

答案 0 :(得分:1)

以下是用于BigQuery标准SQL

#standardSQL
SELECT * REPLACE(
  (SELECT AS STRUCT(SELECT AS STRUCT a.b.* EXCEPT (field_name)) b)
  AS a) 
FROM `project.dataset.table`  

您可以使用以下哑数据测试,玩游戏

#standardSQL
WITH `project.dataset.table` AS (
  SELECT STRUCT<b STRUCT<field_name STRING, other_field_name STRING>>(STRUCT('1', '2')) a
)
SELECT * REPLACE(
  (SELECT AS STRUCT(SELECT AS STRUCT a.b.* EXCEPT (field_name)) b)
  AS a) 
FROM `project.dataset.table`