-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[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
base: master
Are you sure you want to change the base?
Changes from all 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 |
|---|---|---|
|
|
@@ -1371,17 +1371,22 @@ case class RenameTable( | |
| case class ShowTables( | ||
| namespace: LogicalPlan, | ||
| pattern: Option[String], | ||
| asJson: Boolean = false, | ||
| override val output: Seq[Attribute] = ShowTables.getOutputAttrs) extends UnaryCommand { | ||
| override def child: LogicalPlan = namespace | ||
| override protected def withNewChildInternal(newChild: LogicalPlan): ShowTables = | ||
| copy(namespace = newChild) | ||
| override protected def stringArgs: Iterator[Any] = Iterator(pattern, output) | ||
|
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 Suggestion: Drop the |
||
| } | ||
|
|
||
| object ShowTables { | ||
| def getOutputAttrs: Seq[Attribute] = Seq( | ||
| AttributeReference("namespace", StringType, nullable = false)(), | ||
| AttributeReference("tableName", StringType, nullable = false)(), | ||
| AttributeReference("isTemporary", BooleanType, nullable = false)()) | ||
|
|
||
| def getJsonOutputAttrs: Seq[Attribute] = Seq( | ||
| AttributeReference("json_metadata", StringType, nullable = false)()) | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1390,10 +1395,12 @@ object ShowTables { | |
| case class ShowTablesExtended( | ||
| namespace: LogicalPlan, | ||
| pattern: String, | ||
| asJson: Boolean = false, | ||
| override val output: Seq[Attribute] = ShowTablesUtils.getOutputAttrs) extends UnaryCommand { | ||
| override def child: LogicalPlan = namespace | ||
| override protected def withNewChildInternal(newChild: LogicalPlan): ShowTablesExtended = | ||
| copy(namespace = newChild) | ||
| override protected def stringArgs: Iterator[Any] = Iterator(pattern, output) | ||
| } | ||
|
|
||
| object ShowTablesUtils { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ import scala.util.control.NonFatal | |
|
|
||
| import org.apache.hadoop.fs.{FileContext, FsConstants, Path} | ||
| import org.apache.hadoop.fs.permission.{AclEntry, AclEntryScope, AclEntryType, FsAction, FsPermission} | ||
| import org.json4s._ | ||
| import org.json4s.jackson.JsonMethods._ | ||
|
|
||
| import org.apache.spark.sql.{Row, SparkSession} | ||
| import org.apache.spark.sql.catalyst.{SQLConfHelper, TableIdentifier} | ||
|
|
@@ -927,9 +929,21 @@ case class ShowTablesCommand( | |
| tableIdentifierPattern: Option[String], | ||
| override val output: Seq[Attribute], | ||
| isExtended: Boolean = false, | ||
| partitionSpec: Option[TablePartitionSpec] = None) extends LeafRunnableCommand { | ||
| partitionSpec: Option[TablePartitionSpec] = None, | ||
| asJson: Boolean = false) extends LeafRunnableCommand { | ||
|
|
||
| override protected def stringArgs: Iterator[Any] = | ||
| Iterator(databaseName, tableIdentifierPattern, output, isExtended, partitionSpec) | ||
|
|
||
| override def run(sparkSession: SparkSession): Seq[Row] = { | ||
| if (asJson) { | ||
| runAsJson(sparkSession) | ||
| } else { | ||
| runAsText(sparkSession) | ||
| } | ||
| } | ||
|
|
||
| private def runAsText(sparkSession: SparkSession): Seq[Row] = { | ||
| // Since we need to return a Seq of rows, we will call getTables directly | ||
| // instead of calling tables in sparkSession. | ||
| val catalog = sparkSession.sessionState.catalog | ||
|
|
@@ -972,6 +986,45 @@ case class ShowTablesCommand( | |
| Seq(Row(database, tableName, isTemp, s"$information\n")) | ||
| } | ||
| } | ||
|
|
||
| private def runAsJson(sparkSession: SparkSession): Seq[Row] = { | ||
|
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.
Suggestion: collapse V1 + V2 into one |
||
| val catalog = sparkSession.sessionState.catalog | ||
| val db = databaseName.getOrElse(catalog.getCurrentDatabase) | ||
| val tables = | ||
| tableIdentifierPattern.map(catalog.listTables(db, _)).getOrElse(catalog.listTables(db)) | ||
|
|
||
| val jsonTables = tables.map { tableIdent => | ||
| val isTemp = catalog.isTempView(tableIdent) | ||
| val ns = tableIdent.database.toList | ||
|
|
||
| if (isExtended) { | ||
| val tableType = if (isTemp) { | ||
| "VIEW" | ||
| } else { | ||
| val meta = catalog.getTempViewOrPermanentTableMetadata(tableIdent) | ||
| if (meta.tableType == CatalogTableType.VIEW) "VIEW" else "TABLE" | ||
| } | ||
|
|
||
| JObject( | ||
| "name" -> JString(tableIdent.table), | ||
| "catalog" -> JString( | ||
| sparkSession.sessionState.catalogManager.v2SessionCatalog.name()), | ||
| "namespace" -> JArray(ns.map(JString(_))), | ||
| "type" -> JString(tableType), | ||
| "isTemporary" -> JBool(isTemp) | ||
| ) | ||
| } else { | ||
| JObject( | ||
| "name" -> JString(tableIdent.table), | ||
| "namespace" -> JArray(ns.map(JString(_))), | ||
| "isTemporary" -> JBool(isTemp) | ||
| ) | ||
| } | ||
| }.toList | ||
|
|
||
| val jsonOutput = JObject("tables" -> JArray(jsonTables)) | ||
| Seq(Row(compact(render(jsonOutput)))) | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * 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.datasources.v2 | ||
|
|
||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
| import org.json4s._ | ||
| import org.json4s.jackson.JsonMethods._ | ||
|
|
||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions.Attribute | ||
| import org.apache.spark.sql.catalyst.util.StringUtils | ||
| import org.apache.spark.sql.connector.catalog.{CatalogV2Util, Identifier, TableCatalog, TableViewCatalog} | ||
| import org.apache.spark.sql.execution.LeafExecNode | ||
|
|
||
| /** | ||
| * Physical plan node for `SHOW TABLES AS JSON` and `SHOW TABLE EXTENDED AS JSON`. | ||
| * | ||
| * For a [[TableViewCatalog]] (non-extended only), listing is done via | ||
| * [[TableViewCatalog#listTableAndViewSummaries]] so that views appear alongside tables, | ||
| * matching the v1 `SHOW TABLES` semantics. | ||
| */ | ||
| case class ShowTablesJsonExec( | ||
| output: Seq[Attribute], | ||
| catalog: TableCatalog, | ||
| namespace: Seq[String], | ||
| pattern: String, | ||
| isExtended: Boolean) extends V2CommandExec with LeafExecNode { | ||
|
|
||
| override protected def run(): Seq[InternalRow] = { | ||
| val identifiers: Array[Identifier] = if (!isExtended) { | ||
| catalog match { | ||
| case mc: TableViewCatalog => | ||
| mc.listTableAndViewSummaries(namespace.toArray).map(_.identifier()) | ||
| case _ => catalog.listTables(namespace.toArray) | ||
| } | ||
| } else { | ||
| catalog.listTables(namespace.toArray) | ||
| } | ||
|
|
||
| val filteredIdents = identifiers.filter { ident => | ||
| StringUtils.filterPattern(Seq(ident.name()), pattern).nonEmpty | ||
| } | ||
|
|
||
| val jsonRows = new ArrayBuffer[JObject]() | ||
| filteredIdents.foreach { ident => | ||
| jsonRows += toJsonEntry(ident.name(), ident.namespace(), isTempView(ident)) | ||
| } | ||
|
|
||
| // For non-session V2 catalogs that don't surface temp views via listTables() or | ||
| // listTableAndViewSummaries(), fetch them separately. For V2SessionCatalog, | ||
| // listTables() already includes local temp views, so we skip this to avoid duplicates. | ||
| // For TableViewCatalog (non-extended path), views come from listTableAndViewSummaries(). | ||
| if (!CatalogV2Util.isSessionCatalog(catalog) && | ||
|
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. This non-session V2 catalog branch
Suggestion: Remove the branch |
||
| (isExtended || !catalog.isInstanceOf[TableViewCatalog])) { | ||
| val sessionCatalog = session.sessionState.catalog | ||
| val db = namespace match { | ||
| case Seq(db) => db | ||
| case _ => "" | ||
| } | ||
| sessionCatalog.listTempViews(db, pattern).foreach { tempView => | ||
| jsonRows += toJsonEntry( | ||
| tempView.identifier.table, | ||
| tempView.identifier.database.toArray, | ||
| isTemporary = true) | ||
| } | ||
| } | ||
|
|
||
| val jsonOutput = JObject("tables" -> JArray(jsonRows.toList)) | ||
| Seq(toCatalystRow(compact(render(jsonOutput)))) | ||
| } | ||
|
|
||
| private def toJsonEntry( | ||
| name: String, | ||
| namespace: Array[String], | ||
| isTemporary: Boolean): JObject = { | ||
| val nsArray = JArray(namespace.map(JString(_)).toList) | ||
| if (isExtended) { | ||
| JObject( | ||
| "name" -> JString(name), | ||
| "catalog" -> JString(catalog.name()), | ||
| "namespace" -> nsArray, | ||
| "type" -> JString(if (isTemporary) "VIEW" else "TABLE"), | ||
| "isTemporary" -> JBool(isTemporary) | ||
| ) | ||
| } else { | ||
| JObject( | ||
| "name" -> JString(name), | ||
| "namespace" -> nsArray, | ||
| "isTemporary" -> JBool(isTemporary) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private def isTempView(ident: Identifier): Boolean = { | ||
| if (CatalogV2Util.isSessionCatalog(catalog)) { | ||
| session.sessionState.catalog.isTempView((ident.namespace() :+ ident.name()).toSeq) | ||
| } else false | ||
| } | ||
| } | ||
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.
Currently have duplicated JSON emission code across Exec classes. Consider a ShowTablesJsonCommand which takes the resolved namespace/catalog and handles all variants centrally, similar to DescribeRelationJsonCommand
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.
Addressed. We extracted a single ShowTablesJsonExec physical plan node covering both SHOW TABLES AS JSON and SHOW TABLE EXTENDED AS JSON, controlled by an isExtended: Boolean flag.