首页 > 解决方案 > 如何检查结构的所有字段是否为 NULL?

问题描述

我想检查我的 BigQuery 结构(RECORD类型,不重复)中的所有字段是否为 NULL。usingstruct_column IS NULL不起作用,因为false即使其中的所有字段struct_column都是 NULL,它也会返回。

标签: google-bigquery

解决方案


整个结构为 NULL 在语义上与其所有字段为 NULL 不同,您必须逐个字段检查。

create temp function all_fields_is_null(s struct<x int64, y int64>) 
as (s.x is null and s.y is null);

select 
 all_fields_is_null((null, null)),
 all_fields_is_null((1, null)),
 all_fields_is_null((1, 1));

输出:

+------+-------+-------+
| f0_  |  f1_  |  f2_  |
+------+-------+-------+
| true | false | false |
+------+-------+-------+

推荐阅读