peter-toth opened a new pull request, #60506:
URL: https://github.com/apache/spark/pull/60506

   ### Differences from #59916
   
   This is the branch-4.x backport of #59916, together with its follow-up 
#60206. The description below is the original one.
   - The main code is the same as on master. It also takes the later master 
change to this code from SPARK-60882 (#60119): `cannotFail` no longer replaces 
a numeric up-cast, since `ExprUtils.canEvaluateUnconditionally` takes such a 
cast itself. branch-4.x has that `ExprUtils` change already.
   - `InjectRuntimeFilterSuite` takes the later master changes to these tests 
too:
     - SPARK-60882 (#60119) runs several of them in ANSI mode, where their keys 
can fail.
     - SPARK-60871 (#60060) keeps runtime filters off nonbinary collated keys, 
so the collated key test expects no bloom filter without DPP either. branch-4.x 
has that change already.
   - The test with a pushed top-N uses a pushed `TABLESAMPLE` on 
`InMemoryTableWithTableSampleCatalog` instead, since `StorageFilterTestSource` 
is not on branch-4.x. A pushed sample picks rows too, and 
`V2ScanRelationPushDown` marks it the same way. branch-4.x has that mark since 
SPARK-30628 (#60326).
   - The `StorageFilterPushDownSuite` and `StorageFilterTestSource` changes are 
left out, since storage filters are not on branch-4.x.
   - Ran on branch-4.x: `InjectRuntimeFilterSuite`, 
`KeyGroupedPartitioningSuite`, `BloomFilterAggregateQuerySuite`, the DPP 
suites, the runtime filter suites, and the TPC-DS and TPC-H plan stability 
suites. All 1056 tests of these 24 suites pass, with no golden file changes. 
Without the main change, 19 of the 34 SPARK-60702 tests fail. `dev/lint-scala` 
passes.
   
   ### What changes were proposed in this pull request?
   
   `InjectRuntimeFilter` adds no bloom filter on a join key that already has a 
DPP filter. An `onlyInBroadcast` DPP filter runs only if physical planning 
finds a broadcast to reuse. Otherwise physical planning replaces it with 
`true`. This PR stops counting such a filter when nothing in the plan could 
give it a broadcast. For a key with a DPP filter, the rule now takes three 
steps:
   
   1. **Reusable broadcasts (`reused`).** The rule collects the join sides that 
a broadcast join could broadcast and a DPP filter could reuse:
      - for an equi-join, the side that `getBroadcastHashJoinBuildSide` picks. 
That is the side with a broadcast hint, or else a side small enough to 
broadcast that the join type can build from, unless a `SHUFFLE_HASH` hint asks 
for a shuffle hash join. It ignores the other join hints;
      - for a null-aware anti join, its right side, under its own thresholds, 
and only without AQE. Physical planning keeps a DPP filter over that plan, 
though the filter builds its own broadcast (SPARK-60758). AQE plans such a 
filter as `true`.
   
      A side is compared without the DPP filters on top of it, since cleanup 
can still drop them. It stays in the list only if it has the same result as a 
plan that a DPP filter could reuse: the filter's build plan, or the source plan 
of its broadcast value projection (SPARK-58265). The DPP filters include those 
that the optimizer merged into a larger filter in a subquery. With exchange 
reuse off, the list is empty.
   2. **Kept DPP filters (`isKept`).** An `onlyInBroadcast` DPP filter counts 
as kept if one of its plans has the same result as a plan in `reused`. Any 
other DPP filter counts as kept. So does every DPP filter in a subquery. An 
uncorrelated IN subquery is joined with the rest of the query later, where 
`PartitionPruning` puts a new DPP filter on the key, and the rule decides that 
one against every broadcast. A scalar subquery, which includes an uncorrelated 
EXISTS, is planned on its own, so a key there with a DPP filter gets no bloom 
filter, as on master. A key with a kept DPP filter gets no bloom filter, as on 
master.
   3. **Safe pushdown (`canPushDown`).** For a key whose DPP filters are all 
dropped, pushdown moves the new bloom filter along its key, down to the leaves 
the key comes from, and into every branch of a union. It rewrites the key 
through the aliases on its way. The rule follows the same lineages, with the 
new `findExpressionAndTrackAllLineagesDown` in `PredicateHelper`, which with 
`strict` gives up when it cannot follow the key into a branch. The bloom filter 
is added only if, at every leaf:
      - the leaf is not in a plan in `reused`. There the bloom filter would 
change the broadcast, and physical planning would drop the DPP filter that 
reuses it. A reused broadcast elsewhere in the join side does not matter, such 
as a broadcast dimension joined to the same fact table;
      - the key cannot fail, as `ExprUtils.canEvaluateUnconditionally` decides. 
A numeric up-cast over a key that cannot fail cannot fail either 
(`Cast.canUpCast` between numeric types), nor can the normalization of a 
floating point key. Each is checked against its own child. On a partition 
column, the bloom filter becomes a partition filter, which evaluates the key on 
every partition before the data filters. So a key that can fail could fail the 
query (SPARK-60809).
   
   For a key whose DPP filters are all dropped, the key's type must also 
compare by its bytes (`UnsafeRowUtils.isBinaryStable`), since the bloom filter 
hashes them. A collated string does not. The bloom filter also evaluates its 
creation side again, apart from the join. So the rule also:
   - adds the bloom filter only if the plan that its subquery reads gives the 
same rows again (SPARK-60808). It must read no CTE, whose reference hides the 
determinism of its plan, a cache only if Spark can tell that scanning it again 
gives the same rows, and a V2 scan only if `V2ScanRelationPushDown` marks every 
operation pushed into it as working on each row by itself, unlike a pushed 
limit or top-N. Nor may it read a file metadata column, such as 
`_metadata.file_block_start`, which depends on how the scan splits its files. A 
part of the join side outside that plan can only drop rows from the join, so 
the bloom filter still holds every key that the join sees;
   - adds the bloom filter only if nothing that its subquery evaluates can 
fail, as for the key above. That covers its filters, and the projections that 
the key and the filters need. A DPP filter there counts only if it is 
`onlyInBroadcast`, since it finds no broadcast in the subquery and is `true` 
there. A DPP filter with a benefit would run its own subquery, though cleanup 
can drop it from the join. The subquery can read more of the creation side than 
the join does, since filters on other keys, DPP filters or bloom filters, can 
narrow only the join's side (SPARK-60842 for keys without a DPP filter);
   - leaves the key's own DPP filters out of the creation side, since physical 
planning drops them.
   
   Keys without a DPP filter get their bloom filters as on master, apart from 
the known limits about the filter count.
   
   ### Why are the changes needed?
   
   With the default 
`spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnly`, 
`PartitionPruning` adds an `onlyInBroadcast` DPP filter on a join key that the 
scan offers for runtime filtering, such as a partition column. 
`InjectRuntimeFilter` then skips that key. In a shuffle join or a 
storage-partitioned join, physical planning replaces the DPP filter with 
`true`. So the key ends up with no runtime filter at all.
   
   For example:
   - a V1 table partitioned by the join key, in a shuffle join;
   - a V2 scan that offers the source column of a `bucket(N, id)` partition 
transform for runtime filtering, as Iceberg's does, in a shuffle join and in a 
storage-partitioned join. A bloom filter does not prune any splits there. It 
filters rows, and a scan can apply it as a storage filter.
   
   The same happens in a broadcast join that broadcasts the pruned side, and 
with exchange reuse off.
   
   The key still has to pass the usual checks for a bloom filter. With the 
default thresholds, `spark.sql.autoBroadcastJoinThreshold` and 
`spark.sql.optimizer.runtime.bloomFilter.creationSideThreshold` are both 10MB. 
A join side small enough to be the creation side is then usually small enough 
to broadcast. So the examples need a lower broadcast threshold, or a creation 
side that is a smaller part of the join side, such as a filtered scan under 
another join.
   
   Discussed on #58635: 
https://github.com/apache/spark/pull/58635#issuecomment-6468432320
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, a plan change. Such a join key can now get a runtime bloom filter, 
where it had no runtime filter before. The results stay the same, apart from 
the cases in the Known limits.
   
   ### How was this patch tested?
   
   `InjectRuntimeFilterSuite`:
   - The two existing DPP tests used a shuffle join with an `onlyInBroadcast` 
DPP filter, which is the case this PR changes. They now turn 
`reuseBroadcastOnly` off, so their DPP filter runs as its own subquery and 
still keeps the bloom filter off its key. They also check that subquery.
   - New tests, with AQE on and off unless noted:
     - a shuffle join gets the bloom filter, with the partitioned table on 
either side, and physical planning replaces the DPP filter with `true`. The 
creation side is not empty, so the bloom filter meets the partition values, and 
the query returns rows;
     - no bloom filter when the DPP filter reuses the broadcast of a broadcast 
join that has a shuffle below it. The join broadcasts by a hint, with the 
partitioned table on either side, or by size. With DPP off, the bloom filter. 
With exchange reuse off, the bloom filter, and the DPP filter is `true`;
     - no bloom filter when the DPP filter reuses the broadcast of another 
join, in the other branch of a union;
     - with AQE off, no bloom filter when a null-aware anti join broadcasts the 
build plan of the DPP filter. Physical planning keeps the filter, though it 
builds its own broadcast. With AQE on, which plans the filter as `true`, and 
with DPP off, the bloom filter;
     - a right outer join whose right side is small enough to broadcast: the 
join type rules out that broadcast, so the DPP filter does not count and is 
`true`. The aggregate below the join shuffles, so the join gets the bloom 
filter;
     - a join whose `SHUFFLE_HASH` hint rules out the broadcast: the DPP filter 
does not count and is `true`. The aggregate below the join shuffles, so the 
join gets the bloom filter;
     - no bloom filter if the creation side that the subquery reads is not 
deterministic, or, with AQE off, if the pruned side is not deterministic and 
would be the creation side. Without DPP, the latter gets the bloom filter, as 
on master;
     - no bloom filter for a key with a dropped DPP filter that can fail, also 
behind an alias, under a narrowing cast, also over an up-cast or under the 
normalization of a floating point key, a decimal to bigint cast or, with AQE 
off, a date to timestamp cast, or in one branch of a union, also in a branch 
whose lineage the rule cannot follow. A numeric up-cast of the key, and a 
floating point key, also over a `coalesce`, get the bloom filter. So does a 
creation filter with a `coalesce`, which cannot fail. A cast over a widened 
`coalesce` in a creation filter gives the bloom filter if it widens it again, 
and none if it narrows it. Neither fails to optimize, and a filter nested 28 
levels deep plans quickly;
     - with AQE off, no bloom filter from a creation side that reads a 
non-deterministic CTE;
     - a non-deterministic column that the bloom filter does not read keeps the 
bloom filter;
     - with AQE off, no bloom filter from a cache that is not materialized yet, 
or whose plan Spark cannot show to be repeatable. A materialized cache of a 
scan gives the bloom filter;
     - with AQE off, no bloom filter from a creation side that filters on 
`_metadata.file_block_start`;
     - with AQE off, no bloom filter from a creation side that reads a V2 scan 
with a pushed top-N. With DPP off, that scan gives `p.f5` the bloom filter. 
With DPP on, the other side of the join gets it;
     - no bloom filter for a key with a dropped DPP filter of a collated string 
type. The query returns its row;
     - no bloom filter whose subquery would cast `'abc'` on partitions that a 
kept DPP filter prunes only in the join. That filter comes from an upper join, 
with the cast in the creation key or in a creation filter, or, with AQE off, 
from another branch of a union. With the cast only in the filter, the other 
side of the join gets the bloom filter instead, also with the cast under `or`, 
`not`, `in` or a null check. A creation side whose DPP filter runs as its own 
subquery gives no bloom filter, and the other side of the join gets it. With 
`reuseBroadcastOnly` on, that DPP filter is `true` in the subquery, and both 
keys get the bloom filter;
     - no bloom filter whose creation key would cast `'abc'` on a row that the 
bloom filter of another key drops before the join;
     - no bloom filter in an IN subquery whose DPP filter reuses a broadcast 
outside the subquery. Without that broadcast, the key gets the bloom filter;
     - with AQE off, no bloom filter for a key whose DPP filter physical 
planning drops, where the bloom filter would go into the broadcast that another 
DPP filter reuses. The bloom filter is for a shuffle join inside that 
broadcast, with the partitioned table on either side. Or it goes through a 
union into the other branch, where the broadcast is over a cache. The other DPP 
filter keeps reusing its broadcast. With DPP off, the shuffle join gets the 
bloom filter;
     - no bloom filter on a key whose DPP filter reuses a broadcast side that 
has a DPP filter of its own, which cleanup drops later, with that side on 
either side of the join. The other key of the join, whose DPP filter cleanup 
drops, gets one;
     - with AQE off and `reuseBroadcastOnly` off, no bloom filter that would go 
into the broadcast that a DPP filter with a benefit reuses;
     - a key without a DPP filter keeps the bloom filter that master adds, also 
where pushdown moves it into a broadcast that a DPP filter reuses;
     - when the pruned side is the creation side of the bloom filter, on either 
side of the join, the bloom filter's subquery reads that side and leaves out 
the key's DPP filter;
     - a broadcast join that broadcasts the pruned side gets the bloom filter, 
and the DPP filter is `true`;
     - with a broadcast value projection: when nothing can broadcast the 
projection's source, the bloom filter, and the DPP filter is `true`. With AQE 
off and the inner join broadcasting the source, no bloom filter, and the DPP 
filter reads the projected broadcast values. With AQE on, no bloom filter, and 
the DPP filter is `true` (SPARK-60703). With the projection off, the bloom 
filter.
   
   `KeyGroupedPartitioningCatalystRuntimeFilterSuite`: a storage-partitioned 
join over a scan that offers the source column of `bucket(4, id)` for runtime 
filtering gets the bloom filter among the scan's runtime filters, with AQE on 
and off. The DPP filter is `true`, and the join plans no shuffle.
   
   `StorageFilterPushDownSuite`: two tests turned DPP off, so that DPP would 
not take the bloom filter's place. They now run with DPP on. The test source 
now counts a runtime filter call only for a filter on a column, so the `true` 
DPP filter does not count as the bloom filter reaching runtime filtering.
   
   A temporary check in both DPP planners, not part of this PR and attached in 
a comment, logged every DPP filter that physical planning keeps on a key that 
also has a bloom filter. A bloom filter needs an application side over 
`spark.sql.optimizer.runtime.bloomFilter.applicationSideScanSizeThreshold`, 
10GB by default. So at the default thresholds, only the suites that lower it 
and the TPC-DS sf100 suites with statistics can show the case. The check ran:
   - over these suites, the DPP suites (V1, V2 and V2 filter, AQE on and off), 
`KeyGroupedPartitioningSuite`, and the TPC-DS and TPC-H plan stability suites. 
It logged none with this PR, none on master, and 62 with a rule that ignores 
every `onlyInBroadcast` DPP filter, as a control, of them 54 in the TPC-DS 
sf100 plans;
   - over the DPP suites and the TPC-DS v1.4 and TPC-H plan stability suites, 
with that threshold at 0. It logged none with this PR, and 36 with a control 
rule that counts no `onlyInBroadcast` DPP filter as kept, of them 24 in the DPP 
suites. Some plan assertions of these suites fail at that threshold, as 
expected.
   
   The plan stability suites pass without golden file changes.
   
   A scratch run, not part of this PR, with AQE on and off:
   - a V1 table partitioned by the join key, in a shuffle join;
   - a V2 scan that offers the source column of `bucket(4, id)` for runtime 
filtering, in a shuffle join and in a storage-partitioned join.
   
   With DPP on, each gets the bloom filter, and the storage-partitioned join 
still plans no shuffle.
   
   Known limits:
   - The check counts a DPP filter whenever a join could broadcast its build 
plan, even when physical planning does not reuse that broadcast. The key then 
keeps getting no runtime filter, as on master. For example:
     - a join whose `MERGE` hint asks for a sort merge join, or a 
`SHUFFLE_REPLICATE_NL` hint on an inner join. The check does not model those 
hints. A veto would have to follow every condition of `JoinSelection`, and a 
later change there could put a bloom filter next to a kept DPP filter;
     - join keys that no hash join supports;
     - a broadcast side that keeps a DPP filter of its own, which the DPP 
filter's build plan does not have;
     - under AQE, a join that broadcasts the same plan on other keys. AQE 
compares the broadcast mode too.
   - The rule also skips a new bloom filter when pushdown would stop above the 
broadcast, and under AQE, which may not reuse the broadcast. The key then gets 
no bloom filter, as on master.
   - If the rule cannot follow the key into a union branch, the key gets no 
bloom filter, as on master. That includes a branch whose key is a literal.
   - In a scalar subquery, which includes an uncorrelated EXISTS, a key with a 
DPP filter gets no bloom filter, as on master.
   - A creation side that reads a CTE gives no bloom filter, as on master, even 
a deterministic `MATERIALIZED` CTE. So does a cache that Spark cannot show to 
give the same rows again, such as one that is not materialized yet, and any 
read of a `_metadata` column, though only its split fields depend on how the 
scan splits the files. So does a V2 scan that Spark does not mark as reading 
its rows one by one, such as one with a pushed limit, top-N, offset, sample, 
aggregate or join, or the target scan of a copy-on-write DELETE, UPDATE or 
MERGE.
   - For a key with a dropped DPP filter, a creation side whose subquery would 
evaluate something that can fail gives no bloom filter, as on master. For 
example, a string function, a string to int cast in a filter, or the bloom 
filter of a lower join. The search for the creation side does not then try the 
other side of an inner join.
   - The bloom filter's subquery reads the partitions that a kept DPP filter of 
another key prunes only in the join. If such a partition has a file that cannot 
be read, the subquery fails the query (SPARK-60842 for keys without a DPP 
filter).
   - A `LogicalRDD` that is not a materialized checkpoint also hides the 
determinism of its plan, such as the DataFrame that `foreachBatch` gets for a 
micro-batch, or a lazy checkpoint (`Dataset.checkpoint(eager = false)`). The 
rule cannot see it, since `LogicalRDD` is in `sql/core`. A bloom filter from a 
non-deterministic plan under such a leaf can then drop rows of the join.
   - A source can also pick its rows in its own query, such as a JDBC `query` 
option or a database view with a `LIMIT`. A second read can then return other 
rows. The rule cannot see that either, so a bloom filter from such a source can 
drop rows of the join, as for a key without a DPP filter on master.
   - A bloom filter for a key without a DPP filter can still go into the 
broadcast that a DPP filter reuses, as on master. Physical planning then 
replaces that DPP filter with `true`.
   - `MergeSubplans` can merge the new bloom filter's subquery with a scalar 
subquery inside a reused broadcast. That changes the broadcast in the main 
plan, but not the DPP filter's build plan, so physical planning replaces the 
DPP filter with `true`.
   - A new bloom filter on a scan can keep two scans from sharing an exchange, 
as any bloom filter does. On master, a `true` DPP filter does not keep them 
apart.
   - A DPP filter with a broadcast value projection counts as kept whenever a 
join could broadcast the projection's source plan. Physical planning also 
needs, without AQE, a broadcast hash join that is not null-aware and has the 
same broadcast mode. Under AQE, it plans such a filter as `true` when the plan 
under that broadcast has no shuffle, see SPARK-60703. And at run time the 
projection gives up past its size limits. The key then gets no runtime filter, 
as on master.
   - The new bloom filters count against 
`spark.sql.optimizer.runtimeFilter.number.threshold`. In a query that reaches 
that limit, a bloom filter that master adds for a later join can be left out.
   - A new bloom filter at a join also keeps a later join on the same key from 
adding its own, as any bloom filter on that key does. So the key can get its 
bloom filter from another join than on master.
   - The usual bloom filter checks still apply. `isProbablyShuffleJoin` does 
not look at the join type, so a join whose type rules out the broadcast needs a 
shuffle below it. A partitioned catalog table that was never analyzed reports 
the default size, which the application side check counts as 0, so it gets no 
bloom filter.
   
   An exact fix would decide at physical planning, where the fate of the DPP 
filter is known. That is a larger change, left for a follow-up.
   
   ### 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]

Reply via email to