LuciferYang commented on code in PR #60387:
URL: https://github.com/apache/spark/pull/60387#discussion_r4612259685
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala:
##########
@@ -905,17 +905,26 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan]
with PredicateHelper {
* - `pushedOffset` -- pushed OFFSET
* - `pushedSample` -- pushed table sample
* - `sortOrders` -- pushed sort order (top-N / ordering)
+ * - `joinedRelationsPushedDownOperators` -- table sample pushed into a
side of a pushed 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 they see one pushed after an
aggregate or join. A
+ * sample pushed into a side of a pushed join is kept with that side's
pushed operators.
*/
private def hasRowSelectingPushdown(holder: ScanBuilderHolder): Boolean =
holder.pushedLimit.isDefined ||
holder.pushedOffset.isDefined ||
holder.pushedSample.isDefined ||
- holder.sortOrders.nonEmpty
+ holder.sortOrders.nonEmpty ||
+ holder.joinedRelationsPushedDownOperators.exists(hasPushedSample)
+
+ private def hasPushedSample(operators: PushedDownOperators): Boolean =
Review Comment:
Done in ffe633321db. `hasRowSelectingOperators` checks `limit`, `offset`,
`sortValues` and `sample` of each pushed join side, recursively, so it no
longer depends on `pushDownLimitAndOffset` running after `pushDownJoin`.
##########
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
+ // A filter that a DSv2 scan took in fully leaves no `Filter` for it, as
a built-in file scan
+ // does with its partition filters, so a selective one counts as a
selective `Filter` does.
+ // The size check of `bloomFilterAggregate` uses the scan's estimate,
which for a V2 file
+ // scan covers all its partitions.
+ case leaf: LeafNode if hasHitSelectiveFilter ||
hasSelectivePushedFilters(leaf) =>
Some(FilterCreationSide(
targetKey,
currentPlan,
- useMaterializedThreshold = false))
+ useMaterializedThreshold = false)).filter { side =>
+ hasHitSelectiveFilter || (bloomFilterAggregate(side).isDefined &&
takes(side))
Review Comment:
Done in ffe633321db. A side found through pushed filters now carries the
aggregate the search built for it, in `FilterCreationSide.checkedAggregate`.
`injectBloomFilter` uses it instead of building it again, and `creationSide`
skips `takes` for such a side, since the search already ran it.
##########
sql/core/src/test/scala/org/apache/spark/sql/InjectRuntimeFilterSuite.scala:
##########
@@ -2162,6 +2165,251 @@ class InjectRuntimeFilterSuite extends
SharedSparkSession
}
}
+ test("SPARK-61173: a DSv2 scan that took a selective filter in fully can be
the creation " +
+ "side, as in V1") {
+ // `FileScanBuilder` takes a partition filter in fully and leaves no
`Filter` for it above the
+ // V2 scan, while V1 keeps the `Filter`. The rule counts a selective
pushed filter as it counts
+ // that `Filter`. V1 is the reference.
+ withTempDir { dir =>
+ val factPath = new File(dir, "fact").getCanonicalPath
+ val dimPath = new File(dir, "dim").getCanonicalPath
+ spark.range(100).selectExpr("id", "CAST(id % 10 AS INT) AS part")
+ .write.partitionBy("part").parquet(factPath)
+ spark.range(10).selectExpr("CAST(id AS INT) AS k", "CAST(id AS INT) AS
v",
+ "parse_json(CONCAT('{\"a\": ', id, '}')) AS j", "CAST(id % 2 AS INT)
AS dp",
+ "IF(id < 5, 'a1', 'b1') AS ds").write.partitionBy("dp",
"ds").parquet(dimPath)
+ def createViews(): Unit = {
+ // A temp view keeps the relation it was created with, so create both
under the conf.
+ spark.read.parquet(factPath).createOrReplaceTempView("rf_fact")
+ spark.read.parquet(dimPath).createOrReplaceTempView("rf_dim")
+ }
+ def keyNames(plan: LogicalPlan): Seq[String] =
plan.flatMap(_.expressions)
+ .flatMap(_.collect { case BloomFilterMightContain(_,
XxHash64(Seq(key), _)) => key })
+ .flatMap(_.references.map(_.name))
+ // Runs `query` with DPP off and on, under V1 and V2. Checks that DPP
adds its filter only
+ // when on, that the bloom filters check `withoutDpp`, or
`withDroppedDpp` when the shuffle
+ // join drops the DPP filter, and that the query returns `expected`.
+ def check(
+ query: String,
+ withoutDpp: Seq[String],
+ withDroppedDpp: Seq[String],
+ expected: Seq[Row],
+ checkPlan: LogicalPlan => Unit = _ => ()): Unit = {
+ for (dpp <- Seq(false, true); v1Sources <- Seq("parquet", "")) {
+ withSQLConf(SQLConf.USE_V1_SOURCE_LIST.key -> v1Sources,
+ SQLConf.DYNAMIC_PARTITION_PRUNING_ENABLED.key -> dpp.toString) {
+ createViews()
+ val df = sql(query)
+ val optimized = df.queryExecution.optimizedPlan
+ val clue = s"`$query` with dpp = $dpp, v1 sources
'$v1Sources':\n$optimized"
+ assert(optimized.exists(_.isInstanceOf[DataSourceV2ScanRelation])
===
+ v1Sources.isEmpty, clue)
+ assert(optimized.exists(_.expressions.exists(
+ _.exists(_.isInstanceOf[DynamicPruningSubquery]))) === dpp, clue)
+ assert(keyNames(optimized) == (if (dpp) withDroppedDpp else
withoutDpp), clue)
+ if (v1Sources.isEmpty) {
+ // The dimension's scan takes its selective filter on partition
columns in fully.
+ assert(optimized.exists {
+ case r: DataSourceV2ScanRelation =>
+ r.output.exists(_.name == "k") && r.hasSelectivePushedFilters
+ case _ => false
+ }, clue)
+ checkPlan(optimized)
+ }
+ checkAnswer(df, expected)
+ }
+ }
+ }
+ def factRows(keeps: Int => Boolean): Seq[Row] =
+ (0 until 100).filter(i => keeps(i % 10)).map(i => Row(i.toLong))
+ withTempView("rf_fact", "rf_dim") {
+ withDppTestConf(SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
Review Comment:
Done in ffe633321db. The block runs with AQE off, `check` creates the views
once per source mode, and the `d.ds LIKE 'a_'` row is gone. That makes 62 query
executions instead of 136. `d.ds LIKE 'a_' AND d.v > 2` stays, since it is the
one row with a selective `Filter` above a scan that recorded a filter that is
not simple.
--
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]