-
Notifications
You must be signed in to change notification settings - Fork 998
[KYUUBI #6943][1/2]HiveScan support dpp #7436
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
maomaodev
wants to merge
5
commits into
apache:master
Choose a base branch
from
maomaodev:kyuubi-6943
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.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
115 changes: 115 additions & 0 deletions
115
...src/main/scala/org/apache/kyuubi/spark/connector/hive/read/HiveRuntimeFilterSupport.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,115 @@ | ||
| /* | ||
| * 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.kyuubi.spark.connector.hive.read | ||
|
|
||
| import java.util.Locale | ||
|
|
||
| import org.apache.spark.internal.Logging | ||
| import org.apache.spark.sql.catalyst.expressions.{AttributeReference, Expression, InSet} | ||
| import org.apache.spark.sql.connector.expressions.{Expressions, Literal => V2Literal, NamedReference} | ||
| import org.apache.spark.sql.connector.expressions.filter.Predicate | ||
| import org.apache.spark.sql.hive.kyuubi.connector.HiveBridgeHelper.{sameType, StructTypeHelper} | ||
| import org.apache.spark.sql.types.StructType | ||
|
|
||
| /** | ||
| * Helpers for a Hive-backed V2 [[org.apache.spark.sql.connector.read.Scan]] to | ||
| * implement [[org.apache.spark.sql.connector.read.SupportsRuntimeV2Filtering]] | ||
| * for Dynamic Partition Pruning (DPP). | ||
| * | ||
| * Spark's `DataSourceV2Strategy` currently only emits the `IN` form of V2 | ||
| * [[Predicate]] as a DPP runtime filter, so translation here handles `IN` only. | ||
| * Any predicate that does not match the expected shape (single partition column | ||
| * ref + scalar literals with matching dataType) is dropped as a whole to | ||
| * avoid incorrect pruning; drops are logged at DEBUG. | ||
| */ | ||
| object HiveRuntimeFilterSupport extends Logging { | ||
|
|
||
| /** | ||
| * Build the runtime-filterable attribute array. Only partition columns are exposed | ||
| * because DPP is only beneficial at the partition directory granularity. | ||
| */ | ||
| def filterAttributes(partitionColumnNames: Seq[String]): Array[NamedReference] = { | ||
| partitionColumnNames.map(Expressions.column).toArray | ||
| } | ||
|
|
||
| /** | ||
| * Translate Spark's runtime V2 `IN` predicates into catalyst `InSet(attr, Set[Any])` | ||
| * expressions bound to the given partition attributes. | ||
| */ | ||
| def toCatalystPartitionFilters( | ||
| predicates: Array[Predicate], | ||
| partitionSchema: StructType, | ||
| isCaseSensitive: Boolean): Seq[Expression] = { | ||
| val attrByName: Map[String, AttributeReference] = | ||
| partitionSchema.toAttributes | ||
| .map(a => normalize(a.name, isCaseSensitive) -> a).toMap | ||
|
|
||
| val accepted = predicates.toSeq.flatMap(p => convertIn(p, attrByName, isCaseSensitive)) | ||
| if (accepted.length < predicates.length) { | ||
| logDebug( | ||
| s"Dropped ${predicates.length - accepted.length} of ${predicates.length} runtime " + | ||
| s"filter(s) not applicable to partition columns " + | ||
| s"[${partitionSchema.fieldNames.mkString(", ")}]") | ||
| } | ||
| accepted | ||
| } | ||
|
|
||
| /** | ||
| * Convert a single V2 `IN` predicate into a catalyst [[InSet]], or `None` if it cannot | ||
| * be safely converted. A predicate is accepted only when its first child is a | ||
| * [[NamedReference]] resolving to a known partition column and every remaining child | ||
| * is a scalar V2 [[V2Literal]] whose dataType matches the partition column's dataType. | ||
| */ | ||
| private def convertIn( | ||
| predicate: Predicate, | ||
| attrByName: Map[String, AttributeReference], | ||
| isCaseSensitive: Boolean): Option[InSet] = { | ||
| val children = predicate.children() | ||
| if (predicate.name() != "IN" || children.length < 2) { | ||
| None | ||
| } else { | ||
| children.head match { | ||
| case ref: NamedReference => | ||
| val colName = normalize(ref.fieldNames().mkString("."), isCaseSensitive) | ||
| attrByName.get(colName).flatMap { attr => | ||
| val values = children.tail | ||
| val allLiteralsMatch = values.forall { | ||
| case lit: V2Literal[_] => sameType(lit.dataType(), attr.dataType) | ||
| case _ => false | ||
| } | ||
| if (allLiteralsMatch) { | ||
| val literalValues = values.iterator.map { | ||
| case lit: V2Literal[_] => lit.value() | ||
| }.toSet[Any] | ||
| Some(InSet(attr, literalValues)) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
| case _ => None | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private def normalize(name: String, isCaseSensitive: Boolean): String = | ||
| if (isCaseSensitive) { | ||
| name | ||
| } else { | ||
| name.toLowerCase(Locale.ROOT) | ||
| } | ||
| } | ||
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
93 changes: 93 additions & 0 deletions
93
.../src/test/scala/org/apache/kyuubi/spark/connector/hive/DynamicPartitionPruningSuite.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,93 @@ | ||
| /* | ||
| * 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.kyuubi.spark.connector.hive | ||
|
|
||
| import scala.annotation.tailrec | ||
|
|
||
| import org.apache.spark.sql.{Row, SparkSession} | ||
| import org.apache.spark.sql.connector.read.{Scan, SupportsRuntimeV2Filtering} | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
| import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanExec | ||
| import org.apache.spark.sql.execution.datasources.v2.BatchScanExec | ||
|
|
||
| class DynamicPartitionPruningSuite extends KyuubiHiveTest { | ||
|
|
||
| private def findScan(spark: SparkSession, sql: String, tableNameHint: String): Scan = { | ||
| @tailrec | ||
| def findBatchScan(plan: SparkPlan): Option[BatchScanExec] = plan match { | ||
| case aqe: AdaptiveSparkPlanExec => findBatchScan(aqe.inputPlan) | ||
| case _ => plan.collectFirst { | ||
| case b: BatchScanExec if b.toString.contains(tableNameHint) => b | ||
| } | ||
| } | ||
| val exec = findBatchScan(spark.sql(sql).queryExecution.executedPlan) | ||
| assert(exec.isDefined) | ||
| exec.get.scan | ||
| } | ||
|
|
||
| test("HiveScan supports DPP runtime filtering on partition columns") { | ||
| Seq( | ||
| ("true", Seq("dt")), | ||
| ("false", Seq.empty[String])).foreach { case (enabled, expectedFilterAttrs) => | ||
| withSparkSession(Map( | ||
| "hive.exec.dynamic.partition.mode" -> "nonstrict", | ||
| "spark.sql.kyuubi.hive.connector.read.runtimeFilter.enabled" -> enabled)) { spark => | ||
| val suffix = if (enabled == "true") "on" else "off" | ||
| val fact = s"hive.default.dpp_fact_$suffix" | ||
| val dim = s"hive.default.dpp_dim_$suffix" | ||
|
|
||
| withTable(fact, dim) { | ||
| spark.sql( | ||
| s""" | ||
| | CREATE TABLE $fact (id INT, v STRING) PARTITIONED BY (dt STRING) | ||
| | STORED AS TEXTFILE | ||
| |""".stripMargin).collect() | ||
| spark.sql(s"INSERT INTO $fact PARTITION (dt='2026-01-01') VALUES (1, 'a'), (2, 'b')") | ||
| spark.sql(s"INSERT INTO $fact PARTITION (dt='2026-05-01') VALUES (3, 'c'), (4, 'd')") | ||
| spark.sql(s"INSERT INTO $fact PARTITION (dt='2026-09-01') VALUES (5, 'e'), (6, 'f')") | ||
|
|
||
| spark.sql( | ||
| s""" | ||
| | CREATE TABLE $dim (dt STRING, tag STRING) | ||
| | STORED AS TEXTFILE | ||
| |""".stripMargin).collect() | ||
| spark.sql(s"INSERT INTO $dim VALUES ('2026-05-01', 'target')") | ||
|
|
||
| val sql = | ||
| s""" | ||
| | SELECT f.id, f.v, f.dt | ||
| | FROM $fact f JOIN $dim d ON f.dt = d.dt | ||
| | WHERE d.tag = 'target' | ||
| |""".stripMargin | ||
|
|
||
| checkAnswer( | ||
| spark.sql(sql), | ||
| Seq( | ||
| Row(3, "c", "2026-05-01"), | ||
| Row(4, "d", "2026-05-01"))) | ||
|
|
||
| val scan = findScan(spark, sql, fact.split('.').last) | ||
| assert(scan.isInstanceOf[SupportsRuntimeV2Filtering]) | ||
| val filterAttrs = scan.asInstanceOf[SupportsRuntimeV2Filtering] | ||
| .filterAttributes().map(_.fieldNames().mkString(".")) | ||
| assert(filterAttrs.toSeq == expectedFilterAttrs) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
This makes the code compatibility very fragile, catalyst code is treated as internal implementation details and does not provide any compatibility guarantee.
Given the situation, why choose to implement
SupportsRuntimeV2Filteringand translate the V2Predicateto the catalystFilterinstead of just implementSupportsRuntimeFiltering?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.
Good point. I’ll use
SupportsRuntimeFilteringin the latest commit, and try to maintain compatibility with Spark 3.3+.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.
I've replaced
SupportsRuntimeV2FilteringwithSupportsRuntimeFilteringand updated the PR description; ready for further review.