Skip to content

chore: refactor SortMergeJoin bitwise stream to generators and simplify to be textbook like as possible - #23761

Merged
rluvaton merged 8 commits into
apache:mainfrom
rluvaton:let-claude-try-refactor-bitwise-stream-to-use-generator
Jul 28, 2026
Merged

chore: refactor SortMergeJoin bitwise stream to generators and simplify to be textbook like as possible#23761
rluvaton merged 8 commits into
apache:mainfrom
rluvaton:let-claude-try-refactor-bitwise-stream-to-use-generator

Conversation

@rluvaton

@rluvaton rluvaton commented Jul 21, 2026

Copy link
Copy Markdown
Member

Which issue does this PR close?

N/A

Rationale for this change

SortMergeJoin bitwise stream implementation is very complex and hard to understand while on paper it should be pretty simple.

the reason for that is we have to store state between polls (we had boundary) and handle the case where both can get Poll::Pending from child and Poll::Ready from child which further complicate the code

What changes are included in this PR?

  1. Move to async generators
  2. Rewrote main loop to be textbook like as possible

(this was entirely written by Claude Fable, sorry, I tried manually but the code was too complex to hold in my head 😅 )

Are these changes tested?

existing tests

Are there any user-facing changes?

The join_time now includes the time to read from the async spill stream between pending which is arguable more correct since this time is part of the operator, although long waits between pending calls will be counted in the op join_time while the alternative is not counting the read from file and decoding...

@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Jul 21, 2026
@rluvaton

Copy link
Copy Markdown
Member Author

run benchmark smj

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c5035366186-1210-zh2ds 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 let-claude-try-refactor-bitwise-stream-to-use-generator (d3e32e9) to ed22a4a (merge-base) diff using: smj
Results will be posted here when complete


File an issue against this benchmark runner

@mbutrovich
mbutrovich self-requested a review July 21, 2026 14:54
@mbutrovich

Copy link
Copy Markdown
Contributor

Love to see a negative line count!

@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 let-claude-try-refactor-bitwise-stream-to-use-generator
--------------------
Benchmark smj.json
--------------------
┏━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query     ┃                                 HEAD ┃ let-claude-try-refactor-bitwise-stream-to-use-generator ┃        Change ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 1  │          8.67 / 8.78 ±0.11 / 9.00 ms │                             8.51 / 8.62 ±0.06 / 8.68 ms │     no change │
│ QQuery 2  │    160.82 / 162.68 ±1.51 / 164.41 ms │                       160.60 / 162.90 ±2.17 / 166.47 ms │     no change │
│ QQuery 3  │    102.85 / 104.99 ±1.14 / 106.20 ms │                       103.37 / 104.21 ±0.95 / 105.87 ms │     no change │
│ QQuery 4  │       28.01 / 28.16 ±0.14 / 28.33 ms │                          28.18 / 28.32 ±0.11 / 28.48 ms │     no change │
│ QQuery 5  │       20.44 / 20.70 ±0.20 / 21.01 ms │                          20.43 / 20.64 ±0.12 / 20.78 ms │     no change │
│ QQuery 6  │    158.63 / 160.95 ±1.61 / 162.78 ms │                       156.35 / 158.72 ±1.92 / 160.53 ms │     no change │
│ QQuery 7  │   198.19 / 207.84 ±17.87 / 243.53 ms │                       197.47 / 198.75 ±1.97 / 202.67 ms │     no change │
│ QQuery 8  │       19.42 / 20.02 ±0.49 / 20.62 ms │                          18.72 / 18.96 ±0.16 / 19.16 ms │ +1.06x faster │
│ QQuery 9  │   204.45 / 211.38 ±10.53 / 232.26 ms │                       202.64 / 204.24 ±1.01 / 205.43 ms │     no change │
│ QQuery 10 │       57.92 / 58.15 ±0.17 / 58.37 ms │                          57.80 / 57.91 ±0.09 / 58.03 ms │     no change │
│ QQuery 11 │       27.02 / 30.57 ±6.70 / 43.96 ms │                          26.84 / 27.01 ±0.15 / 27.29 ms │ +1.13x faster │
│ QQuery 12 │       66.89 / 70.20 ±4.25 / 78.49 ms │                          66.99 / 70.53 ±2.41 / 74.53 ms │     no change │
│ QQuery 13 │    103.38 / 107.06 ±2.83 / 111.45 ms │                       105.03 / 107.11 ±1.60 / 109.49 ms │     no change │
│ QQuery 14 │       58.17 / 61.68 ±5.45 / 72.39 ms │                          57.53 / 58.55 ±1.47 / 61.46 ms │ +1.05x faster │
│ QQuery 15 │       58.09 / 58.32 ±0.18 / 58.63 ms │                          57.75 / 57.97 ±0.15 / 58.21 ms │     no change │
│ QQuery 16 │       11.57 / 11.64 ±0.04 / 11.69 ms │                          11.36 / 11.42 ±0.06 / 11.54 ms │     no change │
│ QQuery 17 │    145.34 / 149.69 ±8.14 / 165.96 ms │                       145.09 / 146.44 ±2.14 / 150.69 ms │     no change │
│ QQuery 18 │    110.40 / 110.62 ±0.18 / 110.86 ms │                       110.07 / 110.74 ±0.34 / 111.02 ms │     no change │
│ QQuery 19 │    288.93 / 295.86 ±5.88 / 305.91 ms │                       288.20 / 296.69 ±6.69 / 305.20 ms │     no change │
│ QQuery 20 │ 1252.41 / 1261.46 ±6.61 / 1269.78 ms │                    1253.21 / 1259.24 ±3.67 / 1264.36 ms │     no change │
│ QQuery 21 │      94.00 / 99.30 ±8.43 / 116.07 ms │                          93.89 / 94.55 ±0.68 / 95.82 ms │     no change │
│ QQuery 22 │    101.14 / 103.54 ±1.99 / 106.18 ms │                       100.72 / 104.62 ±6.00 / 116.55 ms │     no change │
│ QQuery 23 │    110.18 / 110.54 ±0.29 / 110.98 ms │                       108.03 / 110.56 ±2.67 / 115.65 ms │     no change │
│ QQuery 24 │       27.15 / 27.25 ±0.11 / 27.45 ms │                          27.18 / 27.31 ±0.10 / 27.46 ms │     no change │
│ QQuery 25 │       64.21 / 69.89 ±4.05 / 76.54 ms │                          62.73 / 69.56 ±5.81 / 80.22 ms │     no change │
│ QQuery 26 │     96.12 / 107.22 ±5.86 / 112.20 ms │                      107.21 / 117.12 ±12.02 / 140.08 ms │  1.09x slower │
└───────────┴──────────────────────────────────────┴─────────────────────────────────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Benchmark Summary                                                      ┃           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Total Time (HEAD)                                                      │ 3658.49ms │
│ Total Time (let-claude-try-refactor-bitwise-stream-to-use-generator)   │ 3632.67ms │
│ Average Time (HEAD)                                                    │  140.71ms │
│ Average Time (let-claude-try-refactor-bitwise-stream-to-use-generator) │  139.72ms │
│ Queries Faster                                                         │         3 │
│ Queries Slower                                                         │         1 │
│ Queries with No Change                                                 │        22 │
│ Queries with Failure                                                   │         0 │
└────────────────────────────────────────────────────────────────────────┴───────────┘

Resource Usage

smj — base (merge-base)

Metric Value
Wall time 20.0s
Peak memory 1.2 GiB
Avg memory 455.1 MiB
CPU user 156.8s
CPU sys 3.5s
Peak spill 0 B

smj — branch

Metric Value
Wall time 20.0s
Peak memory 1.1 GiB
Avg memory 454.7 MiB
CPU user 157.7s
CPU sys 3.4s
Peak spill 0 B

File an issue against this benchmark runner

@codecov-commenter

codecov-commenter commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.35088% with 56 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.66%. Comparing base (77b172e) to head (d88d6d7).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...l-plan/src/joins/sort_merge_join/bitwise_stream.rs 80.28% 18 Missing and 38 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23761      +/-   ##
==========================================
- Coverage   80.67%   80.66%   -0.01%     
==========================================
  Files        1094     1095       +1     
  Lines      371770   372197     +427     
  Branches   371770   372197     +427     
==========================================
+ Hits       299907   300233     +326     
- Misses      53964    54032      +68     
- Partials    17899    17932      +33     

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

@rluvaton

Copy link
Copy Markdown
Member Author

Love to see a negative line count!

Now its not due to added tests to make sure we are not counting child/consumer time, but the stream file still have negative line count!

@rluvaton
rluvaton marked this pull request as ready for review July 26, 2026 20:25
@rluvaton

Copy link
Copy Markdown
Member Author

@mbutrovich Can you please review? 😅

@mbutrovich

Copy link
Copy Markdown
Contributor

@mbutrovich Can you please review? 😅

I'll find some time this week, definitely. Thanks for the PR!

@mbutrovich

Copy link
Copy Markdown
Contributor

Out of curiosity, is the non-bitwise SMJ stream up next?

@rluvaton

rluvaton commented Jul 28, 2026

Copy link
Copy Markdown
Member Author

Out of curiosity, is the non-bitwise SMJ stream up next?

if I can make it more readable yes, afterward I will try to migrate the new hash aggregate exec

@mbutrovich mbutrovich 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 for tackling this, @rluvaton! It reads much better now.

Traced the control flow by hand against the removed poll-based version: every branch (outer/inner key groups crossing batch boundaries, filtered and unfiltered, spill buffer vs spill file, inner exhaustion mid-group) produces the same state transitions. PendingBoundary, batch_emitted, buffering_inner_pending, and spill_stream_has_data only existed to survive unwinding out of poll_join on Pending; with await the function suspends in place, so removing them is correct, not just simpler.

The two new sync fast paths (try_skip_inner_key_group, try_process_key_match) skip the async machinery when a key group does not reach a batch boundary. Checked their output against the general path for that case and they compute the same cursor and matched-bit state.

CI is green (clippy, cargo test, extended_tests), the existing semi/anti/mark fuzz tests in join_fuzz.rs exercise this stream end to end, and the posted smj benchmark shows no regression.

One request before merge: datafusion/physical-plan/src/joins/sort_merge_join/bitwise_stream.rs:744 notes that the clock now keeps running across spill read-back, where before a genuine Pending from the spill stream stopped the per-call timer early and that wait was invisible to join_time. That is a real change to what the metric measures, not just an implementation detail. Please add a line to the PR description calling this out, since it currently says "no user-facing changes" and join_time is visible in EXPLAIN ANALYZE.

@rluvaton
rluvaton added this pull request to the merge queue Jul 28, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 28, 2026
@rluvaton
rluvaton added this pull request to the merge queue Jul 28, 2026
Merged via the queue into apache:main with commit b0b9dae Jul 28, 2026
41 checks passed
@rluvaton
rluvaton deleted the let-claude-try-refactor-bitwise-stream-to-use-generator branch July 28, 2026 19:57
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