google-bigquery - BQ:不支持引用其他表的相关子查询 - 其中 ARRAY_AGG 不实用
问题描述
我们在 BQ 中利用ARRAY
了STRUCT
很多,直到由于主题错误而无法更改数组的内容。请参阅下面使用公共数据的简单示例。假设INNER JOIN
由于图像丢失、故意或错误而无法正常工作。
现在,我知道通常您可以将left join
数组重新定义移至FROM
子句并使用ARRAY_AGG
,但这并不总是可能的。
在我们的例子中,要更新的数组之外的“其他字段”是其他数组或结构——比如github-nested
表。
由于您无法执行SELECT DISTINCT
on STRUCT
orARRAY
字段,因此您最终需要 UNNEST 所有内容并使用许多 ARRAY_AGG、大量资源消耗和 OOM 风险从头开始重新创建表。这对于具有大量嵌套字段的表来说是不可能的。
SELECT
* EXCEPT(webDetection),
STRUCT(
webDetection.partialMatchingImages,
webDetection.pagesWithMatchingImages,
webDetection.fullMatchingImages,
ARRAY(
SELECT AS STRUCT
fmi.score,
fmi.url,
i.object_id
FROM
data.webDetection.fullMatchingImages fmi
LEFT JOIN
`bigquery-public-data.the_met.images` i
ON
fmi.url = i.original_image_url
) AS fullMatchingImages_from_met,
webDetection.webEntities
) AS webDetection
FROM
`bigquery-public-data.the_met.vision_api_data` data
知道如何避免重新聚合吗?
解决方案
假设从概念上讲您的查询是正确的,唯一的问题是错误correlated subqueries that reference other tables are not supported
- 尝试替换下面的片段
FROM
data.webDetection.fullMatchingImages fmi
LEFT JOIN
`bigquery-public-data.the_met.images` i
ON
fmi.url = i.original_image_url
和
FROM
data.webDetection.fullMatchingImages fmi
CROSS JOIN
`bigquery-public-data.the_met.images` i
WHERE
fmi.url = i.original_image_url
更新,添加不匹配的网址
SELECT * EXCEPT(webDetection),
STRUCT(
webDetection.partialMatchingImages,
webDetection.pagesWithMatchingImages,
webDetection.fullMatchingImages,
ARRAY(
SELECT AS STRUCT *
FROM t.webDetection.fullMatchingImages_from_met_temp
UNION ALL
SELECT AS STRUCT *, NULL
FROM t.webDetection.fullMatchingImages
WHERE NOT url IN (SELECT url FROM t.webDetection.fullMatchingImages_from_met_temp)
) AS fullMatchingImages_from_met,
webDetection.webEntities
) AS webDetection
FROM (
SELECT * EXCEPT(webDetection),
STRUCT(
webDetection.partialMatchingImages,
webDetection.pagesWithMatchingImages,
webDetection.fullMatchingImages,
ARRAY(
SELECT AS STRUCT
fmi.score,
fmi.url,
i.object_id
FROM data.webDetection.fullMatchingImages fmi
JOIN `bigquery-public-data.the_met.images` i
ON fmi.url = i.original_image_url
) AS fullMatchingImages_from_met_temp,
webDetection.webEntities
) AS webDetection
FROM `bigquery-public-data.the_met.vision_api_data` data
) t
推荐阅读
- android - 制作Banner Ad上方的所有片段
- image-processing - 如 Su 的二值化论文中所述,如何创建对比度图像?
- java - 字符串索引值访问
- javascript - React/redux prev state 随着下一个 statewide 不断变化
- ssl - 如何使用配置 OpenSSL 的策略来签署具有多个 OU 的证书?
- java - 创建名称为 authenticationTokenFilterBean 的 bean 时出错:通过字段 tokenUtils 表达的依赖关系不满足
- java - 请求相机权限会导致 String.equals 中出现 NullPointerException
- python-3.x - 我将如何在自制的 python 环境中获取 python 的内置“输入”函数的用户输入?
- javascript - 条件(三元)运算符未按预期工作(Javascript)
- javascript - 如何在新页面或标签中打开此 javascript?