Skip to content

Correlated subquery fails with "No field named" when a JOIN ... ON in the subquery reads an outer column #27801

Backlog

Description

@tlfua

Describe the bug

A correlated subquery fails to plan when the ON clause of a join inside the subquery reads an outer column.

To Reproduce

CREATE TABLE t (a INT) AS VALUES (1), (2), (4);
CREATE TABLE i (a INT) AS VALUES (1), (2), (2), (3);
CREATE TABLE u (b INT) AS VALUES (10), (20);

SELECT o.a FROM t o WHERE EXISTS (
  SELECT 1 FROM i JOIN u ON u.b > o.a * 5
) ORDER BY o.a;

DataFusion main:

DataFusion error: Optimizer rule 'simplify_expressions' failed
caused by
Schema error: No field named o.a. Did you mean 't.a'?
Valid fields are t.a, __correlated_sq_2.b.

The same happens with a correlated WHERE as well (... ON u.b > o.a * 5 WHERE i.a = o.a).

Expected behavior

1, 2 (DuckDB 2.0.1), or a "not implemented" error if this shape can't be decorrelated.

Additional context

The outer reference in the join filter seems to survive decorrelation as a plain o.a column. This is different from #27781 , where the subquery itself is in the outer query's JOIN ... ON. Found while testing #27783 .

Willingness to contribute

I cannot contribute a fix for this bug at this time

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions