cloud-fan commented on code in PR #49466: URL: https://github.com/apache/spark/pull/49466#discussion_r1912742465
########## sql/core/src/main/scala/org/apache/spark/sql/execution/command/DescribeRelationJsonCommand.scala: ########## @@ -0,0 +1,313 @@ +/* + * 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 + +import org.json4s._ +import org.json4s.JsonAST.JObject +import org.json4s.jackson.JsonMethods._ + +import org.apache.spark.sql.{Row, SparkSession} +import org.apache.spark.sql.catalyst.analysis.{ResolvedPersistentView, ResolvedTable, ResolvedTempView} +import org.apache.spark.sql.catalyst.catalog.{CatalogTable, CatalogTableType, SessionCatalog} +import org.apache.spark.sql.catalyst.catalog.CatalogTypes.TablePartitionSpec +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.quoteIfNeeded +import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ +import org.apache.spark.sql.connector.catalog.V1Table +import org.apache.spark.sql.errors.QueryCompilationErrors +import org.apache.spark.sql.types._ +import org.apache.spark.sql.util.PartitioningUtils + +/** + * The command for `DESCRIBE ... AS JSON`. + */ +case class DescribeRelationJsonCommand( + child: LogicalPlan, + partitionSpec: TablePartitionSpec, + isExtended: Boolean, + override val output: Seq[Attribute] = Seq( + AttributeReference( + "json_metadata", + StringType, + nullable = false, + new MetadataBuilder().putString("comment", "JSON metadata of the table").build())() + )) extends UnaryRunnableCommand { + + override def run(sparkSession: SparkSession): Seq[Row] = { + val jsonMap = mutable.LinkedHashMap[String, JValue]() + child match { + case v: ResolvedTempView => + if (partitionSpec.nonEmpty) { + throw QueryCompilationErrors.descPartitionNotAllowedOnTempView(v.identifier.name()) + } + describeIdentifier(Seq("system", "session", v.identifier.name()), jsonMap) + describeColsJson(v.metadata.schema, jsonMap) + describeFormattedTableInfoJson(v.metadata, jsonMap) + + case v: ResolvedPersistentView => + if (partitionSpec.nonEmpty) { + throw QueryCompilationErrors.descPartitionNotAllowedOnView(v.identifier.name()) + } + describeIdentifier(v.identifier.toQualifiedNameParts(v.catalog), jsonMap) + describeColsJson(v.metadata.schema, jsonMap) + describeFormattedTableInfoJson(v.metadata, jsonMap) + + case ResolvedTable(catalog, identifier, V1Table(metadata), _) => + describeIdentifier(identifier.toQualifiedNameParts(catalog), jsonMap) + val schema = if (metadata.schema.isEmpty) { + // In older versions of Spark, + // the table schema can be empty and should be inferred at runtime. + sparkSession.table(metadata.identifier).schema + } else { + metadata.schema + } + describeColsJson(schema, jsonMap) + describeClusteringInfoJson(metadata, jsonMap) + if (partitionSpec.nonEmpty) { + // Outputs the partition-specific info for the DDL command: + // "DESCRIBE [EXTENDED|FORMATTED] table_name PARTITION (partitionVal*)" + describePartitionInfoJson( + sparkSession, sparkSession.sessionState.catalog, metadata, jsonMap) + } else { + describeFormattedTableInfoJson(metadata, jsonMap) + } + + case _ => throw QueryCompilationErrors.describeAsJsonNotSupportedForV2TablesError() + } + + Seq(Row(compact(render(JObject(jsonMap.toList))))) + } + + private def addKeyValueToMap( Review Comment: The code below is moved from https://github.com/apache/spark/pull/49466/files#diff-632efff0ffceb2ff47b97f39570b4f2cff24401f132f559158acaa651b27af26L759 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org