dongjoon-hyun commented on code in PR #55579:
URL: https://github.com/apache/spark/pull/55579#discussion_r4515586911


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/CleanupDynamicPruningFilters.scala:
##########
@@ -34,24 +34,35 @@ import 
org.apache.spark.sql.execution.datasources.v2.DataSourceV2ScanRelation
  */
 object CleanupDynamicPruningFilters extends Rule[LogicalPlan] with 
PredicateHelper {
 
+  private val equalityKey: PartialFunction[Expression, Expression] = {
+    case EqualTo(l, r) if l.deterministic && r.foldable => l
+    case EqualTo(l, r) if r.deterministic && l.foldable => r
+    case EqualNullSafe(l, r) if l.deterministic && r.foldable => l
+    case EqualNullSafe(l, r) if r.deterministic && l.foldable => r
+  }
+
   private def collectEqualityConditionExpressions(condition: Expression): 
Seq[Expression] = {
-    splitConjunctivePredicates(condition).flatMap(_.collect {
-      case EqualTo(l, r) if l.deterministic && r.foldable => l
-      case EqualTo(l, r) if r.deterministic && l.foldable => r
-      case EqualNullSafe(l, r) if l.deterministic && r.foldable => l
-      case EqualNullSafe(l, r) if r.deterministic && l.foldable => r
-    })
+    splitConjunctivePredicates(condition).flatMap(_.collect(equalityKey))

Review Comment:
   **39. [Low] An equality below `NOT` or `OR` in the `Filter` above a V2 file 
scan drops a DPP filter that could still prune**
   
   `_.collect(equalityKey)` finds an equality anywhere in a conjunct, as L51 
now documents, so `f.part = 3` inside `(f.part = 3 OR f.id > 50)` counts as 
fixing `part`, although that conjunct does not fix it. With a V2 parquet 
`fact(id, part INT)` partitioned by `part` and `dim(dim_id INT, dim_val INT)`:
   
   ```sql
   SELECT f.id FROM fact f JOIN dim d ON f.part = d.dim_id
   WHERE d.dim_val = 7 AND (f.part = 3 OR f.id > 50)
   ```
   
   The `OR` references a data column, so it stays in the `Filter` above the 
scan, and this rule replaces the DPP filter on `part` with `true`. The scan 
then reads every directory instead of only `part=7`. Results are right, and 
this is V1's behavior, which is why the test at 
DataSourceV2FileSourceDPPSuite.scala:764-767 checks its last two cases under V2 
only. So it is no regression from master, where this scan gets no DPP at all. 
But with this PR it reaches every V2 file scan, where the pushed path at L53-56 
counts only top-level conjuncts: `f.part <> 3` keeps the DPP filter, while 
`(f.part <> 3 OR f.id > 50)` drops it. Could the `Filter` path also count only 
top-level `=` and `<=>` conjuncts, here or in a follow-up that covers V1 too?



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/RuntimePartitionPruningFileScan.scala:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.sql.execution.datasources.v2
+
+import scala.util.control.NonFatal
+
+import org.apache.spark.SparkException
+import org.apache.spark.internal.LogKeys
+import org.apache.spark.sql.catalyst.expressions.{Attribute, BoundReference, 
Expression, Predicate}
+import org.apache.spark.sql.connector.expressions.{FieldReference, 
NamedReference}
+import org.apache.spark.sql.connector.read.InputPartition
+import org.apache.spark.sql.execution.datasources.{FilePartition, 
PartitionDirectory}
+import org.apache.spark.sql.internal.connector.SupportsRuntimeCatalystFiltering
+import org.apache.spark.util.TransientBestEffortLazyVal
+
+/**
+ * A [[FileScan]] that prunes its partition directories with the runtime 
filters Spark derives over
+ * its partition columns (SPARK-30628). The built-in file formats mix this in.
+ *
+ * Both read paths start from one listing of the file index and split and pack 
the files they
+ * keep through `filePartitions`, so `partitions`, `planInputPartitions()` and
+ * `planInputPartitionsWithRuntimeFilters` are final here. A scan that reports 
a partitioning or an
+ * ordering should not mix this in. Spark gives it no group filter in a 
row-level operation, see
+ * `RowLevelOperationRuntimeGroupFiltering`.
+ */
+trait RuntimePartitionPruningFileScan extends FileScan with 
SupportsRuntimeCatalystFiltering {
+
+  /**
+   * The file index listing and its packing, each taken once per scan so 
planning and execution
+   * read one snapshot. A `lazy val` would compute them while holding the 
scan's monitor. Only a
+   * scan with runtime-filterable columns keeps the listing, since nothing 
else reads it again.
+   */
+  private val listing = new TransientBestEffortLazyVal(() => listFiles())
+
+  private val allPartitions = new TransientBestEffortLazyVal(() =>
+    filePartitions(if (filterAttributes().isEmpty) listFiles() else listing()))
+
+  private def listFiles(): Seq[PartitionDirectory] =
+    fileIndex.listFiles(partitionFilters, dataFilters)
+
+  // Final so that a subclass of a built-in scan cannot change one read path 
without the other.
+  final override protected def partitions: Seq[FilePartition] = allPartitions()
+
+  final override def planInputPartitions(): Array[InputPartition] = 
allPartitions().toArray
+
+  /**
+   * The read partition columns that `readSchema()` still exposes; a 
pushed-down aggregate keeps
+   * only the ones it groups by. None is declared fully pushed, so a scalar 
subquery filter over
+   * them is evaluated again above the scan.
+   */
+  override def filterAttributes(): Array[NamedReference] = {
+    val readFields = 
readSchema().fieldNames.map(normalizePartitionColumnName).toSet
+    readPartitionSchema.fieldNames
+      .filter(name => readFields.contains(normalizePartitionColumnName(name)))
+      .map(FieldReference.column)
+  }
+
+  /**
+   * Keeps the listed partition directories whose values satisfy 
`expressions`, then splits and
+   * packs their files for their own size, as V1 does for 
`dynamicallySelectedPartitions`.
+   *
+   * A filter that fails to evaluate on a directory keeps it, since the join 
or the `FilterExec`
+   * above the scan applies the filter again. Each call logs at most one 
warning for such failures.
+   */
+  final override def planInputPartitionsWithRuntimeFilters(
+      expressions: Array[Expression]): Array[InputPartition] = {
+    if (expressions.isEmpty) return allPartitions().toArray
+    // Directory values follow the table's partition schema, not the read one, 
and bind as V1
+    // binds them: with that schema's types, nullable.
+    val partitionSchema = fileIndex.partitionSchema
+    val fieldIndex = partitionSchema.fieldNames.zipWithIndex
+      .map { case (name, i) => normalizePartitionColumnName(name) -> i }.toMap
+    val predicates = expressions.toSeq.map { e =>
+      e -> Predicate.createInterpreted(e.transform {
+        case a: Attribute =>
+          // PushDownUtils passes only filters over `filterAttributes()`.
+          val i = fieldIndex.getOrElse(normalizePartitionColumnName(a.name),
+            throw SparkException.internalError("A file scan can only apply a 
runtime filter " +
+              s"over its partition columns 
${partitionSchema.fieldNames.mkString("[", ", ", "]")}" +
+              s", got $e"))
+          BoundReference(i, partitionSchema(i).dataType, nullable = true)
+      })
+    }
+    var failedDirs = 0
+    var firstFailure: Option[(Expression, Throwable)] = None
+    val all = listing()
+    val kept = all.filter { dir =>
+      var failure: Option[(Expression, Throwable)] = None
+      val keep = predicates.forall { case (e, predicate) =>
+        try predicate.eval(dir.values) catch {
+          case NonFatal(t) =>
+            if (failure.isEmpty) failure = Some(e -> t)
+            true
+        }
+      }
+      // A directory another filter prunes does not count.
+      if (keep && failure.isDefined) {
+        failedDirs += 1
+        if (firstFailure.isEmpty) firstFailure = failure
+      }
+      keep
+    }
+    firstFailure.foreach { case (e, t) =>
+      logWarning(log"Kept partition directories on which a runtime filter 
could not be " +
+        log"evaluated: ${MDC(LogKeys.COUNT, failedDirs)}. First failure: " +
+        log"${MDC(LogKeys.EXPR, e)}.", t)
+    }
+    if (kept.length == all.length) allPartitions().toArray else 
filePartitions(kept).toArray

Review Comment:
   **41. [Nit] Following up on item 30 
([thread](https://github.com/apache/spark/pull/55579#discussion_r4503842458)): 
a direct call on a scan without runtime-filterable columns can return the 
packing of another listing**
   
   Since c1ba7eacfd7, a scan whose `filterAttributes()` is empty packs a 
listing it does not keep (L50), so a call here with a non-empty array lists 
again at L100, as "only a scan with runtime-filterable columns keeps its file 
listing" checks. When that second listing prunes nothing, this line still 
returns `allPartitions()`, packed from the first one. If the file index was 
refreshed in between (`InMemoryFileIndex.refresh()`, as in item 30), the call 
compares against one listing and returns another, so it can return files the 
second listing no longer has, or miss new ones. That breaks the one snapshot of 
L43-44. Spark's screening never passes such a scan a non-empty array, so only a 
direct call gets here. Could this line return `allPartitions()` only when 
`filterAttributes()` is not empty, and `filePartitions(kept)` otherwise?



##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2FileSourceDPPSuite.scala:
##########
@@ -0,0 +1,1018 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.connector
+
+import java.io.File
+
+import org.apache.logging.log4j.Level
+
+import org.apache.spark.SparkException
+import org.apache.spark.sql.{DataFrame, QueryTest, Row}
+import org.apache.spark.sql.catalyst.ProjectingInternalRow
+import org.apache.spark.sql.catalyst.expressions.{AttributeReference, Cast, 
DynamicPruningExpression, DynamicPruningSubquery, EqualTo, EvalMode, 
Expression, GreaterThanOrEqual, In, Literal, Not}
+import org.apache.spark.sql.catalyst.planning.{DeltaBasedRowLevelOperation, 
GroupBasedRowLevelOperation}
+import org.apache.spark.sql.catalyst.plans.logical.{Filter, LogicalPlan, 
ReplaceData, WriteDelta}
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.util.{ReplaceDataProjections, 
WriteDeltaProjections}
+import org.apache.spark.sql.connector.catalog.{Column, InMemoryTableCatalog, 
SupportsRowLevelOperations, Table, TableCapability}
+import org.apache.spark.sql.connector.expressions.{FieldReference, 
NamedReference}
+import org.apache.spark.sql.connector.read.{InputPartition, ScanBuilder}
+import org.apache.spark.sql.connector.write.{DeltaWriteBuilder, 
LogicalWriteInfo, RowLevelOperation, RowLevelOperationBuilder, 
RowLevelOperationInfo, RowLevelOperationTable, SupportsDelta, WriteBuilder}
+import org.apache.spark.sql.execution.{FilterExec, ScalarSubquery}
+import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanHelper, 
DisableAdaptiveExecutionSuite, EnableAdaptiveExecutionSuite}
+import org.apache.spark.sql.execution.datasources.{FilePartition, 
InMemoryFileIndex, PartitionDirectory}
+import org.apache.spark.sql.execution.datasources.v2.{BatchScanExec, 
DataSourceV2ScanRelation, FileScan, PushDownUtils, 
RuntimePartitionPruningFileScan}
+import org.apache.spark.sql.execution.datasources.v2.parquet.ParquetScan
+import org.apache.spark.sql.execution.datasources.v2.text.TextScan
+import org.apache.spark.sql.execution.dynamicpruning.{PartitionPruning, 
RowLevelOperationRuntimeGroupFiltering}
+import org.apache.spark.sql.internal.SQLConf
+import org.apache.spark.sql.test.SharedSparkSession
+import org.apache.spark.sql.types.{IntegerType, LongType, StringType}
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+/**
+ * Tests for SPARK-30628: subquery partition pruning and DPP for V2 file 
sources.
+ *
+ * These tests exercise V2 file scans (e.g. ParquetScan) -- distinct from the 
in-memory V2
+ * catalog used by [[org.apache.spark.sql.DynamicPartitionPruningV2Suite]] and 
from the V2
+ * iterative-pushdown work covered by 
[[DataSourceV2EnhancedRuntimePartitionFilterSuite]].
+ */
+abstract class DataSourceV2FileSourceDPPSuiteBase extends QueryTest
+  with SharedSparkSession
+  with AdaptiveSparkPlanHelper {
+
+  // The tests register their temp views over temp directories deleted when 
the test ends.
+  override def afterEach(): Unit = {
+    try {
+      spark.sessionState.catalog.clearTempTables()
+    } finally {
+      super.afterEach()
+    }
+  }
+
+  // Standard V2-only conf for DPP tests on file sources. AQE is set by the 
concrete subclasses,
+  // which mix in DisableAdaptiveExecutionSuite / 
EnableAdaptiveExecutionSuite: the DPP subquery is
+  // planned by PlanDynamicPruningFilters without AQE and by 
PlanAdaptiveDynamicPruningFilters with
+  // it, and those are two separate rules.
+  private def withDppV2Conf[T](thunk: => T): T = {
+    withSQLConf(
+      SQLConf.USE_V1_SOURCE_LIST.key -> "",
+      SQLConf.DYNAMIC_PARTITION_PRUNING_ENABLED.key -> "true",
+      SQLConf.DYNAMIC_PARTITION_PRUNING_REUSE_BROADCAST_ONLY.key -> "false",
+      SQLConf.DYNAMIC_PARTITION_PRUNING_FALLBACK_FILTER_RATIO.key -> 
"2")(thunk)
+  }
+
+  // Writes a partitioned `fact` and an unpartitioned `dim` table in `format` 
and registers them
+  // as temp views. `fact` has 100 rows across 10 partitions (10 rows each); 
`dim` has 10 rows
+  // with `dim_id` and `dim_val` both in [0, 10). The read schema is given 
explicitly because csv
+  // is written without a header; it also keeps the join key `cast(part as 
bigint)` in every format.
+  private def writeFactAndDim(dir: File, format: String = "parquet"): Unit = {
+    val factPath = new File(dir, "fact").getCanonicalPath
+    val dimPath = new File(dir, "dim").getCanonicalPath
+    spark.range(100)
+      .selectExpr("id", "id % 10 AS part")
+      .write.format(format).partitionBy("part").save(factPath)
+    spark.read.format(format).schema("id long, part int").load(factPath)
+      .createOrReplaceTempView("fact")
+    spark.range(10)
+      .selectExpr("id AS dim_id", "id AS dim_val")
+      .write.format(format).save(dimPath)
+    spark.read.format(format).schema("dim_id long, dim_val long").load(dimPath)
+      .createOrReplaceTempView("dim")
+  }
+
+  // The BatchScanExec reading `fact`, the only partitioned table of the two. 
The lookup descends
+  // into AQE query stages, so it finds the scan whichever stage it ended up 
in.
+  private def factScanOf(df: DataFrame): BatchScanExec = {
+    val executedPlan = df.queryExecution.executedPlan
+    collectFirst(executedPlan) {
+      case b: BatchScanExec if b.scan.isInstanceOf[FileScan] &&
+        b.scan.asInstanceOf[FileScan].readPartitionSchema.nonEmpty => b
+    }.getOrElse(fail("no fact BatchScanExec found in:\n" + 
executedPlan.treeString))
+  }
+
+  private def fileScanOf(df: DataFrame): RuntimePartitionPruningFileScan =
+    factScanOf(df).scan.asInstanceOf[RuntimePartitionPruningFileScan]
+
+  // A copy of `scan` whose file index runs `onList` every time it lists files.
+  private def withListingHook(scan: ParquetScan)(onList: => Unit): ParquetScan 
= {
+    val index = scan.fileIndex
+    scan.copy(fileIndex = new InMemoryFileIndex(spark, index.rootPaths, 
Map.empty, None,
+        userSpecifiedPartitionSpec = Some(index.partitionSpec())) {
+      override def listFiles(
+          partitionFilters: Seq[Expression],
+          dataFilters: Seq[Expression]): Seq[PartitionDirectory] = {
+        onList
+        super.listFiles(partitionFilters, dataFilters)
+      }
+    })
+  }
+
+  // The splits of each partition, as (path, start, length).
+  private def layoutOf(parts: Array[InputPartition]): Seq[Seq[(String, Long, 
Long)]] =
+    parts.toSeq.map(_.asInstanceOf[FilePartition].files.toSeq.map { f =>
+      (f.filePath.toString, f.start, f.length)
+    })
+
+  private def collectDppFilters(plan: LogicalPlan): Seq[Expression] = {
+    plan.collect {
+      case f: Filter if 
f.condition.exists(_.isInstanceOf[DynamicPruningSubquery]) => f.condition
+    }
+  }
+
+  test("DPP inserts DynamicPruningSubquery for V2 parquet partitioned table") {
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val df = sql(
+          """SELECT f.id FROM fact f JOIN dim d
+            |ON f.part = d.dim_id WHERE d.dim_val = 7""".stripMargin)
+        val optimized = df.queryExecution.optimizedPlan
+        assert(collectDppFilters(optimized).nonEmpty,
+          "expected DynamicPruningSubquery in optimized plan over V2 parquet, 
got plan:\n" +
+            optimized.treeString)
+      }
+    }
+  }
+
+  test("DPP does not fire when join key is a data column (non-partition)") {
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        // Join on f.id (data column), not f.part (partition column). `f.part` 
stays in the
+        // projection so the fact scan still reads a partition column: without 
it the scan declares
+        // nothing filterable for a reason that has nothing to do with the 
join key.
+        val df = sql(
+          """SELECT f.id, f.part FROM fact f JOIN dim d
+            |ON f.id = d.dim_id WHERE d.dim_val = 7""".stripMargin)
+        val optimized = df.queryExecution.optimizedPlan
+        assert(collectDppFilters(optimized).isEmpty,
+          "DPP should not fire on non-partition join keys, got plan:\n" +
+            optimized.treeString)
+        assert(df.collect().length === 1)
+        // And the scan is one that could have been pruned, had the join key 
been its partition
+        // column: a filterAttributes() reporting every read column would make 
DPP fire above, and
+        // the fact scan would read fewer than all 100 rows.
+        assert(fileScanOf(df).filterAttributes().flatMap(_.fieldNames).toSeq 
=== Seq("part"))
+        val numOutputRows = factScanOf(df).metrics("numOutputRows").value
+        assert(numOutputRows === 100,
+          s"expected the fact scan to read every row, got $numOutputRows")
+      }
+    }
+  }
+
+  test("DPP fires when partitioned fact is on the right side of the join") {
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        // dim on left, fact on right; join on partition column
+        val df = sql(
+          """SELECT f.id FROM dim d JOIN fact f
+            |ON d.dim_id = f.part WHERE d.dim_val = 7""".stripMargin)
+        val optimized = df.queryExecution.optimizedPlan
+        assert(collectDppFilters(optimized).nonEmpty,
+          "expected DPP on right-side partitioned fact, got plan:\n" +
+            optimized.treeString)
+        checkAnswer(df, (0 until 10).map(i => Row(i * 10 + 7)))
+        val numOutputRows = factScanOf(df).metrics("numOutputRows").value
+        assert(numOutputRows === 10,
+          s"expected the fact scan to read only partition 7, got 
$numOutputRows rows")
+      }
+    }
+  }
+
+  test("DPP filter reaches BatchScanExec.runtimeFilters as 
DynamicPruningExpression") {
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val df = sql(
+          """SELECT f.id FROM fact f JOIN dim d
+            |ON f.part = d.dim_id WHERE d.dim_val = 7""".stripMargin)
+        df.collect()
+        val factScan = factScanOf(df)
+        // A DPP filter that degraded to true is also a 
DynamicPruningExpression, so match on the
+        // payload: only a subquery child means the filter can actually prune 
anything.
+        val dppFilters = factScan.runtimeFilters.collect {
+          case d @ DynamicPruningExpression(child) if 
!child.isInstanceOf[Literal] => d
+        }
+        assert(dppFilters.nonEmpty,
+          "expected the fact scan's runtimeFilters to carry a 
DynamicPruningExpression over a " +
+            s"subquery, got ${factScan.runtimeFilters}")
+      }
+    }
+  }
+
+  test("DPP that prunes every partition leaves the scan with no input 
partition") {
+    // `fact5` has partitions 0..4 while the dim row selects 7, so the build 
side is not empty and
+    // the surviving partition set is. That combination is what reaches the 
zero-partition
+    // DataSourceRDD: BatchScanExec's EmptyRDDWithPartitions branch keys off 
SinglePartition, which
+    // a file scan never reports.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val factPath = new File(dir, "fact5").getCanonicalPath
+        spark.range(50).selectExpr("id", "id % 5 AS part")
+          .write.format("parquet").partitionBy("part").save(factPath)
+        spark.read.format("parquet").schema("id long, part int").load(factPath)
+          .createOrReplaceTempView("fact5")
+        val df = sql(
+          """SELECT f.id FROM fact5 f JOIN dim d
+            |ON f.part = d.dim_id WHERE d.dim_val = 7""".stripMargin)
+        assert(df.collect().isEmpty)
+        val factScan = factScanOf(df)
+        assert(factScan.filteredPartitions.flatten.isEmpty,
+          s"expected no input partition, got 
${factScan.filteredPartitions.flatten.size}")
+        // What the assertion above does not see: BatchScanExec turning an 
empty partition set into
+        // an RDD with one empty partition still satisfies it.
+        assert(factScan.inputRDD.partitions.isEmpty,
+          s"expected an RDD with no partition, got 
${factScan.inputRDD.partitions.length}")
+        val numOutputRows = factScan.metrics("numOutputRows").value
+        assert(numOutputRows == 0,
+          s"expected the fact scan to read nothing, got $numOutputRows rows")
+      }
+    }
+  }
+
+  test("a DPP filter a union branch cannot apply is dropped at planning") {
+    // PartitionPruning resolves a union key through the first branch only, 
and the filter it
+    // inserts is then pushed positionally into every branch. So the 
unpartitioned branch's scan
+    // is offered a filter over a column it never declared filterable. 
Dropping it costs nothing,
+    // since that branch cannot prune on it and the join re-applies it; 
keeping it would plan its
+    // subquery for nothing and leave a dead filter in EXPLAIN.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val otherPath = new File(dir, "fact_other").getCanonicalPath
+        spark.range(100).selectExpr("id", "id % 10 AS other")
+          .write.format("parquet").save(otherPath)
+        spark.read.format("parquet").schema("id long, other 
long").load(otherPath)
+          .createOrReplaceTempView("fact_other")
+        val df = sql(
+          """SELECT u.id FROM (
+            |  SELECT id, part FROM fact UNION ALL SELECT id, other FROM 
fact_other
+            |) u JOIN dim d ON u.part = d.dim_id WHERE d.dim_val = 
7""".stripMargin)
+        val expected = (0 until 10).map(i => Row(i * 10 + 7))
+        checkAnswer(df, expected ++ expected)
+        // The filter did reach the branch that cannot use it in the optimized 
plan, which is what
+        // makes this a regression test rather than a query that happens to 
work.
+        val optimizedBranchFilters = df.queryExecution.optimizedPlan.collect {
+          case f: Filter if 
f.condition.exists(_.isInstanceOf[DynamicPruningSubquery]) &&
+            f.child.output.exists(_.name == "other") => f
+        }
+        assert(optimizedBranchFilters.size == 1,
+          "expected the DPP filter over the branch that cannot apply it, got 
plan:\n" +
+            df.queryExecution.optimizedPlan.treeString)
+        // The strategy dropped it at planning, so the scan of that branch has 
no runtime filter.
+        val otherScans = collect(df.queryExecution.executedPlan) {
+          case b: BatchScanExec if b.output.exists(_.name == "other") => b
+        }
+        assert(otherScans.size == 1, s"expected one scan of fact_other, got 
$otherScans")
+        assert(otherScans.head.runtimeFilters.isEmpty,
+          s"expected no runtime filter on fact_other, got 
${otherScans.head.runtimeFilters}")
+        // And the branch that can use it still pruned.
+        val numOutputRows = factScanOf(df).metrics("numOutputRows").value
+        assert(numOutputRows == 10,
+          s"expected the partitioned branch to read 10 rows, got 
$numOutputRows")
+      }
+    }
+  }
+
+  test("a runtime filter keeps a partition directory it cannot evaluate") {
+    // The join applies a DPP filter again, and the FilterExec above the scan 
a scalar subquery
+    // filter, so the scan keeps a directory whose value fails the filter's 
cast under ANSI.
+    withDppV2Conf {
+      withSQLConf(SQLConf.ANSI_ENABLED.key -> "true") {
+        withTempDir { dir =>
+          writeFactAndDim(dir)
+          def readDirs(df: DataFrame): Seq[String] = 
factScanOf(df).filteredPartitions.flatten
+            
.flatMap(_.asInstanceOf[FilePartition].files.map(_.partitionValues.getString(0)))
+            .distinct.sorted
+          val strPath = new File(dir, "fact_str").getCanonicalPath
+          spark.range(10)
+            .selectExpr("id", "CASE WHEN id = 3 THEN 'abc' ELSE CAST(id AS 
STRING) END AS part")
+            .write.format("parquet").partitionBy("part").save(strPath)
+          spark.read.format("parquet").schema("id long, part 
string").load(strPath)
+            .createOrReplaceTempView("fact_str")
+          // `f.id <> 3` drops the row of directory 'abc' before the join 
casts its key.
+          val dpp = sql(
+            """SELECT f.id FROM fact_str f JOIN dim d
+              |ON f.part = d.dim_id WHERE d.dim_val = 7 AND f.id <> 
3""".stripMargin)
+          val logAppender = new LogAppender("runtime filter evaluation 
failure")
+          withLogAppender(logAppender, Seq(classOf[ParquetScan].getName), 
Some(Level.WARN)) {
+            checkAnswer(dpp, Row(7))
+          }
+          assert(logAppender.loggingEvents.exists { e =>
+            e.getLevel == Level.WARN && 
e.getMessage.getFormattedMessage.contains(
+              "on which a runtime filter could not be evaluated: 1.")
+          }, s"expected a warning, got 
${logAppender.loggingEvents.map(_.getMessage)}")
+          
assert(factScanOf(dpp).runtimeFilters.exists(_.isInstanceOf[DynamicPruningExpression]),
+            s"expected a DPP filter, got ${factScanOf(dpp).runtimeFilters}")
+          assert(readDirs(dpp) === Seq("7", "abc"))
+          // Directory 'abc' holds a file with no row, so only the scan 
evaluates the filter on it.
+          val emptyPath = new File(dir, "fact_empty").getCanonicalPath
+          spark.range(10).selectExpr("id", "CAST(id AS STRING) AS part")
+            .write.format("parquet").partitionBy("part").save(emptyPath)
+          spark.range(0).write.parquet(new File(emptyPath, 
"part=abc").getCanonicalPath)
+          spark.read.format("parquet").schema("id long, part 
string").load(emptyPath)
+            .createOrReplaceTempView("fact_empty")
+          val scalar = sql(
+            """SELECT f.id FROM fact_empty f
+              |WHERE f.part = (SELECT max(dim_id) FROM dim WHERE dim_val = 
7)""".stripMargin)
+          checkAnswer(scalar, Row(7))
+          val scalarFilters = factScanOf(scalar).runtimeFilters
+          
assert(scalarFilters.exists(_.exists(_.isInstanceOf[ScalarSubquery])),
+            s"expected the scalar subquery filter on the scan, got 
$scalarFilters")
+          assert(readDirs(scalar) === Seq("7", "abc"))
+        }
+      }
+    }
+  }
+
+  test("several runtime filters on one file scan prune it together") {
+    // A DPP filter on p2 and a scalar subquery filter on p3 reach the same 
scan, which keeps only
+    // the directories that satisfy both.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val tPath = new File(dir, "t").getCanonicalPath
+        spark.range(100).selectExpr("id", "id % 5 AS p2", "id % 3 AS p3")
+          .write.format("parquet").partitionBy("p2", "p3").save(tPath)
+        spark.read.format("parquet").schema("id long, p2 int, p3 
int").load(tPath)
+          .createOrReplaceTempView("t")
+        val df = sql(
+          """SELECT t.id FROM t JOIN dim d ON t.p2 = d.dim_id
+            |WHERE d.dim_val = 2 AND t.p3 = (SELECT max(dim_id) FROM dim WHERE 
dim_val = 1)
+            |""".stripMargin)
+        checkAnswer(df, (0 until 100).filter(i => i % 5 == 2 && i % 3 == 
1).map(i => Row(i)))
+        val scan = factScanOf(df)
+        
assert(scan.runtimeFilters.exists(_.isInstanceOf[DynamicPruningExpression]) &&
+          scan.runtimeFilters.exists(_.exists(_.isInstanceOf[ScalarSubquery])),
+          s"expected a DPP and a scalar subquery filter, got 
${scan.runtimeFilters}")
+        val dirs = 
scan.filteredPartitions.flatten.flatMap(_.asInstanceOf[FilePartition].files
+          .map(f => (f.partitionValues.getInt(0), 
f.partitionValues.getInt(1)))).distinct
+        assert(dirs === Seq((2, 1)))
+      }
+      // A directory that one filter fails on and another filter prunes is 
dropped and not counted
+      // as a failure.
+      withTempDir { dir =>
+        val strPath = new File(dir, "fact_str").getCanonicalPath
+        spark.range(10).selectExpr("id",
+          "CASE id WHEN 3 THEN 'abc' WHEN 4 THEN 'xyz' ELSE CAST(id AS STRING) 
END AS part")
+          .write.format("parquet").partitionBy("part").save(strPath)
+        spark.read.format("parquet").schema("id long, part 
string").load(strPath)
+          .createOrReplaceTempView("fact_str")
+        val df = sql("SELECT f.id, f.part FROM fact_str f")
+        df.collect()
+        val scan = fileScanOf(df)
+        val part = AttributeReference("part", StringType)()
+        val failsOnLetters = EqualTo(Cast(part, LongType, None, 
EvalMode.ANSI), Literal(7L))
+        val prunesXyz = Not(EqualTo(part, Literal("xyz")))
+        var planned = Array.empty[InputPartition]
+        val logAppender = new LogAppender("runtime filter evaluation failure")
+        withLogAppender(logAppender, Seq(classOf[ParquetScan].getName), 
Some(Level.WARN)) {
+          // failsOnLetters goes first, so it fails on 'xyz' before prunesXyz 
prunes it; in the
+          // other order 'xyz' would be pruned without a failure.
+          planned = 
scan.planInputPartitionsWithRuntimeFilters(Array(failsOnLetters, prunesXyz))
+        }
+        assert(planned.toSeq.flatMap(_.asInstanceOf[FilePartition].files
+          .map(_.partitionValues.getString(0))).distinct.sorted === Seq("7", 
"abc"))
+        
assert(logAppender.loggingEvents.exists(_.getMessage.getFormattedMessage.contains(
+          "on which a runtime filter could not be evaluated: 1.")),
+          s"expected a warning, got 
${logAppender.loggingEvents.map(_.getMessage)}")
+      }
+    }
+  }
+
+  test("a runtime filter that is true for a null partition value keeps that 
directory") {
+    // A directory's null partition value binds as null, not as its type's 
default.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val nullPath = new File(dir, "fact_null").getCanonicalPath
+        spark.range(10)
+          .selectExpr("id", "CASE WHEN id = 3 THEN NULL ELSE CAST(id AS INT) 
END AS part")
+          .write.format("parquet").partitionBy("part").save(nullPath)
+        spark.read.format("parquet").schema("id long, part int").load(nullPath)
+          .createOrReplaceTempView("fact_null")
+        // The subquery returns null, so only the null directory satisfies the 
filter.
+        val df = sql(
+          """SELECT f.id FROM fact_null f
+            |WHERE f.part <=> (SELECT max(dim_id) FROM dim WHERE dim_val > 
100)""".stripMargin)
+        checkAnswer(df, Row(3))
+        val scan = factScanOf(df)
+        
assert(scan.runtimeFilters.exists(_.exists(_.isInstanceOf[ScalarSubquery])),
+          s"expected the scalar subquery filter on the scan, got 
${scan.runtimeFilters}")
+        val values = 
scan.filteredPartitions.flatten.flatMap(_.asInstanceOf[FilePartition].files
+          .map(f => 
Option.when(!f.partitionValues.isNullAt(0))(f.partitionValues.getInt(0))))
+        assert(values.distinct === Seq(None))
+      }
+    }
+  }
+
+  test("a runtime filter that prunes nothing returns the partitions 
planInputPartitions " +
+    "planned") {
+    // Spark passes an empty array when every runtime filter of a scan node is 
screened out, e.g. a
+    // DPP filter degraded to true, and a filter can keep every directory. 
Each packing builds new
+    // partitions, so getting the planned ones back means the files were not 
packed again.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val df = sql("SELECT f.id, f.part FROM fact f")
+        df.collect()
+        val scan = fileScanOf(df)
+        val planned = scan.planInputPartitions()
+        val keepAll = GreaterThanOrEqual(AttributeReference("part", 
IntegerType)(), Literal(0))
+        Seq(Array.empty[Expression], Array[Expression](keepAll)).foreach { 
filters =>
+          assert(planned.nonEmpty && 
scan.planInputPartitionsWithRuntimeFilters(filters).toSeq
+            .corresponds(planned.toSeq)(_ eq _),
+            s"expected the planned partitions for [${filters.mkString(", ")}]")
+        }
+      }
+    }
+  }
+
+  test("the plain and the runtime-filtered read paths share one listing") {
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val df = sql("SELECT f.id, f.part FROM fact f")
+        df.collect()
+        var listings = 0
+        val scan = withListingHook(fileScanOf(df).asInstanceOf[ParquetScan]) { 
listings += 1 }
+        def partValues(parts: Array[InputPartition]): Seq[Int] =
+          
parts.flatMap(_.asInstanceOf[FilePartition].files.map(_.partitionValues.getInt(0)))
+            .distinct.sorted.toSeq
+        val partAttr = AttributeReference("part", IntegerType)()
+        assert(partValues(scan.planInputPartitions()) === (0 until 10))
+        assert(partValues(scan.planInputPartitionsWithRuntimeFilters(
+          Array(EqualTo(partAttr, Literal(3))))) === Seq(3))
+        assert(partValues(scan.planInputPartitions()) === (0 until 10))
+        assert(listings === 1, s"expected one listing, got $listings")
+      }
+    }
+  }
+
+  test("only a scan with runtime-filterable columns keeps its file listing") {
+    // Nothing reads the listing of another scan again, so planning packs it 
without keeping it.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val df = sql("SELECT f.id FROM fact f")
+        df.collect()
+        val base = collectFirst(df.queryExecution.executedPlan) {
+          case b: BatchScanExec => b.scan.asInstanceOf[ParquetScan]
+        }.getOrElse(fail(s"no scan in:\n${df.queryExecution.executedPlan}"))
+        assert(base.filterAttributes().isEmpty)
+        var listings = 0
+        val scan = withListingHook(base) { listings += 1 }
+        scan.planInputPartitions()
+        scan.planInputPartitionsWithRuntimeFilters(
+          Array(EqualTo(AttributeReference("part", IntegerType)(), 
Literal(3))))
+        assert(listings === 2, s"expected the runtime-filtered call to list 
again, got $listings")
+      }
+    }
+  }
+
+  test("another thread can read the scan while it lists its files") {
+    // The thread forces a lazy val of the scan through `equals`. Listing 
under the scan's
+    // monitor would block it.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val df = sql("SELECT f.id, f.part FROM fact f")
+        df.collect()
+        val scan = fileScanOf(df).asInstanceOf[ParquetScan]
+        val partAttr = AttributeReference("part", IntegerType)()
+        Seq[ParquetScan => Array[InputPartition]](
+          _.planInputPartitions(),
+          _.planInputPartitionsWithRuntimeFilters(Array(EqualTo(partAttr, 
Literal(3))))
+        ).foreach { firstCall =>
+          var probe: ParquetScan = null
+          probe = withListingHook(scan) {
+            val reader = new Thread(() => probe.equals(probe))
+            reader.start()
+            reader.join(30000)
+            assert(!reader.isAlive, "the other thread is still blocked on the 
scan")
+          }
+          firstCall(probe)
+        }
+      }
+    }
+  }
+
+  test("scalar subquery on partition column of v2 parquet prunes partitions") {
+    // Scalar subquery (SELECT max(dim_id) FROM dim WHERE dim_val = 7) 
evaluates to 7.
+    // WHERE f.part = 7 should select exactly partition 7 -> 10 rows. Without 
partition
+    // pruning, the scan reads all 100 rows and the post-filter keeps 10.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val df = sql(
+          """SELECT f.id FROM fact f
+            |WHERE f.part = (SELECT max(dim_id) FROM dim WHERE dim_val = 
7)""".stripMargin)
+        val rows = df.collect()
+        assert(rows.length == 10,
+          s"expected 10 rows after scalar-subquery filter, got ${rows.length}")
+        val numOutputRows = factScanOf(df).metrics("numOutputRows").value
+        assert(numOutputRows == 10,
+          s"expected fact scan to read 10 rows after scalar-subquery pruning, 
got " +
+            s"$numOutputRows. plan:\n" + 
df.queryExecution.executedPlan.treeString)
+      }
+    }
+  }
+
+  test("filterAttributes reports nothing once a pushed aggregate drops the 
partition columns") {
+    // With an aggregate pushed down, ParquetScan.readSchema() returns only 
the aggregate schema, so
+    // the partition columns are not in the scan relation output. Spark 
resolves every declared
+    // runtime-filter attribute against that output and fails the query if one 
is missing, so
+    // filterAttributes() must not report them.
+    withDppV2Conf {
+      withSQLConf(SQLConf.PARQUET_AGGREGATE_PUSHDOWN_ENABLED.key -> "true") {
+        withTempDir { dir =>
+          writeFactAndDim(dir)
+          val df = sql("SELECT count(part) FROM fact")
+          checkAnswer(df, Row(100))
+          val declared = 
fileScanOf(df).filterAttributes().flatMap(_.fieldNames).toSeq
+          assert(declared.isEmpty,
+            s"expected no runtime-filterable attribute under a pushed 
aggregate, got $declared")
+        }
+      }
+    }
+  }
+
+  test("filterAttributes keeps a partition column a pushed aggregate groups 
by") {
+    // With a GROUP BY, aggregate pushdown only happens when it groups by 
exactly the partition
+    // columns, and those columns go into the pushed-down schema, so they stay 
in the scan relation
+    // output and stay runtime-filterable. This is the other side of the 
intersection from the
+    // test above.
+    withDppV2Conf {
+      withSQLConf(SQLConf.PARQUET_AGGREGATE_PUSHDOWN_ENABLED.key -> "true") {
+        withTempDir { dir =>
+          writeFactAndDim(dir)
+          val df = sql("SELECT part, count(*) FROM fact GROUP BY part")
+          checkAnswer(df, (0 until 10).map(p => Row(p, 10)))
+          val scan = fileScanOf(df)
+          // Without the pushdown the assertion below says nothing: `part` is 
in the read partition
+          // schema either way. Pin that the aggregate really went into the 
scan.
+          assert(scan.asInstanceOf[ParquetScan].pushedAggregate.isDefined,
+            s"expected the aggregate to be pushed into the scan, got 
${scan.description()}")
+          val declared = scan.filterAttributes().flatMap(_.fieldNames).toSeq
+          assert(declared == Seq("part"),
+            s"expected the group-by partition column to stay declared, got 
$declared")
+        }
+      }
+    }
+  }
+
+  test("a scalar subquery filter on a partition column is evaluated again 
after the scan") {
+    // The scan does not declare its partition columns fully pushed, so a 
plugin node that reads
+    // the scan's planned partitions instead of pruning them still returns the 
right rows, and one
+    // that goes through PushDownUtils.pushRuntimeFilters gets false back 
rather than an error.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val df = sql(
+          """SELECT f.id FROM fact f
+            |WHERE f.part = (SELECT max(dim_id) FROM dim WHERE dim_val = 
7)""".stripMargin)
+        checkAnswer(df, (0 until 10).map(i => Row(i * 10 + 7)))
+        assert(fileScanOf(df).fullyPushedFilterAttributes().isEmpty)
+        val executedPlan = df.queryExecution.executedPlan
+        assert(collect(executedPlan) {
+          case f: FilterExec if 
f.condition.exists(_.isInstanceOf[ScalarSubquery]) => f
+        }.nonEmpty, s"expected a FilterExec applying the scalar subquery, 
got:\n" +
+          executedPlan.treeString)
+        val factScan = factScanOf(df)
+        
assert(factScan.runtimeFilters.exists(_.exists(_.isInstanceOf[ScalarSubquery])),
+          s"expected the scalar subquery filter on the scan, got 
${factScan.runtimeFilters}")
+        assert(!PushDownUtils.pushRuntimeFilters(
+          factScan.scan, factScan.runtimeFilters, factScan.table, 
factScan.output))
+      }
+    }
+  }
+
+  test("a runtime filter is ANDed with the compile-time partition filters, not 
substituted") {
+    // The compile-time filter goes through `fileIndex.listFiles`, and the 
runtime filter prunes
+    // that listing, so the scan reads only what both keep.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        // min(dim_id) over dim_val > 6 is 7, so this keeps partitions 7 and 
8: 7 from the runtime
+        // filter's lower bound, 8 from the compile-time upper bound.
+        val df = sql(
+          """SELECT f.id FROM fact f
+            |WHERE f.part <= 8
+            |  AND f.part >= (SELECT min(dim_id) FROM dim WHERE dim_val > 
6)""".stripMargin)
+        checkAnswer(df, (0 until 10).flatMap(i => Seq(Row(i * 10 + 7), Row(i * 
10 + 8))))
+        val numOutputRows = factScanOf(df).metrics("numOutputRows").value
+        assert(numOutputRows == 20,
+          s"expected the fact scan to read only partitions 7 and 8, got 
$numOutputRows rows")
+      }
+    }
+  }
+
+  test("a runtime filter the scan cannot apply is rejected rather than 
silently dropped") {
+    // Unreachable through SQL: PushDownUtils screens a filter against the 
attributes the scan
+    // declared before handing it over, so the scan is called directly. A 
mixed-case reference is
+    // matched the way the scan matches partition column names, so it binds.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val df = sql("SELECT f.id, f.part FROM fact f")
+        df.collect()
+        val scan = fileScanOf(df)
+        val e = intercept[SparkException] {
+          scan.planInputPartitionsWithRuntimeFilters(
+            Array(EqualTo(AttributeReference("id", LongType)(), Literal(1L))))
+        }
+        assert(e.getCondition === "INTERNAL_ERROR")
+        assert(e.getMessage.contains("can only apply a runtime filter over its 
partition"),
+          s"unexpected message: ${e.getMessage}")
+
+        val mixedCase = scan.planInputPartitionsWithRuntimeFilters(
+          Array(EqualTo(AttributeReference("PART", IntegerType)(), 
Literal(7))))
+        assert(mixedCase.flatMap(
+          
_.asInstanceOf[FilePartition].files.map(_.partitionValues.getInt(0))).distinct.toSeq
+          === Seq(7))
+      }
+    }
+  }
+
+  test("runtime pruning splits the surviving files for their own size") {
+    // As in V1, the split size follows the files that survive, so a 
runtime-pruned scan is split
+    // like the same partitions selected at compile time.
+    withDppV2Conf {
+      withSQLConf(
+        SQLConf.FILES_MAX_PARTITION_BYTES.key -> "1g",
+        SQLConf.FILES_MIN_PARTITION_NUM.key -> "20",
+        SQLConf.FILES_OPEN_COST_IN_BYTES.key -> "0") {
+        withTempDir { dir =>
+          writeFactAndDim(dir)
+          def layout(df: DataFrame): Seq[Seq[(String, Long, Long)]] =
+            layoutOf(factScanOf(df).filteredPartitions.flatten.toArray)
+          val expected = (0 until 10).flatMap(i => Seq(Row(i * 10 + 8, 8), 
Row(i * 10 + 9, 9)))
+          val compileTime = sql("SELECT f.id, f.part FROM fact f WHERE f.part 
>= 8")
+          checkAnswer(compileTime, expected)
+          val unpruned = sql("SELECT f.id, f.part FROM fact f")
+          unpruned.collect()
+          // Sized for the whole table, the files of partitions 8 and 9 are 
cut into fewer splits.
+          val unprunedSplits = factScanOf(unpruned).filteredPartitions.flatten
+            .flatMap(_.asInstanceOf[FilePartition].files)
+            .count(_.partitionValues.getInt(0) >= 8)
+          assert(layout(compileTime).flatten.size > unprunedSplits,
+            s"expected more than $unprunedSplits splits at compile time, got " 
+
+              s"${layout(compileTime).flatten.size}")
+          Seq(
+            // min(dim_id) over dim_val > 7 is 8.
+            """SELECT f.id, f.part FROM fact f
+              |WHERE f.part >= (SELECT min(dim_id) FROM dim WHERE dim_val > 
7)""".stripMargin,
+            """SELECT f.id, f.part FROM fact f JOIN dim d
+              |ON f.part = d.dim_id WHERE d.dim_val > 
7""".stripMargin).foreach { query =>
+            val runtime = sql(query)
+            checkAnswer(runtime, expected)
+            val sameLayout = layout(runtime) == layout(compileTime)
+            assert(sameLayout, s"expected the 
${layout(compileTime).flatten.size} splits of " +
+              s"the compile-time scan, got ${layout(runtime).flatten.size}")
+          }
+        }
+      }
+    }
+  }
+
+  test("a DPP filter degraded to true leaves the v2 file scan unpruned") {
+    // With reuse-only DPP and no broadcast to reuse, the subquery is replaced 
by true before
+    // execution.
+    withDppV2Conf {
+      withSQLConf(
+        SQLConf.DYNAMIC_PARTITION_PRUNING_REUSE_BROADCAST_ONLY.key -> "true",
+        SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") {
+        withTempDir { dir =>
+          writeFactAndDim(dir)
+          val df = sql(
+            """SELECT f.id FROM fact f JOIN dim d
+              |ON f.part = d.dim_id WHERE d.dim_val = 7""".stripMargin)
+          val rows = df.collect()
+          assert(rows.length == 10, s"expected 10 rows after join+filter, got 
${rows.length}")
+          val factScan = factScanOf(df)
+          assert(factScan.runtimeFilters.exists {
+            case DynamicPruningExpression(l: Literal) => l == 
Literal.TrueLiteral
+            case _ => false
+          }, s"expected a degraded DPP filter, got ${factScan.runtimeFilters}")
+          val numOutputRows = factScan.metrics("numOutputRows").value
+          assert(numOutputRows == 100,
+            s"expected the fact scan to read all 100 rows, got $numOutputRows")
+        }
+      }
+    }
+  }
+
+  test("a runtime filter binds to the read partition columns when only some of 
them are read") {
+    // The filter is evaluated on directory values, which follow the table's 
partition schema
+    // (p1, p2, p3), while the scan reads only p2 and p3.
+    withDppV2Conf {
+      withTempDir { dir =>
+        val tPath = new File(dir, "t").getCanonicalPath
+        spark.range(100).selectExpr("id", "id % 2 AS p1", "id % 5 AS p2", "id 
% 3 AS p3")
+          .write.format("parquet").partitionBy("p1", "p2", "p3").save(tPath)
+        spark.read.format("parquet").schema("id long, p1 int, p2 int, p3 
int").load(tPath)
+          .createOrReplaceTempView("t")
+        writeFactAndDim(dir)
+        val expected = (0 until 100).filter(_ % 5 == 2).map(i => Row(i.toLong, 
i % 3))
+        Seq(
+          """SELECT id, p3 FROM t
+            |WHERE p2 = (SELECT max(dim_id) FROM dim WHERE dim_val = 
2)""".stripMargin,
+          """SELECT t.id, t.p3 FROM t JOIN dim d
+            |ON t.p2 = d.dim_id WHERE d.dim_val = 2""".stripMargin).foreach { 
query =>
+          val df = sql(query)
+          checkAnswer(df, expected)
+          val scan = factScanOf(df)
+          
assert(scan.scan.asInstanceOf[FileScan].readPartitionSchema.fieldNames.toSeq ===
+            Seq("p2", "p3"), "the scan must read a strict subset of the 
partition columns")
+          val numOutputRows = scan.metrics("numOutputRows").value
+          assert(numOutputRows == expected.size,
+            s"expected the scan to read ${expected.size} rows, got 
$numOutputRows")
+        }
+      }
+    }
+  }
+
+  test("no DPP over a partition column a static equality already fixes, as in 
V1") {
+    // `FileScanBuilder` pushes the static partition filter fully into the 
scan, so the cleanup
+    // reads it from the scan relation's pushed filters, as it reads it from 
the `Filter` over a V1
+    // relation. V1 is the reference.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        // An int join key, so the pruning key is `part` itself rather than a 
cast of it.
+        val dimIntPath = new File(dir, "dim_int").getCanonicalPath
+        spark.range(10).selectExpr("CAST(id AS INT) AS dim_id", "CAST(id AS 
INT) AS dim_val")
+          .write.format("parquet").save(dimIntPath)
+        // (condition, the partitions it keeps, whether DPP stays, whether V1 
is checked). The first
+        // six are the SPARK-38148 cases. The last two do not fix the key, so 
V2 keeps the DPP; they
+        // are checked under V2 only, because V1 reads an equality anywhere in 
its filter and drops
+        // the DPP.
+        Seq[(String, Int => Boolean, Boolean, Boolean)](
+          ("f.part = 3", _ == 3, false, true),
+          ("3 = f.part", _ == 3, false, true),
+          ("f.part <=> 3", _ == 3, false, true),
+          ("3 <=> f.part", _ == 3, false, true),
+          ("f.part > 3", _ > 3, true, true),
+          ("5 > f.part", _ < 5, true, true),
+          ("f.part <> 3", _ != 3, true, false),
+          ("(f.part = 3 OR f.part = 4)", p => p == 3 || p == 4, true, false)
+        ).foreach { case (condition, keeps, hasDpp, checkV1) =>
+          val query =
+            s"""SELECT /*+ broadcast(d) */ f.id FROM fact f JOIN dim_int d
+               |ON f.part = d.dim_id WHERE $condition AND d.dim_val > 
2""".stripMargin
+          val expected = (0 until 100).filter { i =>
+            val part = i % 10
+            part > 2 && keeps(part)
+          }.map(i => Row(i.toLong))
+          (if (checkV1) Seq("parquet", "") else Seq("")).foreach { v1Sources =>
+            withSQLConf(SQLConf.USE_V1_SOURCE_LIST.key -> v1Sources) {
+              // A temp view keeps the relation it was created with, so create 
both under this conf.
+              spark.read.format("parquet").schema("id long, part int")
+                .load(new File(dir, 
"fact").getCanonicalPath).createOrReplaceTempView("fact")
+              
spark.read.format("parquet").load(dimIntPath).createOrReplaceTempView("dim_int")
+              val df = sql(query)
+              val optimized = df.queryExecution.optimizedPlan
+              
assert(optimized.exists(_.isInstanceOf[DataSourceV2ScanRelation]) ===
+                v1Sources.isEmpty, s"wrong source version for 
'$v1Sources':\n$optimized")
+              assert(collectDppFilters(optimized).nonEmpty === hasDpp,
+                s"`$condition` with v1 sources '$v1Sources': expected DPP 
$hasDpp, got\n$optimized")
+              checkAnswer(df, expected)
+            }
+          }
+        }
+      }
+    }
+  }
+
+  test("DPP for a dimension filtered only on its partition column, as in V1") {
+    // `FileScanBuilder` takes the dimension's partition filter in without 
leaving a `Filter` above
+    // the scan, so PartitionPruning finds the selective predicate in the scan 
relation's pushed
+    // filters. V1 keeps the `Filter`. V1 is the reference.
+    withDppV2Conf {
+      withTempDir { dir =>
+        writeFactAndDim(dir)
+        val dimPartPath = new File(dir, "dim_part").getCanonicalPath
+        spark.range(10).selectExpr("id AS k", "CAST(id % 2 AS INT) AS dp")
+          .write.format("parquet").partitionBy("dp").save(dimPartPath)
+        Seq("parquet", "").foreach { v1Sources =>
+          withSQLConf(SQLConf.USE_V1_SOURCE_LIST.key -> v1Sources) {
+            // A temp view keeps the relation it was created with, so create 
both under this conf.
+            spark.read.format("parquet").schema("id long, part int")
+              .load(new File(dir, 
"fact").getCanonicalPath).createOrReplaceTempView("fact")
+            spark.read.format("parquet").schema("k long, dp 
int").load(dimPartPath)
+              .createOrReplaceTempView("dim_part")
+            val df = sql(
+              """SELECT f.id FROM fact f JOIN dim_part d
+                |ON f.part = d.k WHERE d.dp = 1""".stripMargin)
+            val optimized = df.queryExecution.optimizedPlan
+            assert(optimized.exists(_.isInstanceOf[DataSourceV2ScanRelation]) 
===
+              v1Sources.isEmpty, s"wrong source version for 
'$v1Sources':\n$optimized")
+            assert(collectDppFilters(optimized).nonEmpty,
+              s"expected DPP with v1 sources '$v1Sources', got\n$optimized")
+            checkAnswer(df, (0 until 100).filter(_ % 2 == 1).map(i => 
Row(i.toLong)))
+            if (v1Sources.isEmpty) {
+              val factScan = collectFirst(df.queryExecution.executedPlan) {
+                case b: BatchScanExec if b.output.exists(_.name == "id") => b
+              }.getOrElse(fail(s"no fact scan 
in:\n${df.queryExecution.executedPlan}"))
+              val numOutputRows = factScan.metrics("numOutputRows").value
+              assert(numOutputRows == 50,
+                s"expected the fact scan to read the 5 odd partitions, got 
$numOutputRows rows")
+            }
+          }
+        }
+      }
+    }
+  }
+
+  test("DPP for a dimension whose filter a connector scan took in fully") {
+    // The in-memory scan takes `dp IN (1, 3)` on its single identity 
partition column in fully,
+    // so no `Filter` above it keeps a selective predicate.
+    withDppV2Conf {
+      withSQLConf("spark.sql.catalog.dpp_mem" -> 
classOf[InMemoryTableCatalog].getName) {

Review Comment:
   **42. [Nit] The `dpp_mem` catalog stays loaded after its test**
   
   `withSQLConf` restores the conf, but `CatalogManager.catalog` caches the 
catalog it loads (CatalogManager.scala:130-136), so `dpp_mem` still resolves in 
the later tests of this suite, with no conf behind it. Could this test reset 
the catalog manager when it ends, e.g. with 
`spark.sessionState.catalogManager.reset()` as 
`CreateTableWriteOrderSuite.afterEach` does 
(CreateTableWriteOrderSuite.scala:58-61)?



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/BatchScanExec.scala:
##########
@@ -170,9 +170,14 @@ case class BatchScanExec(
     redact(result)
   }
 
+  // A fully pushed runtime filter has no FilterExec above the scan, so this 
is the only place

Review Comment:
   **43. [Nit] Following up on my 
[comment](https://github.com/apache/spark/pull/55579#discussion_r4169230676) on 
the formatted explain: the reason at L173-174 no longer holds for the built-in 
scans**
   
   Thank you for adding this line. I asked for it while the partition columns 
were declared fully pushed, but since e721ae736a6 no built-in scan declares any 
(RuntimePartitionPruningFileScan.scala:61-63), so the scalar subquery filter of 
the new `ExplainSuite` case also shows in the `FilterExec` above the scan. The 
line still helps, since a DPP filter of a V2 scan never gets a `FilterExec`, 
but the comment gives only the fully pushed case. It also joins the filters 
with `", "`, while `simpleString` (L166) and `StorageFilters` (L185) use `","`. 
Could the comment name the DPP case too, and could the separators match?



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/RuntimePartitionPruningFileScan.scala:
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.sql.execution.datasources.v2
+
+import scala.util.control.NonFatal
+
+import org.apache.spark.SparkException
+import org.apache.spark.internal.LogKeys
+import org.apache.spark.sql.catalyst.expressions.{Attribute, BoundReference, 
Expression, Predicate}
+import org.apache.spark.sql.connector.expressions.{FieldReference, 
NamedReference}
+import org.apache.spark.sql.connector.read.InputPartition
+import org.apache.spark.sql.execution.datasources.{FilePartition, 
PartitionDirectory}
+import org.apache.spark.sql.internal.connector.SupportsRuntimeCatalystFiltering
+import org.apache.spark.util.TransientBestEffortLazyVal
+
+/**
+ * A [[FileScan]] that prunes its partition directories with the runtime 
filters Spark derives over
+ * its partition columns (SPARK-30628). The built-in file formats mix this in.
+ *
+ * Both read paths start from one listing of the file index and split and pack 
the files they
+ * keep through `filePartitions`, so `partitions`, `planInputPartitions()` and
+ * `planInputPartitionsWithRuntimeFilters` are final here. A scan that reports 
a partitioning or an
+ * ordering should not mix this in. Spark gives it no group filter in a 
row-level operation, see
+ * `RowLevelOperationRuntimeGroupFiltering`.
+ */
+trait RuntimePartitionPruningFileScan extends FileScan with 
SupportsRuntimeCatalystFiltering {
+
+  /**
+   * The file index listing and its packing, each taken once per scan so 
planning and execution
+   * read one snapshot. A `lazy val` would compute them while holding the 
scan's monitor. Only a
+   * scan with runtime-filterable columns keeps the listing, since nothing 
else reads it again.
+   */
+  private val listing = new TransientBestEffortLazyVal(() => listFiles())
+
+  private val allPartitions = new TransientBestEffortLazyVal(() =>
+    filePartitions(if (filterAttributes().isEmpty) listFiles() else listing()))
+
+  private def listFiles(): Seq[PartitionDirectory] =
+    fileIndex.listFiles(partitionFilters, dataFilters)
+
+  // Final so that a subclass of a built-in scan cannot change one read path 
without the other.
+  final override protected def partitions: Seq[FilePartition] = allPartitions()
+
+  final override def planInputPartitions(): Array[InputPartition] = 
allPartitions().toArray
+
+  /**
+   * The read partition columns that `readSchema()` still exposes; a 
pushed-down aggregate keeps
+   * only the ones it groups by. None is declared fully pushed, so a scalar 
subquery filter over
+   * them is evaluated again above the scan.
+   */
+  override def filterAttributes(): Array[NamedReference] = {
+    val readFields = 
readSchema().fieldNames.map(normalizePartitionColumnName).toSet
+    readPartitionSchema.fieldNames
+      .filter(name => readFields.contains(normalizePartitionColumnName(name)))
+      .map(FieldReference.column)
+  }
+
+  /**
+   * Keeps the listed partition directories whose values satisfy 
`expressions`, then splits and
+   * packs their files for their own size, as V1 does for 
`dynamicallySelectedPartitions`.
+   *
+   * A filter that fails to evaluate on a directory keeps it, since the join 
or the `FilterExec`
+   * above the scan applies the filter again. Each call logs at most one 
warning for such failures.
+   */
+  final override def planInputPartitionsWithRuntimeFilters(
+      expressions: Array[Expression]): Array[InputPartition] = {
+    if (expressions.isEmpty) return allPartitions().toArray
+    // Directory values follow the table's partition schema, not the read one, 
and bind as V1
+    // binds them: with that schema's types, nullable.
+    val partitionSchema = fileIndex.partitionSchema
+    val fieldIndex = partitionSchema.fieldNames.zipWithIndex
+      .map { case (name, i) => normalizePartitionColumnName(name) -> i }.toMap
+    val predicates = expressions.toSeq.map { e =>
+      e -> Predicate.createInterpreted(e.transform {

Review Comment:
   **40. [Nit] Each runtime filter is evaluated interpreted, where V1 compiles 
it**
   
   V1 binds the same kind of predicate with `Predicate.create(..., Nil)` 
(DataSourceScanExec.scala:432-436), which generates code and falls back to the 
interpreted one only if that fails. This binds each filter with 
`Predicate.createInterpreted` and evaluates it per directory on the driver, so 
a table with 10^5 partition directories, a DPP filter and a scalar subquery 
filter costs up to 2x10^5 interpreted evaluations per scan node at execution. 
Could each filter use `Predicate.create(bound, Nil)`, keeping the per-filter 
`try` at L104-108?



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala:
##########
@@ -211,7 +212,8 @@ class DataSourceV2Strategy(session: SparkSession) extends 
Strategy with Predicat
       // read must not prune it: the negated condition that selects the rows 
to copy over would
       // drop the groups where every row matches. The group filter of
       // RowLevelOperationRuntimeGroupFiltering prunes it instead, since it 
keeps every group that
-      // has a matching row.
+      // has a matching row. A built-in file scan gets no group filter, so no 
runtime filter prunes

Review Comment:
   **45. [Nit] The comment on routed scalar subquery filters still names only 
`SupportsRuntimeV2Filtering.filter()`**
   
   L200-203 say the routed filters let `BatchScanExec` prune "via 
SupportsRuntimeV2Filtering.filter()". With this PR every built-in file scan 
uses them too, as Catalyst expressions through 
`planInputPartitionsWithRuntimeFilters` (PushDownUtils.scala:370-375), with no 
translation to V2 predicates. L200-205 are outside the diff, so I left this 
next to the sentence this PR added to the same comment. Could it name both 
routes?



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/RowLevelOperationRuntimeGroupFiltering.scala:
##########
@@ -69,7 +69,12 @@ class 
RowLevelOperationRuntimeGroupFiltering(optimizeSubqueries: Rule[LogicalPla
       scanRelation: DataSourceV2ScanRelation): Boolean = {
     conf.runtimeRowLevelOperationGroupFilterEnabled &&
       cond != TrueLiteral &&
-      scanRelation.runtimeFilterAttrs.nonEmpty
+      scanRelation.runtimeFilterAttrs.nonEmpty &&
+      // A built-in file scan gets no group filter. It does not record the 
groups each call
+      // returned, which a group-based write relies on. The group filter's IN 
is never true on a
+      // directory with a null partition value, so the scan's pruning would 
drop it, and a
+      // delta-based write would miss the matching rows there.
+      !scanRelation.scan.isInstanceOf[RuntimePartitionPruningFileScan]

Review Comment:
   **44. [Nit] The class doc still says every scan that supports runtime 
filtering gets a group filter**
   
   L39-41 say this rule injects a group filter "if the primary scan in a 
row-level operation supports runtime filtering". With this PR the built-in file 
scans support runtime filtering, but this check gives them no group filter, for 
either kind of operation, and only the comment at L73-76 says so. L39-41 are 
outside the diff, so I left this here. Could the class doc mention the 
exception?



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