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


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowViewsSuiteBase.scala:
##########
@@ -64,4 +79,72 @@ trait ShowViewsSuiteBase extends QueryTest with 
DDLCommandTestUtils {
         s"isTemporary must be false for persistent view rows: $r"))
     }
   }
+
+  test("AS JSON returns a single json_metadata column") {
+    sql(s"CREATE VIEW $catalog.$namespace.v_json_schema AS SELECT 1 AS x")
+    val df = sql(s"SHOW VIEWS IN $catalog.$namespace AS JSON")
+    assert(df.schema.length == 1)
+    assert(df.schema.head.name == "json_metadata")
+  }
+
+  test("AS JSON lists user-created views") {
+    sql(s"CREATE VIEW $catalog.$namespace.v_json_a AS SELECT 1 AS x")
+    sql(s"CREATE VIEW $catalog.$namespace.v_json_b AS SELECT 2 AS x")
+    val views = showViewsAsJson(s"SHOW VIEWS IN $catalog.$namespace AS JSON")
+    val names = viewNames(views)
+    assert(names.contains("v_json_a"), s"v_json_a missing: $names")
+    assert(names.contains("v_json_b"), s"v_json_b missing: $names")
+    assert(names.distinct.length == names.length, s"duplicate entries: $names")
+    // Scope to the two views this test created: this suite also runs under 
v1, where a
+    // leftover temp view (empty namespace, isTemporary=true) would otherwise 
fail the asserts.
+    // Uses a literal set, not `names` (which is derived from `views` itself 
and would match
+    // everything, silently disabling this scoping).
+    val createdNames = Set("v_json_a", "v_json_b")
+    val created = views.filter(v => createdNames.contains((v \ 
"viewName").extract[String]))
+    created.foreach { v =>
+      assert((v \ "namespace").extract[Seq[String]] == Seq(namespace))
+      assert(!(v \ "isTemporary").extract[Boolean])
+    }
+  }
+
+  test("AS JSON applies the LIKE pattern") {
+    sql(s"CREATE VIEW $catalog.$namespace.v_json_match AS SELECT 1 AS x")
+    sql(s"CREATE VIEW $catalog.$namespace.v_json_skip AS SELECT 1 AS x")
+    val names = viewNames(
+      showViewsAsJson(s"SHOW VIEWS IN $catalog.$namespace LIKE 'v_json_match' 
AS JSON"))
+    assert(names == Seq("v_json_match"), s"pattern did not filter: $names")
+  }
+
+  test("AS JSON excludes non-view table entries") {
+    val table = s"$catalog.$namespace.t_json_not_a_view"
+    sql(s"CREATE VIEW $catalog.$namespace.v_json_only AS SELECT 1 AS x")
+    withTable(table) {
+      sql(s"CREATE TABLE $table (x INT) USING parquet")
+      val names = viewNames(showViewsAsJson(s"SHOW VIEWS IN 
$catalog.$namespace AS JSON"))
+      assert(names.contains("v_json_only"), s"v_json_only missing: $names")
+      assert(!names.contains("t_json_not_a_view"), s"non-view leaked: $names")
+    }
+  }
+
+  test("AS JSON emits an empty array when no view matches") {
+    val rows = sql(s"SHOW VIEWS IN $catalog.$namespace LIKE 
'v_json_no_such_view' AS JSON")
+      .collect()
+    assert(rows.length == 1)
+    // Asserted literally so that a rename of the `views` key cannot pass 
unnoticed.
+    assert(rows.head.getString(0) == """{"views":[]}""")
+  }
+
+  test("AS JSON agrees with the tabular output") {
+    sql(s"CREATE VIEW $catalog.$namespace.v_json_parity AS SELECT 1 AS x")
+    val expected = sql(s"SHOW VIEWS IN $catalog.$namespace").collect()
+      .map(r => (r.getString(0), r.getString(1), r.getBoolean(2))).toSet
+    // mkString(".") matches QuotingUtils.quoted only because these namespaces 
are single-part;

Review Comment:
   Updated the comment to clarify that the relevant condition is that no 
namespace part requires quoting, rather than the namespace being single-part.



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