spark-test commented on code in PR #60387:
URL: https://github.com/apache/spark/pull/60387#discussion_r4571992044
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InjectRuntimeFilter.scala:
##########
@@ -244,6 +245,18 @@ object InjectRuntimeFilter extends Rule[LogicalPlan] with
PredicateHelper with J
targetKey,
currentPlan,
useMaterializedThreshold = false))
+ // A DSv2 scan can take a filter in fully and leave no `Filter` for it,
as a built-in file
+ // scan does with its partition filters. With no selective `Filter`
above the scan, its
+ // pushed filters count as such a `Filter` would, but only for a scan
whose pushed operations
+ // all work on each row by itself, since the bloom filter's subquery
reads it again. A pushed
+ // limit, for example, lets the source pick other rows on a second read.
+ case scan: DataSourceV2ScanRelation if scan.onlyRowWisePushdowns &&
+ scan.pushedFilters.forall(isSimpleExpression) &&
+ scan.pushedFilters.exists(isLikelySelective) =>
+ Some(FilterCreationSide(
Review Comment:
**Non-blocking (P2):** Returning `Some` here ends the search before the
checks that can still reject this side: `injectBloomFilter`'s size, Python UDF
and determinism checks, and, for a key whose DPP filter is dropped,
`canEvaluateAgain`. For an equi-join creation side, `extract` tries the
transitive side only through `.orElse` on `None`, and
`extractBeneficialFilterCreatePlan` runs its cache pass only when the first
pass saw a materialized leaf, so a key side that this case accepts and a later
check rejects leaves the query with no bloom filter.
For example, reading the code, in `SELECT f.* FROM fact f JOIN (SELECT a.k
FROM pg.dim_a a JOIN dim_b b ON a.k = b.k WHERE a.region = 'US' AND b.flag =
'Y') x ON f.k = x.k`, with `pg` a JDBC catalog that takes `region = 'US'` in
fully, the JDBC scan reports no statistics, so this case accepts it and
`injectBloomFilter` then drops it for its size. On the base branch the scan
fell through to `case _ => None`, and `f.k` got a bloom filter built from
`Filter(flag = 'Y', dim_b)`. A V2 file key side whose whole-table estimate
exceeds the creation-side threshold behaves the same way. Results do not
change, but these queries lose pruning the base branch gave them.
Letting this case decline a side that those later checks would reject, by
sharing the checks with it rather than restating part of them, keeps the
fallback.
**Verification:**
- **Regression:** An equi-join creation side whose key side is a DSv2 scan
that takes its selective filter in fully but reports no size or a size above
the creation-side threshold, joined on the key to a side with a selective
Filter that fits the threshold, gets a bloom filter on the application key
built from that other side, as on the base branch, and so does the same shape
when the other side is a fully materialized cache that qualifies by its exact
statistics.
- **Regression:** For a key whose DPP filter physical planning drops, the
same nested shape whose key side canEvaluateAgain rejects, once for a pushed
filter that fails cannotFail, such as a partition RLIKE, and once for a key
projection that can fail, such as an ANSI cast, gets the bloom filter from the
other side when that side passes the dropped-DPP checks.
- **Inspection:** The check the new case applies is the same code that
injectBloomFilter and creationSide apply to a creation side, not a restated
subset of it.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InjectRuntimeFilter.scala:
##########
@@ -244,6 +245,18 @@ object InjectRuntimeFilter extends Rule[LogicalPlan] with
PredicateHelper with J
targetKey,
currentPlan,
useMaterializedThreshold = false))
+ // A DSv2 scan can take a filter in fully and leave no `Filter` for it,
as a built-in file
+ // scan does with its partition filters. With no selective `Filter`
above the scan, its
+ // pushed filters count as such a `Filter` would, but only for a scan
whose pushed operations
+ // all work on each row by itself, since the bloom filter's subquery
reads it again. A pushed
+ // limit, for example, lets the source pick other rows on a second read.
+ case scan: DataSourceV2ScanRelation if scan.onlyRowWisePushdowns &&
Review Comment:
**Blocking (P1):** This case accepts the scan whatever the join key's type,
and the `UnsafeRowUtils.isBinaryStable` check that guards a bloom filter's key,
in `creationSide`, runs only for a key with DPP filters. `injectBloomFilter`
then hashes both keys with `XxHash64`, which hashes the raw bytes of a string
whose collation lacks binary equality unless
`spark.sql.legacy.collationAwareHashFunctions` is on, so values that are equal
under the collation can fail the bloom filter.
Reading the code, this drops rows for a query like `SELECT f.id FROM fact f
WHERE f.k IN (SELECT d.k FROM dim d WHERE d.dp = 1)`, where `k` is `STRING
COLLATE UTF8_LCASE` on both sides, `dim` is a V2 Parquet table partitioned by
`dp` with `k = 'ABC'`, the fact holds `k = 'abc'`, and `k` gets no DPP filter.
The semi join is created after `RewriteCollationJoin` runs, so its keys stay
collated (the SPARK-60702 collated-key test shows such a key reaching this
rule). `dp = 1` is taken in fully, so this case builds the bloom filter from
`'ABC'`, and the fact row is dropped before the join. On the base branch this
dimension got no bloom filter and the row was returned. The selective-`Filter`
arm already has the same gap (SPARK-58486); this case extends it to dimensions
that were not affected before.
Accepting the scan here only when both join keys are binary stable, the
condition `extractBeneficialFilterCreatePlan` already computes for materialized
caches, would close it.
**Verification:**
- **Regression:** For a join key whose type is not binary stable and that
has no DPP filter, in a join that reaches the rule with that key, such as a
semi join from an IN subquery, both on a UTF8_LCASE string key and on a struct
key holding a UTF8_LCASE field, a DSv2 dimension that takes its only selective
filter in fully, such as a V2 file dimension filtered on a partition column,
gives no bloom filter, and the query returns the fact rows whose keys match the
dimension's only under the collation, such as 'abc' against 'ABC'.
--
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]