我想知道 BigQuery 的
SELECT
AS STRUCT|VALUES
修饰符除了文档中的教科书示例之外还有哪些可能的用例。
AS STRUCT
将非标量行减少为标量`STRUCT,所以类似:
SELECT "David" AS name, 20 AS age
可以通过执行以下操作将其转换为可子查询(标量)项:
SELECT (
SELECT AS STRUCT "David" AS name, 20 AS age
)
或者通过执行以下操作将一堆调试信息添加到单个(标量)列中:
SELECT "David" AS name, 20 AS age,
(SELECT AS STRUCT 1 AS a, 2 AS b) debug
FROM (SELECT NULL)
但除此之外,我看不出有太多用处,而且我自己也从未在琐碎的调试查询之外使用过它。我想知道 BigQuery 团队中是否有人可以解释一下:
在下面的示例中,结构体用于将 tbl2 左连接到 tbl1。如果 tbl1 中有多个匹配条目,则仅取一个 (
LIMIT 1
)。因此 tbl1 的行大小保持不变。 SELECT AS STRUCT
将 tbl2 的所有行放入一个结构中。
WITH
tbl1 as (SELECT * FROM UNNEST([1,2]) AS x),
tbl2 AS (SELECT "a" AS name, 1 AS val
UNION ALL SELECT "b", 2
UNION ALL SELECT "bb", 2 )
SELECT *,
(SELECT AS STRUCT *
FROM (SELECT * FROM tbl2 WHERE x=val LIMIT 1)
) AS joined_table
FROM tbl1