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
Add a comment  | 

1 Answer 1

Reset to default 0

Division 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