如何在 BigQuery 中连接两个不同数据类型的数组?
Posted
技术标签:
【中文标题】如何在 BigQuery 中连接两个不同数据类型的数组?【英文标题】:How do I concatenate two arrays of different datatypes in BigQuery? 【发布时间】:2019-01-25 23:09:48 【问题描述】:当我尝试连接两个相同数据类型的数组时,它工作得非常好。
一个例子如下所示
#standardSQL
WITH
table1 AS (
SELECT 'a' id UNION ALL
SELECT 'b' UNION ALL
SELECT 'c'
),
table2 AS (
SELECT 'a' id, [1,2,3,4,5] array_1 UNION ALL
SELECT 'b', [1,2,3,4,5] UNION ALL
SELECT 'c', [1,2,3,4,5]
),
table3 AS (
SELECT 'a' id, [10,20,30,40,50] array_2 UNION ALL
SELECT 'b', [10,20,30,40,50] UNION ALL
SELECT 'c', []
),
joined_table as (
select table1.id,
table2.array_1,
table3.array_2
from
table1
JOIN table2
USING(id)
JOIN table3
using(id)
)
SELECT
joined_table.*,
ARRAY_CONCAT(IFNULL(joined_table.array_1,[]), IFNULL(joined_table.array_2,[])) as concatanated_arrays
FROM joined_table
但是当我尝试进行连接时,数组的数据类型不同,如下面的示例查询:
#standardSQL
WITH
table1 AS (
SELECT 'a' id UNION ALL
SELECT 'b' UNION ALL
SELECT 'c'
),
table2 AS (
SELECT 'a' id, [1,2,3,4,5] array_1 UNION ALL
SELECT 'b', [1,2,3,4,5] UNION ALL
SELECT 'c', [1,2,3,4,5]
),
table3 AS (
SELECT 'a' id, ['10','20','30','40','50'] array_2 UNION ALL
SELECT 'b', ['10','20','30','40','50'] UNION ALL
SELECT 'c', []
),
joined_table as (
select table1.id,
table2.array_1,
table3.array_2
from
table1
JOIN table2
USING(id)
JOIN table3
using(id)
)
SELECT
joined_table.*,
ARRAY_CONCAT(IFNULL(joined_table.array_1,[]), IFNULL(joined_table.array_2,[])) as concatanated_arrays
FROM joined_table
我的查询没有运行并显示如下错误:
Error: No matching signature for function ARRAY_CONCAT for argument types: ARRAY<INT64>, ARRAY<STRING>. Supported signature: ARRAY_CONCAT(ARRAY, [ARRAY, ...]) at [31:3]
如何连接这两个不同类型的数组?
【问题讨论】:
【参考方案1】:无法在 BigQuery 中连接具有不同数据类型的数组。连接数组时需要相同的数据类型。
解决手头问题的一种方法是将整数数组转换为字符串,然后将它们连接起来得到组合数组。
可以在 BigQuery 标准 SQL 中使用以下查询来演示一个这样的示例:
#standardSQL
WITH
table1 AS (
SELECT 'a' id UNION ALL
SELECT 'b' UNION ALL
SELECT 'c'
),
table2 AS (
SELECT 'a' id, [1,2,3,4,5] array_1 UNION ALL
SELECT 'b', [1,2,3,4,5] UNION ALL
SELECT 'c', [1,2,3,4,5]
),
table3 AS (
SELECT 'a' id, ['10','20','30','40','50'] array_2 UNION ALL
SELECT 'b', ['10','20','30','40','50'] UNION ALL
SELECT 'c', []
),
joined_table as (
select table1.id,
table2.array_1,
table3.array_2
from
table1
JOIN table2
USING(id)
JOIN table3
using(id)
)
SELECT
joined_table.*,
ARRAY_CONCAT(IFNULL(ARRAY(SELECT CAST(value as string) FROM UNNEST(joined_table.array_1) value),[]), IFNULL(joined_table.array_2,[])) as concatanated_arrays
FROM joined_table
但是结果最后的concatenated_arrays
变成了一个重复的字符串字段。
【讨论】:
【参考方案2】:替代(更轻量)版本 - BigQuery 标准 SQL
#standardSQL
SELECT id,
ARRAY(
SELECT CAST(val AS STRING) FROM t.array_1 val UNION ALL
SELECT * FROM t.array_2
) concatanated_arrays
FROM joined_table t
如果应用于有问题的样本数据 - 产生预期结果
【讨论】:
以上是关于如何在 BigQuery 中连接两个不同数据类型的数组?的主要内容,如果未能解决你的问题,请参考以下文章
如何将带有 POLYGON 的字符串类型转换为地理类型 - BigQuery
对两个不同用户的 BigQuery 数据访问权限,无需复制数据