Skip to content

Support correlated UNNEST in subquery #11773

Open

Description

@paf31

Describe the bug

I cannot use UNNEST inside a subquery:

> SELECT id, (SELECT * FROM UNNEST(arr) LIMIT 1) FROM 
    (
      SELECT 1 id, array [1, 2] arr
        UNION
      SELECT 2 id, array [] arr
    ) rows;

check_analyzed_plan
caused by
Error during planning: Unsupported operator in the subquery plan.

Nor can I use UNNEST in a subquery on the right hand side of EXISTS:

> SELECT id FROM 
    (
      SELECT 1 id, array [1, 2] arr
        UNION
      SELECT 2 id, array [] arr
    ) rows
  WHERE EXISTS (SELECT 1 FROM UNNEST(arr));

check_analyzed_plan
caused by
Error during planning: Unsupported operator in the subquery plan.

To Reproduce

The queries above can be used in datafusion-cli.

Expected behavior

The queries above should ideally work like they do in Postgres (with some type annotations):

# SELECT id, (SELECT * FROM UNNEST(arr) LIMIT 1) FROM (SELECT 1 id, array [1, 2] arr UNION                                                                    SELECT 2 id, array [] :: integer[] arr) rows;

 id | unnest 
----+--------
  1 |      1
  2 |       
(2 rows)

# SELECT id FROM (SELECT 1 id, array [1, 2] arr UNION SELECT 2 id, array [] :: integer[] arr) rows WHERE EXISTS (SELECT 1 FROM UNNEST(arr));
 id 
----
  1
(1 row)

Additional context

Sorry if this is already tracked. It seems like there is a missing case in this function, and if this is a simple fix then I'd be happy to try to make a PR:

_ => plan_err!("Unsupported operator in the subquery plan."),

Thanks!

Activity

  1. added
    bugSomething isn't working
    on Aug 1, 2024
  2. jonahgao commented on Aug 21, 2024

    @jonahgao
    Member

    After fixing that function, it will give the following error.

    DataFusion CLI v41.0.0
    
    > SELECT id, (SELECT * FROM UNNEST(arr) LIMIT 1) FROM
        (
          SELECT 1 id, array [1, 2] arr
            UNION
          SELECT 2 id, array [] arr
        ) rows;
    This feature is not implemented: Physical plan does not support logical expression ScalarSubquery(<subquery>)

    Similar to select (select * from values(a) limit 1) from (values(1) t(a)).

    DataFusion's current support for subqueries is limited, so fixing it might require more work.

  3. findepi commented on Feb 4, 2025

    @findepi
    Member

    I cannot use UNNEST inside a subquery:

    SELECT id, (SELECT * FROM UNNEST(arr) LIMIT 1) FROM
    (
    SELECT 1 id, array [1, 2] arr
    UNION
    SELECT 2 id, array [] arr
    ) rows;

    This currently compiles, but doesn't run

    DataFusion CLI v45.0.0
    > SELECT id, (SELECT * FROM UNNEST(arr) LIMIT 1) FROM
        (
          SELECT 1 id, array [1, 2] arr
            UNION
          SELECT 2 id, array [] arr
        ) rows;
    Internal error: Physical input schema should be the same as the one converted from logical input schema. Differences:
    	- field nullability at index 1 [arr]: (physical) false vs (logical) true.
    This was likely caused by a bug in DataFusion's code and we would welcome that you file an bug report in our issue tracker
    

    This error looks like same as /similar to #13190
    I guess the original problem was fixed in #13523

    or can I use UNNEST in a subquery on the right hand side of EXISTS:

    SELECT id FROM
    (
    SELECT 1 id, array [1, 2] arr
    UNION
    SELECT 2 id, array [] arr
    ) rows
    WHERE EXISTS (SELECT 1 FROM UNNEST(arr));

    check_analyzed_plan
    caused by
    Error during planning: Unsupported operator in the subquery plan.

    This still doesn't work.
    Supporting unnest decorelation (e.g. like in Trino) would indeed be cool.

  4. added
    enhancementNew feature or request
    and removed
    bugSomething isn't working
    on Feb 4, 2025
  5. changed the title [-]UNNEST cannot appear inside a subquery[/-] [+]Support correlated UNNEST in subquery[/+] on Feb 4, 2025
  6. alamb commented on Oct 28, 2026

    @alamb
    Contributor

    I think we should implement support support for this feature as part of the more general subquery decorrelation project described here

    in my opinion WE SHOULD NOT try and extend the existing subquery rewrites to support this type of query as that risks introducing many more wrong results, see #5492 (comment)

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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions