ayushbilala commented on code in PR #59235:
URL: https://github.com/apache/spark/pull/59235#discussion_r4246335899


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ShowViewsJsonCommand.scala:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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 org.json4s._
+import org.json4s.jackson.JsonMethods.{compact, render}
+
+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, ViewCatalog}
+import org.apache.spark.sql.errors.QueryCompilationErrors
+import org.apache.spark.sql.types.{MetadataBuilder, StringType}
+
+/**
+ * The command for `SHOW VIEWS AS JSON`.
+ *
+ * The output is a single row with a `json_metadata` column holding a JSON 
object of the form:
+ * {{{
+ *   {"views":[{"namespace":["db"],"viewName":"v1","isTemporary":false}]}
+ * }}}
+ *
+ * The syntax of using this command in SQL is:
+ * {{{
+ *   SHOW VIEWS [(FROM | IN) namespace] [[LIKE] pattern] AS JSON;
+ * }}}
+ */
+case class ShowViewsJsonCommand(
+    child: LogicalPlan,
+    pattern: Option[String],
+    override val output: Seq[Attribute] = Seq(
+      AttributeReference(
+        "json_metadata",
+        StringType,
+        nullable = false,
+        new MetadataBuilder().putString("comment", "JSON metadata of the 
views").build())()))
+    extends UnaryRunnableCommand {
+
+  override def run(sparkSession: SparkSession): Seq[Row] = {
+    val views = child match {
+      // A catalog that implements `ViewCatalog` owns its views, even when it 
overrides the
+      // session catalog. This mirrors the routing between 
`ResolveSessionCatalog` and
+      // `DataSourceV2Strategy` for the non-JSON `SHOW VIEWS`.
+      case ResolvedNamespace(catalog: ViewCatalog, ns, _) =>
+        listViewCatalogViews(catalog, ns)
+      case ResolvedNamespace(catalog, ns, _) if 
CatalogV2Util.isSessionCatalog(catalog) =>
+        listSessionCatalogViews(sparkSession, ns)
+      case ResolvedNamespace(catalog, _, _) =>
+        throw QueryCompilationErrors.missingCatalogViewsAbilityError(catalog)
+      case other =>
+        throw SparkException.internalError(
+          s"Unexpected child in ShowViewsJsonCommand: 
${other.getClass.getSimpleName}")
+    }
+    Seq(Row(compact(render(JObject("views" -> JArray(views.toList))))))
+  }
+
+  private def listSessionCatalogViews(
+      sparkSession: SparkSession,
+      ns: Seq[String]): Seq[JObject] = {
+    val catalog = sparkSession.sessionState.catalog
+    val db = databaseInSessionCatalog(ns)
+    catalog.listViews(db, pattern.getOrElse("*")).map { ident =>
+      toJson(ident.database.toSeq, ident.table, catalog.isTempView(ident))
+    }
+  }
+
+  private def listViewCatalogViews(catalog: ViewCatalog, ns: Seq[String]): 
Seq[JObject] = {
+    catalog.listViews(ns.toArray).filter { ident =>
+      pattern.forall(p => StringUtils.filterPattern(Seq(ident.name), 
p).nonEmpty)
+    }.map { ident =>
+      // v2 catalogs have no temp views, matching `ShowViewsExec`.
+      toJson(ident.namespace().toSeq, ident.name(), isTemporary = false)

Review Comment:
   Added "V2: AS JSON lists a view under a nested namespace" against 
`test_view_catalog.ns1.ns2`.
   It asserts "namespace":["ns1","ns2"] element by element, so collapsing or 
truncating the parts would now fail it.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/command/ShowViewsJsonCommand.scala:
##########
@@ -0,0 +1,117 @@
+/*
+ * 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 org.json4s._
+import org.json4s.jackson.JsonMethods.{compact, render}
+
+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, ViewCatalog}
+import org.apache.spark.sql.errors.QueryCompilationErrors
+import org.apache.spark.sql.types.{MetadataBuilder, StringType}
+
+/**
+ * The command for `SHOW VIEWS AS JSON`.
+ *
+ * The output is a single row with a `json_metadata` column holding a JSON 
object of the form:
+ * {{{
+ *   {"views":[{"namespace":["db"],"viewName":"v1","isTemporary":false}]}
+ * }}}
+ *
+ * The syntax of using this command in SQL is:
+ * {{{
+ *   SHOW VIEWS [(FROM | IN) namespace] [[LIKE] pattern] AS JSON;
+ * }}}
+ */
+case class ShowViewsJsonCommand(
+    child: LogicalPlan,
+    pattern: Option[String],
+    override val output: Seq[Attribute] = Seq(
+      AttributeReference(
+        "json_metadata",
+        StringType,
+        nullable = false,
+        new MetadataBuilder().putString("comment", "JSON metadata of the 
views").build())()))
+    extends UnaryRunnableCommand {
+
+  override def run(sparkSession: SparkSession): Seq[Row] = {
+    val views = child match {
+      // A catalog that implements `ViewCatalog` owns its views, even when it 
overrides the
+      // session catalog. This mirrors the routing between 
`ResolveSessionCatalog` and
+      // `DataSourceV2Strategy` for the non-JSON `SHOW VIEWS`.
+      case ResolvedNamespace(catalog: ViewCatalog, ns, _) =>
+        listViewCatalogViews(catalog, ns)
+      case ResolvedNamespace(catalog, ns, _) if 
CatalogV2Util.isSessionCatalog(catalog) =>
+        listSessionCatalogViews(sparkSession, ns)
+      case ResolvedNamespace(catalog, _, _) =>
+        throw QueryCompilationErrors.missingCatalogViewsAbilityError(catalog)
+      case other =>
+        throw SparkException.internalError(
+          s"Unexpected child in ShowViewsJsonCommand: 
${other.getClass.getSimpleName}")
+    }
+    Seq(Row(compact(render(JObject("views" -> JArray(views.toList))))))
+  }
+
+  private def listSessionCatalogViews(
+      sparkSession: SparkSession,
+      ns: Seq[String]): Seq[JObject] = {
+    val catalog = sparkSession.sessionState.catalog
+    val db = databaseInSessionCatalog(ns)
+    catalog.listViews(db, pattern.getOrElse("*")).map { ident =>
+      toJson(ident.database.toSeq, ident.table, catalog.isTempView(ident))
+    }
+  }
+
+  private def listViewCatalogViews(catalog: ViewCatalog, ns: Seq[String]): 
Seq[JObject] = {
+    catalog.listViews(ns.toArray).filter { ident =>
+      pattern.forall(p => StringUtils.filterPattern(Seq(ident.name), 
p).nonEmpty)
+    }.map { ident =>
+      // v2 catalogs have no temp views, matching `ShowViewsExec`.
+      toJson(ident.namespace().toSeq, ident.name(), isTemporary = false)

Review Comment:
   Added "V2: AS JSON lists a view under a nested namespace" against 
`test_view_catalog.ns1.ns2`.
   It asserts "namespace":["ns1","ns2"] element by element, so collapsing or 
truncating the parts would now fail it.



-- 
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