[英]big query - array_agg nested within array_concat doesn't support ignore nulls properly
我不知道这是否是预期的行为,但对我来说似乎很麻烦:
如果您运行此查询...
select array_concat(
array_agg(x)
)
from unnest([1,2,3,4]) as x
您会得到一个包含[1,2,3,4]
数组的行,这是预期的。
如果你添加一个空数组,你会得到与预期相同的[1,2,3,4]
单行......
select array_concat(
array_agg(x),
[]
)
from unnest([1,2,3,4]) as x
但是,如果您添加array_agg(null ignore nulls)
,整个事情就会中断......
select array_concat(
array_agg(x),
array_agg(null ignore nulls)
)
from unnest([1,2,3,4]) as x
这将返回一个空数组!
如果上面的简化示例看起来很奇怪,请考虑这个,它适用于x=4
,并且完全打破x=5
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
问题:这是预期的吗? 如果是这样,为什么? 如果没有,是否有人知道现有的错误报告?
这是预期的行为,原因如下:
首先,此查询返回null
而不是空数组
select array_concat(
array_agg(x),
array_agg(null ignore nulls)
)
from unnest([1,2,3,4]) as x
array_agg文档说: If there are zero input rows, this function returns NULL.
,因此,array_agg(null ignore nulls) => null。
array_concat说The function returns NULL if any input argument is NULL.
所以上面的查询最终看起来像:
array_concat(
array_agg(x),
null,
)
预计会返回 null 给您。
如果您依赖某个为您转换查询结果的库。 该库可能会将 NULL 数组视为空数组,这可能是一个有问题的行为。
如果上面的简化示例看起来很奇怪,请考虑这个,它适用于 x=4,并且完全适用于 x=5
正如您 [希望] 已经意识到的那样 - 您看到的行为是预期的并且是设计使然,所以我不会重复已经回答的内容:o)
同时,我认为,您在这里真正需要的只是将您的案例重写为如下所示的内容
select array_concat_agg(
[x] ||
array(select x from unnest(['whatever']) where x = 4)
)
from unnest([1,2,3,4]) as x
与 output
现在 - 如果你用x = 5
替换x = 4
select array_concat_agg(
[x] ||
array(select x from unnest(['whatever']) where x = 5)
)
from unnest([1,2,3,4]) as x
output 是
如果您阅读 array_concat 的文档,它会指出“如果任何输入参数为 NULL,则 function 返回 NULL” - 所以您看到的行为是预期的。
顺便说一句,空数组与 null 不同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.