Skip to content

Scalar subquery returning no rows is incorrectly marked non-nullable #24513

Description

@fornwall

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:

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:

+--------------+
| 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

Activity

  1. added theissue type on Aug 20, 2026
  2. added a commit that references this issue on Aug 20, 2026
    4798476
  3. added a commit that references this issue on Aug 21, 2026
    425ebe7
  4. added 2 commits that reference this issue on Sep 27, 2026
    d1815c2
    e9dc1dd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions