peter-toth opened a new pull request, #60752:
URL: https://github.com/apache/spark/pull/60752
### What changes were proposed in this pull request?
SPARK-60541 (#59866) checks each partition of the application side against
the bloom filter of the matching partition of the creation side, in a semi join
above the scan. This PR puts those bloom filters into the scan instead, as a
storage filter (SPARK-60465, #59715). Each split of the scan gets the bloom
filter of the creation side's partition with the split's partition key. A split
whose partition key has none is not read.
The scan of the application side gets the per-partition bloom filter, and
the aggregate that builds it as its child:
```
Join on fact.k = dim.k
storage-partitioned
:- Project [fact columns]
: +- BatchScan fact StorageFilters: [might_contain(bloomFilter,
xxhash64(fact.k))]
: +- Aggregate [bucket(4, dim.k)], [bucket(4, dim.k) AS partitionKey,
bloom_filter_agg(xxhash64(dim.k)) AS bloomFilter]
: +- <creation side>
KeyedPartitioning(bucket(4, k))
+- <the other side of the join>
```
How it works:
- `InjectRuntimeFilter` adds the placeholder of a per-partition bloom filter
as before, but only when the scan of the application side takes a storage
filter on the key. That needs:
- `spark.sql.sources.v2.storageFilterPushdown.enabled` on;
- the key's column among those the scan declares;
- every operation pushed into the scan to work on each row by itself.
- `CleanupPerPartitionRuntimeFilters` replaces
`InsertPerPartitionRuntimeFilterJoins`. It keeps a placeholder only where
pushdown left it right above that scan, with only filters and projections in
between. Every other placeholder gives way to `true`, as before when it got no
semi join.
- The V2 scan strategy finds the placeholder among the filters of the scan.
The `BatchScanExec` it plans gets the `might_contain` check and the aggregate.
The aggregate groups the creation side by its partition transform, with a bloom
filter per group.
- `SizePerPartitionStorageFilters` replaces
`ChoosePerPartitionRuntimeFilters`. It sizes the bloom filters as before. When
the creation side has too few partition keys for the size threshold, it plans
the scan without them.
- The scan maps each split to the partitions of the aggregate that report
the same partition key, and ORs their bloom filters. A task reads each of those
partitions once, however many splits of the key it reads. The readers get the
result in the split's `StorageFilterRequest`, with the scan's other storage
filters.
The join above implies the bloom filter, so nothing above the scan checks
it. The scan keeps the partitioning it reports, and requires nothing of the
aggregate's. So it adds no shuffle and cuts no tasks. It needs none of the
planning that judged the semi join.
To give `BatchScanExec` a child:
- `DataSourceV2ScanExecBase` is no longer a `LeafExecNode`. The streaming
scans are.
- `SafeForKWayMerge` names the children whose rows a node returns. So the
aggregate under a scan does not keep a `GroupPartitionsExec` from merging the
scan's splits in order.
It also takes out what only the semi join needed:
1. The semi join itself, and `ChoosePerPartitionRuntimeFilters` with the
planning it ran to judge each semi join.
2. The reuse of a hash existence join's bloom filter build column (part of
SPARK-60541).
3. `might_contain` reading its bloom filter from a column (part of
SPARK-60541). The scan gives its readers each split's bloom filter as a
constant in the check.
Known limits:
- the limits of SPARK-60541 on where a placeholder goes still hold: a
partitioning by a single transform, the same on both sides, and one copy of the
placeholder only;
- a scan that takes no storage filter gets no per-partition bloom filter, as
the scan of a connector that does not implement `SupportsStorageFilters`. No
built-in V2 source implements it yet;
- the creation side is read twice, once for the join and once for the
aggregate, as for a global bloom filter;
- under partially clustered distribution, the splits of one key run in
several tasks. Each task reads that key's partitions of the aggregate, and the
creation side under them, again;
- a node above that reads the scan more than once, such as a cartesian
product, reads the aggregate again too;
- a per-partition bloom filter kept in the build side of a broadcast join
keeps a DPP filter from reusing that broadcast, as a bloom filter of the whole
creation side does;
- each bloom filter is sized for an even share of the creation side's
expected items, with the creation side threshold of the global bloom filter;
- no test covers a columnar scan, since no in-memory test scan is columnar.
Over one, the aggregate gets a transition to columnar, which the scan reads
back as rows.
Possible follow-ups:
- the reader of Apache Iceberg's DSv2 scan applying the per-partition bloom
filter;
- a creation side threshold and a sizing of their own for per-partition
bloom filters;
- a metric of the rows the readers reject.
### Why are the changes needed?
A storage-partitioned join does not shuffle the application side. So the
semi join of SPARK-60541 saved only the work between the scan and the join.
In-memory measurements of #59866 had the plan without it fastest. A bloom
filter pays off when the scan applies it, and skips the rows it rules out while
reading. That is what #59715 lets a reader do with a bloom filter of the whole
creation side, and what this PR lets it do with one bloom filter per partition.
### Does this PR introduce _any_ user-facing change?
Yes, with `spark.sql.optimizer.runtime.bloomFilter.perPartition.enabled` on,
which is off by default:
- A per-partition bloom filter now needs a scan that takes a storage filter
on its key. Without one, there is none. Before, the application side got a semi
join, where that added no shuffle and cut no tasks.
- `EXPLAIN` shows no existence join for it. The scan shows the
`might_contain` check among its `StorageFilters`, and the aggregate as its
child.
- The scan counts the splits it skips for having no bloom filter, in the
metric "number of splits skipped by runtime filters".
This conf and `spark.sql.sources.v2.storageFilterPushdown.enabled` are both
new in 5.0.0. This PR is for master only, as is the storage-filter work it
builds on.
### How was this patch tested?
- `PerPartitionRuntimeFilterSuite` is reworked for the storage filter, over
the new test catalog `StorageFilterInMemoryCatalog`. Its scans apply the filter
and record what each reader read, with a filter or without. The suite tests:
- that each split gets the bloom filter of its own partition key, and a
split with none is not read and is counted in the new metric;
- that the scan keeps its bloom filters:
- under a join that shuffles;
- under partially clustered distribution;
- when a node above reads it more than once;
- below a limit, a top-N or a write;
- in each iteration of a recursive CTE;
- that a shuffle of the scan with its bloom filters is not reused for a
scan of the same table without them;
- that physical planning plans the subqueries of the scan's runtime and
storage filters, and that a bloom filter of a whole creation side, on the same
key or another, joins the same storage filter;
- that a `GroupPartitionsExec` above the scan still merges its splits in
order;
- where a placeholder is kept and where it gives way, the sizing, and the
cases with no per-partition bloom filter;
- what a conf turned off between optimization and planning keeps or drops;
- each with AQE on and off where the planning differs.
- The tests of the semi join and of the two reverted pieces go with them.
The test that `might_contain` rejects a non-constant bloom filter comes back.
- The tests that match `BatchScanExec` by its fields get the two new ones.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code with Claude Opus 5.5
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]