admin管理员组文章数量:1122832
Seen in Sqlite's SqlLogicTest corpus:
SELECT DISTINCT
( + + 88 ) + 69 + - - 22 / - + 0 * - 93 *
CASE -
CASE ( - COUNT ( * ) )
WHEN - 77 * - 44 - COUNT ( * ) + + 99
THEN - MIN ( - 30 )
WHEN - 30
THEN NULL
WHEN + ( + 80 ) + + 33 / + COALESCE ( - - 93, - MAX ( DISTINCT 58 ) ) + - 99 * + 90
THEN - + COUNT ( * ) - AVG ( DISTINCT - 74 ) / 10
ELSE
NULL
END
WHEN 16 THEN 3
END * + 78
----
NULL
Here we have, simplified:
Query: SELECT DISTINCT (some math containing a division by 0) * (CASE that will return NULL) * + 78
Expected result: NULL
If the division by 0 were in a branch of the CASE that was not true, I'd expect it to slip by, but here we have an unconditional division by 0. Intuitively, it seems like this should be raising an error, particularly because the unconditional division by 0 occurs to the left of the CASE and so you'd expect it to be evaluated first. But instead, the test expects to see a null from multiplying the null result of the CASE by the unconditional value, and I've confirmed on a few different databases that this is indeed the observed behavior.
Why does this happen?
Seen in Sqlite's SqlLogicTest corpus:
SELECT DISTINCT
( + + 88 ) + 69 + - - 22 / - + 0 * - 93 *
CASE -
CASE ( - COUNT ( * ) )
WHEN - 77 * - 44 - COUNT ( * ) + + 99
THEN - MIN ( - 30 )
WHEN - 30
THEN NULL
WHEN + ( + 80 ) + + 33 / + COALESCE ( - - 93, - MAX ( DISTINCT 58 ) ) + - 99 * + 90
THEN - + COUNT ( * ) - AVG ( DISTINCT - 74 ) / 10
ELSE
NULL
END
WHEN 16 THEN 3
END * + 78
----
NULL
Here we have, simplified:
Query: SELECT DISTINCT (some math containing a division by 0) * (CASE that will return NULL) * + 78
Expected result: NULL
If the division by 0 were in a branch of the CASE that was not true, I'd expect it to slip by, but here we have an unconditional division by 0. Intuitively, it seems like this should be raising an error, particularly because the unconditional division by 0 occurs to the left of the CASE and so you'd expect it to be evaluated first. But instead, the test expects to see a null from multiplying the null result of the CASE by the unconditional value, and I've confirmed on a few different databases that this is indeed the observed behavior.
Why does this happen?
Share Improve this question edited Nov 22, 2024 at 19:54 T.S. 19.3k11 gold badges67 silver badges93 bronze badges asked Nov 22, 2024 at 18:53 Mason WheelerMason Wheeler 84.5k54 gold badges284 silver badges502 bronze badges 1- In Postgres you get division by zero error – T.S. Commented Nov 22, 2024 at 20:00
1 Answer
Reset to default 0Division by zero returns NULL in SQLite -> multiplying by NULL results in NULL etc.
SQLite does not raise arithmetic exceptions (eg. divide by zero, 1/0). SQLite returns a NULL value for 1/0.
sqllogictest wiki
本文标签: sqlWhy does this query return null and not an errorStack Overflow
版权声明:本文标题:sql - Why does this query return null and not an error? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301458a1931184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论