Skip to content

[EXPERIMENTAL] perf: closing the TPC-H SF100 gap with Polars (do not merge) - #27985

Draft
andygrove wants to merge 16 commits into
apache:mainfrom
andygrove:polars-gap
Draft

andygrove wants to merge 16 commits into
apache:mainfrom
andygrove:polars-gap

Conversation

@andygrove

@andygrove andygrove commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Warning

Experimental. Not intended to be merged. This PR collects a set of experiments
toward #26169 so the results and code can be discussed in one place. Pieces that
hold up would be split into separate, focused PRs with tests.

Which issue does this PR close?

Rationale for this change

On TPC-H SF100 (Parquet, 16 cores / 32 threads, 125 GB RAM), DataFusion main takes
134.1 s in total and Polars 2.2 (streaming engine) takes 58.5 s. Most of the gap
comes from a few queries (q2, q7, q9, q11, q15, q17, q18, q21).

This branch tests whether incremental changes to the existing planner, statistics,
and operators can close most of that gap. The aim was to stay within today's
architecture and avoid a rewrite.

Result: 63.8 s in total, 2.10x faster than main and within 9% of Polars. The
branch is faster than Polars on 7 of the 22 queries.

query main (ms) this PR (ms) Polars (ms)
q1 6,085 2,512 4,754
q2 1,668 589 163
q3 4,698 3,050 2,656
q4 1,307 1,244 1,198
q5 7,989 3,164 2,871
q6 1,494 1,051 1,408
q7 13,390 3,297 3,560
q8 8,711 4,340 3,185
q9 15,276 10,339 6,404
q10 6,579 4,581 4,112
q11 1,753 725 194
q12 1,462 1,112 493
q13 4,174 3,815 3,762
q14 2,815 1,750 2,104
q15 5,863 2,047 2,028
q16 943 836 372
q17 14,394 2,496 2,738
q18 11,001 4,952 5,342
q19 3,558 1,778 2,266
q20 5,159 2,410 1,818
q21 15,130 7,039 6,387
q22 680 690 656
total 134,131 63,815 58,471

Best of 3 runs. Both DataFusion builds use the LTO release profile. Each change
below was measured on its own on the full suite and kept only if it helped.
benchmarks/polars-gap/REPORT.md has the step-by-step numbers, the profiles behind
each change, what was tried and dropped, and the remaining gaps.

What changes are included in this PR?

Each change has an environment variable that switches back to the old behavior, so
one binary can compare both. The last commit makes all of them the default.

  1. target_partitions defaults to physical cores (Linux with SMT). Running 32
    partitions on 16 cores slows down cache-heavy operators. About 12% faster.

  2. pushdown_filters = true, with all filter conditions combined into one Parquet
    RowFilter.
    A filter with no dynamic (join-generated) part is pushed into the
    reader only when the scan outputs more columns outside the filter than inside it.
    Otherwise it stays in a FilterExec and is still used for pruning. Dynamic join
    filters are always pushed. Together with item 3, about 30% faster.

  3. arrow-rs: decode at most batch_size rows per mask chunk before filtering
    (andygrove/arrow-rs@f27661c, applied through [patch.crates-io]). With a sparse
    selection, the reader used to decode hundreds of thousands of rows into one array
    before filtering. This was the cause of the pushdown slowdowns in q6, q12 and q15.

  4. Better statistics for choosing the hash join build side.

    • Pushed-down filters now count toward a scan's estimated row count.
    • Composite keys get a combined distinct-value estimate (for example partsupp's
      two-column primary key).
    • String equality defaults to 10% selectivity when there is no distinct count.
    • LIKE selectivity falls as the pattern gets longer.
    • The build side is chosen by estimated rows instead of bytes.

    q17 goes from 14.4 s to 2.5 s.

  5. Partial aggregation stays small enough to fit in cache. The partial hash table
    emits its groups and starts over at 4096 groups. If it is not at least halving its
    input, it doubles the limit instead. q18 goes from 11.0 s to 5.0 s.

  6. New logical rule MaterializeCommonSubplans. A subplan that aggregates or
    joins and appears more than once is computed once, using the existing
    MaterializedCte. q15 goes from 5.9 s to 2.0 s.

  7. New logical rule DeferAggregateJoinKeys. A join key that is an aggregate
    result (q2's ps_supplycost = min(ps_supplycost)) moves into the join filter. The
    remaining keys' dynamic filter can then reach below the aggregate. q2 goes from
    1.7 s to 0.59 s.

  8. Join filters that keep more than 90% of rows switch themselves off after the
    first 1M rows.

  9. Benchmark tooling. dfbench tpch can write per-query flamegraphs
    (DF_PPROF) and show plan statistics (DF_SHOW_STATS). Scripts and the report
    are under benchmarks/polars-gap/.

What is the testing strategy for this PR?

There are no new tests yet. That work belongs in the follow-up PRs.

  • datafusion-datasource-parquet unit tests pass in every pushdown mode. The one
    exception is a test that expects a non-dynamic filter to be pushed down, which the
    new auto mode declines on purpose.
  • The full sqllogictest suite with every change on shows only EXPLAIN plan
    differences. The one exception is a row-order change in a pipe_operator.slt
    query that has no ORDER BY.
  • arrow-rs parquet unit tests pass with the patch.
  • Known gaps: the new pushdown defaults and the two optimizer rules change many
    EXPLAIN-based expected outputs, which have not been updated.

Are there any user-facing changes?

Yes, but this PR is not meant to merge as is.

  • New defaults: datafusion.execution.parquet.pushdown_filters = true, and
    target_partitions equals physical cores on Linux.
  • Query plans change: different hash join build sides, MaterializedCteExec for
    repeated subplans, and different filter placement.
  • Several behaviors are switched by DF_* environment variables, for
    experimentation only.
  • FilterExec::statistics_helper becomes pub (#[doc(hidden)]).
  • Two new public optimizer rules.

AI usage disclosure

This work was done with Claude Code (Claude Opus) under my direction. Claude profiled
the queries, read plans, wrote the code and ran the benchmarks. I have not reviewed
the code. Treat it as unreviewed experimental output. Before any of it is split into
real PRs, I will review it, starting with the parts most likely to hide bugs:

  • the arrow-rs mask-chunk change;
  • the sticky push-static-filters flag in ParquetSource::try_pushdown_filters;
  • the plan hashing in MaterializeCommonSubplans.

The heuristics (10% string equality, 0.8 per LIKE character, 4096-group flush, 90%
filter cutoff) were tuned on TPC-H only and need validation on other workloads such
as ClickBench and TPC-DS.

🤖 Generated with Claude Code

andygrove and others added 16 commits December 2, 2026 19:20
…_SHOW_STATS) in tpch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…k-chunk decoding)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…MODE) and dynamic-only pushdown (DF_PUSHDOWN_MODE)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…efault, composite-key join NDV

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…educe input <2x

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…izedCte (DF_AUTO_CTE)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…an projects more other columns than filter columns

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… its selectivity (DF_LIKE_CHAR_SEL)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…amic filters reach below the aggregate

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…true, single row filter, auto static pushdown, auto CTE, row-based build side, physical-core target_partitions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cripts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… branch

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added optimizer Optimizer rules common Related to common crate datasource Changes to the datasource crate physical-plan Changes to the physical-plan crate labels Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

common Related to common crate datasource Changes to the datasource crate optimizer Optimizer rules physical-plan Changes to the physical-plan crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant