Skip to content

fix: Correct ANY/ALL results for unaliased subqueries on the same table - #25273

Open
AdamGS wants to merge 2 commits into
apache:mainfrom
AdamGS:adamg/fix-25258
Open

fix: Correct ANY/ALL results for unaliased subqueries on the same table#25273
AdamGS wants to merge 2 commits into
apache:mainfrom
AdamGS:adamg/fix-25258

Conversation

@AdamGS

@AdamGS AdamGS commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

During the various decorrelation passes, stripping outer references too early makes the outer and inner columns indistinguishable. Both operands are then rewritten to reference the subquery, turning the join predicate into a comparison of an inner column with itself (something like col1 > col1).

What changes are included in this PR?

Its mostly a minor change in where we strip the outer column reference from the join filter in the optimizer.

What is the testing strategy for this PR?

New SLT tests provided in the initial issue, asserting both results and logical plan shape.

Are there any user-facing changes?

Depreciating the find_join_exprs which isn't called internally anymore, replacing it with a helper function in decorrelate.rs.

@AdamGS

AdamGS commented Sep 13, 2026

Copy link
Copy Markdown
Contributor Author

I also suspect the mark join shape here is suboptimal, I'll follow up on that with a proper issue and hopefully a PR.

@codecov-commenter

codecov-commenter commented Sep 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.65079% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.91%. Comparing base (7b00b63) to head (471e01a).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/optimizer/src/decorrelate.rs 96.07% 0 Missing and 2 partials ⚠️
...on/optimizer/src/decorrelate_predicate_subquery.rs 83.33% 1 Missing ⚠️
...atafusion/optimizer/src/scalar_subquery_to_join.rs 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #25273      +/-   ##
==========================================
- Coverage   81.91%   81.91%   -0.01%     
==========================================
  Files        1134     1134              
  Lines      425637   425745     +108     
  Branches   425637   425745     +108     
==========================================
+ Hits       348654   348740      +86     
- Misses      56302    56326      +24     
+ Partials    20681    20679       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

github-actions Bot commented Sep 14, 2026

Copy link
Copy Markdown

Thank you for opening this pull request!

Reviewer note: cargo-semver-checks reported the current version number is not SemVer-compatible with the changes in this pull request (compared against the base branch).

Details
     Cloning apache/main
    Building datafusion-expr v55.1.0 (current)
       Built [  29.065s] (current)
     Parsing datafusion-expr v55.1.0 (current)
      Parsed [   0.074s] (current)
    Building datafusion-expr v55.1.0 (baseline)
       Built [  29.371s] (baseline)
     Parsing datafusion-expr v55.1.0 (baseline)
      Parsed [   0.075s] (baseline)
    Checking datafusion-expr v55.1.0 -> v55.1.0 (no change; assume patch)
     Checked [   1.551s] 223 checks: 222 pass, 1 fail, 0 warn, 31 skip

--- failure function_marked_deprecated: function #[deprecated] added ---

Description:
A function is now #[deprecated]. Downstream crates will get a compiler warning when using this function.
        ref: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-deprecated-attribute
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.50.0/src/lints/function_marked_deprecated.ron

Failed in:
  function datafusion_expr::utils::find_join_exprs in /home/runner/work/datafusion/datafusion/datafusion/expr/src/utils.rs:1434

     Summary semver requires new minor version: 0 major and 1 minor checks failed
    Finished [  61.445s] datafusion-expr
    Building datafusion-optimizer v55.1.0 (current)
       Built [  27.879s] (current)
     Parsing datafusion-optimizer v55.1.0 (current)
      Parsed [   0.037s] (current)
    Building datafusion-optimizer v55.1.0 (baseline)
       Built [  27.669s] (baseline)
     Parsing datafusion-optimizer v55.1.0 (baseline)
      Parsed [   0.030s] (baseline)
    Checking datafusion-optimizer v55.1.0 -> v55.1.0 (no change; assume patch)
     Checked [   0.187s] 223 checks: 223 pass, 31 skip
     Summary no semver update required
    Finished [  56.734s] datafusion-optimizer
    Building datafusion-sqllogictest v55.1.0 (current)
       Built [ 100.027s] (current)
     Parsing datafusion-sqllogictest v55.1.0 (current)
      Parsed [   0.022s] (current)
    Building datafusion-sqllogictest v55.1.0 (baseline)
       Built [ 100.819s] (baseline)
     Parsing datafusion-sqllogictest v55.1.0 (baseline)
      Parsed [   0.023s] (baseline)
    Checking datafusion-sqllogictest v55.1.0 -> v55.1.0 (no change; assume patch)
     Checked [   0.110s] 223 checks: 223 pass, 31 skip
     Summary no semver update required
    Finished [ 203.796s] datafusion-sqllogictest

@github-actions github-actions Bot added the auto detected api change Auto detected API change label Sep 14, 2026
/// # Return value
///
/// Tuple of (expressions containing joins, remaining non-join expressions)
#[deprecated(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function strips outer references which I think is somewhat unexpected, and was the root cause of this bug.
Its not called anymore, so deprecating it seems like a nice improvement, reducing the overall API surface.

@AdamGS AdamGS changed the title fix: Set comparison subqueries on the same table without an alias silently return 0 rows fix: Correct ANY/ALL results for unaliased subqueries on the same table Sep 14, 2026
Signed-off-by: Adam Gutglick <adamgsal@gmail.com>
Signed-off-by: Adam Gutglick <adamgsal@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto detected api change Auto detected API change logical-expr Logical plan and expressions optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)

Projects

None yet

2 participants