fornwall opened a new issue, #24513:
URL: https://github.com/apache/datafusion/issues/24513
### Describe the bug
A scalar subquery that returns no rows evaluates to `NULL`, even if its
projected expression is non-nullable. DataFusion instead derives the scalar
subquery's nullability from its projected field.
As a result, a zero-row scalar subquery with a non-nullable projection can
produce a `NULL` value in a schema declared non-nullable. It can also cause
expression simplification to incorrectly fold `IS NULL` to `false`.
### To Reproduce
Run:
```console
cargo run --quiet -p datafusion-cli -- -q -c \
"SELECT (SELECT 1 WHERE FALSE) AS scalar_value;"
cargo run --quiet -p datafusion-cli -- -q -c \
"SELECT (SELECT 1 WHERE FALSE) IS NULL AS is_null;"
```
The first query fails because the result contains a null value while its
schema declares the field non-nullable. The second query returns `false`
because the optimizer simplifies the predicate using that incorrect nullability.
### Expected behavior
The first query should return one row containing `NULL`, and the second
query should return `true`:
```text
+--------------+
| scalar_value |
+--------------+
| NULL |
+--------------+
+---------+
| is_null |
+---------+
| true |
+---------+
```
Scalar subqueries should be considered nullable whenever they may return no
rows, regardless of the nullability of their projected field.
### Additional context
_No response_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]