-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[SPARK-55596][SQL] DSV2 Enhanced Partition Stats Filtering #54459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
szehon-ho
wants to merge
1
commit into
apache:master
Choose a base branch
from
szehon-ho:partition_filter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+590
−12
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
...t/src/main/java/org/apache/spark/sql/connector/expressions/filter/PartitionPredicate.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package org.apache.spark.sql.connector.expressions.filter; | ||
|
|
||
| import org.apache.spark.sql.catalyst.InternalRow; | ||
| import org.apache.spark.sql.connector.expressions.Expression; | ||
|
|
||
| /** | ||
| * Represents a partition filter expression (an expression targeting only the schema of | ||
| * {@link org.apache.spark.sql.connector.catalog.Table#partitioning()}). | ||
| * <p> | ||
| * This can be used to evaluate individual partition keys against this partition expression | ||
| * by {@link #accept(InternalRow)}. | ||
| * </p> | ||
| * @since 4.2.0 | ||
| */ | ||
| public abstract class PartitionPredicate extends Predicate { | ||
|
|
||
| /** | ||
| * Default predicate name for partition predicates. | ||
| */ | ||
| public static final String NAME = "PartitionPredicate"; | ||
|
|
||
| public PartitionPredicate(String name, Expression[] children) { | ||
| super(name, children); | ||
| } | ||
|
|
||
| /** | ||
| * Evaluates this predicate against a single partition's keys. | ||
| * | ||
| * @param partitionKey keys of a single partition, represented the values of the partition | ||
| * corresponding to | ||
| * {@link org.apache.spark.sql.connector.catalog.Table#partitioning()} | ||
| * @return true if this partition evaluates to true for this partition expression. | ||
| */ | ||
| public abstract boolean accept(InternalRow partitionKey); | ||
|
|
||
| /** | ||
| * Returns the ordinal position(s) of the partition transform(s) in | ||
| * {@link org.apache.spark.sql.connector.catalog.Table#partitioning()} that are | ||
| * referenced by this partition filter expression. | ||
| * | ||
| * <p><b>Example:</b> Suppose {@code Table.partitioning()} returns three partition | ||
| * transforms: {@code [years(ts), months(ts), bucket(32, id)]} with ordinals 0, 1, 2. | ||
| * <ul> | ||
| * <li>A filter expression {@code years(ts) = 2026} returns {@code [0]}.</li> | ||
| * <li>A filter expression {@code years(ts) = 2026 and months(ts) = 01} | ||
| * returns {@code [0, 1]}.</li> | ||
| * <li>A filter expression {@code bucket(32, id) = 1} returns {@code [2]}.</li> | ||
| * </ul> | ||
| * <p> | ||
| * Data sources can use this to evaluate PartitionPredicates pushed down by | ||
| * {@link org.apache.spark.sql.connector.read.SupportsRuntimeV2Filtering#filter(Predicate[])} | ||
| * to determine whether the PartitionPredicate can be satisfied completely, | ||
| * or whether it must be returned to Spark for post-scan filtering. | ||
| * <p> | ||
| * For example, data sources supporting partition spec evolution | ||
| * should return PartitionPredicates that reference later-added partition | ||
| * transforms (for which data in the the table is incompletely partitioned) | ||
| * to Spark for post-scan filter. Initially-added partition transforms | ||
| * (for which data in the table is completely partitioned) do not need to be returned | ||
| * for post-scan filter. | ||
| * @return array of 0-based ordinals for the transform(s) in | ||
| * {@link org.apache.spark.sql.connector.catalog.Table#partitioning()} referenced by this | ||
| * PartitionPredicate's partition filter expression. | ||
| */ | ||
| public abstract int[] referencedPartitionColumnOrdinals(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...alyst/src/main/scala/org/apache/spark/sql/internal/connector/PartitionPredicateImpl.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * 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.internal.connector | ||
|
|
||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.connector.expressions.filter.PartitionPredicate | ||
|
|
||
| /** | ||
| * An implementation for [[PartitionPredicate]] that wraps a Catalyst Expression representing a | ||
| * partition filter. | ||
| * <p> | ||
| * Supporting data sources receive these via | ||
| * [[org.apache.spark.sql.connector.read.SupportsPushDownV2Filters#pushPredicates pushPredicates]] | ||
| * and may use them for partition filtering. | ||
| */ | ||
| class PartitionPredicateImpl( | ||
| private val catalystExpression: Expression, | ||
| private val partitionSchema: Seq[AttributeReference]) | ||
| extends PartitionPredicate( | ||
| PartitionPredicate.NAME, | ||
| org.apache.spark.sql.connector.expressions.Expression.EMPTY_EXPRESSION) with Logging { | ||
|
|
||
| /** The wrapped partition filter Catalyst Expression. */ | ||
| def expression: Expression = catalystExpression | ||
|
|
||
| override def toString(): String = | ||
| s"PartitionPredicate(${catalystExpression.sql})" | ||
|
|
||
| override def accept(partitionValues: InternalRow): Boolean = { | ||
| // defensive checks | ||
| if (partitionSchema.isEmpty) { | ||
| logWarning(s"Cannot evaluate partition predicate ${catalystExpression.sql}: " + | ||
| s"partition schema is empty, including partition") | ||
| return true | ||
| } | ||
| if (partitionValues.numFields != partitionSchema.length) { | ||
| logWarning(s"Cannot evaluate partition predicate ${catalystExpression.sql}: " + | ||
| s"partition value field count (${partitionValues.numFields}) does not match schema " + | ||
| s"(${partitionSchema.length}), including partition") | ||
| return true | ||
| } | ||
| val refNames = catalystExpression.references.map(_.name).toSet | ||
| val partitionNames = partitionSchema.map(_.name).toSet | ||
| if (!refNames.subsetOf(partitionNames)) { | ||
| logWarning(s"Cannot evaluate partition predicate ${catalystExpression.sql}: " + | ||
| s"expression references ${refNames.mkString(", ")} not all in partition columns " + | ||
| s"${partitionNames.mkString(", ")}, including partition") | ||
| return true | ||
| } | ||
|
|
||
| // evaluate the catalyst partition filter expression | ||
| try { | ||
| val boundExpr = catalystExpression.transform { | ||
| case a: AttributeReference => | ||
| val index = partitionSchema.indexWhere(_.name == a.name) | ||
| BoundReference(index, partitionSchema(index).dataType, nullable = true) | ||
| } | ||
| val boundPredicate = Predicate.createInterpreted(boundExpr) | ||
| boundPredicate.eval(partitionValues) | ||
| } catch { | ||
| case e: Exception => | ||
| logWarning(s"Failed to evaluate partition predicate ${catalystExpression.sql}, " + | ||
| s"including partition", e) | ||
| true | ||
| } | ||
| } | ||
|
|
||
| override def referencedPartitionColumnOrdinals(): Array[Int] = Array.empty[Int] | ||
| } |
128 changes: 128 additions & 0 deletions
128
...t/scala/org/apache/spark/sql/connector/catalog/InMemoryEnhancedPartitionFilterTable.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| /* | ||
| * 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.catalog | ||
|
|
||
| import java.util | ||
|
|
||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits.MultipartIdentifierHelper | ||
| import org.apache.spark.sql.connector.expressions.Transform | ||
| import org.apache.spark.sql.connector.expressions.filter.PartitionPredicate | ||
| import org.apache.spark.sql.connector.expressions.filter.Predicate | ||
| import org.apache.spark.sql.connector.read.{InputPartition, Scan, ScanBuilder, SupportsPushDownRequiredColumns, SupportsPushDownV2Filters} | ||
| import org.apache.spark.sql.connector.write.{LogicalWriteInfo, WriteBuilder} | ||
| import org.apache.spark.sql.types.StructType | ||
| import org.apache.spark.sql.util.CaseInsensitiveStringMap | ||
| import org.apache.spark.util.ArrayImplicits._ | ||
|
|
||
| /** | ||
| * In-memory table whose scan builder implements enhanced partition filtering using | ||
| * PartitionPredicates pushed in a second pass. | ||
| */ | ||
| class InMemoryEnhancedPartitionFilterTable( | ||
| name: String, | ||
| columns: Array[Column], | ||
| partitioning: Array[Transform], | ||
| properties: util.Map[String, String]) | ||
| extends InMemoryTable(name, columns, partitioning, properties) { | ||
|
|
||
| override def newScanBuilder(options: CaseInsensitiveStringMap): ScanBuilder = { | ||
| new InMemoryEnhancedPartitionFilterScanBuilder(schema()) | ||
| } | ||
|
|
||
| override def newWriteBuilder(info: LogicalWriteInfo): WriteBuilder = { | ||
| InMemoryBaseTable.maybeSimulateFailedTableWrite(new CaseInsensitiveStringMap(properties)) | ||
| InMemoryBaseTable.maybeSimulateFailedTableWrite(info.options) | ||
| new InMemoryWriterBuilderWithOverWrite(info) | ||
| } | ||
|
|
||
| class InMemoryEnhancedPartitionFilterScanBuilder( | ||
| tableSchema: StructType) | ||
| extends ScanBuilder | ||
| with SupportsPushDownV2Filters | ||
| with SupportsPushDownRequiredColumns { | ||
|
|
||
| private var readSchema: StructType = tableSchema | ||
| private var partitionPredicates: Array[PartitionPredicate] = Array.empty | ||
| private var firstPassPushedPredicates: Array[Predicate] = Array.empty | ||
| private var _pushedPredicates: Array[Predicate] = Array.empty | ||
|
|
||
| override def supportsEnhancedPartitionFiltering(): Boolean = true | ||
|
|
||
| override def pushPredicates( | ||
| predicates: Array[Predicate]) | ||
| : Array[Predicate] = { | ||
| val partitionOnly = predicates.filter(_.isInstanceOf[PartitionPredicate]) | ||
| if (partitionOnly.nonEmpty) { | ||
| // Second call: partition-only predicates (e.g. UDF(partition_col) = value) | ||
| partitionPredicates = partitionOnly.map(_.asInstanceOf[PartitionPredicate]).toArray | ||
| _pushedPredicates = firstPassPushedPredicates ++ | ||
| partitionPredicates.map(p => p: Predicate) | ||
| Array.empty | ||
| } else { | ||
| // First call: push partition-only predicates we can evaluate to prune InputPartitions | ||
| val partNames = InMemoryEnhancedPartitionFilterTable.this.partCols | ||
| .flatMap(_.toSeq).toSet | ||
| def referencesOnlyPartitionCols(p: Predicate): Boolean = | ||
| p.references().forall(ref => | ||
| partNames.contains(ref.fieldNames().mkString("."))) | ||
| val partitionOnlyFirstPass = predicates.filter(referencesOnlyPartitionCols) | ||
| firstPassPushedPredicates = partitionOnlyFirstPass.filter(p => | ||
| InMemoryTableWithV2Filter.supportsPredicates(Array(p))) | ||
| _pushedPredicates = firstPassPushedPredicates | ||
| predicates.filterNot(firstPassPushedPredicates.contains) | ||
| } | ||
| } | ||
|
|
||
| override def pushedPredicates(): Array[Predicate] = _pushedPredicates | ||
|
|
||
| override def pruneColumns(requiredSchema: StructType): Unit = { | ||
| readSchema = requiredSchema | ||
| } | ||
|
|
||
| override def build(): Scan = { | ||
| val allPartitions = data.map(_.asInstanceOf[InputPartition]).toImmutableArraySeq | ||
| val filteredByFirstPass = if (firstPassPushedPredicates.isEmpty) { | ||
| allPartitions | ||
| } else { | ||
| val partNames = | ||
| InMemoryEnhancedPartitionFilterTable.this.partCols.map(_.toSeq.quoted) | ||
| .toImmutableArraySeq | ||
| val allKeys = allPartitions.map(_.asInstanceOf[BufferedRows].key) | ||
| val matchingKeys = InMemoryTableWithV2Filter.filtersToKeys( | ||
| allKeys, partNames, firstPassPushedPredicates).toSet | ||
| allPartitions.filter(p => | ||
| matchingKeys.contains(p.asInstanceOf[BufferedRows].key)) | ||
| } | ||
| val filtered = if (partitionPredicates.isEmpty) { | ||
| filteredByFirstPass | ||
| } else { | ||
| filteredByFirstPass.filter { p => | ||
| val partRow = p.asInstanceOf[BufferedRows].partitionKey() | ||
| partitionPredicates.forall(_.accept(partRow)) | ||
| } | ||
| } | ||
| InMemoryEnhancedPartitionFilterBatchScan(filtered, readSchema, tableSchema) | ||
| } | ||
| } | ||
|
|
||
| case class InMemoryEnhancedPartitionFilterBatchScan( | ||
| _data: Seq[InputPartition], | ||
| readSchema: StructType, | ||
| tableSchema: StructType) | ||
| extends BatchScanBaseClass(_data, readSchema, tableSchema) | ||
| } |
49 changes: 49 additions & 0 deletions
49
.../org/apache/spark/sql/connector/catalog/InMemoryTableEnhancedPartitionFilterCatalog.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * 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.catalog | ||
|
|
||
| import java.util | ||
|
|
||
| import org.apache.spark.sql.catalyst.analysis.TableAlreadyExistsException | ||
| import org.apache.spark.sql.connector.expressions.Transform | ||
|
|
||
| class InMemoryTableEnhancedPartitionFilterCatalog extends InMemoryTableCatalog { | ||
| import CatalogV2Implicits._ | ||
|
|
||
| override def createTable( | ||
| ident: Identifier, | ||
| columns: Array[Column], | ||
| partitions: Array[Transform], | ||
| properties: util.Map[String, String]): Table = { | ||
| if (tables.containsKey(ident)) { | ||
| throw new TableAlreadyExistsException(ident.asMultipartIdentifier) | ||
| } | ||
|
|
||
| InMemoryTableCatalog.maybeSimulateFailedTableCreation(properties) | ||
|
|
||
| val tableName = s"$name.${ident.quoted}" | ||
| val table = new InMemoryEnhancedPartitionFilterTable(tableName, columns, partitions, properties) | ||
| tables.put(ident, table) | ||
| namespaces.putIfAbsent(ident.namespace.toList, Map()) | ||
| table | ||
| } | ||
|
|
||
| override def createTable(ident: Identifier, tableInfo: TableInfo): Table = { | ||
| createTable(ident, tableInfo.columns(), tableInfo.partitions(), tableInfo.properties()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
License header