-
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * 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, In, Literal} | ||
| import org.apache.spark.sql.connector.expressions.{Expressions, NamedReference} | ||
| import org.apache.spark.sql.hive.kyuubi.connector.HiveBridgeHelper.StructTypeHelper | ||
| import org.apache.spark.sql.sources.{Filter, In => FilterIn} | ||
| 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.SupportsRuntimeFiltering]] | ||
| * for Dynamic Partition Pruning (DPP). | ||
| * | ||
| * Spark's `DataSourceV2Strategy` currently only emits the `IN` form as a DPP | ||
| * runtime filter, so translation here handles `In` only. Any filter whose | ||
| * attribute does not match a known partition column is dropped; drops are | ||
| * logged at DEBUG. | ||
| * | ||
| * We deliberately use the V1 `SupportsRuntimeFiltering` instead of the newer | ||
| * `SupportsRuntimeV2Filtering` to keep this connector compilable against | ||
| * Spark 3.3, where `SupportsRuntimeV2Filtering` was introduced in Spark 3.4. | ||
| */ | ||
| 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 V1 `In` filters into catalyst [[In]] expressions | ||
| * bound to the given partition attributes. | ||
| * | ||
| * A filter is accepted only when it is a [[FilterIn]] whose attribute resolves | ||
| * to a known partition column. | ||
| */ | ||
| def toCatalystPartitionFilters( | ||
|
Member
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. 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
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. Good point. I’ll use
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. I've replaced |
||
| filters: Array[Filter], | ||
| partitionSchema: StructType, | ||
| isCaseSensitive: Boolean): Seq[Expression] = { | ||
| val attrByName: Map[String, AttributeReference] = | ||
| partitionSchema.toAttributes | ||
| .map(a => normalize(a.name, isCaseSensitive) -> a).toMap | ||
|
|
||
| val accepted = filters.toSeq.flatMap { | ||
| case FilterIn(attribute, values) => | ||
| attrByName.get(normalize(attribute, isCaseSensitive)).map { attr => | ||
| In(attr, values.toSeq.map(v => Literal.create(v, attr.dataType))) | ||
| } | ||
| case _ => None | ||
| } | ||
|
|
||
| if (accepted.length < filters.length) { | ||
| logDebug( | ||
| s"Dropped ${filters.length - accepted.length} of ${filters.length} runtime " + | ||
| s"filter(s) not applicable to partition columns " + | ||
| s"[${partitionSchema.fieldNames.mkString(", ")}]") | ||
| } | ||
| accepted | ||
| } | ||
|
|
||
| private def normalize(name: String, isCaseSensitive: Boolean): String = | ||
| if (isCaseSensitive) { | ||
| name | ||
| } else { | ||
| name.toLowerCase(Locale.ROOT) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| /* | ||
| * 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, SupportsRuntimeFiltering} | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
| import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanExec | ||
| import org.apache.spark.sql.execution.datasources.v2.BatchScanExec | ||
|
|
||
| import org.apache.kyuubi.spark.connector.hive.read.HiveScan | ||
|
|
||
| class DynamicPartitionPruningSuite extends KyuubiHiveTest { | ||
|
|
||
| private def findScan(spark: SparkSession, sql: String, tableNameHint: String): Scan = { | ||
| // Match on `HiveScan.catalogTable` rather than the node's `toString` because | ||
| // `BatchScanExec.toString` shape differs across Spark versions. | ||
| def matchesHint(b: BatchScanExec): Boolean = b.scan match { | ||
| case h: HiveScan => h.catalogTable.identifier.table == tableNameHint | ||
| case _ => false | ||
| } | ||
|
|
||
| @tailrec | ||
| def findBatchScan(plan: SparkPlan): Option[BatchScanExec] = plan match { | ||
| case aqe: AdaptiveSparkPlanExec => findBatchScan(aqe.inputPlan) | ||
| case _ => plan.collectFirst { | ||
| case b: BatchScanExec if matchesHint(b) => 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[SupportsRuntimeFiltering]) | ||
| val filterAttrs = scan.asInstanceOf[SupportsRuntimeFiltering] | ||
| .filterAttributes().map(_.fieldNames().mkString(".")) | ||
| assert(filterAttrs.toSeq == expectedFilterAttrs) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
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 think in “To restore the legacy behavior, set it to true.” it should be false instead of true?
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.
Oh, my mistake, thanks for review! I've already made the changes.