-
Notifications
You must be signed in to change notification settings - Fork 29k
[SQL][SPARK-39528] Use V2 Filter in SupportsRuntimeFiltering #36918
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cc62244
Support runtime V2 filtering
huaxingao 25413ff
fix style
huaxingao 66b4370
use pattern matching instead of if-else
huaxingao 6157001
address comments
huaxingao 1aadfc6
address comments
huaxingao 277222f
fix java doc build failure
huaxingao 6221609
address comments
huaxingao 9e0799f
address comments
huaxingao d6eb931
address comments
huaxingao d07d61b
address comments
huaxingao 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
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
67 changes: 67 additions & 0 deletions
67
...atalyst/src/main/java/org/apache/spark/sql/connector/read/SupportsRuntimeV2Filtering.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,67 @@ | ||
| /* | ||
| * 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.read; | ||
|
|
||
| import org.apache.spark.annotation.Experimental; | ||
| import org.apache.spark.sql.connector.expressions.NamedReference; | ||
| import org.apache.spark.sql.connector.expressions.filter.Predicate; | ||
| import org.apache.spark.sql.sources.Filter; | ||
|
|
||
| /** | ||
| * A mix-in interface for {@link Scan}. Data sources can implement this interface if they can | ||
| * filter initially planned {@link InputPartition}s using predicates Spark infers at runtime. | ||
| * This interface is very similar to {@link SupportsRuntimeFiltering} except it uses | ||
| * data source V2 {@link Predicate} instead of data source V1 {@link Filter}. | ||
| * {@link SupportsRuntimeV2Filtering} is preferred over {@link SupportsRuntimeFiltering} | ||
| * and only one of them should be implemented by the data sources. | ||
| * | ||
| * <p> | ||
| * Note that Spark will push runtime filters only if they are beneficial. | ||
| * | ||
| * @since 3.4.0 | ||
| */ | ||
| @Experimental | ||
| public interface SupportsRuntimeV2Filtering extends Scan { | ||
| /** | ||
| * Returns attributes this scan can be filtered by at runtime. | ||
| * <p> | ||
| * Spark will call {@link #filter(Predicate[])} if it can derive a runtime | ||
| * predicate for any of the filter attributes. | ||
| */ | ||
| NamedReference[] filterAttributes(); | ||
|
|
||
| /** | ||
| * Filters this scan using runtime predicates. | ||
| * <p> | ||
| * The provided expressions must be interpreted as a set of predicates that are ANDed together. | ||
| * Implementations may use the predicates to prune initially planned {@link InputPartition}s. | ||
| * <p> | ||
| * If the scan also implements {@link SupportsReportPartitioning}, it must preserve | ||
| * the originally reported partitioning during runtime filtering. While applying runtime | ||
| * predicates, the scan may detect that some {@link InputPartition}s have no matching data. It | ||
| * can omit such partitions entirely only if it does not report a specific partitioning. | ||
| * Otherwise, the scan can replace the initially planned {@link InputPartition}s that have no | ||
| * matching data with empty {@link InputPartition}s but must preserve the overall number of | ||
| * partitions. | ||
| * <p> | ||
| * Note that Spark will call {@link Scan#toBatch()} again after filtering the scan at runtime. | ||
| * | ||
| * @param predicates data source V2 predicates used to filter the scan at runtime | ||
| */ | ||
| void filter(Predicate[] predicates); | ||
| } |
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
49 changes: 49 additions & 0 deletions
49
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/connector/PredicateUtils.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.internal.connector | ||
|
|
||
| import org.apache.spark.sql.catalyst.CatalystTypeConverters | ||
| import org.apache.spark.sql.connector.expressions.{LiteralValue, NamedReference} | ||
| import org.apache.spark.sql.connector.expressions.filter.Predicate | ||
| import org.apache.spark.sql.sources.{Filter, In} | ||
|
|
||
| private[sql] object PredicateUtils { | ||
|
|
||
| def toV1(predicate: Predicate): Option[Filter] = { | ||
| predicate.name() match { | ||
| // TODO: add conversion for other V2 Predicate | ||
| case "IN" if predicate.children()(0).isInstanceOf[NamedReference] => | ||
| val attribute = predicate.children()(0).toString | ||
| val values = predicate.children().drop(1) | ||
| if (values.length > 0) { | ||
| if (!values.forall(_.isInstanceOf[LiteralValue[_]])) return None | ||
| val dataType = values(0).asInstanceOf[LiteralValue[_]].dataType | ||
| if (!values.forall(_.asInstanceOf[LiteralValue[_]].dataType.sameType(dataType))) { | ||
| return None | ||
| } | ||
| val inValues = values.map(v => | ||
| CatalystTypeConverters.convertToScala(v.asInstanceOf[LiteralValue[_]].value, dataType)) | ||
| Some(In(attribute, inValues)) | ||
| } else { | ||
| Some(In(attribute, Array.empty[Any])) | ||
| } | ||
|
|
||
| case _ => None | ||
| } | ||
| } | ||
| } | ||
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
77 changes: 77 additions & 0 deletions
77
...yst/src/test/scala/org/apache/spark/sql/connector/catalog/InMemoryTableWithV2Filter.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,77 @@ | ||
| /* | ||
| * 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.expressions.{FieldReference, LiteralValue, NamedReference, Transform} | ||
| import org.apache.spark.sql.connector.expressions.filter.Predicate | ||
| import org.apache.spark.sql.connector.read.{InputPartition, Scan, ScanBuilder, SupportsRuntimeV2Filtering} | ||
| import org.apache.spark.sql.types.StructType | ||
| import org.apache.spark.sql.util.CaseInsensitiveStringMap | ||
|
|
||
| class InMemoryTableWithV2Filter( | ||
| name: String, | ||
| schema: StructType, | ||
| partitioning: Array[Transform], | ||
| properties: util.Map[String, String]) | ||
| extends InMemoryTable(name, schema, partitioning, properties) { | ||
|
|
||
| override def newScanBuilder(options: CaseInsensitiveStringMap): ScanBuilder = { | ||
| new InMemoryV2FilterScanBuilder(schema) | ||
| } | ||
|
|
||
| class InMemoryV2FilterScanBuilder(tableSchema: StructType) | ||
| extends InMemoryScanBuilder(tableSchema) { | ||
| override def build: Scan = | ||
| InMemoryV2FilterBatchScan(data.map(_.asInstanceOf[InputPartition]), schema, tableSchema) | ||
| } | ||
|
|
||
| case class InMemoryV2FilterBatchScan( | ||
| var _data: Seq[InputPartition], | ||
| readSchema: StructType, | ||
| tableSchema: StructType) | ||
| extends BatchScanBaseClass (_data, readSchema, tableSchema) with SupportsRuntimeV2Filtering { | ||
|
|
||
| override def filterAttributes(): Array[NamedReference] = { | ||
| val scanFields = readSchema.fields.map(_.name).toSet | ||
| partitioning.flatMap(_.references) | ||
| .filter(ref => scanFields.contains(ref.fieldNames.mkString("."))) | ||
| } | ||
|
|
||
| override def filter(filters: Array[Predicate]): Unit = { | ||
| if (partitioning.length == 1 && partitioning.head.references().length == 1) { | ||
| val ref = partitioning.head.references().head | ||
| filters.foreach { | ||
| case p : Predicate if p.name().equals("IN") => | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. feels like some
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| if (p.children().length > 1) { | ||
| val filterRef = p.children()(0).asInstanceOf[FieldReference].references.head | ||
| if (filterRef.toString.equals(ref.toString)) { | ||
| val matchingKeys = | ||
| p.children().drop(1).map(_.asInstanceOf[LiteralValue[_]].value.toString).toSet | ||
| data = data.filter(partition => { | ||
| val key = partition.asInstanceOf[BufferedRows].keyString | ||
| matchingKeys.contains(key) | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
46 changes: 46 additions & 0 deletions
46
.../test/scala/org/apache/spark/sql/connector/catalog/InMemoryTableWithV2FilterCatalog.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,46 @@ | ||
| /* | ||
| * 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 | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| class InMemoryTableWithV2FilterCatalog extends InMemoryTableCatalog { | ||
| import CatalogV2Implicits._ | ||
|
|
||
| override def createTable( | ||
| ident: Identifier, | ||
| schema: StructType, | ||
| partitions: Array[Transform], | ||
| properties: util.Map[String, String]): Table = { | ||
| if (tables.containsKey(ident)) { | ||
| throw new TableAlreadyExistsException(ident) | ||
| } | ||
|
|
||
| InMemoryTableCatalog.maybeSimulateFailedTableCreation(properties) | ||
|
|
||
| val tableName = s"$name.${ident.quoted}" | ||
| val table = new InMemoryTableWithV2Filter(tableName, schema, partitions, properties) | ||
| tables.put(ident, table) | ||
| namespaces.putIfAbsent(ident.namespace.toList, Map()) | ||
| table | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.