-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-56520][SQL] Persist SQL PATH in views and SQL functions, expose in DESCRIBE #55383
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 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
Some comments aren't visible on the classic Files Changed page.
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
90 changes: 90 additions & 0 deletions
90
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SqlPathFormat.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,90 @@ | ||
| /* | ||
| * 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.catalog | ||
|
|
||
| import scala.util.Try | ||
|
|
||
| import org.json4s.JsonAST.{JArray, JObject, JString, JValue} | ||
| import org.json4s.jackson.JsonMethods.parse | ||
|
|
||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ | ||
|
|
||
| /** | ||
| * Formatting helpers for the SQL Path stored in view and SQL function | ||
| * metadata. The on-disk property stores path entries as a JSON array | ||
| * of arrays: | ||
| * {{{ | ||
| * [["spark_catalog","default"],["system","builtin"]] | ||
| * }}} | ||
| * `toDescribeJson` converts these to the object form used by | ||
| * `DESCRIBE AS JSON`: | ||
| * {{{ | ||
| * {"catalog_name": "spark_catalog", "namespace": ["default"]} | ||
| * }}} | ||
| * This supports multi-level namespaces. | ||
| */ | ||
| private[sql] object SqlPathFormat { | ||
|
|
||
| /** | ||
| * Build a JSON value for DESCRIBE AS JSON from a stored resolution | ||
| * path string (JSON array of arrays persisted in the property). | ||
| */ | ||
| def toDescribeJson(storedPathStr: String): Option[JValue] = { | ||
| Try(parse(storedPathStr)) match { | ||
| case scala.util.Success(JArray(entries)) if entries.nonEmpty => | ||
| val converted = entries.flatMap { | ||
| case JArray(parts) => | ||
| val partStrs = parts.collect { case JString(s) => s } | ||
| if (partStrs.isEmpty) None | ||
| else Some(JObject( | ||
| "catalog_name" -> JString(partStrs.head), | ||
| "namespace" -> JArray( | ||
| partStrs.tail.map(JString).toList))) | ||
| case _ => None | ||
| } | ||
| if (converted.nonEmpty) Some(JArray(converted)) else None | ||
| case _ => None | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Format a JSON path value (array of objects with catalog_name and | ||
| * namespace) as a human-readable string for DESCRIBE EXTENDED. | ||
| * Example: `` `spark_catalog`.`default`, `system`.`builtin` `` | ||
| */ | ||
| def formatForDisplay(jValue: JValue): Option[String] = { | ||
| jValue match { | ||
| case JArray(entries) => | ||
| Some(entries.map { | ||
| case JObject(fields) => | ||
| val m = fields.toMap | ||
| val cat = m.get("catalog_name") | ||
| .map(_.values.toString).getOrElse("") | ||
| val ns = m.get("namespace") match { | ||
| case Some(JArray(parts)) => | ||
| parts.map(_.values.toString) | ||
| case _ => Nil | ||
| } | ||
| val parts = (cat +: ns).filter(_.nonEmpty) | ||
| if (parts.nonEmpty) parts.quoted else "" | ||
| case _ => "" | ||
| }.mkString(", ")) | ||
| case _ => Some(jValue.values.toString) | ||
| } | ||
| } | ||
| } |
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
89 changes: 89 additions & 0 deletions
89
.../src/main/scala/org/apache/spark/sql/execution/command/DescribeFunctionCommandUtils.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,89 @@ | ||
| /* | ||
| * 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.execution.command | ||
|
|
||
| import java.util | ||
|
|
||
| import org.apache.spark.sql.SparkSession | ||
| import org.apache.spark.sql.catalyst.FunctionIdentifier | ||
| import org.apache.spark.sql.catalyst.catalog.{SQLFunction, SqlPathFormat, UserDefinedFunction} | ||
| import org.apache.spark.sql.catalyst.expressions.ExpressionInfo | ||
|
|
||
| /** | ||
| * Helpers for [[DescribeFunctionCommand]] to retrieve and format | ||
| * the frozen SQL PATH stored in SQL function metadata. | ||
| */ | ||
| private[command] object DescribeFunctionCommandUtils { | ||
|
|
||
| /** | ||
| * Returns the frozen SQL PATH persisted for a SQL function, formatted | ||
| * for display. Persistent functions: loads [[CatalogFunction]] metadata | ||
| * from the catalog. Temporary SQL UDFs (not in catalog): falls back to | ||
| * parsing the usage JSON blob produced by [[SQLFunction.toExpressionInfo]]. | ||
| */ | ||
| private[command] def storedResolutionPathString( | ||
| sparkSession: SparkSession, | ||
| identifier: FunctionIdentifier, | ||
| info: ExpressionInfo): Option[String] = { | ||
| val rawJson = try { | ||
| val meta = sparkSession.sessionState.catalog | ||
| .getFunctionMetadata(identifier) | ||
| if (meta.isUserDefinedFunction) { | ||
| val udf = UserDefinedFunction.fromCatalogFunction( | ||
| meta, | ||
| sparkSession.sessionState.sqlParser) | ||
| udf.asInstanceOf[SQLFunction].functionStoredResolutionPath | ||
| } else { | ||
| None | ||
| } | ||
| } catch { | ||
| case _: org.apache.spark.sql.catalyst.analysis | ||
| .NoSuchFunctionException | | ||
| _: org.apache.spark.sql.catalyst.analysis | ||
| .NoSuchDatabaseException => | ||
| extractResolutionPathFromSqlUdfUsage(info.getUsage) | ||
| } | ||
| rawJson.flatMap(formatStoredPath) | ||
| } | ||
|
|
||
| private def formatStoredPath(pathStr: String): Option[String] = { | ||
| SqlPathFormat.toDescribeJson(pathStr) | ||
| .flatMap(SqlPathFormat.formatForDisplay) | ||
| } | ||
|
|
||
| /** | ||
| * For temporary SQL UDFs not in the catalog, the resolution path may | ||
| * be embedded in the ExpressionInfo usage JSON blob. Returns None if | ||
| * the usage string is not JSON or does not contain the path key. | ||
| */ | ||
| private def extractResolutionPathFromSqlUdfUsage( | ||
| usage: String): Option[String] = { | ||
| if (usage == null || usage.isEmpty) return None | ||
| try { | ||
| val map = UserDefinedFunction.mapper.readValue( | ||
| usage, classOf[util.HashMap[String, String]]) | ||
| Option(map.get(SQLFunction.FUNCTION_RESOLUTION_PATH)) | ||
| .filter(_.nonEmpty) | ||
| } catch { | ||
| case e: com.fasterxml.jackson.core.JsonProcessingException => | ||
| throw new org.apache.spark.SparkException( | ||
| s"Corrupted SQL UDF metadata: expected JSON usage blob " + | ||
| s"but failed to parse: ${e.getMessage}", e) | ||
|
srielau marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.