Skip to content

chore: simplify SortPreservingMergeStream to be as textbook-like as possible#23702

Open
rluvaton wants to merge 6 commits into
apache:mainfrom
rluvaton:simplify-sort-preserving-merge-stream
Open

chore: simplify SortPreservingMergeStream to be as textbook-like as possible#23702
rluvaton wants to merge 6 commits into
apache:mainfrom
rluvaton:simplify-sort-preserving-merge-stream

Conversation

@rluvaton

Copy link
Copy Markdown
Member

Which issue does this PR close?

N/A

Rationale for this change

SortPreservingMergeStream is a little complex, so add some guiding comments and make it as textbook-like as possible

What changes are included in this PR?

Added comments, reorder code

Are these changes tested?

Existing tests

Are there any user-facing changes?

No

@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Jul 19, 2026
@rluvaton
rluvaton marked this pull request as ready for review July 19, 2026 16:02
Comment on lines +298 to +313
let should_poll_next_batch_for_stream =
self.advance_cursors(winner_stream);

// Fast path: skip the `maybe_poll_stream` call (and its `Poll`
// plumbing) unless the winner's cursor is exhausted and needs a
// fresh batch — it is live for almost every row.
if should_poll_next_batch_for_stream {
assert!(
self.cursors[winner_stream].is_none(),
"cursor should be exhausted"
);

drop(timer);
poll_fn(|cx| self.maybe_poll_stream(cx, winner_stream)).await?;
timer = elapsed_compute.timer();
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I wanted to move the poll stream inside the advance_cursors but got lifetime issues so kept like this

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.70968% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.67%. Comparing base (67947b6) to head (7175283).

Files with missing lines Patch % Lines
datafusion/physical-plan/src/sorts/merge.rs 90.56% 2 Missing and 3 partials ⚠️
...afusion/physical-plan/src/sorts/streaming_merge.rs 77.77% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23702      +/-   ##
==========================================
- Coverage   80.67%   80.67%   -0.01%     
==========================================
  Files        1088     1088              
  Lines      367591   367618      +27     
  Branches   367591   367618      +27     
==========================================
+ Hits       296563   296572       +9     
- Misses      53374    53385      +11     
- Partials    17654    17661       +7     

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

@alamb

alamb commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

run benchmark topk_sorted_tpch sort_tpch

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c5027404962-1191-vh5l7 6.12.85+ #1 SMP Mon May 11 08:17:35 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing simplify-sort-preserving-merge-stream (7175283) to 67947b6 (merge-base) diff using: topk_sorted_tpch
Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c5027404962-1192-d86vh 6.12.85+ #1 SMP Mon May 11 08:17:35 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing simplify-sort-preserving-merge-stream (7175283) to 67947b6 (merge-base) diff using: sort_tpch
Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

Comparing HEAD and simplify-sort-preserving-merge-stream
--------------------
Benchmark run_topk_sorted_tpch.json
--------------------
┏━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query ┃                           HEAD ┃ simplify-sort-preserving-merge-stream ┃        Change ┃
┡━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ Q1    │    2.17 / 2.86 ±0.84 / 4.36 ms │           2.12 / 2.67 ±0.90 / 4.46 ms │ +1.07x faster │
│ Q2    │    2.24 / 2.41 ±0.15 / 2.67 ms │           2.29 / 2.42 ±0.12 / 2.64 ms │     no change │
│ Q3    │ 32.48 / 33.51 ±0.93 / 35.17 ms │        32.11 / 32.52 ±0.30 / 33.04 ms │     no change │
│ Q4    │    2.67 / 3.28 ±0.62 / 4.11 ms │           2.64 / 2.72 ±0.13 / 2.98 ms │ +1.21x faster │
│ Q5    │ 10.48 / 10.83 ±0.20 / 11.02 ms │        10.32 / 10.42 ±0.09 / 10.57 ms │     no change │
│ Q6    │ 17.59 / 17.65 ±0.06 / 17.75 ms │        17.42 / 17.59 ±0.10 / 17.71 ms │     no change │
│ Q7    │ 37.52 / 38.00 ±0.46 / 38.87 ms │        37.80 / 38.37 ±0.61 / 39.51 ms │     no change │
│ Q8    │    3.39 / 3.88 ±0.76 / 5.39 ms │           3.44 / 3.53 ±0.07 / 3.66 ms │ +1.10x faster │
│ Q9    │    4.85 / 5.54 ±0.63 / 6.40 ms │           4.88 / 5.99 ±1.32 / 7.68 ms │  1.08x slower │
│ Q10   │   6.97 / 7.68 ±1.33 / 10.33 ms │           6.74 / 6.92 ±0.13 / 7.12 ms │ +1.11x faster │
│ Q11   │    3.96 / 4.02 ±0.07 / 4.16 ms │           4.00 / 4.06 ±0.06 / 4.18 ms │     no change │
└───────┴────────────────────────────────┴───────────────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ Benchmark Summary                                    ┃          ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩
│ Total Time (HEAD)                                    │ 129.65ms │
│ Total Time (simplify-sort-preserving-merge-stream)   │ 127.20ms │
│ Average Time (HEAD)                                  │  11.79ms │
│ Average Time (simplify-sort-preserving-merge-stream) │  11.56ms │
│ Queries Faster                                       │        4 │
│ Queries Slower                                       │        1 │
│ Queries with No Change                               │        6 │
│ Queries with Failure                                 │        0 │
└──────────────────────────────────────────────────────┴──────────┘

Resource Usage

topk_sorted_tpch — base (merge-base)

Metric Value
Wall time 5.0s
Peak memory 256.0 KiB
Avg memory 42.7 KiB
CPU user 0.0s
CPU sys 0.0s
Peak spill 0 B

topk_sorted_tpch — branch

Metric Value
Wall time 5.0s
Peak memory 236.0 KiB
Avg memory 39.3 KiB
CPU user 0.0s
CPU sys 0.0s
Peak spill 0 B

File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

Comparing HEAD and simplify-sort-preserving-merge-stream
--------------------
Benchmark sort_tpch1.json
--------------------
┏━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Query ┃                               HEAD ┃ simplify-sort-preserving-merge-stream ┃    Change ┃
┡━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Q1    │  121.22 / 122.11 ±0.86 / 123.76 ms │     119.72 / 120.45 ±0.93 / 122.27 ms │ no change │
│ Q2    │  104.75 / 106.13 ±1.44 / 108.00 ms │     104.73 / 107.00 ±2.39 / 110.51 ms │ no change │
│ Q3    │  627.37 / 629.90 ±1.65 / 632.43 ms │     629.75 / 634.79 ±4.65 / 641.17 ms │ no change │
│ Q4    │  192.50 / 198.66 ±4.89 / 207.03 ms │     194.73 / 199.81 ±5.54 / 210.59 ms │ no change │
│ Q5    │  258.28 / 259.24 ±0.91 / 260.94 ms │     259.84 / 262.06 ±1.25 / 263.67 ms │ no change │
│ Q6    │  272.47 / 273.67 ±0.85 / 274.80 ms │     274.16 / 274.91 ±0.47 / 275.59 ms │ no change │
│ Q7    │  443.23 / 444.22 ±0.86 / 445.23 ms │     444.69 / 447.01 ±1.53 / 448.88 ms │ no change │
│ Q8    │  314.95 / 321.13 ±7.98 / 336.22 ms │     314.83 / 322.62 ±8.32 / 336.92 ms │ no change │
│ Q9    │ 332.87 / 351.85 ±15.05 / 374.22 ms │    332.73 / 349.70 ±12.82 / 368.86 ms │ no change │
│ Q10   │  480.38 / 485.70 ±3.66 / 491.85 ms │     483.29 / 486.94 ±3.26 / 492.23 ms │ no change │
│ Q11   │ 247.42 / 262.40 ±20.76 / 302.76 ms │    249.33 / 265.36 ±19.86 / 303.12 ms │ no change │
└───────┴────────────────────────────────────┴───────────────────────────────────────┴───────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Benchmark Summary                                    ┃           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Total Time (HEAD)                                    │ 3455.02ms │
│ Total Time (simplify-sort-preserving-merge-stream)   │ 3470.64ms │
│ Average Time (HEAD)                                  │  314.09ms │
│ Average Time (simplify-sort-preserving-merge-stream) │  315.51ms │
│ Queries Faster                                       │         0 │
│ Queries Slower                                       │         0 │
│ Queries with No Change                               │        11 │
│ Queries with Failure                                 │         0 │
└──────────────────────────────────────────────────────┴───────────┘

Resource Usage

sort_tpch — base (merge-base)

Metric Value
Wall time 20.0s
Peak memory 2.5 GiB
Avg memory 1.1 GiB
CPU user 63.5s
CPU sys 2.9s
Peak spill 0 B

sort_tpch — branch

Metric Value
Wall time 20.0s
Peak memory 2.5 GiB
Avg memory 1.2 GiB
CPU user 63.4s
CPU sys 2.9s
Peak spill 0 B

File an issue against this benchmark runner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants