ayushbilala commented on code in PR #54824:
URL: https://github.com/apache/spark/pull/54824#discussion_r3208576876
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuiteBase.scala:
##########
@@ -461,4 +466,179 @@ trait ShowTablesSuiteBase extends QueryTest with
DDLCommandTestUtils {
}
}
}
+
+ test("SHOW TABLES AS JSON returns single row with json_metadata column") {
+ withNamespaceAndTable("ns", "tbl") { t =>
+ sql(s"CREATE TABLE $t (id INT, data STRING) $defaultUsing")
+ val df = sql(s"SHOW TABLES IN $catalog.ns AS JSON")
+ assert(df.schema.fieldNames === Seq("json_metadata"))
+ assert(df.count() == 1)
+
+ val jsonStr = df.collect()(0).getString(0)
+ val json = parse(jsonStr)
+ val tables = (json \ "tables").asInstanceOf[JArray].arr
+ val tblEntry = tables.find(t => (t \ "name").extract[String] == "tbl")
+ assert(tblEntry.isDefined)
+ assert((tblEntry.get \ "isTemporary").extract[Boolean] == false)
+ assert((tblEntry.get \ "namespace").isInstanceOf[JArray])
+ }
+ }
+
+ test("SHOW TABLES AS JSON with empty database") {
+ withNamespace(s"$catalog.ns_empty") {
+ sql(s"CREATE NAMESPACE $catalog.ns_empty")
+ val df = sql(s"SHOW TABLES IN $catalog.ns_empty AS JSON")
+ assert(df.count() == 1)
+
+ val jsonStr = df.collect()(0).getString(0)
+ val json = parse(jsonStr)
+ val tables = (json \ "tables").asInstanceOf[JArray].arr
+ assert(tables.isEmpty)
+ }
+ }
+
+ test("SHOW TABLE EXTENDED AS JSON returns single row with json_metadata
column") {
+ withNamespaceAndTable("ns", "tbl") { t =>
+ sql(s"CREATE TABLE $t (id INT, data STRING) $defaultUsing")
+ val df = sql(s"SHOW TABLE EXTENDED IN $catalog.ns LIKE 'tbl' AS JSON")
+ assert(df.schema.fieldNames === Seq("json_metadata"))
+ assert(df.count() == 1)
+
+ val jsonStr = df.collect()(0).getString(0)
+ val json = parse(jsonStr)
+ val tables = (json \ "tables").asInstanceOf[JArray].arr
+ assert(tables.length == 1)
+ val entry = tables.head
+ assert((entry \ "name").extract[String] == "tbl")
+ assert((entry \ "type").extract[String] == "TABLE")
+ assert((entry \ "isTemporary").extract[Boolean] == false)
+ assert((entry \ "catalog").isInstanceOf[JString])
+ assert((entry \ "namespace").isInstanceOf[JArray])
+ }
+ }
+
+ test("SHOW TABLES AS JSON includes temp views") {
+ withNamespaceAndTable("ns", "tbl") { t =>
+ sql(s"CREATE TABLE $t (id INT) $defaultUsing")
+ withTempView("tv") {
+ sql("CREATE TEMP VIEW tv AS SELECT 1 AS id")
+ val df = sql(s"SHOW TABLES IN $catalog.ns AS JSON")
+ val jsonStr = df.collect()(0).getString(0)
+ val json = parse(jsonStr)
+ val tables = (json \ "tables").asInstanceOf[JArray].arr
+ val tempView = tables.find(t => (t \ "name").extract[String] == "tv")
+ assert(tempView.isDefined)
+ assert((tempView.get \ "isTemporary").extract[Boolean] == true)
+ }
+ }
+ }
+
+ test("SHOW TABLE EXTENDED AS JSON with local temp view") {
+ withNamespaceAndTable("ns", "tbl") { t =>
+ sql(s"CREATE TABLE $t (id INT) $defaultUsing")
+ val localTmpViewName = "tbl_local_tmp"
+ withTempView(localTmpViewName) {
+ sql(s"CREATE TEMPORARY VIEW $localTmpViewName AS SELECT id FROM $t")
+
+ val df = sql(s"SHOW TABLE EXTENDED IN $catalog.ns LIKE 'tbl*' AS JSON")
+ assert(df.schema.fieldNames === Seq("json_metadata"))
+ assert(df.count() == 1)
+
+ val jsonStr = df.collect()(0).getString(0)
+ val json = parse(jsonStr)
+ val tables = (json \ "tables").asInstanceOf[JArray].arr
+
+ val tblEntry = tables.find(e => (e \ "name").extract[String] == "tbl")
+ assert(tblEntry.isDefined)
+ assert((tblEntry.get \ "isTemporary").extract[Boolean] == false)
+ assert((tblEntry.get \ "type").extract[String] == "TABLE")
+
+ val tempViewEntry = tables.find(e => (e \ "name").extract[String] ==
localTmpViewName)
+ assert(tempViewEntry.isDefined)
+ assert((tempViewEntry.get \ "isTemporary").extract[Boolean] == true)
+ assert((tempViewEntry.get \ "type").extract[String] == "VIEW")
+ }
+ }
+ }
+
+ test("SHOW TABLE EXTENDED AS JSON with global temp view") {
+ withNamespaceAndTable("ns", "tbl") { t =>
+ sql(s"CREATE TABLE $t (id INT) $defaultUsing")
+ val globalTmpViewName = "ext_json_gtv"
+ val globalNamespace = "global_temp"
+ withView(s"$globalNamespace.$globalTmpViewName") {
+ sql(s"CREATE OR REPLACE GLOBAL TEMP VIEW $globalTmpViewName AS SELECT
id FROM $t")
+
+ val df = sql(s"SHOW TABLE EXTENDED IN $globalNamespace LIKE
'ext_json*' AS JSON")
+ assert(df.schema.fieldNames === Seq("json_metadata"))
+ assert(df.count() == 1)
+
+ val jsonStr = df.collect()(0).getString(0)
+ val json = parse(jsonStr)
+ val tables = (json \ "tables").asInstanceOf[JArray].arr
+
+ val globalTempViewEntry =
+ tables.find(e => (e \ "name").extract[String] == globalTmpViewName)
+ assert(globalTempViewEntry.isDefined)
+ assert((globalTempViewEntry.get \ "isTemporary").extract[Boolean] ==
true)
+ assert((globalTempViewEntry.get \ "type").extract[String] == "VIEW")
+ }
+ }
+ }
+
+ test("SHOW TABLES AS JSON with global temp view") {
+ withNamespaceAndTable("ns", "tbl") { t =>
+ sql(s"CREATE TABLE $t (id INT) $defaultUsing")
+ val globalTmpViewName = "show_json_gtv"
+ val globalNamespace = "global_temp"
+ withView(s"$globalNamespace.$globalTmpViewName") {
+ sql(s"CREATE OR REPLACE GLOBAL TEMP VIEW $globalTmpViewName AS SELECT
id FROM $t")
+
+ val df = sql(s"SHOW TABLES IN $globalNamespace AS JSON")
+ assert(df.schema.fieldNames === Seq("json_metadata"))
+ assert(df.count() == 1)
+
+ val jsonStr = df.collect()(0).getString(0)
+ val json = parse(jsonStr)
+ val tables = (json \ "tables").asInstanceOf[JArray].arr
+
+ val globalTempViewEntry =
+ tables.find(e => (e \ "name").extract[String] == globalTmpViewName)
+ assert(globalTempViewEntry.isDefined)
+ assert((globalTempViewEntry.get \ "isTemporary").extract[Boolean] ==
true)
+ }
+ }
+ }
+
+ test("SHOW TABLE EXTENDED AS JSON with both local and global temp views") {
Review Comment:
Fixed - added assert(tables.length == N) to the tests that used .find()
without a count assertion. This will catch silent duplicates.
--
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]