LuciferYang commented on code in PR #55579: URL: https://github.com/apache/spark/pull/55579#discussion_r4505535485
########## sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/RuntimePartitionPruningFileScan.scala: ########## @@ -0,0 +1,108 @@ +/* + * 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 java.util.Locale + +import scala.util.control.NonFatal + +import org.apache.spark.SparkException +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 +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` and `planInputPartitions()` are final here. A + * scan that reports a partitioning or an ordering should not mix this in. + */ +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. + */ + private val listing = new TransientBestEffortLazyVal(() => Review Comment: Done in c1ba7eacfd7. Only a scan with runtime-filterable columns keeps the listing. Any other scan packs a listing it does not keep. "only a scan with runtime-filterable columns keeps its file listing" checks that such a scan lists again for a direct call with a runtime filter. ########## sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala: ########## @@ -226,22 +227,29 @@ class DataSourceV2Strategy(session: SparkSession) extends Strategy with Predicat } // Screen with the same test pushdown applies, or a filter dropped here and rejected there // would be evaluated nowhere. - val fullyPushedRuntimeFilters = scalarSubqueryFilters.filter { f => - f.references.subsetOf(relation.fullyPushedRuntimeFilterAttrs) && - PushDownUtils.isPushablePartitionFilter(f, includeSubquery = true) - } + val fullyPushedRuntimeFilters = scalarSubqueryFilters.filter( + PushDownUtils.isFullyPushedRuntimeFilter(_, relation.fullyPushedRuntimeFilterAttrs)) // dynamicFilters need no such check: a DynamicPruningSubquery over a non-deterministic // filtering plan is itself non-deterministic, so CleanupDynamicPruningFilters has already // rewritten it to TrueLiteral by the time we get here. + // A Catalyst runtime-filtering scan only applies a filter over attributes it declared, so a + // DPP filter over any other column is dropped here rather than planned for nothing. One + // PartitionPruning inserted over a union lands in every branch, including one that cannot + // use it; the join it was derived from still applies it. + val usableDynamicFilters = relation.scan match { + case _: SupportsRuntimeCatalystFiltering => + dynamicFilters.filter(_.references.subsetOf(relation.runtimeFilterAttrs)) Review Comment: It is in the description now, with the bloom-filter bullet. ########## sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/RuntimePartitionPruningFileScan.scala: ########## @@ -0,0 +1,108 @@ +/* + * 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 java.util.Locale + +import scala.util.control.NonFatal + +import org.apache.spark.SparkException +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 +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` and `planInputPartitions()` are final here. A + * scan that reports a partitioning or an ordering should not mix this in. + */ +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. + */ + private val listing = new TransientBestEffortLazyVal(() => + fileIndex.listFiles(partitionFilters, dataFilters)) + + private val allPartitions = new TransientBestEffortLazyVal(() => filePartitions(listing())) + + // 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(normalizePartitionName).toSet + readPartitionSchema.fieldNames + .filter(name => readFields.contains(normalizePartitionName(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. + */ + 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) => normalizePartitionName(name) -> i }.toMap + val predicates = expressions.toSeq.map { e => + Predicate.createInterpreted(e.transform { + case a: Attribute => + // PushDownUtils passes only filters over `filterAttributes()`. + val i = fieldIndex.getOrElse(normalizePartitionName(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) + }) + } + val all = listing() + val kept = all.filter { dir => + predicates.forall { predicate => + try predicate.eval(dir.values) catch { case NonFatal(_) => true } + } + } + if (kept.length == all.length) allPartitions().toArray else filePartitions(kept).toArray Review Comment: Agreed, and I opened SPARK-61174 for it. The reader factories decide from the schema and the conf only (Parquet and ORC, the other four return false), and nothing else reads `inputPartitions` at planning for a file scan, so reporting the mode would skip the packing. Since the merge-base pays the same, I'd keep it out of this PR, so it does not also change how the built-in scans decide columnar reads. ########## sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Strategy.scala: ########## @@ -226,22 +227,29 @@ class DataSourceV2Strategy(session: SparkSession) extends Strategy with Predicat } // Screen with the same test pushdown applies, or a filter dropped here and rejected there // would be evaluated nowhere. - val fullyPushedRuntimeFilters = scalarSubqueryFilters.filter { f => - f.references.subsetOf(relation.fullyPushedRuntimeFilterAttrs) && - PushDownUtils.isPushablePartitionFilter(f, includeSubquery = true) - } + val fullyPushedRuntimeFilters = scalarSubqueryFilters.filter( + PushDownUtils.isFullyPushedRuntimeFilter(_, relation.fullyPushedRuntimeFilterAttrs)) // dynamicFilters need no such check: a DynamicPruningSubquery over a non-deterministic // filtering plan is itself non-deterministic, so CleanupDynamicPruningFilters has already // rewritten it to TrueLiteral by the time we get here. + // A Catalyst runtime-filtering scan only applies a filter over attributes it declared, so a + // DPP filter over any other column is dropped here rather than planned for nothing. One + // PartitionPruning inserted over a union lands in every branch, including one that cannot + // use it; the join it was derived from still applies it. + val usableDynamicFilters = relation.scan match { + case _: SupportsRuntimeCatalystFiltering => + dynamicFilters.filter(_.references.subsetOf(relation.runtimeFilterAttrs)) + case _ => dynamicFilters + } // A group-based read keeps only the group filter, for the reason above. PartitionPruning // plans a DynamicPruningSubquery from the join of one read, so it is a filter of one read // too. With an IN subquery it lands on the read of the updated rows of an UPDATE. With a // NOT EXISTS subquery the negated condition becomes a semi join, so it lands on the read // of the rows to copy over. val runtimeFilters = if (groupBasedRead) { - dynamicFilters.filterNot(_.isInstanceOf[DynamicPruningSubquery]) + usableDynamicFilters.filterNot(_.isInstanceOf[DynamicPruningSubquery]) } else { - dynamicFilters ++ scalarSubqueryFilters + usableDynamicFilters ++ scalarSubqueryFilters Review Comment: It is in the user-facing section now. -- 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]
