如何在 BigQuery 中连接两个不同数据类型的数组?

How do I concatenate two arrays of different datatypes in BigQuery?

当我尝试连接两个相同数据类型的数组时,它工作得很好。

示例如下

#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]

如何连接这两个不同类型的数组?

不同数据类型的数组不能在 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变成了一个重复的字符串字段。

替代(轻量级)版本 - 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   

如果应用于有问题的样本数据 - 产生预期结果