Skip to content

[branch-56] feat/perf: derive SortMergeJoin sort options from input ordering and allow SMJ for a single partition (backport #26261) - #27850

Draft
xudong963 wants to merge 4 commits into
apache:branch-56from
xudong963:backport-56/26261-smj-sort-options
Draft

xudong963 wants to merge 4 commits into
apache:branch-56from
xudong963:backport-56/26261-smj-sort-options

Conversation

@xudong963

@xudong963 xudong963 commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Which issue does this PR close?

#26261 has not merged to main yet, so this stays a draft until it does. Once it merges, I will replace these commits with the merge commit.

Rationale for this change

With prefer_hash_join = false, the planner hardcodes ASC NULLS FIRST for every sort-merge join key. Inputs already sorted DESC, NULLS LAST, or with different options per key get sorted again. With target_partitions = 1, prefer_hash_join = false is ignored and a hash join is planned. #26261 fixes both.

What changes are included in this PR?

The four commits of #26261 (head f9bae8c), cherry-picked with -x.

The conflicts were all in physical_planner.rs. main has a null-aware sort-merge join path for single-key LeftAnti joins that branch-56 does not have. I kept the branch-56 structure without it and applied only the parts of #26261: the key order and sort option derivation (sort_merge_join_keys) and the new SMJ condition. I adjusted the nearby comments, which on main describe the null-aware path.

What is the testing strategy for this PR?

The physical_planner unit tests, sort_merge_join*.slt (including the new sort_merge_join_sort_options.slt and the new target_partitions = 1 cases), joins.slt and subquery.slt pass on this branch.

Applied on branch-56 together with the other backports, #27846 (#27021), #27847 (#27150), #27848 (#27547) and #27849 (#27732), the full sqllogictest suite, cargo fmt --check and clippy on the touched crates also pass.

Are there any user-facing changes?

Plans change only with prefer_hash_join = false. Fewer SortExecs are added under SortMergeJoinExec when the inputs are already sorted on the join keys. With target_partitions = 1, a sort-merge join is planned instead of a hash join. Results are unchanged. No API changes.

Bert Vermeiren and others added 4 commits November 30, 2026 09:42
…s.slt

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
(cherry picked from commit 71e51af)
sqllogictest now covers NULL keys, a FULL JOIN and an unsorted right input

(cherry picked from commit f9bae8c)
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.66667% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.31%. Comparing base (12d8e90) to head (84e8bc3).

Files with missing lines Patch % Lines
datafusion/core/src/physical_planner.rs 86.66% 0 Missing and 6 partials ⚠️
Flag Coverage Δ
all-tests 83.31% <86.66%> (-0.01%) ⬇️
sqllogictests 60.89% <86.66%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

@@              Coverage Diff              @@
##           branch-56   #27850      +/-   ##
=============================================
- Coverage      83.31%   83.31%   -0.01%     
=============================================
  Files           1171     1171              
  Lines         477062   477103      +41     
  Branches      477062   477103      +41     
=============================================
+ Hits          397480   397511      +31     
- Misses         55523    55526       +3     
- Partials       24059    24066       +7     
Files with missing lines Coverage Δ
datafusion/core/src/physical_planner.rs 82.66% <86.66%> (+0.02%) ⬆️

... and 4 files with indirect coverage changes

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

xudong963 added a commit that referenced this pull request Dec 1, 2026
…imestamps at minute and finer granularities (backport #27547) (#27848)

## Which issue does this PR close?

- Backport of #27547 to `branch-56`, for #24463, which lists it as a
regression in 56.
- Part of #25464.

## Rationale for this change

#25409, new in 56, stopped `date_trunc` from propagating its input
ordering for every timezone-aware timestamp. So since 56.0.0, sorting or
grouping by `date_trunc('minute', ts)` over input ordered on `ts` adds a
sort and loses clustered aggregation, where 55 did not. #27547 restores
the ordering for `minute` and finer granularities, where truncation is
arithmetic on the epoch value and cannot reorder values.

## What changes are included in this PR?

A clean cherry-pick of #27547 (`477c7b9`) with `-x`. No conflicts.

## What is the testing strategy for this PR?

The `date_trunc` unit tests and `datetime/timestamps.slt` pass on this
branch.

Applied on `branch-56` together with the other backports, #27846
(#27021), #27847 (#27150), #27849 (#27732) and #27850 (#26261), the full
sqllogictest suite, `cargo fmt --check` and clippy on the touched crates
also pass.

## Are there any user-facing changes?

Plans only: no `SortExec`, and clustered aggregation, for `date_trunc`
at `minute` and finer over ordered timezone-aware input. Results are
unchanged. No API changes.
alamb pushed a commit that referenced this pull request Dec 1, 2026
…ue` (backport #27150) (#27847)

## Which issue does this PR close?

- Backport of #27150 to `branch-56`, for #24463.
- Closes #27149 for the 56 line.

## Rationale for this change

Without an `ORDER BY`, `first_value` has no `GroupsAccumulator`, so
grouped aggregation keeps one boxed accumulator per group. Every
`DISTINCT ON` without `ORDER BY` plans unordered `first_value`. #27150
measured 6x to 18x faster queries and up to 2.7x lower peak memory on
ClickBench queries with 17.6M groups.

## What changes are included in this PR?

A clean cherry-pick of #27150 (`26de352`) with `-x`. No conflicts.

## What is the testing strategy for this PR?

The `first_last` unit tests from #27150 pass on this branch, as do
`aggregate*.slt`, `group_by.slt` and `window*.slt`.

Applied on `branch-56` together with the other backports, #27846
(#27021), #27848 (#27547), #27849 (#27732) and #27850 (#26261), the full
sqllogictest suite, `cargo fmt --check` and clippy on the touched crates
also pass.

## Are there any user-facing changes?

No API changes. `first_value` without `ORDER BY`, including `DISTINCT
ON`, is faster. The intermediate state layout is unchanged
(`[first_value, is_set]`), so partial and final aggregation see the same
schema as before.

Co-authored-by: Rohan Krishnaswamy <47869999+rkrishn7@users.noreply.github.com>
xudong963 added a commit that referenced this pull request Dec 2, 2026
…#27021) (#27846)

## Which issue does this PR close?

- Backport of #27021 to `branch-56`, for #24463.
- Closes #27020 for the 56 line.

## Rationale for this change

arrow-csv maps CSV columns by position. When files in one table have
different column layouts, values land in the wrong columns or parsing
fails. #27021 adds an opt-in `match_columns_by_header` option that maps
each file's columns to the table schema by header name. Users on 56 who
read CSV files whose layout changed over time need this before 57.

## What changes are included in this PR?

A cherry-pick of #27021 (`3f837ad`) with `-x`. The only conflict was in
`docs/source/library-user-guide/upgrading/56.0.0.md`: on `main` that
file also has a section from an unrelated PR (`CreateFunction` field
types), so I kept only the `match_columns_by_header` section.

## What is the testing strategy for this PR?

The tests from #27021 pass on this branch: the
`datafusion-datasource-csv` unit tests, the CSV tests in
`core_integration`, the proto round-trip tests, `csv_files.slt` and
`information_schema.slt`.

Applied on `branch-56` together with the other backports, #27847
(#27150), #27848 (#27547), #27849 (#27732) and #27850 (#26261), the full
sqllogictest suite, `cargo fmt --check` and clippy on the touched crates
also pass.

## Are there any user-facing changes?

- New opt-in `format.match_columns_by_header` option, default `false`.
Existing reads are unchanged.
- API: `CsvOptions` and `CsvReadOptions` gain a public
`match_columns_by_header` field. Neither struct is `#[non_exhaustive]`,
so code that builds them with an exhaustive struct literal stops
compiling until it adds the field or uses `..Default::default()`. This
is noted in the 56.0.0 upgrade guide. Please weigh this for a minor
release.
- Proto: `CsvOptions` (field 23) and `CsvScanExecNode` (field 10) gain
the same field, with the same field numbers as `main`.

Co-authored-by: Wyatt Herkamp <wyatt.herkamp@massive.com>
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants