-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-54682][SQL] Support showing parameters in DESCRIBE PROCEDURE #53437
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
allisonwang-db
wants to merge
1
commit into
apache:master
Choose a base branch
from
allisonwang-db:desc-procedure
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.
+79
−32
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,16 +19,13 @@ package org.apache.spark.sql.execution.command | |
|
|
||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
| import org.apache.spark.{SparkException, SparkThrowable} | ||
| import org.apache.spark.SparkException | ||
| import org.apache.spark.sql.{Row, SparkSession} | ||
| import org.apache.spark.sql.catalyst.analysis.ResolvedIdentifier | ||
| import org.apache.spark.sql.catalyst.analysis.ResolvedProcedure | ||
| import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference} | ||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.connector.catalog.{Identifier, ProcedureCatalog} | ||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ | ||
| import org.apache.spark.sql.connector.catalog.procedures.UnboundProcedure | ||
| import org.apache.spark.sql.errors.QueryCompilationErrors | ||
| import org.apache.spark.sql.types.StringType | ||
| import org.apache.spark.sql.connector.catalog.procedures.{ProcedureParameter, UnboundProcedure} | ||
| import org.apache.spark.sql.types.{StringType, StructType} | ||
|
|
||
| /** | ||
| * A command for users to describe a procedure. | ||
|
|
@@ -45,34 +42,63 @@ case class DescribeProcedureCommand( | |
|
|
||
| override def run(sparkSession: SparkSession): Seq[Row] = { | ||
| child match { | ||
| case ResolvedIdentifier(catalog, ident) => | ||
| val procedure = load(catalog.asProcedureCatalog, ident) | ||
| describeV2Procedure(procedure) | ||
| case ResolvedProcedure(catalog, ident, procedure) => | ||
| describeV2Procedure(procedure.asInstanceOf[UnboundProcedure]) | ||
| case _ => | ||
| throw SparkException.internalError(s"Invalid procedure identifier: ${child.getClass}") | ||
| } | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
| } | ||
|
|
||
| private def describeV2Procedure(procedure: UnboundProcedure): Seq[Row] = { | ||
| val buffer = new ArrayBuffer[(String, String)] | ||
| append(buffer, "Procedure:", procedure.name()) | ||
| append(buffer, "Description:", procedure.description()) | ||
|
|
||
| // UnboundProcedure requires binding to retrieve parameters. We try to bind with an empty | ||
| // argument list to get the parameters. If the procedure requires arguments, binding might | ||
| // fail. In that case, we suppress the exception and just show the procedure metadata | ||
| // without parameters. | ||
|
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. I'm afraid this is a common case, not rare |
||
| try { | ||
| val bound = procedure.bind(new StructType()) | ||
| val params = bound.parameters() | ||
| if (params != null && params.nonEmpty) { | ||
| val formattedParams = formatProcedureParameters(params) | ||
| append(buffer, "Parameters:", formattedParams.head) | ||
| formattedParams.tail.foreach(s => append(buffer, "", s)) | ||
| } else { | ||
| append(buffer, "Parameters:", "()") | ||
| } | ||
| } catch { | ||
| case _: Exception => | ||
| // Ignore if binding fails | ||
| } | ||
|
|
||
| val keys = tabulate(buffer.map(_._1).toSeq) | ||
| val values = buffer.map(_._2) | ||
| keys.zip(values).map { case (key, value) => Row(s"$key $value") } | ||
| } | ||
|
|
||
| // This helper is needed because the V2 Procedure API returns an array of ProcedureParameter, | ||
| // which differs from the StructType used by internal stored procedures (handled by | ||
| // formatParameters). | ||
| private def formatProcedureParameters(params: Array[ProcedureParameter]): Seq[String] = { | ||
| val modes = tabulate(params.map(_.mode().toString).toSeq) | ||
|
Contributor
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. The impl here is really awkward, can we iterate |
||
| val names = tabulate(params.map(_.name()).toSeq) | ||
| val dataTypes = tabulate(params.map(_.dataType().sql).toSeq) | ||
| val comments = params.map { p => | ||
| if (p.comment() != null) s" '${p.comment()}'" else "" | ||
| } | ||
| val defaults = params.map { p => | ||
| val defaultVal = if (p.defaultValue() != null) p.defaultValue().getSql else null | ||
| if (defaultVal != null) s" DEFAULT $defaultVal" else "" | ||
| } | ||
| modes zip names zip dataTypes zip defaults zip comments map { | ||
| case ((((mode, name), dataType), default), comment) => | ||
| s"$mode $name $dataType$default$comment" | ||
| } | ||
| } | ||
|
|
||
| private def append(buffer: ArrayBuffer[(String, String)], key: String, value: String): Unit = { | ||
| buffer += (key -> value) | ||
| } | ||
|
|
||
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
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.
does
catalog.asProcedureCatalogdo this check?