-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
[SPARK-48782][SQL] Add support for executing procedures in catalogs #47943
Changes from 2 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 |
---|---|---|
|
@@ -26,7 +26,7 @@ import scala.collection.mutable.ArrayBuffer | |
import scala.jdk.CollectionConverters._ | ||
import scala.util.{Failure, Random, Success, Try} | ||
|
||
import org.apache.spark.{SparkException, SparkUnsupportedOperationException} | ||
import org.apache.spark.{SparkException, SparkThrowable, SparkUnsupportedOperationException} | ||
import org.apache.spark.sql.AnalysisException | ||
import org.apache.spark.sql.catalyst._ | ||
import org.apache.spark.sql.catalyst.catalog._ | ||
|
@@ -50,6 +50,7 @@ import org.apache.spark.sql.connector.catalog.{View => _, _} | |
import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ | ||
import org.apache.spark.sql.connector.catalog.TableChange.{After, ColumnPosition} | ||
import org.apache.spark.sql.connector.catalog.functions.{AggregateFunction => V2AggregateFunction, ScalarFunction, UnboundFunction} | ||
import org.apache.spark.sql.connector.catalog.procedures.{BoundProcedure, ProcedureParameter, UnboundProcedure} | ||
import org.apache.spark.sql.connector.expressions.{FieldReference, IdentityTransform} | ||
import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryExecutionErrors} | ||
import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation | ||
|
@@ -310,6 +311,8 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor | |
ExtractGenerator :: | ||
ResolveGenerate :: | ||
ResolveFunctions :: | ||
ResolveProcedures :: | ||
BindProcedures :: | ||
ResolveTableSpec :: | ||
ResolveAliases :: | ||
ResolveSubquery :: | ||
|
@@ -2611,6 +2614,71 @@ class Analyzer(override val catalogManager: CatalogManager) extends RuleExecutor | |
} | ||
} | ||
|
||
/** | ||
* A rule that resolves procedures. | ||
*/ | ||
object ResolveProcedures extends Rule[LogicalPlan] { | ||
def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsWithPruning( | ||
_.containsPattern(UNRESOLVED_PROCEDURE), ruleId) { | ||
case Call(UnresolvedProcedure(CatalogAndIdentifier(catalog, ident)), args, execute) => | ||
val procedureCatalog = catalog.asProcedureCatalog | ||
val procedure = load(procedureCatalog, ident) | ||
Call(ResolvedProcedure(procedureCatalog, ident, procedure), args, execute) | ||
} | ||
|
||
private def load(catalog: ProcedureCatalog, ident: Identifier): UnboundProcedure = { | ||
try { | ||
catalog.loadProcedure(ident) | ||
} catch { | ||
case e: Exception if !e.isInstanceOf[SparkThrowable] => | ||
val nameParts = catalog.name +: ident.asMultipartIdentifier | ||
throw QueryCompilationErrors.failedToLoadRoutineError(nameParts, e) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A rule that binds procedures to the input types and rearranges arguments as needed. | ||
*/ | ||
object BindProcedures extends Rule[LogicalPlan] { | ||
def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators { | ||
case Call(ResolvedProcedure(catalog, ident, unbound: UnboundProcedure), args, execute) | ||
if args.forall(_.resolved) => | ||
val inputType = extractInputType(args) | ||
val bound = unbound.bind(inputType) | ||
validateParameterModes(bound) | ||
val rearrangedArgs = NamedParametersSupport.defaultRearrange(bound, args) | ||
Call(ResolvedProcedure(catalog, ident, bound), rearrangedArgs, execute) | ||
} | ||
|
||
private def extractInputType(args: Seq[Expression]): StructType = { | ||
val fields = args.zipWithIndex.map { case (arg, index) => | ||
arg match { | ||
case NamedArgumentExpression(name, value) => | ||
val metadata = argMetadata(byName = true) | ||
StructField(name, value.dataType, value.nullable, metadata) | ||
case _ => | ||
val name = s"param$index" | ||
val metadata = argMetadata(byName = false) | ||
StructField(name, arg.dataType, arg.nullable, metadata) | ||
} | ||
} | ||
StructType(fields) | ||
} | ||
|
||
private def argMetadata(byName: Boolean): Metadata = { | ||
new MetadataBuilder() | ||
.putBoolean(ProcedureParameter.BY_NAME_METADATA_KEY, byName) | ||
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. shall we omit this metadata if the arg is not by name? 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. We can, skipped. |
||
.build() | ||
} | ||
|
||
private def validateParameterModes(procedure: BoundProcedure): Unit = { | ||
procedure.parameters.find(_.mode != ProcedureParameter.Mode.IN).foreach { param => | ||
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. Are we planning to support more parameter modes? 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. In the future, yes. There is no active work at the moment, as far as I know. |
||
throw SparkException.internalError(s"Unsupported parameter mode: ${param.mode}") | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* This rule resolves and rewrites subqueries inside expressions. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* 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.catalyst.plans.logical | ||
|
||
/** | ||
* A logical plan node that requires execution during analysis. | ||
*/ | ||
trait ExecutableDuringAnalysis extends LogicalPlan { | ||
/** | ||
* Returns the logical plan node that should be used for EXPLAIN. | ||
*/ | ||
def stageForExplain(): LogicalPlan | ||
} |
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 can split this into a separate PR, if needed.