Skip to content

Wrong results: scalar subqueries over distinct calls to the same table function are incorrectly deduplicated #27535

Open

Description

@charles-paperman

Describe the bug

Distinct calls to the same table function can be incorrectly deduplicated in scalar subqueries, causing one result to be reused for both columns.

Additional observations

  • The same issue occurs with range(1, 2) and range(2, 3).
  • Reversing the subqueries returns (2, 2).
  • EXPLAIN shows a single scalar subquery reused for both columns.
  • Giving the table-function calls distinct table aliases restores the correct result.

Workaround

The following setting also restores the correct result for these reproducers:

SET datafusion.optimizer.enable_physical_uncorrelated_scalar_subquery = false;

To Reproduce

SELECT
  (SELECT * FROM generate_series(1, 1)) AS first,
  (SELECT * FROM generate_series(2, 2)) AS second;

GOT: (1, 1)

Expected behavior

SELECT
  (SELECT * FROM generate_series(1, 1)) AS first,
  (SELECT * FROM generate_series(2, 2)) AS second;

SHOULD BE: (1, 2)

Additional context

  • The same issue occurs with range(1, 2) and range(2, 3).
  • Reversing the subqueries returns (2, 2).
  • EXPLAIN shows a single scalar subquery reused for both columns.
  • Giving the table-function calls distinct table aliases restores the correct result.
  SET datafusion.optimizer.enable_physical_uncorrelated_scalar_subquery = false;

Willingness to contribute

I would like to contribute a fix for this bug, but request guidance from the DataFusion community

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

      Sponsor
      SponsoredKunjungi sekarang
      Promo