Skip to content

perf(physical_plan): avoid eagerly starting streams in coalesce_partitions - #27126

Draft
tschwarzinger wants to merge 2 commits into
apache:mainfrom
tschwarzinger:lazy-coalesce
Draft

tschwarzinger wants to merge 2 commits into
apache:mainfrom
tschwarzinger:lazy-coalesce

Conversation

@tschwarzinger

@tschwarzinger tschwarzinger commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

CoalescePartitionsExec eagerly starts and polls streams during ExecutionPlan::execute (a task does the polling). This can cause situations where dynamic filters of a parent join are not yet available and thus the first poll of scans is done without the dynamic filters.

This should improve at least 2 TPC-H queries (q21, q7) with pushdown_filters=true.

What changes are included in this PR?

  • Wait for the first poll to actually run the children.

What is the testing strategy for this PR?

  • Existing tests
  • drops_input_plan_after_input_streams_start now polls once before checking, as inputs no longer start in execute
  • inputs_start_on_first_poll_not_on_execute asserts that the stream is not started

Are there any user-facing changes?

  • Could cause better pruning for some queries (as dynamic filters are available)
  • Possibly higher latency due to the delayed opening of files but I could not measure a significant impact locally.

@tschwarzinger

Copy link
Copy Markdown
Contributor Author

Possible Follow-Up: I think SortPreservingMergeExec has the same issue with dynamic filters

@tschwarzinger

tschwarzinger commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor Author

@adriangb Could you run the TPC-H SF10 (SF1 might be also fine) with filter pushdown turned on?

@tschwarzinger

Copy link
Copy Markdown
Contributor Author

cc @devanbenz if you're interested

@github-actions github-actions Bot added the physical-plan Changes to the physical-plan crate label Nov 5, 2026
@tschwarzinger tschwarzinger changed the title perf(physical): avoid eagerly starting streams in coalesce_partitions perf(physical_plan): avoid eagerly starting streams in coalesce_partitions Nov 5, 2026
@adriangb

adriangb commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

run benchmark tpch10

env:
  DATAFUSION_EXECUTION_PARQUET_PUSHDOWN_FILTERS: true

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c6659913675-4518-qkhvg 6.12.105+ #1 SMP Sat Sep 19 07:55:25 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 lazy-coalesce (a2c3adb) to 70f2ed0 (merge-base) diff

Run configuration
run benchmark tpch10
env:
  DATAFUSION_EXECUTION_PARQUET_PUSHDOWN_FILTERS: "true"

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)

Comparing lazy-coalesce (a2c3adb) to 70f2ed0 (merge-base) diff

Run configuration
run benchmark tpch10
env:
  DATAFUSION_EXECUTION_PARQUET_PUSHDOWN_FILTERS: "true"
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 lazy-coalesce
--------------------
Benchmark tpch_sf10.json
--------------------
┏━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query     ┃       HEAD ┃ lazy-coalesce ┃        Change ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 1  │  336.44 ms │     334.56 ms │     no change │
│ QQuery 2  │   89.22 ms │      86.63 ms │     no change │
│ QQuery 3  │  216.54 ms │     216.70 ms │     no change │
│ QQuery 4  │  162.98 ms │     164.42 ms │     no change │
│ QQuery 5  │  293.02 ms │     291.49 ms │     no change │
│ QQuery 6  │  131.04 ms │     130.32 ms │     no change │
│ QQuery 7  │  312.16 ms │     263.05 ms │ +1.19x faster │
│ QQuery 8  │  359.70 ms │     362.24 ms │     no change │
│ QQuery 9  │  567.10 ms │     566.16 ms │     no change │
│ QQuery 10 │  286.07 ms │     286.97 ms │     no change │
│ QQuery 11 │   64.81 ms │      61.52 ms │ +1.05x faster │
│ QQuery 12 │  174.83 ms │     171.33 ms │     no change │
│ QQuery 13 │  339.79 ms │     341.06 ms │     no change │
│ QQuery 14 │  161.77 ms │     162.21 ms │     no change │
│ QQuery 15 │  322.07 ms │     320.51 ms │     no change │
│ QQuery 16 │   68.97 ms │      68.58 ms │     no change │
│ QQuery 17 │  290.95 ms │     293.29 ms │     no change │
│ QQuery 18 │ 1289.56 ms │    1299.65 ms │     no change │
│ QQuery 19 │  207.77 ms │     205.07 ms │     no change │
│ QQuery 20 │  208.93 ms │     211.68 ms │     no change │
│ QQuery 21 │  798.20 ms │     562.27 ms │ +1.42x faster │
│ QQuery 22 │   71.39 ms │      73.40 ms │     no change │
└───────────┴────────────┴───────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Benchmark Summary            ┃           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Total Time (HEAD)            │ 6753.29ms │
│ Total Time (lazy-coalesce)   │ 6473.13ms │
│ Average Time (HEAD)          │  306.97ms │
│ Average Time (lazy-coalesce) │  294.23ms │
│ Queries Faster               │         3 │
│ Queries Slower               │         0 │
│ Queries with No Change       │        19 │
│ Queries with Failure         │         0 │
└──────────────────────────────┴───────────┘

Distribution per query (min / mean ±stddev / max):

Comparing HEAD and lazy-coalesce
--------------------
Benchmark tpch_sf10.json
--------------------
┏━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┓
┃ Query     ┃                                  HEAD ┃                         lazy-coalesce ┃        Change ┃
┡━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━┩
│ QQuery 1  │     336.44 / 338.63 ±1.79 / 341.04 ms │     334.56 / 336.38 ±1.80 / 339.62 ms │     no change │
│ QQuery 2  │        89.22 / 91.53 ±1.58 / 93.26 ms │        86.63 / 87.85 ±1.27 / 89.65 ms │     no change │
│ QQuery 3  │     216.54 / 219.81 ±1.87 / 222.37 ms │     216.70 / 219.32 ±2.13 / 222.39 ms │     no change │
│ QQuery 4  │     162.98 / 164.81 ±1.14 / 166.35 ms │     164.42 / 166.10 ±1.70 / 168.98 ms │     no change │
│ QQuery 5  │     293.02 / 296.76 ±2.96 / 300.58 ms │     291.49 / 294.33 ±2.24 / 297.05 ms │     no change │
│ QQuery 6  │     131.04 / 132.45 ±1.68 / 135.55 ms │     130.32 / 132.22 ±1.96 / 135.86 ms │     no change │
│ QQuery 7  │     312.16 / 314.27 ±1.19 / 315.54 ms │     263.05 / 266.83 ±2.64 / 270.81 ms │ +1.18x faster │
│ QQuery 8  │     359.70 / 361.37 ±1.36 / 363.07 ms │     362.24 / 365.20 ±3.80 / 372.57 ms │     no change │
│ QQuery 9  │     567.10 / 573.90 ±6.44 / 585.78 ms │     566.16 / 571.42 ±5.87 / 582.86 ms │     no change │
│ QQuery 10 │     286.07 / 290.59 ±4.03 / 297.26 ms │     286.97 / 288.90 ±1.21 / 290.62 ms │     no change │
│ QQuery 11 │        64.81 / 70.44 ±4.92 / 77.79 ms │        61.52 / 61.99 ±0.29 / 62.42 ms │ +1.14x faster │
│ QQuery 12 │     174.83 / 176.43 ±1.24 / 178.05 ms │     171.33 / 175.78 ±5.12 / 183.57 ms │     no change │
│ QQuery 13 │    339.79 / 349.94 ±11.20 / 369.36 ms │     341.06 / 347.61 ±6.55 / 358.79 ms │     no change │
│ QQuery 14 │     161.77 / 165.97 ±3.38 / 169.84 ms │     162.21 / 166.05 ±3.65 / 171.42 ms │     no change │
│ QQuery 15 │     322.07 / 329.01 ±5.14 / 336.49 ms │     320.51 / 326.99 ±3.63 / 330.58 ms │     no change │
│ QQuery 16 │        68.97 / 70.75 ±2.28 / 75.04 ms │        68.58 / 70.51 ±2.12 / 74.55 ms │     no change │
│ QQuery 17 │     290.95 / 295.27 ±3.48 / 300.37 ms │     293.29 / 298.97 ±6.71 / 308.23 ms │     no change │
│ QQuery 18 │ 1289.56 / 1342.52 ±31.90 / 1387.56 ms │ 1299.65 / 1340.40 ±39.78 / 1397.33 ms │     no change │
│ QQuery 19 │    207.77 / 230.82 ±16.27 / 258.71 ms │    205.07 / 229.94 ±22.10 / 260.81 ms │     no change │
│ QQuery 20 │     208.93 / 210.74 ±1.80 / 213.95 ms │     211.68 / 213.84 ±2.20 / 217.28 ms │     no change │
│ QQuery 21 │    798.20 / 812.39 ±13.96 / 838.44 ms │     562.27 / 565.67 ±3.82 / 572.22 ms │ +1.44x faster │
│ QQuery 22 │        71.39 / 77.10 ±5.89 / 88.45 ms │        73.40 / 79.46 ±3.66 / 84.93 ms │     no change │
└───────────┴───────────────────────────────────────┴───────────────────────────────────────┴───────────────┘
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┓
┃ Benchmark Summary            ┃           ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━┩
│ Total Time (HEAD)            │ 6915.50ms │
│ Total Time (lazy-coalesce)   │ 6605.76ms │
│ Average Time (HEAD)          │  314.34ms │
│ Average Time (lazy-coalesce) │  300.26ms │
│ Queries Faster               │         3 │
│ Queries Slower               │         0 │
│ Queries with No Change       │        19 │
│ Queries with Failure         │         0 │
└──────────────────────────────┴───────────┘

Resource Usage

tpch10 — base (merge-base)

Metric Value
Wall time 35.0s
Peak memory 13.5 GiB
Avg memory 3.2 GiB
CPU user 351.9s
CPU sys 18.7s
Peak spill 0 B

tpch10 — branch

Metric Value
Wall time 35.0s
Peak memory 13.8 GiB
Avg memory 3.1 GiB
CPU user 340.6s
CPU sys 18.6s
Peak spill 0 B

File an issue against this benchmark runner

@tschwarzinger

Copy link
Copy Markdown
Contributor Author

Yeah that looks similar to my machine. Thanks!

@devanbenz

devanbenz commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

I am unable to run benchmarks in the PR, but, could we get them ran against tpcds with and without pushdown and tpch10 without pushdown?

I ran through some more benchmarks locally and noticed some regressions, these runs all used the default config so no filter pushdown:

Benchmark main (70f2ed0) PR (a2c3adb) Change Slower / faster (>5%)
TPC-DS SF1 7,857 ms 8,663 ms +10.3% 49 / 12
TPC-DS SF1, --simulate-latency 54,417 ms 75,615 ms +39.0% 73 / 13
TPC-H SF10 8,268 ms 8,384 ms +1.4% 1 / 1
Synthetic: 3 CollectLeft aggregate builds, target_partitions=4 573 ms 647 ms +12.9% n/a
Synthetic: same, target_partitions=20 246 ms 263 ms +6.9% n/a
TPC-DS query main PR Change main (latency) PR (latency) Change (latency)
q88 50.2 ms 158.4 ms 3.16x 367 ms 3214 ms 8.76x
q28 57.7 ms 157.4 ms 2.73x 338 ms 1045 ms 3.09x
q46 9.7 ms 12.6 ms 1.29x 123 ms 347 ms 2.81x
q89 30.4 ms 32.2 ms 1.06x 221 ms 480 ms 2.17x
q73 9.4 ms 8.8 ms 0.93x 59 ms 127 ms 2.17x
q38 50.6 ms 58.9 ms 1.16x 423 ms 907 ms 2.14x
q81 24.1 ms 33.5 ms 1.39x 318 ms 672 ms 2.12x
q84 24.1 ms 30.8 ms 1.28x 226 ms 438 ms 1.94x
q19 35.3 ms 35.6 ms 1.01x 296 ms 569 ms 1.92x
q74 139.9 ms 157.5 ms 1.13x 856 ms 1628 ms 1.90x

I think this change makes it so the build sides that used to run in parallel run sequentially now? Very much restricting parallelism. It helps when there is a dynamic filter to prune data but for anything else it limits parallelism.

Something akin to this:

main (eager): all build sides start at execute()
 build A  ████████
 build B  ██████
 build C  ███████
 probe            ████
          └─ total ≈ longest build + probe

PR (lazy): each build starts only when its join is polled
 build A  ████████
 build B          ██████
 build C                ███████
 probe                         ████
          └─ 

(Thanks claude for taking my word vomit and turning it in to a nice little diagram 😆)

@tschwarzinger

tschwarzinger commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @devanbenz !

I think you're observing what I meant with

Possibly higher latency due to the delayed opening of files but I could not measure a significant impact locally.

I've only tested tpch sf10 with filter pushdown, so thanks for pointing out the regressions in the other benchmarks! It definitely makes sense as execution is now more sequentially (as you nice diagram shows :) ). Maybe also doing a run on TPC-DS with dynamic filters on would make sense.

The questions I guess is whether this is an intended design or just a something that grew organically.

Also if there is only a single input partition we don't poll the stream eagerly.

1 => {
    // single-partition path: execute child directly, but ensure fetch is respected
    // (wrap with ObservedStream only if fetch is present so we don't add overhead otherwise)
    let child_stream = self.input.execute(0, context)?;
    if self.fetch.is_some() {
        let baseline_metrics = BaselineMetrics::new(&self.metrics, partition);
        return Ok(Box::pin(ObservedStream::new(
            child_stream,
            baseline_metrics,
            self.fetch,
        )));
    }
    Ok(child_stream)
}

It kind of seems to me that this is an unintended effect (I wasn't aware that CoalescePartitionsExec does eager polling). Anyway, if we land something like this we should figure out a way without regressing the queries.

Personally, I think it's kind of a surprise that CoalescePartitionsExec does eager polling. Maybe the parallelization should happen in a different operator? Maybe the hash joins should execute their build side in a task? (EDIT: Maybe gating it on whether a dyn filter from a parent was pushed down?)

Any takes on this?

I will need to look at the queries though next week to see if there is a good solution. Marking this as draft for now so we don't merge it by accident. Thanks again!

@tschwarzinger
tschwarzinger marked this pull request as draft November 6, 2026 16:46
@tschwarzinger

tschwarzinger commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

What's also interesting / related now is the HashJoinBuffering optimization which explicitly parallelizes build and probe side of a join.

@devanbenz

Copy link
Copy Markdown
Contributor

Also if there is only a single input partition we don't poll the stream eagerly.

My assumption is that this is correct , there is nothing to merge when there is a single input partition so we just pass the child stream through and just starts whenever the child stream starts. That being said whether the eagerness of the streams is a side-effect or an actual design choice is up in the air.

I'll need to look more in to the different Join operators this weekend more as I'm not as intimately familiar with them as I would like to be to offer much in the way of good suggestions. What you brought up from the HashJoinBuffering operator is interesting though. I'll take a peak when I have some time. Let me do some exploratory work and I'll get back to this PR :)

@devanbenz

devanbenz commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Heh yeah it seems to have happened organically over time, there is no specific PR that outlines a reason for the implementation:

Date Commit PR / Issue Change
2019-09-19 ed485a587 ARROW-6563 MergeExec added; execute calls thread::spawn per input partition
2020-09-27 d6d043d93 ARROW-9754 ExecutionPlan made async; threads become tokio::spawn tasks, still in execute
2020-10-14 bd93ee5f2 ARROW-10292 Bounded channel of size input_partitions ("maximize parallelism"); shape still in use today
2021-06-01 c794f2df5 #379 Spawning moved into a shared spawn_execution helper; no behaviour change
2023-06-06 39ee59aeb #6507 Folded into RecordBatchReceiverStream::builder().run_input(..)

None of these commit messages calls the early start deliberate. The stated goal was running all partitions in parallel, and starting at execute() was simply where that code lived. The parallelism across different build sides was never named as a goal either.

Let's work on brainstorming some different approaches 🫡

@tschwarzinger

Copy link
Copy Markdown
Contributor Author

I think I got some good results with controlling the parallelization explicitly from hash joins depending on whether we're still waiting for dynamic filters.

However, I need to polish this a bit more. I guess the best way is to push all changes to this branch for now to see if we can make it work (i.e., run benchmarks) and then move the fixes to production code in smaller PRs.

I hope I'll have some news on that tomorrow.

@adriangb

adriangb commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

IMO the issue with these sorts of optimizations is that they depend on the IO patterns, data and query. E.g. if there is no selective dynamic filter or the build side is very slow (e.g. wrong join order) then buffering the probe side will show great results. If the build side is fast and/or the dynamic filters it generates are very effective then buffering the probe side won't help.

@tschwarzinger

Copy link
Copy Markdown
Contributor Author

I've opened a discussion issue about being more explicit about concurrency in DataFusion plans. Would love your input there, if you're interested!

#27298

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