Skip to content

SQL: Early rejection of query with constant fail (e.g., division by zero) #6533

@philrz

Description

@philrz

The sqllogictests reveal a subtle difference in behavior where Postgres sniffs out the constant division by zero in this query at planning time:

$ psql postgres -c "SELECT DISTINCT + 31 + + NULLIF ( - CAST ( NULL AS REAL ), - + 93 + - 22 / CASE COUNT ( * ) WHEN - NULLIF ( + 11, - 22 * 52 + 20 / + COALESCE ( - COALESCE ( 0, - 31 / 65 ), - 8 ) ) THEN - COUNT ( * ) ELSE NULL END ) AS col1;"
ERROR:  division by zero

$ echo $?
1

Whereas SuperDB surfaces it during query execution, which also creates follow-on error values seemingly due to failed attempts to do things with the root "divide by zero" error:

$ super -version
Version: 292f4415e

$ super -c "SELECT DISTINCT + 31 + + NULLIF ( - CAST ( NULL AS REAL ), - + 93 + - 22 / CASE COUNT ( * ) WHEN - NULLIF ( + 11, - 22 * 52 + 20 / + COALESCE ( - COALESCE ( 0, - 31 / 65 ), - 8 ) ) THEN - COUNT ( * ) ELSE NULL END ) AS col1;"
{col1:error({message:"?-operator: bool predicate required",on:error({message:"type incompatible with unary '-' operator",on:error("divide by zero")})})}

$ echo $?
0

Details

Repro is with super commit 292f441.

It's my understanding that seeing this same result with EXPLAIN is confirmation that it's being sniffed out at planning time:

$ psql postgres -c "EXPLAIN SELECT DISTINCT + 31 + + NULLIF ( - CAST ( NULL AS REAL ), - + 93 + - 22 / CASE COUNT ( * ) WHEN - NULLIF ( + 11, - 22 * 52 + 20 / + COALESCE ( - COALESCE ( 0, - 31 / 65 ), - 8 ) ) THEN - COUNT ( * ) ELSE NULL END ) AS col1;"
ERROR:  division by zero

I became aware of this effect because it's a sqllogictest among several dozen that seemingly should have been hinted as skipif postgresql but was not. I've made a post in the SQLite Forum to see what others make of it. For our own testing purposes I intend to skip these kinds of queries in our nightly automation going forward.

I'm not necessarily advocating that we need match the Postgres behavior here, but figured I'd surface it in the event there's something to be learned or documented here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions