-
Notifications
You must be signed in to change notification settings - Fork 1k
[KYUUBI #6943][2/2] OrcScan and ParquetScan support DPP #7476
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
4
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 all commits
Commits
Show all changes
4 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
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
171 changes: 171 additions & 0 deletions
171
...ector-hive/src/main/scala/org/apache/kyuubi/spark/connector/hive/read/KyuubiOrcScan.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,171 @@ | ||
| /* | ||
| * 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 org.apache.hadoop.conf.Configuration | ||
| import org.apache.hadoop.fs.Path | ||
| import org.apache.spark.sql.SparkSession | ||
| import org.apache.spark.sql.catalyst.catalog.CatalogTable | ||
| import org.apache.spark.sql.catalyst.expressions.Expression | ||
| import org.apache.spark.sql.connector.expressions.NamedReference | ||
| import org.apache.spark.sql.connector.expressions.aggregate.Aggregation | ||
| import org.apache.spark.sql.connector.read.{InputPartition, PartitionReaderFactory, Scan, SupportsRuntimeFiltering} | ||
| import org.apache.spark.sql.execution.WholeStageCodegenExec | ||
| import org.apache.spark.sql.execution.datasources.PartitioningAwareFileIndex | ||
| import org.apache.spark.sql.execution.datasources.orc.OrcUtils | ||
| import org.apache.spark.sql.execution.datasources.v2.FileScan | ||
| import org.apache.spark.sql.execution.datasources.v2.orc.OrcScan | ||
| import org.apache.spark.sql.sources.Filter | ||
| import org.apache.spark.sql.types.StructType | ||
| import org.apache.spark.sql.util.CaseInsensitiveStringMap | ||
|
|
||
| /** | ||
| * A DPP-aware wrapper around Spark's built-in [[OrcScan]] that adds | ||
| * [[SupportsRuntimeFiltering]] so Dynamic Partition Pruning can push runtime | ||
| * IN predicates down to the Hive partitioned scan. | ||
| * | ||
| * Implementation notes: | ||
| * 1. Only DPP-specific methods ([[filter]] / [[filterAttributes]] / | ||
| * [[planInputPartitions]]) contain custom logic, all other methods | ||
| * delegate to the wrapped [[OrcScan]]. | ||
| * 2. [[equals]] uses a `KyuubiOrcScan` type pattern before delegating to | ||
| * `inner.equals`, so a plain [[OrcScan]] never compares equal and is | ||
| * never reused in its place during exchange/subquery reuse. [[hashCode]] | ||
| * is a constant, matching [[FileScan]]'s default. | ||
| * 3. Native engines (Gluten / Comet) identify scans by class name, so this | ||
| * wrapper is not recognized and falls back to JVM reads. | ||
| */ | ||
| class KyuubiOrcScan( | ||
| val sparkSession: SparkSession, | ||
| val hadoopConf: Configuration, | ||
| val fileIndex: PartitioningAwareFileIndex, | ||
| val dataSchema: StructType, | ||
| val readDataSchema: StructType, | ||
| val readPartitionSchema: StructType, | ||
| val options: CaseInsensitiveStringMap, | ||
| val pushedAggregate: Option[Aggregation], | ||
| val pushedFilters: Array[Filter], | ||
| val partitionFilters: Seq[Expression], | ||
| val dataFilters: Seq[Expression], | ||
| val catalogTable: CatalogTable) | ||
| extends FileScan | ||
| with SupportsRuntimeFiltering { | ||
|
|
||
| private[hive] val inner: OrcScan = OrcScan( | ||
| sparkSession, | ||
| hadoopConf, | ||
| fileIndex, | ||
| dataSchema, | ||
| readDataSchema, | ||
| readPartitionSchema, | ||
| options, | ||
| pushedAggregate, | ||
| pushedFilters, | ||
| partitionFilters, | ||
| dataFilters) | ||
|
|
||
| private var runtimeFilters: Seq[Expression] = Seq.empty | ||
|
|
||
| private val isCaseSensitive = sparkSession.sessionState.conf.caseSensitiveAnalysis | ||
|
|
||
| /** | ||
| * The default [[Scan.ColumnarSupportMode.PARTITION_DEFINED]] (SPARK-44505) | ||
| * would drive `DataSourceV2ScanExecBase.supportsColumnar` to materialise | ||
| * `inputPartitions` during planning (via `FileScan.partitions` -> | ||
| * `HiveCatalogFileIndex.listFiles`), triggering a full-table HDFS listing | ||
| * before runtime filters arrive via [[SupportsRuntimeFiltering.filter]] and | ||
| * cancelling DPP's end-to-end win. | ||
| * | ||
| * We instead decide from sqlConf + schema, matching | ||
| * `OrcPartitionReaderFactory.supportColumnarReads` in all non-empty cases. | ||
| * When DPP prunes every partition, Spark's default would return | ||
| * `UNSUPPORTED` on the empty list, we still return `SUPPORTED`, adding a | ||
| * harmless `ColumnarToRow` on an empty RDD. | ||
| */ | ||
| override def columnarSupportMode(): Scan.ColumnarSupportMode = { | ||
| val sqlConf = sparkSession.sessionState.conf | ||
| val schema = StructType(readDataSchema.fields ++ readPartitionSchema.fields) | ||
| val supportsColumnar = sqlConf.orcVectorizedReaderEnabled && | ||
| sqlConf.wholeStageEnabled && | ||
| !WholeStageCodegenExec.isTooManyFields(sqlConf, schema) && | ||
| schema.forall(s => | ||
| OrcUtils.supportColumnarReads( | ||
| s.dataType, | ||
| sqlConf.orcVectorizedReaderNestedColumnEnabled)) | ||
| if (supportsColumnar) Scan.ColumnarSupportMode.SUPPORTED | ||
| else Scan.ColumnarSupportMode.UNSUPPORTED | ||
| } | ||
|
|
||
| override def filterAttributes(): Array[NamedReference] = { | ||
| // Under aggregate pushdown, the scan outputs aggregate columns only, so | ||
| // partition columns may be absent from its output, runtime filtering on | ||
| // them is also meaningless once results are aggregated. | ||
| if (pushedAggregate.nonEmpty) Array.empty[NamedReference] | ||
| else HiveRuntimeFilterSupport.filterAttributes(readPartitionSchema.fieldNames.toSeq) | ||
| } | ||
|
|
||
| override def filter(filters: Array[Filter]): Unit = { | ||
| runtimeFilters = HiveRuntimeFilterSupport.toCatalystPartitionFilters( | ||
| filters, | ||
| fileIndex.partitionSchema, | ||
| isCaseSensitive) | ||
| if (runtimeFilters.nonEmpty) { | ||
| logInfo(s"Received ${runtimeFilters.length} runtime partition filter(s) for " + | ||
| s"${catalogTable.identifier}") | ||
| logDebug(s"Runtime partition filter(s) for ${catalogTable.identifier}: " + | ||
| s"${runtimeFilters.mkString(", ")}") | ||
| } | ||
| } | ||
|
|
||
| override def planInputPartitions(): Array[InputPartition] = { | ||
| if (runtimeFilters.isEmpty) { | ||
| inner.planInputPartitions() | ||
| } else { | ||
| // Delegate planning to a sibling OrcScan carrying the merged | ||
| // partitionFilters ++ runtimeFilters so DPP predicates take effect. | ||
| val sibling = OrcScan( | ||
| sparkSession, | ||
| hadoopConf, | ||
| fileIndex, | ||
| dataSchema, | ||
| readDataSchema, | ||
| readPartitionSchema, | ||
| options, | ||
| pushedAggregate, | ||
| pushedFilters, | ||
| partitionFilters ++ runtimeFilters, | ||
| dataFilters) | ||
| sibling.planInputPartitions() | ||
| } | ||
| } | ||
|
|
||
| override def isSplitable(path: Path): Boolean = inner.isSplitable(path) | ||
|
|
||
| override def readSchema(): StructType = inner.readSchema() | ||
|
|
||
| override def getMetaData(): Map[String, String] = inner.getMetaData() | ||
|
|
||
| override def createReaderFactory(): PartitionReaderFactory = inner.createReaderFactory() | ||
|
|
||
| override def equals(obj: Any): Boolean = obj match { | ||
| case that: KyuubiOrcScan => this.inner.equals(that.inner) | ||
| case _ => false | ||
| } | ||
|
|
||
| override def hashCode(): Int = getClass.hashCode() | ||
| } |
126 changes: 126 additions & 0 deletions
126
...ive/src/main/scala/org/apache/kyuubi/spark/connector/hive/read/KyuubiOrcScanBuilder.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,126 @@ | ||
| /* | ||
| * 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 scala.collection.JavaConverters._ | ||
|
|
||
| import org.apache.hadoop.conf.Configuration | ||
| import org.apache.spark.sql.SparkSession | ||
| import org.apache.spark.sql.catalyst.catalog.CatalogTable | ||
| import org.apache.spark.sql.connector.expressions.aggregate.Aggregation | ||
| import org.apache.spark.sql.connector.read.SupportsPushDownAggregates | ||
| import org.apache.spark.sql.execution.datasources.{AggregatePushDownUtils, PartitioningAwareFileIndex} | ||
| import org.apache.spark.sql.execution.datasources.v2.FileScanBuilder | ||
| import org.apache.spark.sql.hive.kyuubi.connector.HiveBridgeHelper | ||
| import org.apache.spark.sql.internal.SQLConf | ||
| import org.apache.spark.sql.sources.Filter | ||
| import org.apache.spark.sql.types.StructType | ||
| import org.apache.spark.sql.util.CaseInsensitiveStringMap | ||
|
|
||
| import org.apache.kyuubi.spark.connector.hive.HiveTableCatalog | ||
|
|
||
| /** | ||
| * A ScanBuilder that mirrors Spark's built-in [[OrcScanBuilder]] but builds | ||
| * [[KyuubiOrcScan]] instances, which additionally implement | ||
| * `SupportsRuntimeFiltering` so that Dynamic Partition Pruning works when | ||
| * a Hive ORC table goes through Spark's vectorized ORC reader path. | ||
| * | ||
| * Filter, aggregate and column pushdown behaviour matches [[OrcScanBuilder]]. | ||
| */ | ||
| class KyuubiOrcScanBuilder( | ||
| sparkSession: SparkSession, | ||
| fileIndex: PartitioningAwareFileIndex, | ||
| schema: StructType, | ||
| dataSchema: StructType, | ||
| options: CaseInsensitiveStringMap, | ||
| catalogTable: CatalogTable, | ||
| hiveTableCatalog: HiveTableCatalog) | ||
| extends FileScanBuilder(sparkSession, fileIndex, dataSchema) | ||
| with SupportsPushDownAggregates { | ||
|
|
||
| /** | ||
| * Cloned from a freshly-built per-catalog Hadoop [[Configuration]] so | ||
| * per-catalog settings and mid-session confs are both honored, matching | ||
| * Spark's built-in `OrcScanBuilder.hadoopConf`. Cloned so per-scan | ||
| * `options` do not pollute the source instance. | ||
| */ | ||
| lazy val hadoopConf: Configuration = { | ||
| val conf = new Configuration(hiveTableCatalog.newScanHadoopConf()) | ||
| // Hadoop Configurations are case sensitive. | ||
| options.asCaseSensitiveMap.asScala.foreach { case (k, v) => conf.set(k, v) } | ||
| conf | ||
| } | ||
|
|
||
| private var finalSchema = new StructType() | ||
|
|
||
| private var pushedAggregations = Option.empty[Aggregation] | ||
|
|
||
| override protected val supportsNestedSchemaPruning: Boolean = true | ||
|
|
||
| override def build(): KyuubiOrcScan = { | ||
| // the `finalSchema` is either pruned in pushAggregation (if aggregates are | ||
| // pushed down), or pruned in readDataSchema() (in regular column pruning). These | ||
| // two are mutual exclusive. | ||
| if (pushedAggregations.isEmpty) { | ||
| finalSchema = readDataSchema() | ||
| } | ||
| new KyuubiOrcScan( | ||
| sparkSession, | ||
| hadoopConf, | ||
| fileIndex, | ||
| dataSchema, | ||
| finalSchema, | ||
| readPartitionSchema(), | ||
| options, | ||
| pushedAggregations, | ||
| pushedDataFilters, | ||
| partitionFilters, | ||
| dataFilters, | ||
| catalogTable) | ||
| } | ||
|
|
||
| override def pushDataFilters(dataFilters: Array[Filter]): Array[Filter] = { | ||
| if (sparkSession.sessionState.conf.orcFilterPushDown) { | ||
| HiveBridgeHelper.orcConvertibleFilters( | ||
| readDataSchema(), | ||
| SQLConf.get.caseSensitiveAnalysis, | ||
| dataFilters.toSeq).toArray | ||
| } else { | ||
| Array.empty[Filter] | ||
| } | ||
| } | ||
|
|
||
| override def pushAggregation(aggregation: Aggregation): Boolean = { | ||
| if (!sparkSession.sessionState.conf.orcAggregatePushDown) { | ||
| return false | ||
| } | ||
|
|
||
| AggregatePushDownUtils.getSchemaForPushedAggregation( | ||
| aggregation, | ||
| schema, | ||
| partitionNameSet, | ||
| dataFilters) match { | ||
|
|
||
| case Some(schema) => | ||
| finalSchema = schema | ||
| this.pushedAggregations = Some(aggregation) | ||
| true | ||
| case _ => false | ||
| } | ||
| } | ||
| } |
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.
HIGH · ecosystem compatibility
KyuubiParquetScan/KyuubiOrcScanextendFileScandirectly rather than subclassingParquetScan/OrcScan, so native engines that identify the file format by scan type stop recognizing them. Gluten'sgetSubstraitReadFileFormatV2is agetClass.getSimpleNamestring match; it now falls through toUnknownFormat, validation fails, and the plan silently keeps the vanillaBatchScanExec. Comet'sisInstanceOf[ParquetScan]behaves the same way. This path did work before: Gluten's only structural gate isscan.isInstanceOf[FileScan]and it reads the FileIndex generically, so a converted KSHC table was genuinely offloadable and no longer is.Gluten's check is a string match, so subclassing would not save it either, and
ParquetScanbeing a case class makes wrapping the right call in my view. So the ask is not to change the implementation but to record the tradeoff: note in the PR description and release notes that KSHC Parquet/ORC tables fall back to JVM reads under Gluten/Comet. If you want an escape hatch it would have to be a new config, since turning offconvertMetastoreParquettoday routes toHiveScanBuilderrather than to the plain scan.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.
Documented the tradeoff both in the source (class-level scaladoc of
KyuubiParquetScan/KyuubiOrcScan, point 3) and added a paragraph to the PR description: KSHC-converted Parquet/ORC tables fall back to JVM reads under Gluten/Comet. A dedicated escape-hatch config can follow up if a real user hits this.