Skip to content

Correlated scalar subquery with a non-equality predicate fails with a schema error over a renamed column #27691

Done

Description

@adriangb

Describe the bug

A correlated scalar subquery fails to plan when three things hold together:

  • The correlated predicate is a non-equality (<).
  • The subquery reads a projection that renames a column.
  • That projection is joined with another relation inside the subquery.

The scalar_subquery_to_join rule then refers to the column by its name before the rename.

To Reproduce

CREATE TABLE t(a INT) AS VALUES (1);
SELECT (SELECT count(*) FROM (SELECT a x FROM t) e, t u WHERE e.x < o.a) FROM t o;
Optimizer rule 'scalar_subquery_to_join' failed
Schema error: No field named e.a. Did you mean 'u.a'?

The query plans and runs if any one of these changes is made:

  • The subquery has no join (only e).
  • The projection does not rename the column (SELECT a FROM t).
  • The predicate uses = instead of <.

Expected behavior

The query returns one row with the value 0.

Additional context

Reproduced with datafusion-cli built from main at fb20da4.

Possibly related: #25837 (a LATERAL subquery with a schema error over a derived table alias) and #14554.

Activity

  1. added
    bug
    on Nov 23, 2026
  2. tlfua commented on Nov 26, 2026

    @tlfua
    Contributor

    take

  3. tlfua commented on Nov 27, 2026

    @tlfua
    Contributor

    Thanks for the report and the clear reproducer.

    I opened #27783 for the schema error. When a correlated filter is pulled up above a join inside the subquery, the join kept its old schema, so the column the filter reads was missing. With the fix, the same shape with an equality predicate (for example WHERE a = o.a) returns correct results for scalar, EXISTS, NOT EXISTS and IN subqueries.

    For the query in this issue, the result after the fix is This feature is not implemented: Physical plan does not support logical expression ScalarSubquery(<subquery>), not 0. The predicate e.x < o.a is a non-equality correlated predicate, and DataFusion does not decorrelate correlated scalar subqueries with such predicates yet (see #non_equal_correlated_scalar_subquery in subquery.slt).

    I also tried the two variants from the description on current main, without the rename and without the join. Both fail with the same not implemented error, so the rename and the join only change which error you get first.

  4. added a commit that references this issue on Nov 29, 2026
    ff5f253
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions