spark-test commented on code in PR #60387:
URL: https://github.com/apache/spark/pull/60387#discussion_r4585170658


##########
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:
   Thanks, verified at 476c97e8754: `extractBeneficialFilterCreatePlan` now 
requires both join keys to be binary stable before it looks for any creation 
side (SPARK-60871), so a side found through pushed filters can no longer build 
a bloom filter on a collated key. The new test pins this for a plain 
`UTF8_LCASE` key and for one inside a struct.
   
   <!-- SPARK_DEV_REVIEW_REPLY 
{"feedback_id":"inline:4571992040","thread_id":"inline:4571992040","verdict_sha256":"f2b2d07bce3c1af617c7c2a8d6b71ee7736aeaf38a6e8143f4be74666ae112bb"}
 -->



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InjectRuntimeFilter.scala:
##########
@@ -239,11 +251,23 @@ object InjectRuntimeFilter extends Rule[LogicalPlan] with 
PredicateHelper with J
               useMaterializedThreshold = false))
           case _ => None
         }
-      case _: LeafNode if hasHitSelectiveFilter =>
+      // The bloom filter's subquery reads a DSv2 scan again, so only a scan 
marked with
+      // `repeatableRead` counts, with or without a selective `Filter` above 
it. A pushed limit, for
+      // example, lets the source pick other rows on a second read. The pushed 
filters it records
+      // must be simple, as the condition of a `Filter` must.
+      case scan: DataSourceV2ScanRelation
+          if !scan.repeatableRead || 
!scan.pushedFilters.forall(isSimpleExpression) => None

Review Comment:
   **Non-blocking (P2):** For a key without a DPP filter, this arm now also 
rejects a repeatable scan reached through a selective simple `Filter` when one 
of its recorded pushed filters is not simple. On the base branch such a scan 
reached `case _: LeafNode if hasHitSelectiveFilter` and built a bloom filter 
with correct results. These queries are outside the problem this PR fixes, yet 
they lose that bloom filter.
   
   Reading the code, take the new test's `d.ds LIKE 'a_' AND d.v > 2` case with 
parquet read through V2. `FileScanBuilder` takes the partition filter `ds LIKE 
'a_'` in fully and leaves `v > 2` in a `Filter` above the scan, so the base 
branch builds a bloom filter on `f.part` that keeps the rows of 2 of the 10 
fact partitions. At this head the arm returns `None` and `f.part` gets none, as 
the new test expects. Results are the same, but the fact side shuffles and 
joins every row. The same holds for other non-simple pushed filters, such as a 
Scala UDF, `from_json`, `regexp_extract` or `regexp_replace` on a partition 
column, and for variant-extraction scans, which now record their pushed filters.
   
   V1 builds no bloom filter for this query because its `Filter` keeps the 
`LIKE`. But the simplicity rule is a cost heuristic that keeps expensive 
expressions out of the bloom filter's subquery, and a pushed filter adds none 
there: a V2 file scan evaluates a pushed partition filter once, against 
partition directory values, in a listing that both reads of the scan share 
(`RuntimePartitionPruningFileScan`). For a key whose DPP filter is dropped, the 
`cannotFail` check on pushed filters rejects such a scan anyway, so this 
concerns only keys without a DPP filter.
   
   This extends an [existing 
discussion](https://github.com/apache/spark/pull/60387#discussion_r4560012324).
   
   **Recommended change:** Scope the simplicity half of the DSv2 guard arm in 
the bloom filter's creation-side search to sides found through pushed filters 
alone. A scan that is not `repeatableRead` stays out on every path. A scan 
whose recorded pushed filters are not all simple stays out only when no 
selective `Filter` sits above it, so the condition becomes 
`!scan.repeatableRead || (!hasHitSelectiveFilter && 
!scan.pushedFilters.forall(isSimpleExpression))`. Reword the arm's comment to 
match. In the runtime bloom filter tests, expect V2's bloom filter on `part` 
for `d.ds LIKE 'a_' AND d.v > 2` without DPP, while V1 keeps none and the 
dropped-DPP expectation stays none. That case's shared V1/V2 expectation has to 
split. The PR description's bullet that lists a non-simple pushed filter among 
the reasons a scan under a selective `Filter` is no longer a creation side 
needs the same correction. History: in 3a719cba5ee the PR checked pushed-filter 
simplicity only in the pushed-filter case
 , as dongjoon-hyun's review of that commit describes. 7759f3587fd moved the 
check into a guard arm for both paths, and 04f8a85e9ce swapped 
`onlyRowWisePushdowns` for `repeatableRead` in that arm. Start from 
3a719cba5ee's pushed-only scoping and keep the head's `repeatableRead` half.
   
   **Why this works:** `hasHitSelectiveFilter` already records whether a 
simple, likely-selective `Filter` sits above the scan. With the simplicity test 
guarded by `!hasHitSelectiveFilter`, such a scan falls through to the merged 
leaf arm, which returns it as the base branch's leaf arm did. A side found only 
through pushed filters still needs simple pushed filters, as V1 needs a simple 
`Filter`. The `repeatableRead` half is untouched, so a row-selecting scan stays 
out everywhere.
   
   **Scope:** Narrow one guard condition in the bloom filter's creation-side 
search, reword its comment, and split one V1/V2 expectation in the runtime 
bloom filter tests.
   
   **Compatibility:** Every other creation-side decision stays as at head: the 
`repeatableRead` exclusion, the early checks and the fallback for sides found 
only through pushed filters, and the `cannotFail` check on pushed filters for a 
key whose DPP filters are dropped.
   
   **Risks:** For a dimension whose selective residual filter sits over a 
non-simple partition filter, V1 and V2 choose differently again, as on the base 
branch: V2 builds the bloom filter and V1 does not. The change can be rolled 
back by restoring the current guard. For a key whose DPP filters are dropped, a 
nested creation side whose key side is such a scan no longer falls back to the 
other side of its join. The scan is returned, `canEvaluateAgain` rejects it, 
and the search ends, as the PR already does for any side found under a 
selective `Filter`. That path is in no release.
   
   **Constraints:** Keep the `repeatableRead` half on every path, so a scan 
with a pushed limit, offset, top-N or sample is never a creation side. Keep the 
`cannotFail` check on pushed filters in `canEvaluateAgain`, so a key whose DPP 
filters are dropped still gets no bloom filter from a scan with a non-simple or 
fallible pushed filter. A side found only through pushed filters still needs 
simple pushed filters, so the cases without a selective residual filter keep 
expecting no bloom filter under V1 or V2.
   
   **Exclusions:**
   - Relaxing the `cannotFail` check on pushed filters for a key whose DPP 
filters are dropped stays out of this change: that path is in no release, and 
the author's choice there stands.
   
   **Success:** For a key without a DPP filter, a repeatable DSv2 scan under a 
selective simple `Filter` whose recorded pushed filters include a non-simple 
expression is a creation side again, as on the base branch. Under V2, `d.ds 
LIKE 'a_' AND d.v > 2` gets a bloom filter on `part` without DPP and returns 
the same rows. A DSv2 scan whose only selective filters are pushed, one of them 
non-simple, is still not a creation side, matching V1. A DSv2 scan that is not 
`repeatableRead` is still never a creation side, with or without a selective 
`Filter`. For a key whose DPP filters are dropped, a DSv2 scan with a 
non-simple pushed filter still gives no bloom filter.
   
   **Verification:**
   - **Regression:** For a key without a DPP filter, a repeatable DSv2 scan 
under a selective simple residual `Filter` whose recorded pushed filters 
include a non-simple expression is a creation side again: under V2 with DPP 
off, a V2 file dimension filtered by `d.ds LIKE 'a_' AND d.v > 2`, with or 
without a pushed variant extraction in its scan, gets a bloom filter on the 
fact's join key and returns the same rows as V1, which builds none.
   - **Behavior:** A dimension whose only selective filters are pushed 
partition filters that include a non-simple one, such as `d.ds LIKE 'a_'` alone 
or with `d.dp = 1`, still gets no bloom filter under V1 or V2.
   - **Compatibility:** With DPP on and the DPP filter dropped, the selective 
residual case over a non-simple pushed filter still gets no bloom filter. A 
scan with a pushed limit, offset, top-N or sample under a selective filter 
still gets none, with or without DPP.



##########
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:
   Thanks, verified at 476c97e8754. A side found only through pushed filters is 
now taken only if `bloomFilterAggregate`, which `injectBloomFilter` also runs, 
and the caller's `takes` check both accept it. Otherwise the case returns 
`None`, and the search goes on to the other side of the join. A materialized 
cache as that other side and a key projection that can fail go through the same 
check and fallback as the shapes the new tests cover, so I'm fine without 
separate tests for them.
   
   <!-- SPARK_DEV_REVIEW_REPLY 
{"feedback_id":"inline:4571992044","thread_id":"inline:4571992044","verdict_sha256":"f2b2d07bce3c1af617c7c2a8d6b71ee7736aeaf38a6e8143f4be74666ae112bb"}
 -->



-- 
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