-
Notifications
You must be signed in to change notification settings - Fork 232
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
Dynamically load hive and avro using reflection to avoid potential class not found exception [databricks] #5723
Merged
res-life
merged 7 commits into
NVIDIA:branch-22.08
from
res-life:fix-compile-time-reference
Jun 24, 2022
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c89b407
Dynamically load hive and avro using reflection to avoid potential cl…
3d370c1
Fix comments
c5cbe24
Merge branch-22.08
3ead25f
Add AvroProvider and HiveProvider to unshim list
54ef1a7
Refactor code according to comments
a795d58
Sort unshimmed items
156a04a
Sort unshimmed items
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 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 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
119 changes: 119 additions & 0 deletions
119
sql-plugin/src/main/scala/org/apache/spark/sql/hive/rapids/HiveProviderImpl.scala
This file contains 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,119 @@ | ||
/* | ||
* Copyright (c) 2022, NVIDIA CORPORATION. | ||
* | ||
* Licensed 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.hive.rapids | ||
|
||
import com.nvidia.spark.RapidsUDF | ||
import com.nvidia.spark.rapids.{ExprChecks, ExprMeta, ExprRule, GpuExpression, GpuOverrides, RapidsConf, RepeatingParamCheck, TypeSig} | ||
import com.nvidia.spark.rapids.GpuUserDefinedFunction.udfTypeSig | ||
|
||
import org.apache.spark.sql.catalyst.expressions.Expression | ||
import org.apache.spark.sql.hive.{HiveGenericUDF, HiveSimpleUDF} | ||
|
||
class HiveProviderImpl extends HiveProvider { | ||
|
||
/** | ||
* Builds the rules that are specific to spark-hive Catalyst nodes. This will return an empty | ||
* mapping if spark-hive is unavailable. | ||
*/ | ||
override def getExprs: Map[Class[_ <: Expression], ExprRule[_ <: Expression]] = { | ||
Seq( | ||
GpuOverrides.expr[HiveSimpleUDF]( | ||
"Hive UDF, the UDF can choose to implement a RAPIDS accelerated interface to" + | ||
" get better performance", | ||
ExprChecks.projectOnly( | ||
udfTypeSig, | ||
TypeSig.all, | ||
repeatingParamCheck = Some(RepeatingParamCheck("param", udfTypeSig, TypeSig.all))), | ||
(a, conf, p, r) => new ExprMeta[HiveSimpleUDF](a, conf, p, r) { | ||
private val opRapidsFunc = a.function match { | ||
case rapidsUDF: RapidsUDF => Some(rapidsUDF) | ||
case _ => None | ||
} | ||
|
||
override def tagExprForGpu(): Unit = { | ||
if (opRapidsFunc.isEmpty && !conf.isCpuBasedUDFEnabled) { | ||
willNotWorkOnGpu(s"Hive SimpleUDF ${a.name} implemented by " + | ||
s"${a.funcWrapper.functionClassName} does not provide a GPU implementation " + | ||
s"and CPU-based UDFs are not enabled by `${RapidsConf.ENABLE_CPU_BASED_UDF.key}`") | ||
} | ||
} | ||
|
||
override def convertToGpu(): GpuExpression = { | ||
opRapidsFunc.map { _ => | ||
// We use the original HiveGenericUDF `deterministic` method as a proxy | ||
// for simplicity. | ||
GpuHiveSimpleUDF( | ||
a.name, | ||
a.funcWrapper, | ||
childExprs.map(_.convertToGpu()), | ||
a.dataType, | ||
a.deterministic) | ||
}.getOrElse { | ||
// This `require` is just for double check. | ||
require(conf.isCpuBasedUDFEnabled) | ||
GpuRowBasedHiveSimpleUDF( | ||
a.name, | ||
a.funcWrapper, | ||
childExprs.map(_.convertToGpu())) | ||
} | ||
} | ||
}), | ||
GpuOverrides.expr[HiveGenericUDF]( | ||
"Hive Generic UDF, the UDF can choose to implement a RAPIDS accelerated interface to" + | ||
" get better performance", | ||
ExprChecks.projectOnly( | ||
udfTypeSig, | ||
TypeSig.all, | ||
repeatingParamCheck = Some(RepeatingParamCheck("param", udfTypeSig, TypeSig.all))), | ||
(a, conf, p, r) => new ExprMeta[HiveGenericUDF](a, conf, p, r) { | ||
private val opRapidsFunc = a.function match { | ||
case rapidsUDF: RapidsUDF => Some(rapidsUDF) | ||
case _ => None | ||
} | ||
|
||
override def tagExprForGpu(): Unit = { | ||
if (opRapidsFunc.isEmpty && !conf.isCpuBasedUDFEnabled) { | ||
willNotWorkOnGpu(s"Hive GenericUDF ${a.name} implemented by " + | ||
s"${a.funcWrapper.functionClassName} does not provide a GPU implementation " + | ||
s"and CPU-based UDFs are not enabled by `${RapidsConf.ENABLE_CPU_BASED_UDF.key}`") | ||
} | ||
} | ||
|
||
override def convertToGpu(): GpuExpression = { | ||
opRapidsFunc.map { _ => | ||
// We use the original HiveGenericUDF `deterministic` method as a proxy | ||
// for simplicity. | ||
GpuHiveGenericUDF( | ||
a.name, | ||
a.funcWrapper, | ||
childExprs.map(_.convertToGpu()), | ||
a.dataType, | ||
a.deterministic, | ||
a.foldable) | ||
}.getOrElse { | ||
// This `require` is just for double check. | ||
require(conf.isCpuBasedUDFEnabled) | ||
GpuRowBasedHiveGenericUDF( | ||
a.name, | ||
a.funcWrapper, | ||
childExprs.map(_.convertToGpu())) | ||
} | ||
} | ||
}) | ||
).map(r => (r.getClassFor.asSubclass(classOf[Expression]), r)).toMap | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
sql-plugin/src/main/scala/org/apache/spark/sql/rapids/AvroProviderImpl.scala
This file contains 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,116 @@ | ||
/* | ||
* Copyright (c) 2022, NVIDIA CORPORATION. | ||
* | ||
* Licensed 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.rapids | ||
|
||
import com.nvidia.spark.rapids._ | ||
|
||
import org.apache.spark.broadcast.Broadcast | ||
import org.apache.spark.sql.avro.{AvroFileFormat, AvroOptions} | ||
import org.apache.spark.sql.connector.read.{PartitionReaderFactory, Scan} | ||
import org.apache.spark.sql.execution.FileSourceScanExec | ||
import org.apache.spark.sql.execution.datasources.FileFormat | ||
import org.apache.spark.sql.sources.Filter | ||
import org.apache.spark.sql.v2.avro.AvroScan | ||
import org.apache.spark.util.SerializableConfiguration | ||
|
||
class AvroProviderImpl extends AvroProvider { | ||
|
||
/** If the file format is supported as an external source */ | ||
def isSupportedFormat(format: FileFormat): Boolean = { | ||
format match { | ||
case _: AvroFileFormat => true | ||
case _ => false | ||
} | ||
} | ||
|
||
def isPerFileReadEnabledForFormat(format: FileFormat, conf: RapidsConf): Boolean = { | ||
format match { | ||
case _: AvroFileFormat => conf.isAvroPerFileReadEnabled | ||
case _ => false | ||
} | ||
} | ||
|
||
def tagSupportForGpuFileSourceScan(meta: SparkPlanMeta[FileSourceScanExec]): Unit = { | ||
meta.wrapped.relation.fileFormat match { | ||
case _: AvroFileFormat => GpuReadAvroFileFormat.tagSupport(meta) | ||
case f => | ||
meta.willNotWorkOnGpu(s"unsupported file format: ${f.getClass.getCanonicalName}") | ||
} | ||
} | ||
|
||
/** | ||
* Get a read file format for the input format. | ||
* Better to check if the format is supported first by calling 'isSupportedFormat' | ||
*/ | ||
def getReadFileFormat(format: FileFormat): FileFormat = { | ||
format match { | ||
case _: AvroFileFormat => new GpuReadAvroFileFormat | ||
case f => | ||
throw new IllegalArgumentException(s"${f.getClass.getCanonicalName} is not supported") | ||
} | ||
} | ||
|
||
/** | ||
* Create a multi-file reader factory for the input format. | ||
* Better to check if the format is supported first by calling 'isSupportedFormat' | ||
*/ | ||
def createMultiFileReaderFactory( | ||
format: FileFormat, | ||
broadcastedConf: Broadcast[SerializableConfiguration], | ||
pushedFilters: Array[Filter], | ||
fileScan: GpuFileSourceScanExec): PartitionReaderFactory = { | ||
format match { | ||
case _: AvroFileFormat => | ||
GpuAvroMultiFilePartitionReaderFactory( | ||
fileScan.relation.sparkSession.sessionState.conf, | ||
fileScan.rapidsConf, | ||
broadcastedConf, | ||
fileScan.relation.dataSchema, | ||
fileScan.requiredSchema, | ||
fileScan.relation.partitionSchema, | ||
new AvroOptions(fileScan.relation.options, broadcastedConf.value.value), | ||
fileScan.allMetrics, | ||
pushedFilters, | ||
fileScan.queryUsesInputFile) | ||
case _ => | ||
// never reach here | ||
throw new RuntimeException(s"File format $format is not supported yet") | ||
} | ||
} | ||
|
||
def getScans: Map[Class[_ <: Scan], ScanRule[_ <: Scan]] = { | ||
Seq( | ||
GpuOverrides.scan[AvroScan]( | ||
"Avro parsing", | ||
(a, conf, p, r) => new ScanMeta[AvroScan](a, conf, p, r) { | ||
override def tagSelfForGpu(): Unit = GpuAvroScan.tagSupport(this) | ||
|
||
override def convertToGpu(): Scan = | ||
GpuAvroScan(a.sparkSession, | ||
a.fileIndex, | ||
a.dataSchema, | ||
a.readDataSchema, | ||
a.readPartitionSchema, | ||
a.options, | ||
a.pushedFilters, | ||
conf, | ||
a.partitionFilters, | ||
a.dataFilters) | ||
}) | ||
).map(r => (r.getClassFor.asSubclass(classOf[Scan]), r)).toMap | ||
} | ||
} |
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.
I'd rather have only methods in these trait, and not introduce any constraints, and let the caller decide how to use it. Can we change this to:
for consistency.
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.
Done