Google BigQuery:已连接 table 的所有具有重复名称的列的前缀

Google BigQuery: prefix for all columns of joined table with duplicated names

在 Google BigQuery(使用#standardSQL)上,当 2 table 之间有一个 Join 时,我需要对每个 table 的所有列应用固定前缀.

这是场景,我有这样的结构:

#standardSQL
WITH user AS (
  SELECT "john" as name, "smith" as surname, 1 as parent
  UNION ALL
  SELECT "maggie" as name, "smith" as surname, 2 as parent
),

parent AS (
  SELECT 1 as id, "john" as name, "doe" as surname
  UNION ALL
  SELECT 2 as id, "jane" as name, "smith" as surname
)

用户table

+-----+--------+---------+--------+
| Row |  name  | surname | parent |
+-----+--------+---------+--------+
|   1 | john   | smith   |      1 |
|   2 | maggie | smith   |      2 |
+-----+--------+---------+--------+

Parent table

+-----+----+------+---------+
| Row | id | name | surname |
+-----+----+------+---------+
|   1 |  1 | john | doe     |
|   2 |  2 | jane | smith   |
+-----+----+------+---------+

这样的查询

SELECT u.*, p.* FROM user u JOIN parent p ON u.parent = p.id

产生以下错误

Error: Duplicate column names in the result are not supported. Found duplicate(s): name, surname

我想避免像这样table执行自定义别名

SELECT
  u.name as user_name,
  u.surname as user_surname,
  p.name as parent_name,
  p.surname as parent_surname
FROM user u JOIN parent p ON u.parent = p.id

+-----+-----------+--------------+-------------+----------------+
| Row | user_name | user_surname | parent_name | parent_surname |
+-----+-----------+--------------+-------------+----------------+
|   1 | john      | smith        | john        | doe            |
|   2 | maggie    | smith        | jane        | smith          |
+-----+-----------+--------------+-------------+----------------+

如果 table 将更改字段,我将需要每次编辑语句(或语句)以应用具有给定前缀的新字段。所以这种使用固定列名的方法不是suitable方式

有没有办法,查询运算符,为了获得上面提到的 table,自动应用前缀?类似于:

SELECT u.* AS user_*, p.* AS parent_*
FROM user u JOIN parent p ON u.parent = p.id

到目前为止我能想到的唯一选择如下

#standardSQL
WITH user AS (
  SELECT "john" AS name, "smith" AS surname, 1 AS parent UNION ALL
  SELECT "maggie" AS name, "smith" AS surname, 2 AS parent
), parent AS (
  SELECT 1 AS id, "john" AS name, "doe" AS surname UNION ALL
  SELECT 2 AS id, "jane" AS name, "smith" AS surname   
)
SELECT user, parent  
FROM user  
JOIN parent 
ON user.parent = parent.id  

结果为

Row user.name   user.surname    user.parent parent.id   parent.name parent.surname   
1   john        smith           1           1           john        doe  
2   maggie      smith           2           2           jane        smith   

它并不完全符合您的期望,但最接近它,因为它将来自各个连接表的每一行包装到各自的 STRUCT 中 - 例如:

{
"user": {"name": "john", "surname": "smith","parent": "1"},
"parent": {"id": "1","name": "john","surname": "doe"}
}