asl3 commented on code in PR #55441:
URL: https://github.com/apache/spark/pull/55441#discussion_r3151740188


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ShowPartitionsJsonCommand.scala:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.{compact, render}
+
+import org.apache.spark.sql.{Row, SparkSession}
+import org.apache.spark.sql.catalyst.analysis.ResolvedTable
+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.connector.catalog.V1Table
+import org.apache.spark.sql.errors.QueryCompilationErrors
+import org.apache.spark.sql.types.{MetadataBuilder, StringType}
+import org.apache.spark.sql.util.PartitioningUtils
+
+/**
+ * A command for users to list the partition names of a table as a JSON 
document. If a partition
+ * spec is specified, only partitions matching the spec are returned. 
Otherwise all partitions are
+ * returned.
+ *
+ * The output is a single row with a `json_metadata` column containing a JSON 
object of the form:
+ * `{"partitions": ["col=val/col=val", ...]}`.
+ *
+ * This command is the AS JSON variant of [[ShowPartitionsCommand]] and is 
produced by the parser
+ * when `SHOW PARTITIONS ... AS JSON` is issued. Only V1 (session catalog / 
Hive metastore) tables
+ * are supported.
+ *
+ * The syntax of using this command in SQL is:
+ * {{{
+ *   SHOW PARTITIONS multi_part_name [partition_spec] AS JSON;
+ * }}}
+ */
+case class ShowPartitionsJsonCommand(
+    child: LogicalPlan,
+    spec: Option[TablePartitionSpec],
+    override val output: Seq[Attribute] = Seq(
+      AttributeReference(
+        "json_metadata",
+        StringType,
+        nullable = false,
+        new MetadataBuilder().putString("comment", "Partition list of the 
table").build())()))
+    extends UnaryRunnableCommand {
+
+  override def run(sparkSession: SparkSession): Seq[Row] = {
+    child match {
+      case ResolvedTable(_, _, t: V1Table, _) =>
+        val catalog = sparkSession.sessionState.catalog
+        val table = t.catalogTable
+        val tableName = table.identifier
+
+        if (table.partitionColumnNames.isEmpty) {
+          throw 
QueryCompilationErrors.showPartitionNotAllowedOnTableNotPartitionedError(
+            tableName.quotedString)
+        }
+
+        DDLUtils.verifyPartitionProviderIsHive(sparkSession, table, "SHOW 
PARTITIONS")
+
+        val normalizedSpec = spec.map(partitionSpec =>
+          PartitioningUtils.normalizePartitionSpec(
+            partitionSpec,
+            table.partitionSchema,
+            tableName.quotedString,
+            sparkSession.sessionState.conf.resolver))
+
+        val partNames = catalog.listPartitionNames(tableName, normalizedSpec)
+
+        val jsonMap = mutable.LinkedHashMap[String, JValue]()
+        jsonMap("partitions") = JArray(partNames.map(JString(_)).toList)
+        Seq(Row(compact(render(JObject(jsonMap.toList)))))
+
+      case _ =>

Review Comment:
   DescribeRelationJsonCommand handles ResolvedTempView and 
ResolvedPersistentView explicitly, should we have a comment / explicit branches 
here?



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowPartitionsSuite.scala:
##########
@@ -105,6 +105,44 @@ trait ShowPartitionsSuiteBase extends 
command.ShowPartitionsSuiteBase {
         Row("p1=__HIVE_DEFAULT_PARTITION__"))
     }
   }
+

Review Comment:
   Consider adding more test coverage for parity with the non-JSON path:
   - NULL/empty parittion values
   - __HIVE_DEFAULT_PARTITION__
   - SHOW PARTITIONS tbl PARTITION(unknown_col=1)
     AS JSON



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ShowPartitionsJsonCommand.scala:
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.{compact, render}
+
+import org.apache.spark.sql.{Row, SparkSession}
+import org.apache.spark.sql.catalyst.analysis.ResolvedTable
+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.connector.catalog.V1Table
+import org.apache.spark.sql.errors.QueryCompilationErrors
+import org.apache.spark.sql.types.{MetadataBuilder, StringType}
+import org.apache.spark.sql.util.PartitioningUtils
+
+/**
+ * A command for users to list the partition names of a table as a JSON 
document. If a partition
+ * spec is specified, only partitions matching the spec are returned. 
Otherwise all partitions are
+ * returned.
+ *
+ * The output is a single row with a `json_metadata` column containing a JSON 
object of the form:
+ * `{"partitions": ["col=val/col=val", ...]}`.
+ *
+ * This command is the AS JSON variant of [[ShowPartitionsCommand]] and is 
produced by the parser
+ * when `SHOW PARTITIONS ... AS JSON` is issued. Only V1 (session catalog / 
Hive metastore) tables
+ * are supported.
+ *
+ * The syntax of using this command in SQL is:
+ * {{{
+ *   SHOW PARTITIONS multi_part_name [partition_spec] AS JSON;
+ * }}}
+ */
+case class ShowPartitionsJsonCommand(
+    child: LogicalPlan,
+    spec: Option[TablePartitionSpec],
+    override val output: Seq[Attribute] = Seq(
+      AttributeReference(
+        "json_metadata",
+        StringType,
+        nullable = false,
+        new MetadataBuilder().putString("comment", "Partition list of the 
table").build())()))
+    extends UnaryRunnableCommand {
+
+  override def run(sparkSession: SparkSession): Seq[Row] = {
+    child match {
+      case ResolvedTable(_, _, t: V1Table, _) =>
+        val catalog = sparkSession.sessionState.catalog
+        val table = t.catalogTable
+        val tableName = table.identifier
+
+        if (table.partitionColumnNames.isEmpty) {
+          throw 
QueryCompilationErrors.showPartitionNotAllowedOnTableNotPartitionedError(
+            tableName.quotedString)
+        }
+
+        DDLUtils.verifyPartitionProviderIsHive(sparkSession, table, "SHOW 
PARTITIONS")
+
+        val normalizedSpec = spec.map(partitionSpec =>
+          PartitioningUtils.normalizePartitionSpec(
+            partitionSpec,
+            table.partitionSchema,
+            tableName.quotedString,
+            sparkSession.sessionState.conf.resolver))
+
+        val partNames = catalog.listPartitionNames(tableName, normalizedSpec)
+
+        val jsonMap = mutable.LinkedHashMap[String, JValue]()

Review Comment:
   mutable.LinkedHashMap is unnecessary for a single key, consider direct 
JObject:
   
   ```
   Seq(Row(compact(render(JObject("partitions" ->
     JArray(partNames.map(JString(_)).toList))))))
   ```



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowPartitionsParserSuite.scala:
##########
@@ -18,10 +18,12 @@
 package org.apache.spark.sql.execution.command
 
 import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, 
UnresolvedPartitionSpec, UnresolvedTable}
-import org.apache.spark.sql.catalyst.parser.CatalystSqlParser.parsePlan
 import org.apache.spark.sql.catalyst.plans.logical.ShowPartitions
+import org.apache.spark.sql.test.SharedSparkSession
+
+class ShowPartitionsParserSuite extends SharedSparkSession with AnalysisTest {

Review Comment:
   nit: SharedSparkSession is not necessary for this suite, `new
     SparkSqlParser().parsePlan(...)` can keep the suite lightweight as 
v1/ShowPartitionsSuite already has the execution test coverage



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to