我尝试在 Postrgresql、Spark 上测试 NULL NOT IN Empty_Relation 的查询,我得到了不同的结果。
select count(*) from
(select 1)
where null not in
(a empty relation)
Postgresql 输出 1。 其他输出0。
我理解 NOT IN 的 NULL 行为,但我的子查询是空关系,这种情况似乎更有趣。 有很多讨论 NOT IN 的帖子,但我没有找到任何与 NOT IN Empty_Relation 相关的内容。
所以我的问题更像是 ANSI SQL 是否定义了这种行为,或者这实际上是一个灰色区域,这两个答案都可以接受。
最佳答案
tl;dr:PostgreSQL 是正确的。
这是 SQL 规范对这种行为的描述:
4) The expression
RVC NOT IN IPV
is equivalent toNOT ( RVC IN IPV )
5) The expression
RVC IN IPV
is equivalent toRVC = ANY IPV
所以,NULL NOT IN (<empty relation>)
相当于NOT (NULL = ANY (<empty relation>))
然后,它继续说:
The result of
R <comp op> <quantifier> T
is derived by the application of the implied<comparison predicate>
R <comp op> RT
to every rowRT
inT
.[...]
d) If
T
is empty or if the implied<comparison predicate>
is False for every rowRT
inT
, thenR <comp op> <some> T
is False.
(注意:<some>
是 ANY
或 SOME
-- 它们的意思相同)。
根据这条规则,自 T
是空的,NULL = ANY (<empty>)
是假的,所以 NOT (NULL = ANY (<empty relation>)
是真的。
关于sql - SQL 查询的 NULL NOT IN (Empty_Relation) 在不同的引擎上显示不同的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58684922/