NULL NOT IN (Empty_Relation) of SQL query shows different behaviors on different engines

随声附和 提交于 2020-02-25 01:18:58

问题


I try a query which test NULL NOT IN Empty_Relation on Postrgresql, Spark and I got different results.

select count(*) from
(select 1)
where null not in
(a empty relation)

Postgresql outputs 1. The other outputs 0.

I understand the NULL behaviour of NOT IN, but my the subquery is empty relation, this situation seems more interesting. There are a lot of posts discussing NOT IN but I don't find anything related to NOT IN Empty_Relation.

So my question is more like does ANSI SQL define this behavior or this is actually a grey area, both answers could be accepted.


回答1:


tl;dr: PostgreSQL is correct.

This is what the SQL specification says about this behavior:

4) The expression RVC NOT IN IPV is equivalent to NOT ( RVC IN IPV )

5) The expression RVC IN IPV is equivalent to RVC = ANY IPV

So, NULL NOT IN (<empty relation>) is equivalent to NOT (NULL = ANY (<empty relation>))

Then, it goes on to say:

The result of R <comp op> <quantifier> T is derived by the application of the implied <comparison predicate> R <comp op> RT to every row RT in T.

[...]

d) If T is empty or if the implied <comparison predicate> is False for every row RT in T, then R <comp op> <some> T is False.

(Note: <some> is either ANY or SOME -- they both mean the same).

By this rule, since T is empty, NULL = ANY (<empty>) is False, so NOT (NULL = ANY (<empty relation>) is True.




回答2:


I'm pretty sure Postgres is correct.

Although almost every comparison with NULL returns NULL, you have found an exception. If the set is empty, then nothing is in the set. That is, any value is NOT in the set regardless of the value.

Remember, the semantics of NULL mean "unknown" value -- not missing value. "Unknown" means that it can take on any value. The expression <anything> not in (<empty set>) is true, regardless of the value of <anything>.

Incidentally, Postgres is not alone in this behavior. A cursory look shows that SQL Server and Oracle also return 1 for equivalent queries.



来源:https://stackoverflow.com/questions/58684922/null-not-in-empty-relation-of-sql-query-shows-different-behaviors-on-different

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!