-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-53840][SQL] Add AS JSON output support for SHOW TABLES and SHOW TABLE EXTENDED #54824
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
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5ba55f8
[SPARK-53840][SQL] Add AS JSON output support for SHOW TABLES and SHO…
ayushbilala d8ea2eb
[SPARK-53840][SQL] Refactor SHOW TABLES AS JSON to use unified ShowTa…
ayushbilala 99aec51
[SPARK-53840][SQL] Address review: use granular tableType, revert cos…
ayushbilala 543033a
[SPARK-53840][SQL] Fix V2 temp-view test failures, rename parity test
ayushbilala 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
151 changes: 151 additions & 0 deletions
151
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ShowTablesJsonCommand.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,151 @@ | ||
| /* | ||
| * 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 scala.collection.mutable.ArrayBuffer | ||
|
|
||
| import org.json4s._ | ||
| import org.json4s.jackson.JsonMethods._ | ||
|
|
||
| import org.apache.spark.SparkException | ||
| import org.apache.spark.sql.{Row, SparkSession} | ||
| import org.apache.spark.sql.catalyst.analysis.ResolvedNamespace | ||
| import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference} | ||
| import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan | ||
| import org.apache.spark.sql.catalyst.util.StringUtils | ||
| import org.apache.spark.sql.connector.catalog.{CatalogV2Util, TableCatalog, TableViewCatalog} | ||
| import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ | ||
| import org.apache.spark.sql.types.{MetadataBuilder, StringType} | ||
|
|
||
| /** | ||
| * The command for `SHOW TABLES AS JSON` and `SHOW TABLE EXTENDED AS JSON`. | ||
| */ | ||
| case class ShowTablesJsonCommand( | ||
| child: LogicalPlan, | ||
| pattern: Option[String], | ||
| isExtended: Boolean, | ||
| override val output: Seq[Attribute] = Seq( | ||
| AttributeReference( | ||
| "json_metadata", | ||
| StringType, | ||
| nullable = false, | ||
| new MetadataBuilder() | ||
| .putString("comment", "JSON metadata of the tables") | ||
| .build())() | ||
| )) extends UnaryRunnableCommand { | ||
|
|
||
| override def run(sparkSession: SparkSession): Seq[Row] = { | ||
| val jsonOutput = child match { | ||
| case ResolvedNamespace(catalog, ns, _) => | ||
| if (CatalogV2Util.isSessionCatalog(catalog)) { | ||
| runForSessionCatalog(sparkSession, ns) | ||
| } else { | ||
| runForV2Catalog(sparkSession, catalog.asTableCatalog, ns) | ||
| } | ||
| case other => | ||
| throw SparkException.internalError( | ||
| s"Unexpected child in ShowTablesJsonCommand: ${other.getClass.getSimpleName}") | ||
| } | ||
| Seq(Row(compact(render(jsonOutput)))) | ||
| } | ||
|
|
||
| private def runForSessionCatalog( | ||
| sparkSession: SparkSession, | ||
| ns: Seq[String]): JObject = { | ||
| val sessionCatalog = sparkSession.sessionState.catalog | ||
| val db = ns.headOption.getOrElse(sessionCatalog.getCurrentDatabase) | ||
| val tables = pattern | ||
| .map(p => sessionCatalog.listTables(db, p)) | ||
| .getOrElse(sessionCatalog.listTables(db)) | ||
|
|
||
| val jsonTables = tables.map { tableIdent => | ||
| val isTemp = sessionCatalog.isTempView(tableIdent) | ||
| val namespace = tableIdent.database.toList | ||
|
|
||
| if (isExtended) { | ||
| val tableType = if (isTemp) { | ||
| "VIEW" | ||
| } else { | ||
| sessionCatalog.getTempViewOrPermanentTableMetadata(tableIdent).tableType.name | ||
| } | ||
| JObject( | ||
| "name" -> JString(tableIdent.table), | ||
| "catalog" -> JString( | ||
| sparkSession.sessionState.catalogManager.v2SessionCatalog.name()), | ||
| "namespace" -> JArray(namespace.map(JString(_))), | ||
| "type" -> JString(tableType), | ||
| "isTemporary" -> JBool(isTemp) | ||
| ) | ||
| } else { | ||
| JObject( | ||
| "name" -> JString(tableIdent.table), | ||
| "namespace" -> JArray(namespace.map(JString(_))), | ||
| "isTemporary" -> JBool(isTemp) | ||
| ) | ||
| } | ||
| }.toList | ||
|
|
||
| JObject("tables" -> JArray(jsonTables)) | ||
| } | ||
|
|
||
| private def runForV2Catalog( | ||
| sparkSession: SparkSession, | ||
| catalog: TableCatalog, | ||
| ns: Seq[String]): JObject = { | ||
| val identifiers = if (!isExtended) { | ||
| catalog match { | ||
| case mc: TableViewCatalog => | ||
| mc.listTableAndViewSummaries(ns.toArray).map(_.identifier()) | ||
| case _ => catalog.listTables(ns.toArray) | ||
| } | ||
| } else { | ||
| catalog.listTables(ns.toArray) | ||
| } | ||
|
|
||
| val pat = pattern.getOrElse("*") | ||
| val filteredIdents = identifiers.filter { ident => | ||
| StringUtils.filterPattern(Seq(ident.name()), pat).nonEmpty | ||
| } | ||
|
|
||
| val jsonRows = new ArrayBuffer[JObject]() | ||
| filteredIdents.foreach { ident => | ||
| val nsArray = JArray(ident.namespace().map(JString(_)).toList) | ||
| val entry = if (isExtended) { | ||
| JObject( | ||
| "name" -> JString(ident.name()), | ||
| "catalog" -> JString(catalog.name()), | ||
| "namespace" -> nsArray, | ||
| "type" -> JString("TABLE"), | ||
| "isTemporary" -> JBool(false) | ||
| ) | ||
| } else { | ||
| JObject( | ||
| "name" -> JString(ident.name()), | ||
| "namespace" -> nsArray, | ||
| "isTemporary" -> JBool(false) | ||
| ) | ||
| } | ||
| jsonRows += entry | ||
| } | ||
|
|
||
| JObject("tables" -> JArray(jsonRows.toList)) | ||
| } | ||
|
|
||
| override protected def withNewChildInternal(newChild: LogicalPlan): LogicalPlan = | ||
| copy(child = newChild) | ||
| } | ||
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.
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.
(non-blocking) This
matchhandles onlyResolvedNamespace, so any other child would surface as a crypticscala.MatchError. It's unreachable today — the analyzer always resolves the namespace child toResolvedNamespace— but the analogue this command is modeled on,DescribeRelationJsonCommand, uses an explicitcase _ => throw …. Consider an explicitcase _(internal error) so the failure is clear if that invariant ever changes.