dongjoon-hyun commented on code in PR #60672:
URL: https://github.com/apache/spark/pull/60672#discussion_r4620851634
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala:
##########
@@ -931,7 +944,8 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan]
with PredicateHelper {
// changes to aggregate columns. When validConstraints is wired up, this
needs revisiting.
// A pushed aggregate cannot be reproduced by rebuilding the scan, so
this scan stays
// not-mergeable (the default `mergeableScan = false`).
- val scanRelation = DataSourceV2ScanRelation(holder.relation,
wrappedScan, realOutput)
+ val scanRelation = DataSourceV2ScanRelation(holder.relation,
wrappedScan, realOutput,
+ repeatableRead = !hasRowSelectingPushdown(holder))
Review Comment:
**[1] A pushed aggregate is not repeatable when its function is not
deterministic.**
The new doc of `repeatableRead` says that a scan with a pushed aggregate
reads the same rows again, and this build site sets it whenever no limit,
offset, top-N or sample is pushed. But
`V2ExpressionBuilder.generateAggregateFunc` translates any `V2Aggregator` into
a `UserDefinedAggregateFunc`, and `V2Aggregator` does not look at
`BoundFunction.isDeterministic()`. So an aggregate function from a
`FunctionCatalog` that declares itself non-deterministic, such as a sampling or
approximate aggregate, is pushed like any other, and a second read of the scan
can return other values. If such a value is a join key, for example
```sql
SELECT f.id FROM (SELECT g, my_udaf(x) AS v FROM cat.db.src GROUP BY g) a
JOIN fact f ON a.v = f.k WHERE a.v > 0
```
with the aggregate fully pushed, the bloom filter's subquery can see other
`v` values than the join and drop fact rows that the join matches. That
`V2Aggregator` ignores `isDeterministic()` predates this PR, but this PR is the
first to state that such a scan is repeatable. Could `rewriteAggregate` skip
the pushdown, or this site set `repeatableRead = false`, when a `V2Aggregator`
with a non-deterministic function is pushed? Otherwise the doc of
`repeatableRead` could at least exclude it.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala:
##########
@@ -269,7 +277,13 @@ case class DataSourceV2ScanRelation(
pushedFilters: Seq[Expression] = Seq.empty,
inferredFilters: Seq[Expression] = Seq.empty,
mergeableScan: Boolean = false,
- onlyRowWisePushdowns: Boolean = false) extends LeafNode with NamedRelation
{
+ onlyRowWisePushdowns: Boolean = false,
+ repeatableRead: Boolean = false) extends LeafNode with NamedRelation {
Review Comment:
**[5] Scans from other build sites silently lose their bloom filters.**
With `repeatableRead = false` by default, `extract` now drops every
`DataSourceV2ScanRelation` built by a rule other than `V2ScanRelationPushDown`
and `GroupBasedRowLevelOperationScanPlanning`, such as a connector's or an
extension's own scan planning rule. Before this PR such a scan was a creation
side under a selective `Filter`. The results stay right, but those queries lose
their runtime bloom filters after an upgrade, with nothing in the plan or the
logs to say why. The PR description lists it as a user-facing change. Do you
know of such a build site outside Spark? If there is a common one, for example
in a connector's row-level planning, a line in the SQL migration guide would
reach its owners better than the PR description.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala:
##########
@@ -269,7 +277,13 @@ case class DataSourceV2ScanRelation(
pushedFilters: Seq[Expression] = Seq.empty,
inferredFilters: Seq[Expression] = Seq.empty,
mergeableScan: Boolean = false,
- onlyRowWisePushdowns: Boolean = false) extends LeafNode with NamedRelation
{
+ onlyRowWisePushdowns: Boolean = false,
+ repeatableRead: Boolean = false) extends LeafNode with NamedRelation {
+
+ // A scan whose pushed operations all work on each row by itself returns the
same rows when read
+ // again, so DPP and the runtime bloom filter, which read different flags,
agree on it.
+ require(!onlyRowWisePushdowns || repeatableRead,
Review Comment:
**[4] The `require` turns a flag combination into a query failure.**
A rule that sets `onlyRowWisePushdowns = true` without `repeatableRead =
true`, through `copy` or a direct build, now fails the query during
optimization with `IllegalArgumentException: requirement failed`, which does
not read as a Spark bug. The tests in this PR set both flags, but
`StorageFilterPushDownSuite` and `DataSourceV2FileSourceDPPSuite` already copy
`onlyRowWisePushdowns` alone, and pass only because their scans happen to be
repeatable. An extension that opts a scan into storage filters the same way
would fail. Since `onlyRowWisePushdowns` implies `repeatableRead` by
definition, the invariant could hold by construction instead, for example with
a `def isRepeatableRead: Boolean = repeatableRead || onlyRowWisePushdowns` that
`extract` reads. If the `require` stays, `SparkException.internalError` would
at least report it as one.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala:
##########
@@ -905,17 +905,30 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan]
with PredicateHelper {
* - `pushedOffset` -- pushed OFFSET
* - `pushedSample` -- pushed table sample
* - `sortOrders` -- pushed sort order (top-N / ordering)
+ * - `joinedRelationsPushedDownOperators` -- the same operations pushed
into a side of a join
*
* It decides the `onlyRowWisePushdowns` flag of the plain and the
variant-extraction scans, see
* [[DataSourceV2ScanRelation.onlyRowWisePushdowns]], and blocks merging, see
* [[hasBlockingPushdown]]. A pushed aggregate or join never reaches those
builds, and their scans
- * keep the flag's default.
+ * keep the flag's default. It also decides the `repeatableRead` flag of
every scan this rule
+ * builds, see [[DataSourceV2ScanRelation.repeatableRead]]. Limit, offset
and top-N pushdowns run
+ * before the aggregate and the join builds, so those builds see a limit,
offset or top-N pushed
+ * above an aggregate or join. An operation pushed into a side of a pushed
join is kept with that
+ * side's pushed operators, which this checks too, whichever pushdowns run
before `pushDownJoin`.
*/
private def hasRowSelectingPushdown(holder: ScanBuilderHolder): Boolean =
holder.pushedLimit.isDefined ||
holder.pushedOffset.isDefined ||
holder.pushedSample.isDefined ||
- holder.sortOrders.nonEmpty
+ holder.sortOrders.nonEmpty ||
+
holder.joinedRelationsPushedDownOperators.exists(hasRowSelectingOperators)
+
+ private def hasRowSelectingOperators(operators: PushedDownOperators):
Boolean =
Review Comment:
**[6] nit: `hasRowSelectingPushdown` could reuse
`hasRowSelectingOperators`.**
`getPushedDownOperators(holder)` copies exactly these holder fields into a
`PushedDownOperators`: `pushedSample`, `pushedLimit`, `pushedOffset`,
`sortOrders` and `joinedRelationsPushedDownOperators`. So
```scala
private def hasRowSelectingPushdown(holder: ScanBuilderHolder): Boolean =
hasRowSelectingOperators(getPushedDownOperators(holder))
```
would keep one list instead of two. With two lists, a row-selecting pushdown
that a later change adds to only one of them would make either the scan itself
or a side of a pushed join look repeatable. `pruneColumns` also evaluates the
same check three times (`mergeableScan`, `onlyRowWisePushdowns` and
`repeatableRead`), where one `val` would do.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/InjectRuntimeFilter.scala:
##########
@@ -431,8 +435,9 @@ object InjectRuntimeFilter extends Rule[LogicalPlan] with
PredicateHelper with J
// plan it reads must give the same rows again (SPARK-60808). So 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 every operation pushed
into it works on each
- // row by itself. A pushed limit or top-N, for example, lets the source pick
other rows on a
- // second read. It must not read a file metadata column either, such as
+ // row by itself. `extract` already drops a V2 scan that is not
`repeatableRead`, so this keeps
Review Comment:
**[2] `canEvaluateAgain` still reads `onlyRowWisePushdowns` for the question
that `repeatableRead` now answers.**
This comment says the plan that the subquery reads "must give the same rows
again", which is what `repeatableRead` means. `onlyRowWisePushdowns` is
stricter. It also leaves out a pushed aggregate or join and the target read of
a copy-on-write operation, which read the same rows again but must not take
storage filters. So a key whose DPP filter physical planning drops gets no
bloom filter from such a scan, while the same key gets one when DPP is off. The
new test pins this (`dpp = true, repeatableRead = true` expects `Nil`), but
neither this comment nor the PR description says why such a scan cannot be
evaluated again. If there is no other reason, `case scan:
DataSourceV2ScanRelation => !scan.repeatableRead` here would match `extract`,
and the case would then only restate what `extract` already checked. If there
is a reason, could this comment say it?
The same check now appears in a third place.
[SPARK-60541](https://github.com/apache/spark/commit/a02aef505c9f6e0324db570e244c15f6c3d86997)
landed on master after this PR's CI ran. Its new
`injectPerPartitionBloomFilter` checks `case scan: DataSourceV2ScanRelation =>
!scan.onlyRowWisePushdowns` for the same reason: its aggregate reads the
creation side again. It merges cleanly with this PR, and after a rebase that
check could switch to `repeatableRead` together with this one.
##########
sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCV2Suite.scala:
##########
@@ -338,6 +338,10 @@ class JDBCV2Suite extends SharedSparkSession with
ExplainSuiteHelper {
checkAnswer(df, Seq(Row("amy"), Row("alex"), Row("cathy")))
}
+ private def scanOf(df: DataFrame): DataSourceV2ScanRelation =
Review Comment:
**[7] nit: `checkSchemaNames` could use the new `scanOf`.**
`checkSchemaNames` right below collects the same `DataSourceV2ScanRelation`.
`val scan = scanOf(df)` there would remove the duplicate, and a missing scan
would then fail with the plan instead of a bare `NoSuchElementException` from
`.get`.
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala:
##########
@@ -269,7 +277,13 @@ case class DataSourceV2ScanRelation(
pushedFilters: Seq[Expression] = Seq.empty,
inferredFilters: Seq[Expression] = Seq.empty,
mergeableScan: Boolean = false,
- onlyRowWisePushdowns: Boolean = false) extends LeafNode with NamedRelation
{
+ onlyRowWisePushdowns: Boolean = false,
+ repeatableRead: Boolean = false) extends LeafNode with NamedRelation {
+
+ // A scan whose pushed operations all work on each row by itself returns the
same rows when read
+ // again, so DPP and the runtime bloom filter, which read different flags,
agree on it.
Review Comment:
**[3] DPP asks the same question with the other flag.**
This comment says that DPP and the runtime bloom filter read different flags
and agree on a row-wise scan. But they ask the same question.
`PartitionPruning` keeps a standalone DPP subquery off a filtering side with a
scan that is not `onlyRowWisePushdowns`, because "A standalone DPP subquery
reads the filtering side again" ([PartitionPruning.scala
L116-L123](https://github.com/apache/spark/blob/78c51f70a91dd29b148ab68c174dd15cfa3fa4ee/sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/PartitionPruning.scala#L116-L123)).
That is the `repeatableRead` question. So a filtering side over a pushed
aggregate or join can only reuse a broadcast, though it reads the same rows
again. And `DataSourceV2FileSourceDPPSuite` simulates a scan that returns other
rows when read again with `copy(onlyRowWisePushdowns = false)`, which keeps
`repeatableRead = true`. Reading `repeatableRead` there too, as in [2], would
give each flag one meaning: `onlyRowWisePushdowns` whether a reade
r may filter ahead of the pushed operations, and `repeatableRead` whether a
second read gives the same rows. That can also be a separate change if you
prefer to keep this one small.
--
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]