-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-18127] Add hooks and extension points to Spark #17724
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 2 commits
Commits
Show all changes
3 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
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
sql/core/src/main/scala/org/apache/spark/sql/SparkSessionExtensions.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.spark.sql | ||
|
|
||
| import scala.collection.mutable | ||
|
|
||
| import org.apache.spark.annotation.{DeveloperApi, Experimental, InterfaceStability} | ||
| import org.apache.spark.sql.catalyst.parser.ParserInterface | ||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.rules.Rule | ||
|
|
||
| /** | ||
| * :: Experimental :: | ||
| * Holder for injection points to the [[SparkSession]]. We make NO guarantee about the stability | ||
| * regarding binary compatibility and source compatibility of methods here. | ||
| * | ||
| * This current provides the following extension points: | ||
| * - Analyzer Rules. | ||
| * - Check Analysis Rules | ||
| * - Optimizer Rules. | ||
| * - Planning Strategies. | ||
| * - Customized Parser. | ||
| * - (External) Catalog listeners. | ||
| * | ||
| * The extensions can be used by calling withExtension on the [[SparkSession.Builder]], for | ||
| * example: | ||
| * {{{ | ||
| * SparkSession.builder() | ||
| * .master("...") | ||
| * .conf("...", true) | ||
| * .withExtensions { extensions => | ||
| * extensions.injectAnalyzerRule { session => | ||
|
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.
|
||
| * ... | ||
| * } | ||
| * extensions.injectParser { (session, parser) => | ||
| * ... | ||
| * } | ||
| * } | ||
| * .getOrCreate() | ||
| * }}} | ||
| * | ||
| * Note that none of the injected builders should assume that the [[SparkSession]] is fully | ||
| * initialized and should not touch the session's internals (e.g. the SessionState). | ||
| */ | ||
| @DeveloperApi | ||
| @Experimental | ||
| @InterfaceStability.Unstable | ||
| class SparkSessionExtensions { | ||
| type RuleBuilder = SparkSession => Rule[LogicalPlan] | ||
| type CheckRuleBuilder = SparkSession => LogicalPlan => Unit | ||
| type StrategyBuilder = SparkSession => Strategy | ||
| type ParserBuilder = (SparkSession, ParserInterface) => ParserInterface | ||
|
|
||
| private[this] val resolutionRuleBuilders = mutable.Buffer.empty[RuleBuilder] | ||
|
|
||
| /** | ||
| * Build the analyzer resolution `Rule`s using the given [[SparkSession]]. | ||
| */ | ||
| private[sql] def buildResolutionRules(session: SparkSession): Seq[Rule[LogicalPlan]] = { | ||
| resolutionRuleBuilders.map(_.apply(session)) | ||
| } | ||
|
|
||
| /** | ||
| * Inject an analyzer resolution `Rule` builder into the [[SparkSession]]. These analyzer | ||
| * rules will be executed as part of the resolution phase of analysis. | ||
| */ | ||
| def injectResolutionRule(builder: RuleBuilder): Unit = { | ||
| resolutionRuleBuilders += builder | ||
| } | ||
|
|
||
| private[this] val postHocResolutionRuleBuilders = mutable.Buffer.empty[RuleBuilder] | ||
|
|
||
| /** | ||
| * Build the analyzer post-hoc resolution `Rule`s using the given [[SparkSession]]. | ||
| */ | ||
| private[sql] def buildPostHocResolutionRules(session: SparkSession): Seq[Rule[LogicalPlan]] = { | ||
| postHocResolutionRuleBuilders.map(_.apply(session)) | ||
| } | ||
|
|
||
| /** | ||
| * Inject an analyzer `Rule` builder into the [[SparkSession]]. These analyzer | ||
| * rules will be executed after resolution. | ||
| */ | ||
| def injectPostHocResolutionRule(builder: RuleBuilder): Unit = { | ||
| postHocResolutionRuleBuilders += builder | ||
| } | ||
|
|
||
| private[this] val checkRuleBuilders = mutable.Buffer.empty[CheckRuleBuilder] | ||
|
|
||
| /** | ||
| * Build the check analysis `Rule`s using the given [[SparkSession]]. | ||
| */ | ||
| private[sql] def buildCheckRules(session: SparkSession): Seq[LogicalPlan => Unit] = { | ||
| checkRuleBuilders.map(_.apply(session)) | ||
| } | ||
|
|
||
| /** | ||
| * Inject an check analysis `Rule` builder into the [[SparkSession]]. The injected rules will | ||
| * be executed after the analysis phase. A check analysis rule is used to detect problems with a | ||
| * LogicalPlan and should throw an exception when a problem is found. | ||
| */ | ||
| def injectCheckRule(builder: CheckRuleBuilder): Unit = { | ||
| checkRuleBuilders += builder | ||
| } | ||
|
|
||
| private[this] val optimizerRules = mutable.Buffer.empty[RuleBuilder] | ||
|
|
||
| private[sql] def buildOptimizerRules(session: SparkSession): Seq[Rule[LogicalPlan]] = { | ||
| optimizerRules.map(_.apply(session)) | ||
| } | ||
|
|
||
| /** | ||
| * Inject an optimizer `Rule` builder into the [[SparkSession]]. The injected rules will be | ||
| * executed during the operator optimization batch. An optimizer rule is used to improve the | ||
| * quality of an analyzed logical plan; these rules should never modify the result of the | ||
| * LogicalPlan. | ||
| */ | ||
| def injectOptimizerRule(builder: RuleBuilder): Unit = { | ||
| optimizerRules += builder | ||
| } | ||
|
|
||
| private[this] val plannerStrategyBuilders = mutable.Buffer.empty[StrategyBuilder] | ||
|
|
||
| private[sql] def buildPlannerStrategies(session: SparkSession): Seq[Strategy] = { | ||
| plannerStrategyBuilders.map(_.apply(session)) | ||
| } | ||
|
|
||
| /** | ||
| * Inject a planner `Strategy` builder into the [[SparkSession]]. The injected strategy will | ||
| * be used to convert a `LogicalPlan` into a executable | ||
| * [[org.apache.spark.sql.execution.SparkPlan]]. | ||
| */ | ||
| def injectPlannerStrategy(builder: StrategyBuilder): Unit = { | ||
| plannerStrategyBuilders += builder | ||
| } | ||
|
|
||
| private[this] val parserBuilders = mutable.Buffer.empty[ParserBuilder] | ||
|
|
||
| private[sql] def buildParser( | ||
| session: SparkSession, | ||
| initial: ParserInterface): ParserInterface = { | ||
| parserBuilders.foldLeft(initial) { (parser, builder) => | ||
| builder(session, parser) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Inject a custom parser into the [[SparkSession]]. Note that the builder is passed a session | ||
| * and an initial parser. The latter allows for a user to create a partial parser and to delegate | ||
| * to the underlying parser for completeness. If a user injects more parsers, then the parsers | ||
| * are stacked on top of each other. | ||
| */ | ||
| def injectParser(builder: ParserBuilder): Unit = { | ||
| parserBuilders += builder | ||
| } | ||
| } | ||
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.
In the JIRA, the target version is 2.2. Do we still plan to backport it to 2.2.0?