You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

BigQuery中array_concat嵌套array_agg时ignore nulls未正常生效问题问询

BigQuery中array_concat与array_agg结合的行为说明

所有你遇到的现象都是预期行为,具体原因如下:

1. array_agg(null ignore nulls)导致返回空数组的原因

array_agg(expr ignore nulls)的核心逻辑是:只聚合计算结果为非null的行,所有null行都会被忽略。当你传入null作为聚合表达式时,每一行的计算结果都是null,再加上ignore nulls参数,所有行都被过滤掉,最终聚合出来的就是空数组[]。

如果是执行select array_concat(array_agg(null ignore nulls)) ...,本质就是对空数组做拼接,结果自然是空数组;如果是和array_agg(x)拼接(比如array_concat(array_agg(x), array_agg(null ignore nulls))),结果应该还是[1,2,3,4]——如果你的测试结果是空数组,大概率是误把array_agg(x)替换成了array_agg(null ignore nulls)。

2. 针对x=4有效、x=5失效的原因

看这条SQL:

select array_concat(array_agg(x),array_agg(case when x = 4 then x end ignore nulls)) 
from unnest([1,2,3,4]) as x
  • 当数组包含4时:case when x=4 then x end在x=4时返回4(非null),其余行返回null;ignore nulls会保留这个非null的4,所以第二个array_agg得到[4],最终拼接结果是[1,2,3,4,4],符合你说的“有效”。
  • 当数组换成[1,2,3,5]时:case when x=4 then x end对所有行都返回null,ignore nulls过滤掉所有null,第二个array_agg返回空数组[];array_concat([1,2,3,5], [])的结果就是原数组[1,2,3,5]——这里的“失效”其实是正常逻辑,因为没有符合条件的非null值,所以第二个聚合结果是空数组,拼接后不改变原数组内容。

核心逻辑梳理

  • array_agg(ignore nulls):仅保留非null的聚合值,无符合条件的值时返回空数组。
  • array_concat:拼接空数组不会改变其他数组的内容;只有所有拼接参数都是空数组时,结果才是空数组。

内容的提问来源于stack exchange,提问作者dan-man

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 21:48:31