Skip to content

fix: align nested piecewise merge join sorting and comparison - #26043

Merged
jayzhan211 merged 3 commits into
apache:mainfrom
mikamikasuki:fix/25957-nested-piecewise-merge-join
Oct 8, 2026
Merged

jayzhan211 merged 3 commits into
apache:mainfrom
mikamikasuki:fix/25957-nested-piecewise-merge-join

Conversation

@mikamikasuki

@mikamikasuki mikamikasuki commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Piecewise merge joins can return incorrect results for nested range keys containing inner NULLs, and fail when sorting Struct or List-of-List keys. Sorting, extreme-key selection, and comparison must use the same ordering.

What changes are included in this PR?

  • Sort </<= keys descending with NULLs last, reversing both value and inner-NULL ordering to agree with SQL comparisons.
  • Bound classic and left existence scans to non-null keys, preserving trailing NULL rows in outer, anti, and mark results.
  • Select nested min/max keys with Arrow's comparator.
  • Sort streamed keys with lexsort_to_indices, supporting Struct and List-of-List keys.
  • Execute nested range joins with PiecewiseMergeJoinExec.

What is the testing strategy for this PR?

SQL logic tests compare complete result multisets against NestedLoopJoin for List, Struct, and List-of-List keys, all four range operators, classic joins, semi/anti joins, and an EXISTS mark expression. The matrix covers batch sizes 1, 2, and 8192 and partition counts 1 and 4. Plan assertions check that Struct and List-of-List classic joins use PWMJ.

Validation passed: PWMJ SQL logic tests and fuzz tests, the extended workspace suite (including all 527 SQL test files), formatting, Clippy with all targets/features, and the repository lint checks.

Are there any user-facing changes?

Nested range joins produce correct results and support Struct and List-of-List keys with piecewise merge joins enabled. No public API changes.

@github-actions github-actions Bot added core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Oct 5, 2026

@jayzhan211 jayzhan211 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks @mikamikasuki

@jayzhan211
jayzhan211 enabled auto-merge October 5, 2026 13:53
@codecov-commenter

codecov-commenter commented Oct 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.97590% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.74%. Comparing base (3ed377a) to head (a2e660f).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
...n/src/joins/piecewise_merge_join/existence_join.rs 92.50% 3 Missing ⚠️
...lan/src/joins/piecewise_merge_join/classic_join.rs 95.83% 0 Missing and 1 partial ⚠️
...sical-plan/src/joins/piecewise_merge_join/utils.rs 94.44% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #26043   +/-   ##
=======================================
  Coverage   82.74%   82.74%           
=======================================
  Files        1147     1147           
  Lines      449767   449824   +57     
  Branches   449767   449824   +57     
=======================================
+ Hits       372160   372211   +51     
- Misses      54938    54939    +1     
- Partials    22669    22674    +5     

☔ 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.

@jayzhan211
jayzhan211 added this pull request to the merge queue Oct 5, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 5, 2026
@2010YOUY01

Copy link
Copy Markdown
Contributor

Thanks for the fix.

The root cause seem to be sorting and comparison don't agree in PWMJ, is it possible to fix that directly?

If that's tricky, this PR's approach totally makes sense, but we would keep the issue open, and add a note for the remaining fix to the root cause.

@jayzhan211

jayzhan211 commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

@mikamikasuki Would you like to rewrite this PR for the fix? I guess the gaps to the direct fix to the root cause are:

  • Sort </<= keys DESC NULLS LAST, and make the classic and left existence streams skip NULL keys at the end instead of assuming they sort first
  • Pick the min/max of nested keys with arrow's comparator instead of ScalarValue ordering
  • Sort the streamed side with lexsort_to_indices, so Struct and List-of-List keys work

They could be the follow-up if you want, change Close to Part of in the description

@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Oct 7, 2026
@mikamikasuki mikamikasuki changed the title fix: use nested loop join for nested piecewise merge keys fix: align nested piecewise merge join sorting and comparison Oct 7, 2026
@mikamikasuki

Copy link
Copy Markdown
Contributor Author

Thanks for pointing out these gaps. I’ve updated this PR to fix the ordering/comparison mismatch directly:

  • < and <= keys sort DESC NULLS LAST, and the classic and left existence joins skip trailing NULL keys.
  • Nested min/max keys use Arrow’s comparator.
  • The streamed side uses lexsort_to_indices, covering Struct and List-of-List keys.
    I’ve also removed the nested-key fallback and added regression coverage against NestedLoopJoin. The local extended workspace tests and lint checks pass.

@jayzhan211
jayzhan211 enabled auto-merge October 8, 2026 13:31
@jayzhan211

Copy link
Copy Markdown
Contributor

I think it is ready to go, thanks @mikamikasuki @2010YOUY01

@jayzhan211

Copy link
Copy Markdown
Contributor

@mikamikasuki , you don't need to merge main branch

@mikamikasuki

Copy link
Copy Markdown
Contributor Author

@mikamikasuki , you don't need to merge main branch

Got it, thanks for the clarification! I clicked the update button unnecessarily. I'll leave the branch as is and avoid merging main again.

@jayzhan211
jayzhan211 added this pull request to the merge queue Oct 8, 2026
Merged via the queue into apache:main with commit 277ddcc Oct 8, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate physical-plan Changes to the physical-plan crate sqllogictest SQL Logic Tests (.slt) v56.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PiecewiseMergeJoin returns wrong results for List keys with NULL elements and fails on Struct keys

4 participants