![](/img/trans.png)
[英]BigQuery IF condition then append value into Array - Standard SQL
[英]BigQuery standard SQL : checking if an array is null does not work
如果我啟動此查詢:
with a as (
select cast(null as array<string>) as x union all select ['str1','str2'] as x)
select * from a where x is null
我得到這個結果:
這是我預期的結果。
但是如果我第一次啟動這個查詢:
select cast(null as array<string>) as x union all select ['str1', 'str2'] as x
我將其結果保存在數據集“tmp”中的表“a”中,然后啟動此查詢:
select * from `tmp.a` where x is null
我得到這個結果:
我期望結果與第一個相同。 為什么兩個結果之間存在差異?
解決方法:要在兩種情況下都得到相同的結果,可以在where子句中使用函數ARRAY_LENGTH(x)=0
。
來自官方文檔
Currently, all existing data types are nullable, but conditions apply for ARRAYs.
An ARRAY can be NULL.
For example:
SELECT CAST(NULL AS ARRAY<INT64>) IS NULL AS array_is_null;
+---------------+
| array_is_null |
+---------------+
| TRUE |
+---------------+
BigQuery translates a NULL ARRAY into an empty ARRAY in the query result, although inside the query, NULL and empty ARRAYs are two distinct values.
WITH Items AS (
SELECT [] AS numbers UNION ALL
SELECT CAST(NULL AS ARRAY<INT64>))
SELECT numbers FROM Items;
+---------+
| numbers |
+---------+
| [] |
| [] |
+---------+
TL;DR:一個數組可以是 null,但如果你用 where 條件檢查它,如果它不是 null,它將起作用。 如果您想要一個真正的空數組,則必須將其轉換為CAST(NULL AS ARRAY<INT64>)
。 你可以在下面看到我的例子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.