From 58f4134251852ed5af288db7626f39f069f78cfa Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Thu, 5 Nov 2020 22:27:33 +0300 Subject: [PATCH 01/46] Draft of ShowTablesSuite --- .../execution/command/ShowTablesSuite.scala | 35 ++++++++++++++++++ .../command/v1/ShowTablesSuite.scala | 36 +++++++++++++++++++ .../command/v2/ShowTablesSuite.scala | 24 +++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala create mode 100644 sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala create mode 100644 sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala new file mode 100644 index 0000000000000..42b5067a6978a --- /dev/null +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -0,0 +1,35 @@ +/* + * 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.apache.spark.sql.{QueryTest, Row} +import org.apache.spark.sql.test.SharedSparkSession + +trait ShowTablesSuite extends QueryTest with SharedSparkSession { + protected def catalog: String + protected def namespaceColumn: String = "database" + protected def namespace: String = "test" + protected def tableColumn: String = "tableName" + protected def table: String = "people" + + test("show an existing table") { + val tables = sql(s"SHOW TABLES IN $catalog.test") + checkAnswer(tables.select(namespaceColumn), Row(namespace)) + checkAnswer(tables.select(tableColumn), Row(table)) + } +} diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala new file mode 100644 index 0000000000000..a23806200da76 --- /dev/null +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -0,0 +1,36 @@ +/* + * 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.v1 + +import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} + +class ShowTablesSuite extends CommonShowTablesSuite { + override def catalog: String = "spark_catalog" + + protected override def beforeAll(): Unit = { + super.beforeAll() + sql(s"CREATE DATABASE $namespace") + sql(s"CREATE TABLE $namespace.$table (name STRING, id INT) USING PARQUET") + } + + protected override def afterAll(): Unit = { + sql(s"DROP TABLE $namespace.$table") + sql(s"DROP DATABASE $namespace") + super.afterAll() + } +} diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala new file mode 100644 index 0000000000000..da1afaa6a4a07 --- /dev/null +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -0,0 +1,24 @@ +/* + * 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.v2 + +import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} + +class ShowTablesSuite extends CommonShowTablesSuite { + override def catalog: String = "aaa" +} From 7b8746f3d169384f57f0786f83cd068d42dfe271 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Thu, 5 Nov 2020 22:48:20 +0300 Subject: [PATCH 02/46] ShowTablesSuite for v2 --- .../command/v2/ShowTablesSuite.scala | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index da1afaa6a4a07..a935915edc6c0 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -17,8 +17,28 @@ package org.apache.spark.sql.execution.command.v2 +import org.apache.spark.SparkConf +import org.apache.spark.sql.QueryTest +import org.apache.spark.sql.connector.InMemoryTableCatalog import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} +import org.apache.spark.sql.test.SharedSparkSession -class ShowTablesSuite extends CommonShowTablesSuite { - override def catalog: String = "aaa" +class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowTablesSuite { + override def catalog: String = "test_catalog_v2" + override protected def namespaceColumn: String = "namespace" + + override def sparkConf: SparkConf = super.sparkConf + .set(s"spark.sql.catalog.$catalog", classOf[InMemoryTableCatalog].getName) + + protected override def beforeAll(): Unit = { + super.beforeAll() + sql(s"CREATE DATABASE $catalog.$namespace") + sql(s"CREATE TABLE $catalog.$namespace.$table (name STRING, id INT) USING PARQUET") + } + + protected override def afterAll(): Unit = { + sql(s"DROP TABLE $catalog.$namespace.$table") + sql(s"DROP DATABASE $catalog.$namespace") + super.afterAll() + } } From 4c0d2b85c63e2a56429239e761189944ba9d4c94 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Thu, 5 Nov 2020 22:56:01 +0300 Subject: [PATCH 03/46] test "show table in a not existing namespace" --- .../spark/sql/execution/command/v1/ShowTablesSuite.scala | 8 ++++++++ .../spark/sql/execution/command/v2/ShowTablesSuite.scala | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index a23806200da76..47da948a769c1 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -17,6 +17,7 @@ package org.apache.spark.sql.execution.command.v1 +import org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} class ShowTablesSuite extends CommonShowTablesSuite { @@ -33,4 +34,11 @@ class ShowTablesSuite extends CommonShowTablesSuite { sql(s"DROP DATABASE $namespace") super.afterAll() } + + test("show table in a not existing namespace") { + val msg = intercept[NoSuchDatabaseException] { + checkAnswer(sql(s"SHOW TABLES IN $catalog.bad_test"), Seq()) + }.getMessage + assert(msg.contains("Database 'bad_test' not found")) + } } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index a935915edc6c0..0da035b0e5a36 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -41,4 +41,8 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT sql(s"DROP DATABASE $catalog.$namespace") super.afterAll() } + + test("show table in a not existing namespace") { + checkAnswer(sql(s"SHOW TABLES IN $catalog.bad_test"), Seq()) + } } From f1aa46cb8e5054a7082283a08d9bbc6ac4029cfa Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Thu, 5 Nov 2020 23:08:47 +0300 Subject: [PATCH 04/46] test "show an existing table in V1 catalog" --- .../spark/sql/execution/command/v1/ShowTablesSuite.scala | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 47da948a769c1..55e09425612e2 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -17,6 +17,7 @@ package org.apache.spark.sql.execution.command.v1 +import org.apache.spark.sql.Row import org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} @@ -35,6 +36,12 @@ class ShowTablesSuite extends CommonShowTablesSuite { super.afterAll() } + test("show an existing table in V1 catalog") { + val tables = sql(s"SHOW TABLES IN $catalog.test") + assert(tables.schema.fieldNames.toSet === Set(namespaceColumn, tableColumn, "isTemporary")) + checkAnswer(tables.select("isTemporary"), Row(false)) + } + test("show table in a not existing namespace") { val msg = intercept[NoSuchDatabaseException] { checkAnswer(sql(s"SHOW TABLES IN $catalog.bad_test"), Seq()) From 3a85ac3d75bf11b82cb76e03de6412ab9d8b67fa Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Fri, 6 Nov 2020 17:49:34 +0300 Subject: [PATCH 05/46] Move "ShowTables: using v2 catalog" to v2/ShowTablesSuite --- .../sql/connector/DataSourceV2SQLSuite.scala | 11 ----------- .../execution/command/ShowTablesSuite.scala | 7 +++++++ .../command/v1/ShowTablesSuite.scala | 7 +++++++ .../command/v2/ShowTablesSuite.scala | 19 ++++++++++++++++++- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala index 893ee5f130cda..39cf2ac67aed3 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala @@ -898,17 +898,6 @@ class DataSourceV2SQLSuite } } - test("ShowTables: using v2 catalog") { - spark.sql("CREATE TABLE testcat.db.table_name (id bigint, data string) USING foo") - spark.sql("CREATE TABLE testcat.n1.n2.db.table_name (id bigint, data string) USING foo") - - runShowTablesSql("SHOW TABLES FROM testcat.db", Seq(Row("db", "table_name"))) - - runShowTablesSql( - "SHOW TABLES FROM testcat.n1.n2.db", - Seq(Row("n1.n2.db", "table_name"))) - } - test("ShowTables: using v2 catalog with a pattern") { spark.sql("CREATE TABLE testcat.db.table (id bigint, data string) USING foo") spark.sql("CREATE TABLE testcat.db.table_name_1 (id bigint, data string) USING foo") diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index 42b5067a6978a..c67989042387e 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -19,6 +19,7 @@ package org.apache.spark.sql.execution.command import org.apache.spark.sql.{QueryTest, Row} import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.{BooleanType, StringType, StructType} trait ShowTablesSuite extends QueryTest with SharedSparkSession { protected def catalog: String @@ -26,6 +27,12 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { protected def namespace: String = "test" protected def tableColumn: String = "tableName" protected def table: String = "people" + protected def showSchema: StructType + protected def runShowTablesSql(sqlText: String, expected: Seq[Row]): Unit = { + val df = spark.sql(sqlText) + assert(df.schema === showSchema) + assert(expected === df.collect()) + } test("show an existing table") { val tables = sql(s"SHOW TABLES IN $catalog.test") diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 55e09425612e2..a1baac147a329 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -20,9 +20,16 @@ package org.apache.spark.sql.execution.command.v1 import org.apache.spark.sql.Row import org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} +import org.apache.spark.sql.types.{BooleanType, StringType, StructType} class ShowTablesSuite extends CommonShowTablesSuite { override def catalog: String = "spark_catalog" + override protected def showSchema: StructType = { + new StructType() + .add("database", StringType, nullable = false) + .add("tableName", StringType, nullable = false) + .add("isTemporary", BooleanType, nullable = false) + } protected override def beforeAll(): Unit = { super.beforeAll() diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 0da035b0e5a36..35f91dffa6c3e 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -18,14 +18,20 @@ package org.apache.spark.sql.execution.command.v2 import org.apache.spark.SparkConf -import org.apache.spark.sql.QueryTest +import org.apache.spark.sql.{QueryTest, Row} import org.apache.spark.sql.connector.InMemoryTableCatalog import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.{StringType, StructType} class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowTablesSuite { override def catalog: String = "test_catalog_v2" override protected def namespaceColumn: String = "namespace" + override protected def showSchema: StructType = { + new StructType() + .add("namespace", StringType, nullable = false) + .add("tableName", StringType, nullable = false) + } override def sparkConf: SparkConf = super.sparkConf .set(s"spark.sql.catalog.$catalog", classOf[InMemoryTableCatalog].getName) @@ -45,4 +51,15 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT test("show table in a not existing namespace") { checkAnswer(sql(s"SHOW TABLES IN $catalog.bad_test"), Seq()) } + + test("ShowTables: using v2 catalog") { + spark.sql(s"CREATE TABLE $catalog.db.table_name (id bigint, data string) USING foo") + spark.sql(s"CREATE TABLE $catalog.n1.n2.db.table_name (id bigint, data string) USING foo") + + runShowTablesSql(s"SHOW TABLES FROM $catalog.db", Seq(Row("db", "table_name"))) + + runShowTablesSql( + s"SHOW TABLES FROM $catalog.n1.n2.db", + Seq(Row("n1.n2.db", "table_name"))) + } } From 0534480387a7e8d5c3d89f5f1607b4f9f870ba4f Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Fri, 6 Nov 2020 18:03:34 +0300 Subject: [PATCH 06/46] Move v2 pattern tests --- .../sql/connector/DataSourceV2SQLSuite.scala | 26 ---------- .../command/v2/ShowTablesSuite.scala | 47 ++++++++++++++++--- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala index 39cf2ac67aed3..23928325ac61f 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala @@ -898,32 +898,6 @@ class DataSourceV2SQLSuite } } - test("ShowTables: using v2 catalog with a pattern") { - spark.sql("CREATE TABLE testcat.db.table (id bigint, data string) USING foo") - spark.sql("CREATE TABLE testcat.db.table_name_1 (id bigint, data string) USING foo") - spark.sql("CREATE TABLE testcat.db.table_name_2 (id bigint, data string) USING foo") - spark.sql("CREATE TABLE testcat.db2.table_name_2 (id bigint, data string) USING foo") - - runShowTablesSql( - "SHOW TABLES FROM testcat.db", - Seq( - Row("db", "table"), - Row("db", "table_name_1"), - Row("db", "table_name_2"))) - - runShowTablesSql( - "SHOW TABLES FROM testcat.db LIKE '*name*'", - Seq(Row("db", "table_name_1"), Row("db", "table_name_2"))) - - runShowTablesSql( - "SHOW TABLES FROM testcat.db LIKE '*2'", - Seq(Row("db", "table_name_2"))) - } - - test("ShowTables: using v2 catalog, namespace doesn't exist") { - runShowTablesSql("SHOW TABLES FROM testcat.unknown", Seq()) - } - test("ShowTables: using v1 catalog") { runShowTablesSql( "SHOW TABLES FROM default", diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 35f91dffa6c3e..1cb29dac60e60 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -53,13 +53,48 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT } test("ShowTables: using v2 catalog") { - spark.sql(s"CREATE TABLE $catalog.db.table_name (id bigint, data string) USING foo") - spark.sql(s"CREATE TABLE $catalog.n1.n2.db.table_name (id bigint, data string) USING foo") + withTable(s"$catalog.db.table_name") { + spark.sql(s"CREATE TABLE $catalog.db.table_name (id bigint, data string) USING foo") + runShowTablesSql(s"SHOW TABLES FROM $catalog.db", Seq(Row("db", "table_name"))) + } - runShowTablesSql(s"SHOW TABLES FROM $catalog.db", Seq(Row("db", "table_name"))) + withTable(s"$catalog.n1.n2.db") { + spark.sql(s"CREATE TABLE $catalog.n1.n2.db.table_name (id bigint, data string) USING foo") + runShowTablesSql( + s"SHOW TABLES FROM $catalog.n1.n2.db", + Seq(Row("n1.n2.db", "table_name"))) + } + } + + test("ShowTables: using v2 catalog with a pattern") { + withTable( + s"$catalog.db.table", + s"$catalog.db.table_name_1", + s"$catalog.db.table_name_2", + s"$catalog.db2.table_name_2") { + spark.sql(s"CREATE TABLE $catalog.db.table (id bigint, data string) USING foo") + spark.sql(s"CREATE TABLE $catalog.db.table_name_1 (id bigint, data string) USING foo") + spark.sql(s"CREATE TABLE $catalog.db.table_name_2 (id bigint, data string) USING foo") + spark.sql(s"CREATE TABLE $catalog.db2.table_name_2 (id bigint, data string) USING foo") + + runShowTablesSql( + s"SHOW TABLES FROM $catalog.db", + Seq( + Row("db", "table"), + Row("db", "table_name_1"), + Row("db", "table_name_2"))) + + runShowTablesSql( + s"SHOW TABLES FROM $catalog.db LIKE '*name*'", + Seq(Row("db", "table_name_1"), Row("db", "table_name_2"))) + + runShowTablesSql( + s"SHOW TABLES FROM $catalog.db LIKE '*2'", + Seq(Row("db", "table_name_2"))) + } + } - runShowTablesSql( - s"SHOW TABLES FROM $catalog.n1.n2.db", - Seq(Row("n1.n2.db", "table_name"))) + test("ShowTables: using v2 catalog, namespace doesn't exist") { + runShowTablesSql(s"SHOW TABLES FROM $catalog.unknown", Seq()) } } From 7c12702493475e39ff48690ff66353f338ee326e Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Fri, 6 Nov 2020 18:15:09 +0300 Subject: [PATCH 07/46] Move more v1 tests --- .../sql/connector/DataSourceV2SQLSuite.scala | 33 ----------------- .../execution/command/ShowTablesSuite.scala | 2 +- .../command/v1/ShowTablesSuite.scala | 37 ++++++++++++++++++- 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala index 23928325ac61f..a3d0947e81a24 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala @@ -898,39 +898,6 @@ class DataSourceV2SQLSuite } } - test("ShowTables: using v1 catalog") { - runShowTablesSql( - "SHOW TABLES FROM default", - Seq(Row("", "source", true), Row("", "source2", true)), - expectV2Catalog = false) - } - - test("ShowTables: using v1 catalog, db doesn't exist ") { - // 'db' below resolves to a database name for v1 catalog because there is no catalog named - // 'db' and there is no default catalog set. - val exception = intercept[NoSuchDatabaseException] { - runShowTablesSql("SHOW TABLES FROM db", Seq(), expectV2Catalog = false) - } - - assert(exception.getMessage.contains("Database 'db' not found")) - } - - test("ShowTables: using v1 catalog, db name with multipartIdentifier ('a.b') is not allowed.") { - val exception = intercept[AnalysisException] { - runShowTablesSql("SHOW TABLES FROM a.b", Seq(), expectV2Catalog = false) - } - - assert(exception.getMessage.contains("The database name is not valid: a.b")) - } - - test("ShowViews: using v1 catalog, db name with multipartIdentifier ('a.b') is not allowed.") { - val exception = intercept[AnalysisException] { - sql("SHOW TABLES FROM a.b") - } - - assert(exception.getMessage.contains("The database name is not valid: a.b")) - } - test("ShowViews: using v2 catalog, command not supported.") { val exception = intercept[AnalysisException] { sql("SHOW VIEWS FROM testcat") diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index c67989042387e..ccd724b57c80c 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -31,7 +31,7 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { protected def runShowTablesSql(sqlText: String, expected: Seq[Row]): Unit = { val df = spark.sql(sqlText) assert(df.schema === showSchema) - assert(expected === df.collect()) + assert(df.collect() === expected) } test("show an existing table") { diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index a1baac147a329..0cc6d2c239f7a 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -17,7 +17,7 @@ package org.apache.spark.sql.execution.command.v1 -import org.apache.spark.sql.Row +import org.apache.spark.sql.{AnalysisException, Row} import org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} import org.apache.spark.sql.types.{BooleanType, StringType, StructType} @@ -55,4 +55,39 @@ class ShowTablesSuite extends CommonShowTablesSuite { }.getMessage assert(msg.contains("Database 'bad_test' not found")) } + + test("ShowTables: using v1 catalog") { + withTable("source", "source2") { + val df = spark.createDataFrame(Seq((1L, "a"), (2L, "b"), (3L, "c"))).toDF("id", "data") + df.createOrReplaceTempView("source") + val df2 = spark.createDataFrame(Seq((4L, "d"), (5L, "e"), (6L, "f"))).toDF("id", "data") + df2.createOrReplaceTempView("source2") + runShowTablesSql( + "SHOW TABLES FROM default", + Seq(Row("", "source", true), Row("", "source2", true))) + } + } + + test("ShowTables: using v1 catalog, db doesn't exist ") { + // 'db' below resolves to a database name for v1 catalog because there is no catalog named + // 'db' and there is no default catalog set. + val exception = intercept[NoSuchDatabaseException] { + runShowTablesSql("SHOW TABLES FROM db", Seq()) + } + assert(exception.getMessage.contains("Database 'db' not found")) + } + + test("ShowTables: using v1 catalog, db name with multipartIdentifier ('a.b') is not allowed.") { + val exception = intercept[AnalysisException] { + runShowTablesSql("SHOW TABLES FROM a.b", Seq()) + } + assert(exception.getMessage.contains("The database name is not valid: a.b")) + } + + test("ShowViews: using v1 catalog, db name with multipartIdentifier ('a.b') is not allowed.") { + val exception = intercept[AnalysisException] { + sql("SHOW TABLES FROM a.b") + } + assert(exception.getMessage.contains("The database name is not valid: a.b")) + } } From 327179e3f5e501013caa065ee3121d33c19068c3 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 10:31:29 +0300 Subject: [PATCH 08/46] Move more v2 tests --- .../sql/connector/DataSourceV2SQLSuite.scala | 14 -------------- .../execution/command/v2/ShowTablesSuite.scala | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala index a3d0947e81a24..440baa5894d08 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala @@ -898,20 +898,6 @@ class DataSourceV2SQLSuite } } - test("ShowViews: using v2 catalog, command not supported.") { - val exception = intercept[AnalysisException] { - sql("SHOW VIEWS FROM testcat") - } - - assert(exception.getMessage.contains("Catalog testcat doesn't support SHOW VIEWS," + - " only SessionCatalog supports this command.")) - } - - test("ShowTables: using v2 catalog with empty namespace") { - spark.sql("CREATE TABLE testcat.table (id bigint, data string) USING foo") - runShowTablesSql("SHOW TABLES FROM testcat", Seq(Row("", "table"))) - } - test("ShowTables: namespace is not specified and default v2 catalog is set") { spark.conf.set(SQLConf.DEFAULT_CATALOG.key, "testcat") spark.sql("CREATE TABLE testcat.table (id bigint, data string) USING foo") diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 1cb29dac60e60..b04249af54e97 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -18,7 +18,7 @@ package org.apache.spark.sql.execution.command.v2 import org.apache.spark.SparkConf -import org.apache.spark.sql.{QueryTest, Row} +import org.apache.spark.sql.{AnalysisException, QueryTest, Row} import org.apache.spark.sql.connector.InMemoryTableCatalog import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} import org.apache.spark.sql.test.SharedSparkSession @@ -97,4 +97,19 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT test("ShowTables: using v2 catalog, namespace doesn't exist") { runShowTablesSql(s"SHOW TABLES FROM $catalog.unknown", Seq()) } + + test("ShowViews: using v2 catalog, command not supported.") { + val exception = intercept[AnalysisException] { + sql(s"SHOW VIEWS FROM $catalog") + } + assert(exception.getMessage.contains(s"Catalog $catalog doesn't support SHOW VIEWS," + + " only SessionCatalog supports this command.")) + } + + test("ShowTables: using v2 catalog with empty namespace") { + withTable(s"$catalog.table") { + spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) USING foo") + runShowTablesSql(s"SHOW TABLES FROM $catalog", Seq(Row("", "table"))) + } + } } From c07e08b43e5ab089f129cee68a6fe5add8e1311b Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 10:43:07 +0300 Subject: [PATCH 09/46] Move "namespace not specified and default v2 catalog not set - fallback to v1" --- .../sql/connector/DataSourceV2SQLSuite.scala | 20 ------------------- .../command/v1/ShowTablesSuite.scala | 11 ++++++++++ .../command/v2/ShowTablesSuite.scala | 11 ++++++++++ 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala index 440baa5894d08..6930e802639b5 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala @@ -898,26 +898,6 @@ class DataSourceV2SQLSuite } } - test("ShowTables: namespace is not specified and default v2 catalog is set") { - spark.conf.set(SQLConf.DEFAULT_CATALOG.key, "testcat") - spark.sql("CREATE TABLE testcat.table (id bigint, data string) USING foo") - - // v2 catalog is used where default namespace is empty for TestInMemoryTableCatalog. - runShowTablesSql("SHOW TABLES", Seq(Row("", "table"))) - } - - test("ShowTables: namespace not specified and default v2 catalog not set - fallback to v1") { - runShowTablesSql( - "SHOW TABLES", - Seq(Row("", "source", true), Row("", "source2", true)), - expectV2Catalog = false) - - runShowTablesSql( - "SHOW TABLES LIKE '*2'", - Seq(Row("", "source2", true)), - expectV2Catalog = false) - } - test("ShowTables: change current catalog and namespace with USE statements") { sql("CREATE TABLE testcat.ns1.ns2.table (id bigint) USING foo") diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 0cc6d2c239f7a..f0946bdbae67c 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -90,4 +90,15 @@ class ShowTablesSuite extends CommonShowTablesSuite { } assert(exception.getMessage.contains("The database name is not valid: a.b")) } + + test("ShowTables: namespace not specified and default v2 catalog not set - fallback to v1") { + withTable("source", "source2") { + val df = spark.createDataFrame(Seq((1L, "a"), (2L, "b"), (3L, "c"))).toDF("id", "data") + df.createOrReplaceTempView("source") + val df2 = spark.createDataFrame(Seq((4L, "d"), (5L, "e"), (6L, "f"))).toDF("id", "data") + df2.createOrReplaceTempView("source2") + runShowTablesSql("SHOW TABLES", Seq(Row("", "source", true), Row("", "source2", true))) + runShowTablesSql("SHOW TABLES LIKE '*2'", Seq(Row("", "source2", true))) + } + } } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index b04249af54e97..985de9250a846 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -21,6 +21,7 @@ import org.apache.spark.SparkConf import org.apache.spark.sql.{AnalysisException, QueryTest, Row} import org.apache.spark.sql.connector.InMemoryTableCatalog import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} +import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.test.SharedSparkSession import org.apache.spark.sql.types.{StringType, StructType} @@ -112,4 +113,14 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT runShowTablesSql(s"SHOW TABLES FROM $catalog", Seq(Row("", "table"))) } } + + test("ShowTables: namespace is not specified and default v2 catalog is set") { + withSQLConf(SQLConf.DEFAULT_CATALOG.key -> catalog) { + withTable(s"$catalog.table") { + spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) USING foo") + // v2 catalog is used where default namespace is empty for TestInMemoryTableCatalog. + runShowTablesSql("SHOW TABLES", Seq(Row("", "table"))) + } + } + } } From 7738473dc3e62b603d27b99d843d8d50ebd6fa3f Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 10:58:53 +0300 Subject: [PATCH 10/46] Move "SHOW TABLE EXTENDED valid v1" --- .../sql/connector/DataSourceV2SQLSuite.scala | 17 ---------- .../command/v1/ShowTablesSuite.scala | 33 +++++++++++++++---- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala index 6930e802639b5..457de9330456d 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala @@ -962,23 +962,6 @@ class DataSourceV2SQLSuite } } - test("SHOW TABLE EXTENDED valid v1") { - val expected = Seq(Row("", "source", true), Row("", "source2", true)) - val schema = new StructType() - .add("database", StringType, nullable = false) - .add("tableName", StringType, nullable = false) - .add("isTemporary", BooleanType, nullable = false) - .add("information", StringType, nullable = false) - - val df = sql("SHOW TABLE EXTENDED FROM default LIKE '*source*'") - val result = df.collect() - val resultWithoutInfo = result.map{ case Row(db, table, temp, _) => Row(db, table, temp)} - - assert(df.schema === schema) - assert(resultWithoutInfo === expected) - result.foreach{ case Row(_, _, _, info: String) => assert(info.nonEmpty)} - } - test("CreateNameSpace: basic tests") { // Session catalog is used. withNamespace("ns") { diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index f0946bdbae67c..1dfa8aad8d001 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -56,12 +56,18 @@ class ShowTablesSuite extends CommonShowTablesSuite { assert(msg.contains("Database 'bad_test' not found")) } - test("ShowTables: using v1 catalog") { + private def withSourceViews(f: => Unit): Unit = { withTable("source", "source2") { val df = spark.createDataFrame(Seq((1L, "a"), (2L, "b"), (3L, "c"))).toDF("id", "data") df.createOrReplaceTempView("source") val df2 = spark.createDataFrame(Seq((4L, "d"), (5L, "e"), (6L, "f"))).toDF("id", "data") df2.createOrReplaceTempView("source2") + f + } + } + + test("ShowTables: using v1 catalog") { + withSourceViews { runShowTablesSql( "SHOW TABLES FROM default", Seq(Row("", "source", true), Row("", "source2", true))) @@ -92,13 +98,28 @@ class ShowTablesSuite extends CommonShowTablesSuite { } test("ShowTables: namespace not specified and default v2 catalog not set - fallback to v1") { - withTable("source", "source2") { - val df = spark.createDataFrame(Seq((1L, "a"), (2L, "b"), (3L, "c"))).toDF("id", "data") - df.createOrReplaceTempView("source") - val df2 = spark.createDataFrame(Seq((4L, "d"), (5L, "e"), (6L, "f"))).toDF("id", "data") - df2.createOrReplaceTempView("source2") + withSourceViews { runShowTablesSql("SHOW TABLES", Seq(Row("", "source", true), Row("", "source2", true))) runShowTablesSql("SHOW TABLES LIKE '*2'", Seq(Row("", "source2", true))) } } + + test("SHOW TABLE EXTENDED valid v1") { + withSourceViews { + val expected = Seq(Row("", "source", true), Row("", "source2", true)) + val schema = new StructType() + .add("database", StringType, nullable = false) + .add("tableName", StringType, nullable = false) + .add("isTemporary", BooleanType, nullable = false) + .add("information", StringType, nullable = false) + + val df = sql("SHOW TABLE EXTENDED FROM default LIKE '*source*'") + val result = df.collect() + val resultWithoutInfo = result.map { case Row(db, table, temp, _) => Row(db, table, temp) } + + assert(df.schema === schema) + assert(resultWithoutInfo === expected) + result.foreach { case Row(_, _, _, info: String) => assert(info.nonEmpty) } + } + } } From 91521ce37ab65550dc4207c57812678d90481c8d Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 11:01:49 +0300 Subject: [PATCH 11/46] Move "SHOW TABLE EXTENDED not valid v1 database" --- .../sql/connector/DataSourceV2SQLSuite.scala | 27 ------------------- .../command/v2/ShowTablesSuite.scala | 27 +++++++++++++++++++ 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala index 457de9330456d..f0915cb7ed2a2 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala @@ -935,33 +935,6 @@ class DataSourceV2SQLSuite assert(expected === df.collect()) } - test("SHOW TABLE EXTENDED not valid v1 database") { - def testV1CommandNamespace(sqlCommand: String, namespace: String): Unit = { - val e = intercept[AnalysisException] { - sql(sqlCommand) - } - assert(e.message.contains(s"The database name is not valid: ${namespace}")) - } - - val namespace = "testcat.ns1.ns2" - val table = "tbl" - withTable(s"$namespace.$table") { - sql(s"CREATE TABLE $namespace.$table (id bigint, data string) " + - s"USING foo PARTITIONED BY (id)") - - testV1CommandNamespace(s"SHOW TABLE EXTENDED FROM $namespace LIKE 'tb*'", - namespace) - testV1CommandNamespace(s"SHOW TABLE EXTENDED IN $namespace LIKE 'tb*'", - namespace) - testV1CommandNamespace("SHOW TABLE EXTENDED " + - s"FROM $namespace LIKE 'tb*' PARTITION(id=1)", - namespace) - testV1CommandNamespace("SHOW TABLE EXTENDED " + - s"IN $namespace LIKE 'tb*' PARTITION(id=1)", - namespace) - } - } - test("CreateNameSpace: basic tests") { // Session catalog is used. withNamespace("ns") { diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 985de9250a846..4ba55456857fb 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -123,4 +123,31 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT } } } + + test("SHOW TABLE EXTENDED not valid v1 database") { + def testV1CommandNamespace(sqlCommand: String, namespace: String): Unit = { + val e = intercept[AnalysisException] { + sql(sqlCommand) + } + assert(e.message.contains(s"The database name is not valid: ${namespace}")) + } + + val namespace = s"$catalog.ns1.ns2" + val table = "tbl" + withTable(s"$namespace.$table") { + sql(s"CREATE TABLE $namespace.$table (id bigint, data string) " + + s"USING foo PARTITIONED BY (id)") + + testV1CommandNamespace(s"SHOW TABLE EXTENDED FROM $namespace LIKE 'tb*'", + namespace) + testV1CommandNamespace(s"SHOW TABLE EXTENDED IN $namespace LIKE 'tb*'", + namespace) + testV1CommandNamespace("SHOW TABLE EXTENDED " + + s"FROM $namespace LIKE 'tb*' PARTITION(id=1)", + namespace) + testV1CommandNamespace("SHOW TABLE EXTENDED " + + s"IN $namespace LIKE 'tb*' PARTITION(id=1)", + namespace) + } + } } From 79b0fd0638a6adf04597a447907b78e18e3d4592 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 11:44:54 +0300 Subject: [PATCH 12/46] Introduce ShowRow --- .../execution/command/ShowTablesSuite.scala | 13 +++++---- .../command/v1/ShowTablesSuite.scala | 16 ++++++++--- .../command/v2/ShowTablesSuite.scala | 27 ++++++++++++------- 3 files changed, 39 insertions(+), 17 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index ccd724b57c80c..4ec8e64a7c260 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -27,16 +27,19 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { protected def namespace: String = "test" protected def tableColumn: String = "tableName" protected def table: String = "people" + + case class ShowRow(namespace: String, table: String, isTemporary: Boolean) + protected def showSchema: StructType - protected def runShowTablesSql(sqlText: String, expected: Seq[Row]): Unit = { + protected def getRows(showRows: Seq[ShowRow]): Seq[Row] + + protected def runShowTablesSql(sqlText: String, expected: Seq[ShowRow]): Unit = { val df = spark.sql(sqlText) assert(df.schema === showSchema) - assert(df.collect() === expected) + assert(df.collect() === getRows(expected)) } test("show an existing table") { - val tables = sql(s"SHOW TABLES IN $catalog.test") - checkAnswer(tables.select(namespaceColumn), Row(namespace)) - checkAnswer(tables.select(tableColumn), Row(table)) + runShowTablesSql(s"SHOW TABLES IN $catalog.test", Seq(ShowRow(namespace, table, false))) } } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 1dfa8aad8d001..47b8c8332785a 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -30,6 +30,11 @@ class ShowTablesSuite extends CommonShowTablesSuite { .add("tableName", StringType, nullable = false) .add("isTemporary", BooleanType, nullable = false) } + override protected def getRows(showRows: Seq[ShowRow]): Seq[Row] = { + showRows.map { + case ShowRow(namespace, table, isTemporary) => Row(namespace, table, isTemporary) + } + } protected override def beforeAll(): Unit = { super.beforeAll() @@ -70,7 +75,7 @@ class ShowTablesSuite extends CommonShowTablesSuite { withSourceViews { runShowTablesSql( "SHOW TABLES FROM default", - Seq(Row("", "source", true), Row("", "source2", true))) + Seq(ShowRow("", "source", true), ShowRow("", "source2", true))) } } @@ -99,8 +104,13 @@ class ShowTablesSuite extends CommonShowTablesSuite { test("ShowTables: namespace not specified and default v2 catalog not set - fallback to v1") { withSourceViews { - runShowTablesSql("SHOW TABLES", Seq(Row("", "source", true), Row("", "source2", true))) - runShowTablesSql("SHOW TABLES LIKE '*2'", Seq(Row("", "source2", true))) + runShowTablesSql( + "SHOW TABLES", + Seq(ShowRow("", "source", true), + ShowRow("", "source2", true))) + runShowTablesSql( + "SHOW TABLES LIKE '*2'", + Seq(ShowRow("", "source2", true))) } } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 4ba55456857fb..005c55433c1d6 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -33,6 +33,11 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT .add("namespace", StringType, nullable = false) .add("tableName", StringType, nullable = false) } + override protected def getRows(showRows: Seq[ShowRow]): Seq[Row] = { + showRows.map { + case ShowRow(namespace, table, _) => Row(namespace, table) + } + } override def sparkConf: SparkConf = super.sparkConf .set(s"spark.sql.catalog.$catalog", classOf[InMemoryTableCatalog].getName) @@ -56,14 +61,16 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT test("ShowTables: using v2 catalog") { withTable(s"$catalog.db.table_name") { spark.sql(s"CREATE TABLE $catalog.db.table_name (id bigint, data string) USING foo") - runShowTablesSql(s"SHOW TABLES FROM $catalog.db", Seq(Row("db", "table_name"))) + runShowTablesSql( + s"SHOW TABLES FROM $catalog.db", + Seq(ShowRow("db", "table_name", false))) } withTable(s"$catalog.n1.n2.db") { spark.sql(s"CREATE TABLE $catalog.n1.n2.db.table_name (id bigint, data string) USING foo") runShowTablesSql( s"SHOW TABLES FROM $catalog.n1.n2.db", - Seq(Row("n1.n2.db", "table_name"))) + Seq(ShowRow("n1.n2.db", "table_name", false))) } } @@ -81,17 +88,19 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT runShowTablesSql( s"SHOW TABLES FROM $catalog.db", Seq( - Row("db", "table"), - Row("db", "table_name_1"), - Row("db", "table_name_2"))) + ShowRow("db", "table", false), + ShowRow("db", "table_name_1", false), + ShowRow("db", "table_name_2", false))) runShowTablesSql( s"SHOW TABLES FROM $catalog.db LIKE '*name*'", - Seq(Row("db", "table_name_1"), Row("db", "table_name_2"))) + Seq( + ShowRow("db", "table_name_1", false), + ShowRow("db", "table_name_2", false))) runShowTablesSql( s"SHOW TABLES FROM $catalog.db LIKE '*2'", - Seq(Row("db", "table_name_2"))) + Seq(ShowRow("db", "table_name_2", false))) } } @@ -110,7 +119,7 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT test("ShowTables: using v2 catalog with empty namespace") { withTable(s"$catalog.table") { spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) USING foo") - runShowTablesSql(s"SHOW TABLES FROM $catalog", Seq(Row("", "table"))) + runShowTablesSql(s"SHOW TABLES FROM $catalog", Seq(ShowRow("", "table", false))) } } @@ -119,7 +128,7 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT withTable(s"$catalog.table") { spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) USING foo") // v2 catalog is used where default namespace is empty for TestInMemoryTableCatalog. - runShowTablesSql("SHOW TABLES", Seq(Row("", "table"))) + runShowTablesSql("SHOW TABLES", Seq(ShowRow("", "table", false))) } } } From e282bb563f32e26e5e5c2bce9c4db57599a1324b Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 11:59:06 +0300 Subject: [PATCH 13/46] Move "show tables with a pattern" to common trait --- .../execution/command/ShowTablesSuite.scala | 35 +++++++++++++++++++ .../command/v1/ShowTablesSuite.scala | 1 + .../command/v2/ShowTablesSuite.scala | 31 +--------------- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index 4ec8e64a7c260..b78e0f8d33430 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -23,6 +23,7 @@ import org.apache.spark.sql.types.{BooleanType, StringType, StructType} trait ShowTablesSuite extends QueryTest with SharedSparkSession { protected def catalog: String + protected def defaultUsing: String protected def namespaceColumn: String = "database" protected def namespace: String = "test" protected def tableColumn: String = "tableName" @@ -42,4 +43,38 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { test("show an existing table") { runShowTablesSql(s"SHOW TABLES IN $catalog.test", Seq(ShowRow(namespace, table, false))) } + + test("show tables with a pattern") { + withDatabase(s"$catalog.db", s"$catalog.db2") { + sql(s"CREATE DATABASE $catalog.db") + sql(s"CREATE DATABASE $catalog.db2") + withTable( + s"$catalog.db.table", + s"$catalog.db.table_name_1", + s"$catalog.db.table_name_2", + s"$catalog.db2.table_name_2") { + sql(s"CREATE TABLE $catalog.db.table (id bigint, data string) $defaultUsing") + sql(s"CREATE TABLE $catalog.db.table_name_1 (id bigint, data string) $defaultUsing") + sql(s"CREATE TABLE $catalog.db.table_name_2 (id bigint, data string) $defaultUsing") + sql(s"CREATE TABLE $catalog.db2.table_name_2 (id bigint, data string) $defaultUsing") + + runShowTablesSql( + s"SHOW TABLES FROM $catalog.db", + Seq( + ShowRow("db", "table", false), + ShowRow("db", "table_name_1", false), + ShowRow("db", "table_name_2", false))) + + runShowTablesSql( + s"SHOW TABLES FROM $catalog.db LIKE '*name*'", + Seq( + ShowRow("db", "table_name_1", false), + ShowRow("db", "table_name_2", false))) + + runShowTablesSql( + s"SHOW TABLES FROM $catalog.db LIKE '*2'", + Seq(ShowRow("db", "table_name_2", false))) + } + } + } } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 47b8c8332785a..0b4466220874e 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -24,6 +24,7 @@ import org.apache.spark.sql.types.{BooleanType, StringType, StructType} class ShowTablesSuite extends CommonShowTablesSuite { override def catalog: String = "spark_catalog" + override protected def defaultUsing: String = "USING parquet" override protected def showSchema: StructType = { new StructType() .add("database", StringType, nullable = false) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 005c55433c1d6..3e3c14ef4157b 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -27,6 +27,7 @@ import org.apache.spark.sql.types.{StringType, StructType} class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowTablesSuite { override def catalog: String = "test_catalog_v2" + override protected def defaultUsing: String = "USING foo" override protected def namespaceColumn: String = "namespace" override protected def showSchema: StructType = { new StructType() @@ -74,36 +75,6 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT } } - test("ShowTables: using v2 catalog with a pattern") { - withTable( - s"$catalog.db.table", - s"$catalog.db.table_name_1", - s"$catalog.db.table_name_2", - s"$catalog.db2.table_name_2") { - spark.sql(s"CREATE TABLE $catalog.db.table (id bigint, data string) USING foo") - spark.sql(s"CREATE TABLE $catalog.db.table_name_1 (id bigint, data string) USING foo") - spark.sql(s"CREATE TABLE $catalog.db.table_name_2 (id bigint, data string) USING foo") - spark.sql(s"CREATE TABLE $catalog.db2.table_name_2 (id bigint, data string) USING foo") - - runShowTablesSql( - s"SHOW TABLES FROM $catalog.db", - Seq( - ShowRow("db", "table", false), - ShowRow("db", "table_name_1", false), - ShowRow("db", "table_name_2", false))) - - runShowTablesSql( - s"SHOW TABLES FROM $catalog.db LIKE '*name*'", - Seq( - ShowRow("db", "table_name_1", false), - ShowRow("db", "table_name_2", false))) - - runShowTablesSql( - s"SHOW TABLES FROM $catalog.db LIKE '*2'", - Seq(ShowRow("db", "table_name_2", false))) - } - } - test("ShowTables: using v2 catalog, namespace doesn't exist") { runShowTablesSql(s"SHOW TABLES FROM $catalog.unknown", Seq()) } From 70a578d601001501541b827b8211c15249ff48c4 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 14:47:25 +0300 Subject: [PATCH 14/46] Remove duplicate tests --- .../sql/execution/command/v1/ShowTablesSuite.scala | 13 ++----------- .../sql/execution/command/v2/ShowTablesSuite.scala | 6 +----- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 0b4466220874e..59f26eba05bbc 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -57,9 +57,9 @@ class ShowTablesSuite extends CommonShowTablesSuite { test("show table in a not existing namespace") { val msg = intercept[NoSuchDatabaseException] { - checkAnswer(sql(s"SHOW TABLES IN $catalog.bad_test"), Seq()) + runShowTablesSql(s"SHOW TABLES IN $catalog.unknown", Seq()) }.getMessage - assert(msg.contains("Database 'bad_test' not found")) + assert(msg.contains("Database 'unknown' not found")) } private def withSourceViews(f: => Unit): Unit = { @@ -80,15 +80,6 @@ class ShowTablesSuite extends CommonShowTablesSuite { } } - test("ShowTables: using v1 catalog, db doesn't exist ") { - // 'db' below resolves to a database name for v1 catalog because there is no catalog named - // 'db' and there is no default catalog set. - val exception = intercept[NoSuchDatabaseException] { - runShowTablesSql("SHOW TABLES FROM db", Seq()) - } - assert(exception.getMessage.contains("Database 'db' not found")) - } - test("ShowTables: using v1 catalog, db name with multipartIdentifier ('a.b') is not allowed.") { val exception = intercept[AnalysisException] { runShowTablesSql("SHOW TABLES FROM a.b", Seq()) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 3e3c14ef4157b..4bf14fa18e0ce 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -56,7 +56,7 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT } test("show table in a not existing namespace") { - checkAnswer(sql(s"SHOW TABLES IN $catalog.bad_test"), Seq()) + runShowTablesSql(s"SHOW TABLES IN $catalog.unknown", Seq()) } test("ShowTables: using v2 catalog") { @@ -75,10 +75,6 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT } } - test("ShowTables: using v2 catalog, namespace doesn't exist") { - runShowTablesSql(s"SHOW TABLES FROM $catalog.unknown", Seq()) - } - test("ShowViews: using v2 catalog, command not supported.") { val exception = intercept[AnalysisException] { sql(s"SHOW VIEWS FROM $catalog") From d21dd5efb35d6524a6e8a8444e1634b330e2586d Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 15:06:01 +0300 Subject: [PATCH 15/46] Move "SHOW TABLE EXTENDED for default" to the common trait --- .../execution/command/ShowTablesSuite.scala | 29 +++++++++++++++++++ .../command/v1/ShowTablesSuite.scala | 29 ------------------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index b78e0f8d33430..c845bf6f8afb7 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -40,6 +40,16 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { assert(df.collect() === getRows(expected)) } + protected def withSourceViews(f: => Unit): Unit = { + withTable("source", "source2") { + val df = spark.createDataFrame(Seq((1L, "a"), (2L, "b"), (3L, "c"))).toDF("id", "data") + df.createOrReplaceTempView("source") + val df2 = spark.createDataFrame(Seq((4L, "d"), (5L, "e"), (6L, "f"))).toDF("id", "data") + df2.createOrReplaceTempView("source2") + f + } + } + test("show an existing table") { runShowTablesSql(s"SHOW TABLES IN $catalog.test", Seq(ShowRow(namespace, table, false))) } @@ -77,4 +87,23 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { } } } + + test("SHOW TABLE EXTENDED for default") { + withSourceViews { + val expected = Seq(Row("", "source", true), Row("", "source2", true)) + val schema = new StructType() + .add("database", StringType, nullable = false) + .add("tableName", StringType, nullable = false) + .add("isTemporary", BooleanType, nullable = false) + .add("information", StringType, nullable = false) + + val df = sql("SHOW TABLE EXTENDED FROM default LIKE '*source*'") + val result = df.collect() + val resultWithoutInfo = result.map { case Row(db, table, temp, _) => Row(db, table, temp) } + + assert(df.schema === schema) + assert(resultWithoutInfo === expected) + result.foreach { case Row(_, _, _, info: String) => assert(info.nonEmpty) } + } + } } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 59f26eba05bbc..269579901a56c 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -62,16 +62,6 @@ class ShowTablesSuite extends CommonShowTablesSuite { assert(msg.contains("Database 'unknown' not found")) } - private def withSourceViews(f: => Unit): Unit = { - withTable("source", "source2") { - val df = spark.createDataFrame(Seq((1L, "a"), (2L, "b"), (3L, "c"))).toDF("id", "data") - df.createOrReplaceTempView("source") - val df2 = spark.createDataFrame(Seq((4L, "d"), (5L, "e"), (6L, "f"))).toDF("id", "data") - df2.createOrReplaceTempView("source2") - f - } - } - test("ShowTables: using v1 catalog") { withSourceViews { runShowTablesSql( @@ -105,23 +95,4 @@ class ShowTablesSuite extends CommonShowTablesSuite { Seq(ShowRow("", "source2", true))) } } - - test("SHOW TABLE EXTENDED valid v1") { - withSourceViews { - val expected = Seq(Row("", "source", true), Row("", "source2", true)) - val schema = new StructType() - .add("database", StringType, nullable = false) - .add("tableName", StringType, nullable = false) - .add("isTemporary", BooleanType, nullable = false) - .add("information", StringType, nullable = false) - - val df = sql("SHOW TABLE EXTENDED FROM default LIKE '*source*'") - val result = df.collect() - val resultWithoutInfo = result.map { case Row(db, table, temp, _) => Row(db, table, temp) } - - assert(df.schema === schema) - assert(resultWithoutInfo === expected) - result.foreach { case Row(_, _, _, info: String) => assert(info.nonEmpty) } - } - } } From 48509fb77e52eb11a3636303e804e3b517a56722 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 15:12:58 +0300 Subject: [PATCH 16/46] Change default USING in v2 --- .../sql/execution/command/v2/ShowTablesSuite.scala | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 4bf14fa18e0ce..14188edbb9b7f 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -46,7 +46,7 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT protected override def beforeAll(): Unit = { super.beforeAll() sql(s"CREATE DATABASE $catalog.$namespace") - sql(s"CREATE TABLE $catalog.$namespace.$table (name STRING, id INT) USING PARQUET") + sql(s"CREATE TABLE $catalog.$namespace.$table (name STRING, id INT) $defaultUsing") } protected override def afterAll(): Unit = { @@ -61,14 +61,14 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT test("ShowTables: using v2 catalog") { withTable(s"$catalog.db.table_name") { - spark.sql(s"CREATE TABLE $catalog.db.table_name (id bigint, data string) USING foo") + spark.sql(s"CREATE TABLE $catalog.db.table_name (id bigint, data string) $defaultUsing") runShowTablesSql( s"SHOW TABLES FROM $catalog.db", Seq(ShowRow("db", "table_name", false))) } withTable(s"$catalog.n1.n2.db") { - spark.sql(s"CREATE TABLE $catalog.n1.n2.db.table_name (id bigint, data string) USING foo") + spark.sql(s"CREATE TABLE $catalog.n1.n2.db.table_name (id bigint, data string) $defaultUsing") runShowTablesSql( s"SHOW TABLES FROM $catalog.n1.n2.db", Seq(ShowRow("n1.n2.db", "table_name", false))) @@ -85,7 +85,7 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT test("ShowTables: using v2 catalog with empty namespace") { withTable(s"$catalog.table") { - spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) USING foo") + spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) $defaultUsing") runShowTablesSql(s"SHOW TABLES FROM $catalog", Seq(ShowRow("", "table", false))) } } @@ -93,7 +93,7 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT test("ShowTables: namespace is not specified and default v2 catalog is set") { withSQLConf(SQLConf.DEFAULT_CATALOG.key -> catalog) { withTable(s"$catalog.table") { - spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) USING foo") + spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) $defaultUsing") // v2 catalog is used where default namespace is empty for TestInMemoryTableCatalog. runShowTablesSql("SHOW TABLES", Seq(ShowRow("", "table", false))) } @@ -112,7 +112,7 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT val table = "tbl" withTable(s"$namespace.$table") { sql(s"CREATE TABLE $namespace.$table (id bigint, data string) " + - s"USING foo PARTITIONED BY (id)") + s"$defaultUsing PARTITIONED BY (id)") testV1CommandNamespace(s"SHOW TABLE EXTENDED FROM $namespace LIKE 'tb*'", namespace) From b5a6187c332845f0cc2bdc892c7fb36b1a19ebd8 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 15:19:45 +0300 Subject: [PATCH 17/46] Add a comment for "show tables in nested namespaces" --- .../sql/execution/command/v2/ShowTablesSuite.scala | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 14188edbb9b7f..71cd5eabbcdd8 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -59,14 +59,10 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT runShowTablesSql(s"SHOW TABLES IN $catalog.unknown", Seq()) } - test("ShowTables: using v2 catalog") { - withTable(s"$catalog.db.table_name") { - spark.sql(s"CREATE TABLE $catalog.db.table_name (id bigint, data string) $defaultUsing") - runShowTablesSql( - s"SHOW TABLES FROM $catalog.db", - Seq(ShowRow("db", "table_name", false))) - } - + // The test fails for V1 catalog with the error: + // org.apache.spark.sql.AnalysisException: + // The namespace in session catalog must have exactly one name part: spark_catalog.n1.n2.db + test("show tables in nested namespaces") { withTable(s"$catalog.n1.n2.db") { spark.sql(s"CREATE TABLE $catalog.n1.n2.db.table_name (id bigint, data string) $defaultUsing") runShowTablesSql( From bdc249a2dc9ccdaeb8e45dd45ab2848fc483179c Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 15:23:15 +0300 Subject: [PATCH 18/46] Add comment for "show table in a not existing namespace" --- .../apache/spark/sql/execution/command/v1/ShowTablesSuite.scala | 1 + .../apache/spark/sql/execution/command/v2/ShowTablesSuite.scala | 1 + 2 files changed, 2 insertions(+) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 269579901a56c..4dbb730942a0a 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -55,6 +55,7 @@ class ShowTablesSuite extends CommonShowTablesSuite { checkAnswer(tables.select("isTemporary"), Row(false)) } + // `SHOW TABLES` returns empty result in V2 catalog instead of throwing the exception. test("show table in a not existing namespace") { val msg = intercept[NoSuchDatabaseException] { runShowTablesSql(s"SHOW TABLES IN $catalog.unknown", Seq()) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 71cd5eabbcdd8..a06f1c1e28ad3 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -55,6 +55,7 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT super.afterAll() } + // The test fails with the exception `NoSuchDatabaseException` in V1 catalog. test("show table in a not existing namespace") { runShowTablesSql(s"SHOW TABLES IN $catalog.unknown", Seq()) } From ef2b53634a2cbf7c46f2754fc1dd6d2bd2e71c96 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 15:24:15 +0300 Subject: [PATCH 19/46] Remove duplicate test from v1 --- .../spark/sql/execution/command/v1/ShowTablesSuite.scala | 6 ------ 1 file changed, 6 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 4dbb730942a0a..f2e0676aa88c8 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -49,12 +49,6 @@ class ShowTablesSuite extends CommonShowTablesSuite { super.afterAll() } - test("show an existing table in V1 catalog") { - val tables = sql(s"SHOW TABLES IN $catalog.test") - assert(tables.schema.fieldNames.toSet === Set(namespaceColumn, tableColumn, "isTemporary")) - checkAnswer(tables.select("isTemporary"), Row(false)) - } - // `SHOW TABLES` returns empty result in V2 catalog instead of throwing the exception. test("show table in a not existing namespace") { val msg = intercept[NoSuchDatabaseException] { From 122faa5df3f0ccac5d48f1341130b15bc540023c Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 15:38:38 +0300 Subject: [PATCH 20/46] Move SHOW VIEWS test back to DataSourceV2SQLSuite --- .../spark/sql/connector/DataSourceV2SQLSuite.scala | 9 +++++++++ .../spark/sql/execution/command/v2/ShowTablesSuite.scala | 8 -------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala index f0915cb7ed2a2..96ad5e3d60b56 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala @@ -898,6 +898,15 @@ class DataSourceV2SQLSuite } } + test("ShowViews: using v2 catalog, command not supported.") { + val exception = intercept[AnalysisException] { + sql("SHOW VIEWS FROM testcat") + } + + assert(exception.getMessage.contains("Catalog testcat doesn't support SHOW VIEWS," + + " only SessionCatalog supports this command.")) + } + test("ShowTables: change current catalog and namespace with USE statements") { sql("CREATE TABLE testcat.ns1.ns2.table (id bigint) USING foo") diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index a06f1c1e28ad3..ad7e778b9a37f 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -72,14 +72,6 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT } } - test("ShowViews: using v2 catalog, command not supported.") { - val exception = intercept[AnalysisException] { - sql(s"SHOW VIEWS FROM $catalog") - } - assert(exception.getMessage.contains(s"Catalog $catalog doesn't support SHOW VIEWS," + - " only SessionCatalog supports this command.")) - } - test("ShowTables: using v2 catalog with empty namespace") { withTable(s"$catalog.table") { spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) $defaultUsing") From 3016f69b7b6beb9186f0d1dacf57bbe373cbdb12 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 15:41:51 +0300 Subject: [PATCH 21/46] Add comment for the test "using v2 catalog with empty namespace" --- .../spark/sql/execution/command/v2/ShowTablesSuite.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index ad7e778b9a37f..d68bf9d0fd91c 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -72,7 +72,10 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT } } - test("ShowTables: using v2 catalog with empty namespace") { + // The test fails for V1 catalog with the error: + // org.apache.spark.sql.AnalysisException: + // The namespace in session catalog must have exactly one name part: spark_catalog.table + test("using v2 catalog with empty namespace") { withTable(s"$catalog.table") { spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) $defaultUsing") runShowTablesSql(s"SHOW TABLES FROM $catalog", Seq(ShowRow("", "table", false))) From 53d0c778a288a0d9bdeeacc316d50bcfd8b61762 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 15:45:05 +0300 Subject: [PATCH 22/46] Add comment for the test "namespace is not specified and default v2 catalog is set" --- .../spark/sql/execution/command/v2/ShowTablesSuite.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index d68bf9d0fd91c..c8380131021c2 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -82,7 +82,10 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT } } - test("ShowTables: namespace is not specified and default v2 catalog is set") { + // The test fails for V1 catalog with the error: + // org.apache.spark.sql.AnalysisException: + // The namespace in session catalog must have exactly one name part: spark_catalog.table + test("namespace is not specified and default v2 catalog is set") { withSQLConf(SQLConf.DEFAULT_CATALOG.key -> catalog) { withTable(s"$catalog.table") { spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) $defaultUsing") From ebfa6704c59feb16fd766848cf1c15f633fd49ed Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 15:48:25 +0300 Subject: [PATCH 23/46] Add comment for the test "SHOW TABLE EXTENDED not valid v1 database" --- .../spark/sql/execution/command/v2/ShowTablesSuite.scala | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index c8380131021c2..f13368d83a45b 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -95,6 +95,9 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT } } + // The test fails for V1 catalog with the error: + // org.apache.spark.sql.AnalysisException: + // The namespace in session catalog must have exactly one name part: spark_catalog.ns1.ns2.tbl test("SHOW TABLE EXTENDED not valid v1 database") { def testV1CommandNamespace(sqlCommand: String, namespace: String): Unit = { val e = intercept[AnalysisException] { From dbf8e76ebd5b7c4f7523ed2adfadbfe1366a07a1 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 16:00:49 +0300 Subject: [PATCH 24/46] Minor: fix test titles --- .../spark/sql/execution/command/v1/ShowTablesSuite.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index f2e0676aa88c8..ed8c44be2b619 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -57,7 +57,8 @@ class ShowTablesSuite extends CommonShowTablesSuite { assert(msg.contains("Database 'unknown' not found")) } - test("ShowTables: using v1 catalog") { + // `SHOW TABLES` from v2 catalog returns empty result. + test("show views from v1 catalog") { withSourceViews { runShowTablesSql( "SHOW TABLES FROM default", @@ -65,7 +66,7 @@ class ShowTablesSuite extends CommonShowTablesSuite { } } - test("ShowTables: using v1 catalog, db name with multipartIdentifier ('a.b') is not allowed.") { + test("using v1 catalog, db name with multipartIdentifier ('a.b') is not allowed.") { val exception = intercept[AnalysisException] { runShowTablesSql("SHOW TABLES FROM a.b", Seq()) } From 18296854255f355a41f7d1b5b32fb2c1bd7d4d42 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 16:04:53 +0300 Subject: [PATCH 25/46] Move a show view test back to DataSourceV2SQLSuite --- .../apache/spark/sql/connector/DataSourceV2SQLSuite.scala | 8 ++++++++ .../spark/sql/execution/command/v1/ShowTablesSuite.scala | 7 ------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala index 96ad5e3d60b56..7f4dbfd9347e4 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala @@ -898,6 +898,14 @@ class DataSourceV2SQLSuite } } + test("ShowViews: using v1 catalog, db name with multipartIdentifier ('a.b') is not allowed.") { + val exception = intercept[AnalysisException] { + sql("SHOW VIEWS FROM a.b") + } + + assert(exception.getMessage.contains("The database name is not valid: a.b")) + } + test("ShowViews: using v2 catalog, command not supported.") { val exception = intercept[AnalysisException] { sql("SHOW VIEWS FROM testcat") diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index ed8c44be2b619..f8c9d1d22d915 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -73,13 +73,6 @@ class ShowTablesSuite extends CommonShowTablesSuite { assert(exception.getMessage.contains("The database name is not valid: a.b")) } - test("ShowViews: using v1 catalog, db name with multipartIdentifier ('a.b') is not allowed.") { - val exception = intercept[AnalysisException] { - sql("SHOW TABLES FROM a.b") - } - assert(exception.getMessage.contains("The database name is not valid: a.b")) - } - test("ShowTables: namespace not specified and default v2 catalog not set - fallback to v1") { withSourceViews { runShowTablesSql( From 7d3b4b2f01d20e2782ca1d8f848e1b680a9f55da Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 16:05:50 +0300 Subject: [PATCH 26/46] Fix test title --- .../apache/spark/sql/execution/command/v1/ShowTablesSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index f8c9d1d22d915..967c7d22296cd 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -73,7 +73,7 @@ class ShowTablesSuite extends CommonShowTablesSuite { assert(exception.getMessage.contains("The database name is not valid: a.b")) } - test("ShowTables: namespace not specified and default v2 catalog not set - fallback to v1") { + test("namespace not specified and default v2 catalog not set - fallback to v1") { withSourceViews { runShowTablesSql( "SHOW TABLES", From 80eb4a83aaf9831c7adff6b3b9bb83bf57debf49 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 16:08:31 +0300 Subject: [PATCH 27/46] Refactoring --- .../sql/execution/command/ShowTablesSuite.scala | 12 ++++++++++++ .../sql/execution/command/v1/ShowTablesSuite.scala | 12 ------------ .../sql/execution/command/v2/ShowTablesSuite.scala | 12 ------------ 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index c845bf6f8afb7..31b0a030f6876 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -50,6 +50,18 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { } } + protected override def beforeAll(): Unit = { + super.beforeAll() + sql(s"CREATE DATABASE $catalog.$namespace") + sql(s"CREATE TABLE $catalog.$namespace.$table (name STRING, id INT) $defaultUsing") + } + + protected override def afterAll(): Unit = { + sql(s"DROP TABLE $catalog.$namespace.$table") + sql(s"DROP DATABASE $catalog.$namespace") + super.afterAll() + } + test("show an existing table") { runShowTablesSql(s"SHOW TABLES IN $catalog.test", Seq(ShowRow(namespace, table, false))) } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 967c7d22296cd..d7d4a15794267 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -37,18 +37,6 @@ class ShowTablesSuite extends CommonShowTablesSuite { } } - protected override def beforeAll(): Unit = { - super.beforeAll() - sql(s"CREATE DATABASE $namespace") - sql(s"CREATE TABLE $namespace.$table (name STRING, id INT) USING PARQUET") - } - - protected override def afterAll(): Unit = { - sql(s"DROP TABLE $namespace.$table") - sql(s"DROP DATABASE $namespace") - super.afterAll() - } - // `SHOW TABLES` returns empty result in V2 catalog instead of throwing the exception. test("show table in a not existing namespace") { val msg = intercept[NoSuchDatabaseException] { diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index f13368d83a45b..3e8a229f019c6 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -43,18 +43,6 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT override def sparkConf: SparkConf = super.sparkConf .set(s"spark.sql.catalog.$catalog", classOf[InMemoryTableCatalog].getName) - protected override def beforeAll(): Unit = { - super.beforeAll() - sql(s"CREATE DATABASE $catalog.$namespace") - sql(s"CREATE TABLE $catalog.$namespace.$table (name STRING, id INT) $defaultUsing") - } - - protected override def afterAll(): Unit = { - sql(s"DROP TABLE $catalog.$namespace.$table") - sql(s"DROP DATABASE $catalog.$namespace") - super.afterAll() - } - // The test fails with the exception `NoSuchDatabaseException` in V1 catalog. test("show table in a not existing namespace") { runShowTablesSql(s"SHOW TABLES IN $catalog.unknown", Seq()) From f0152e6e51c335253dd1459728a754e1d50ae189 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 16:10:24 +0300 Subject: [PATCH 28/46] Minor changes --- .../spark/sql/execution/command/v1/ShowTablesSuite.scala | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index d7d4a15794267..dd598e0aa208c 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -65,11 +65,8 @@ class ShowTablesSuite extends CommonShowTablesSuite { withSourceViews { runShowTablesSql( "SHOW TABLES", - Seq(ShowRow("", "source", true), - ShowRow("", "source2", true))) - runShowTablesSql( - "SHOW TABLES LIKE '*2'", - Seq(ShowRow("", "source2", true))) + Seq(ShowRow("", "source", true), ShowRow("", "source2", true))) + runShowTablesSql("SHOW TABLES LIKE '*2'", Seq(ShowRow("", "source2", true))) } } } From 8e55686b5f7a5dddf884d24ebd49870445bb8fc2 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 16:12:34 +0300 Subject: [PATCH 29/46] Using foo -> Using _ --- .../apache/spark/sql/execution/command/v2/ShowTablesSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 3e8a229f019c6..02811794c0b4e 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -27,7 +27,7 @@ import org.apache.spark.sql.types.{StringType, StructType} class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowTablesSuite { override def catalog: String = "test_catalog_v2" - override protected def defaultUsing: String = "USING foo" + override protected def defaultUsing: String = "USING _" override protected def namespaceColumn: String = "namespace" override protected def showSchema: StructType = { new StructType() From 4364a02114686ed0d0b21439740a8ef3bea2cd49 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 20:28:03 +0300 Subject: [PATCH 30/46] Remove beforeAll() --- .../execution/command/ShowTablesSuite.scala | 26 +++++++------------ .../command/v2/ShowTablesSuite.scala | 1 - 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index 31b0a030f6876..7d2ea22f05584 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -24,10 +24,6 @@ import org.apache.spark.sql.types.{BooleanType, StringType, StructType} trait ShowTablesSuite extends QueryTest with SharedSparkSession { protected def catalog: String protected def defaultUsing: String - protected def namespaceColumn: String = "database" - protected def namespace: String = "test" - protected def tableColumn: String = "tableName" - protected def table: String = "people" case class ShowRow(namespace: String, table: String, isTemporary: Boolean) @@ -50,20 +46,16 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { } } - protected override def beforeAll(): Unit = { - super.beforeAll() - sql(s"CREATE DATABASE $catalog.$namespace") - sql(s"CREATE TABLE $catalog.$namespace.$table (name STRING, id INT) $defaultUsing") - } - - protected override def afterAll(): Unit = { - sql(s"DROP TABLE $catalog.$namespace.$table") - sql(s"DROP DATABASE $catalog.$namespace") - super.afterAll() - } - test("show an existing table") { - runShowTablesSql(s"SHOW TABLES IN $catalog.test", Seq(ShowRow(namespace, table, false))) + val namespace = "test" + val table = "people" + withDatabase(s"$catalog.$namespace") { + sql(s"CREATE DATABASE $catalog.$namespace") + withTable(s"$catalog.$namespace.$table") { + sql(s"CREATE TABLE $catalog.$namespace.$table (name STRING, id INT) $defaultUsing") + runShowTablesSql(s"SHOW TABLES IN $catalog.test", Seq(ShowRow(namespace, table, false))) + } + } } test("show tables with a pattern") { diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 02811794c0b4e..9d3fcbafd6514 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -28,7 +28,6 @@ import org.apache.spark.sql.types.{StringType, StructType} class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowTablesSuite { override def catalog: String = "test_catalog_v2" override protected def defaultUsing: String = "USING _" - override protected def namespaceColumn: String = "namespace" override protected def showSchema: StructType = { new StructType() .add("namespace", StringType, nullable = false) From 529a8582756e74a1741f53211e3e72ea967335d7 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 21:03:02 +0300 Subject: [PATCH 31/46] Move parsing tests to the common trait --- .../sql/catalyst/parser/DDLParserSuite.scala | 49 --------------- .../execution/command/ShowTablesSuite.scala | 59 +++++++++++++++++-- 2 files changed, 55 insertions(+), 53 deletions(-) diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/DDLParserSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/DDLParserSuite.scala index aca7602bdbcb0..7b4920f8bb12a 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/DDLParserSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/parser/DDLParserSuite.scala @@ -1233,55 +1233,6 @@ class DDLParserSuite extends AnalysisTest { assert(exc.getMessage.contains("There must be at least one WHEN clause in a MERGE statement")) } - test("show tables") { - comparePlans( - parsePlan("SHOW TABLES"), - ShowTables(UnresolvedNamespace(Seq.empty[String]), None)) - comparePlans( - parsePlan("SHOW TABLES '*test*'"), - ShowTables(UnresolvedNamespace(Seq.empty[String]), Some("*test*"))) - comparePlans( - parsePlan("SHOW TABLES LIKE '*test*'"), - ShowTables(UnresolvedNamespace(Seq.empty[String]), Some("*test*"))) - comparePlans( - parsePlan("SHOW TABLES FROM testcat.ns1.ns2.tbl"), - ShowTables(UnresolvedNamespace(Seq("testcat", "ns1", "ns2", "tbl")), None)) - comparePlans( - parsePlan("SHOW TABLES IN testcat.ns1.ns2.tbl"), - ShowTables(UnresolvedNamespace(Seq("testcat", "ns1", "ns2", "tbl")), None)) - comparePlans( - parsePlan("SHOW TABLES IN ns1 '*test*'"), - ShowTables(UnresolvedNamespace(Seq("ns1")), Some("*test*"))) - comparePlans( - parsePlan("SHOW TABLES IN ns1 LIKE '*test*'"), - ShowTables(UnresolvedNamespace(Seq("ns1")), Some("*test*"))) - } - - test("show table extended") { - comparePlans( - parsePlan("SHOW TABLE EXTENDED LIKE '*test*'"), - ShowTableStatement(None, "*test*", None)) - comparePlans( - parsePlan("SHOW TABLE EXTENDED FROM testcat.ns1.ns2 LIKE '*test*'"), - ShowTableStatement(Some(Seq("testcat", "ns1", "ns2")), "*test*", None)) - comparePlans( - parsePlan("SHOW TABLE EXTENDED IN testcat.ns1.ns2 LIKE '*test*'"), - ShowTableStatement(Some(Seq("testcat", "ns1", "ns2")), "*test*", None)) - comparePlans( - parsePlan("SHOW TABLE EXTENDED LIKE '*test*' PARTITION(ds='2008-04-09', hr=11)"), - ShowTableStatement(None, "*test*", Some(Map("ds" -> "2008-04-09", "hr" -> "11")))) - comparePlans( - parsePlan("SHOW TABLE EXTENDED FROM testcat.ns1.ns2 LIKE '*test*' " + - "PARTITION(ds='2008-04-09')"), - ShowTableStatement(Some(Seq("testcat", "ns1", "ns2")), "*test*", - Some(Map("ds" -> "2008-04-09")))) - comparePlans( - parsePlan("SHOW TABLE EXTENDED IN testcat.ns1.ns2 LIKE '*test*' " + - "PARTITION(ds='2008-04-09')"), - ShowTableStatement(Some(Seq("testcat", "ns1", "ns2")), "*test*", - Some(Map("ds" -> "2008-04-09")))) - } - test("show views") { comparePlans( parsePlan("SHOW VIEWS"), diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index 7d2ea22f05584..a14db8b915d36 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -18,17 +18,19 @@ package org.apache.spark.sql.execution.command import org.apache.spark.sql.{QueryTest, Row} +import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, UnresolvedNamespace} +import org.apache.spark.sql.catalyst.parser.CatalystSqlParser.parsePlan +import org.apache.spark.sql.catalyst.plans.logical.{ShowTables, ShowTableStatement} import org.apache.spark.sql.test.SharedSparkSession import org.apache.spark.sql.types.{BooleanType, StringType, StructType} -trait ShowTablesSuite extends QueryTest with SharedSparkSession { +trait ShowTablesSuite extends QueryTest with SharedSparkSession with AnalysisTest { protected def catalog: String protected def defaultUsing: String - case class ShowRow(namespace: String, table: String, isTemporary: Boolean) - - protected def showSchema: StructType protected def getRows(showRows: Seq[ShowRow]): Seq[Row] + // Gets the schema of `SHOW TABLES` + protected def showSchema: StructType protected def runShowTablesSql(sqlText: String, expected: Seq[ShowRow]): Unit = { val df = spark.sql(sqlText) @@ -46,6 +48,55 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { } } + test("show tables") { + comparePlans( + parsePlan("SHOW TABLES"), + ShowTables(UnresolvedNamespace(Seq.empty[String]), None)) + comparePlans( + parsePlan("SHOW TABLES '*test*'"), + ShowTables(UnresolvedNamespace(Seq.empty[String]), Some("*test*"))) + comparePlans( + parsePlan("SHOW TABLES LIKE '*test*'"), + ShowTables(UnresolvedNamespace(Seq.empty[String]), Some("*test*"))) + comparePlans( + parsePlan(s"SHOW TABLES FROM $catalog.ns1.ns2.tbl"), + ShowTables(UnresolvedNamespace(Seq(catalog, "ns1", "ns2", "tbl")), None)) + comparePlans( + parsePlan(s"SHOW TABLES IN $catalog.ns1.ns2.tbl"), + ShowTables(UnresolvedNamespace(Seq(catalog, "ns1", "ns2", "tbl")), None)) + comparePlans( + parsePlan("SHOW TABLES IN ns1 '*test*'"), + ShowTables(UnresolvedNamespace(Seq("ns1")), Some("*test*"))) + comparePlans( + parsePlan("SHOW TABLES IN ns1 LIKE '*test*'"), + ShowTables(UnresolvedNamespace(Seq("ns1")), Some("*test*"))) + } + + test("show table extended") { + comparePlans( + parsePlan("SHOW TABLE EXTENDED LIKE '*test*'"), + ShowTableStatement(None, "*test*", None)) + comparePlans( + parsePlan(s"SHOW TABLE EXTENDED FROM $catalog.ns1.ns2 LIKE '*test*'"), + ShowTableStatement(Some(Seq(catalog, "ns1", "ns2")), "*test*", None)) + comparePlans( + parsePlan(s"SHOW TABLE EXTENDED IN $catalog.ns1.ns2 LIKE '*test*'"), + ShowTableStatement(Some(Seq(catalog, "ns1", "ns2")), "*test*", None)) + comparePlans( + parsePlan("SHOW TABLE EXTENDED LIKE '*test*' PARTITION(ds='2008-04-09', hr=11)"), + ShowTableStatement(None, "*test*", Some(Map("ds" -> "2008-04-09", "hr" -> "11")))) + comparePlans( + parsePlan(s"SHOW TABLE EXTENDED FROM $catalog.ns1.ns2 LIKE '*test*' " + + "PARTITION(ds='2008-04-09')"), + ShowTableStatement(Some(Seq(catalog, "ns1", "ns2")), "*test*", + Some(Map("ds" -> "2008-04-09")))) + comparePlans( + parsePlan(s"SHOW TABLE EXTENDED IN $catalog.ns1.ns2 LIKE '*test*' " + + "PARTITION(ds='2008-04-09')"), + ShowTableStatement(Some(Seq(catalog, "ns1", "ns2")), "*test*", + Some(Map("ds" -> "2008-04-09")))) + } + test("show an existing table") { val namespace = "test" val table = "people" From 947c8bc847b2eed260199f153fd8cd829d4a8fc3 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Sat, 7 Nov 2020 21:26:32 +0300 Subject: [PATCH 32/46] Move the current catalog test --- .../sql/connector/DataSourceV2SQLSuite.scala | 17 ----------------- .../execution/command/v2/ShowTablesSuite.scala | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala index 7f4dbfd9347e4..6c56535d93074 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2SQLSuite.scala @@ -915,23 +915,6 @@ class DataSourceV2SQLSuite " only SessionCatalog supports this command.")) } - test("ShowTables: change current catalog and namespace with USE statements") { - sql("CREATE TABLE testcat.ns1.ns2.table (id bigint) USING foo") - - // Initially, the v2 session catalog (current catalog) is used. - runShowTablesSql( - "SHOW TABLES", Seq(Row("", "source", true), Row("", "source2", true)), - expectV2Catalog = false) - - // Update the current catalog, and no table is matched since the current namespace is Array(). - sql("USE testcat") - runShowTablesSql("SHOW TABLES", Seq()) - - // Update the current namespace to match ns1.ns2.table. - sql("USE testcat.ns1.ns2") - runShowTablesSql("SHOW TABLES", Seq(Row("ns1.ns2", "table"))) - } - private def runShowTablesSql( sqlText: String, expected: Seq[Row], diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 9d3fcbafd6514..c7c4b48dc9e64 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -111,4 +111,21 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT namespace) } } + + // The test fails with the error in V1 session catalog: + // org.apache.spark.sql.AnalysisException: + // The namespace in session catalog must have exactly one name part: spark_catalog.ns1.ns2.table + test("change current catalog and namespace with USE statements") { + withTable(s"$catalog.ns1.ns2.table") { + sql(s"CREATE TABLE $catalog.ns1.ns2.table (id bigint) $defaultUsing") + + // Update the current catalog, and no table is matched since the current namespace is Array(). + sql(s"USE $catalog") + runShowTablesSql("SHOW TABLES", Seq()) + + // Update the current namespace to match ns1.ns2.table. + sql(s"USE $catalog.ns1.ns2") + runShowTablesSql("SHOW TABLES", Seq(ShowRow("ns1.ns2", "table", false))) + } + } } From dd1f3358f3cbb336c4fe3db288e848f0c8109963 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Mon, 9 Nov 2020 13:12:49 +0300 Subject: [PATCH 33/46] Add "TODO(SPARK-33393): Support SHOW TABLE EXTENDED in DSv2" --- .../org/apache/spark/sql/execution/command/ShowTablesSuite.scala | 1 + 1 file changed, 1 insertion(+) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index a14db8b915d36..ccd75d0e92a98 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -143,6 +143,7 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession with AnalysisTes } } + // TODO(SPARK-33393): Support SHOW TABLE EXTENDED in DSv2 test("SHOW TABLE EXTENDED for default") { withSourceViews { val expected = Seq(Row("", "source", true), Row("", "source2", true)) From 70922c7d7301fc50bf629e3c1dfabdf33ae22753 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Mon, 9 Nov 2020 13:17:23 +0300 Subject: [PATCH 34/46] Add "TODO(SPARK-33394): Throw `NoSuchDatabaseException` for not existing namespace" --- .../apache/spark/sql/execution/command/v2/ShowTablesSuite.scala | 1 + 1 file changed, 1 insertion(+) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index c7c4b48dc9e64..7275e4161d23a 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -43,6 +43,7 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT .set(s"spark.sql.catalog.$catalog", classOf[InMemoryTableCatalog].getName) // The test fails with the exception `NoSuchDatabaseException` in V1 catalog. + // TODO(SPARK-33394): Throw `NoSuchDatabaseException` for not existing namespace test("show table in a not existing namespace") { runShowTablesSql(s"SHOW TABLES IN $catalog.unknown", Seq()) } From a62486f7640d62259afa9ef4aad912d4d02a8276 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Mon, 9 Nov 2020 13:19:49 +0300 Subject: [PATCH 35/46] Use CatalogManager.SESSION_CATALOG_NAME --- .../spark/sql/execution/command/v1/ShowTablesSuite.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index dd598e0aa208c..4e75cdfb517b4 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -19,11 +19,12 @@ package org.apache.spark.sql.execution.command.v1 import org.apache.spark.sql.{AnalysisException, Row} import org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException +import org.apache.spark.sql.connector.catalog.CatalogManager import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} import org.apache.spark.sql.types.{BooleanType, StringType, StructType} class ShowTablesSuite extends CommonShowTablesSuite { - override def catalog: String = "spark_catalog" + override def catalog: String = CatalogManager.SESSION_CATALOG_NAME override protected def defaultUsing: String = "USING parquet" override protected def showSchema: StructType = { new StructType() From b608f8b3c593373319eae5e68a4bb6f09c99d7dc Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Mon, 9 Nov 2020 13:29:30 +0300 Subject: [PATCH 36/46] Move parsing tests to ShowTablesParserSuite --- .../command/ShowTablesParserSuite.scala | 76 +++++++++++++++++++ .../execution/command/ShowTablesSuite.scala | 54 +------------ 2 files changed, 77 insertions(+), 53 deletions(-) create mode 100644 sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesParserSuite.scala diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesParserSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesParserSuite.scala new file mode 100644 index 0000000000000..16f3dea8d75ef --- /dev/null +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesParserSuite.scala @@ -0,0 +1,76 @@ +/* + * 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.apache.spark.sql.catalyst.analysis.{AnalysisTest, UnresolvedNamespace} +import org.apache.spark.sql.catalyst.parser.CatalystSqlParser.parsePlan +import org.apache.spark.sql.catalyst.plans.logical.{ShowTables, ShowTableStatement} +import org.apache.spark.sql.test.SharedSparkSession + +class ShowTablesParserSuite extends AnalysisTest with SharedSparkSession { + private val catalog = "test_catalog" + + test("show tables") { + comparePlans( + parsePlan("SHOW TABLES"), + ShowTables(UnresolvedNamespace(Seq.empty[String]), None)) + comparePlans( + parsePlan("SHOW TABLES '*test*'"), + ShowTables(UnresolvedNamespace(Seq.empty[String]), Some("*test*"))) + comparePlans( + parsePlan("SHOW TABLES LIKE '*test*'"), + ShowTables(UnresolvedNamespace(Seq.empty[String]), Some("*test*"))) + comparePlans( + parsePlan(s"SHOW TABLES FROM $catalog.ns1.ns2.tbl"), + ShowTables(UnresolvedNamespace(Seq(catalog, "ns1", "ns2", "tbl")), None)) + comparePlans( + parsePlan(s"SHOW TABLES IN $catalog.ns1.ns2.tbl"), + ShowTables(UnresolvedNamespace(Seq(catalog, "ns1", "ns2", "tbl")), None)) + comparePlans( + parsePlan("SHOW TABLES IN ns1 '*test*'"), + ShowTables(UnresolvedNamespace(Seq("ns1")), Some("*test*"))) + comparePlans( + parsePlan("SHOW TABLES IN ns1 LIKE '*test*'"), + ShowTables(UnresolvedNamespace(Seq("ns1")), Some("*test*"))) + } + + test("show table extended") { + comparePlans( + parsePlan("SHOW TABLE EXTENDED LIKE '*test*'"), + ShowTableStatement(None, "*test*", None)) + comparePlans( + parsePlan(s"SHOW TABLE EXTENDED FROM $catalog.ns1.ns2 LIKE '*test*'"), + ShowTableStatement(Some(Seq(catalog, "ns1", "ns2")), "*test*", None)) + comparePlans( + parsePlan(s"SHOW TABLE EXTENDED IN $catalog.ns1.ns2 LIKE '*test*'"), + ShowTableStatement(Some(Seq(catalog, "ns1", "ns2")), "*test*", None)) + comparePlans( + parsePlan("SHOW TABLE EXTENDED LIKE '*test*' PARTITION(ds='2008-04-09', hr=11)"), + ShowTableStatement(None, "*test*", Some(Map("ds" -> "2008-04-09", "hr" -> "11")))) + comparePlans( + parsePlan(s"SHOW TABLE EXTENDED FROM $catalog.ns1.ns2 LIKE '*test*' " + + "PARTITION(ds='2008-04-09')"), + ShowTableStatement(Some(Seq(catalog, "ns1", "ns2")), "*test*", + Some(Map("ds" -> "2008-04-09")))) + comparePlans( + parsePlan(s"SHOW TABLE EXTENDED IN $catalog.ns1.ns2 LIKE '*test*' " + + "PARTITION(ds='2008-04-09')"), + ShowTableStatement(Some(Seq(catalog, "ns1", "ns2")), "*test*", + Some(Map("ds" -> "2008-04-09")))) + } +} diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index ccd75d0e92a98..18f80a0961df4 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -18,13 +18,10 @@ package org.apache.spark.sql.execution.command import org.apache.spark.sql.{QueryTest, Row} -import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, UnresolvedNamespace} -import org.apache.spark.sql.catalyst.parser.CatalystSqlParser.parsePlan -import org.apache.spark.sql.catalyst.plans.logical.{ShowTables, ShowTableStatement} import org.apache.spark.sql.test.SharedSparkSession import org.apache.spark.sql.types.{BooleanType, StringType, StructType} -trait ShowTablesSuite extends QueryTest with SharedSparkSession with AnalysisTest { +trait ShowTablesSuite extends QueryTest with SharedSparkSession { protected def catalog: String protected def defaultUsing: String case class ShowRow(namespace: String, table: String, isTemporary: Boolean) @@ -48,55 +45,6 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession with AnalysisTes } } - test("show tables") { - comparePlans( - parsePlan("SHOW TABLES"), - ShowTables(UnresolvedNamespace(Seq.empty[String]), None)) - comparePlans( - parsePlan("SHOW TABLES '*test*'"), - ShowTables(UnresolvedNamespace(Seq.empty[String]), Some("*test*"))) - comparePlans( - parsePlan("SHOW TABLES LIKE '*test*'"), - ShowTables(UnresolvedNamespace(Seq.empty[String]), Some("*test*"))) - comparePlans( - parsePlan(s"SHOW TABLES FROM $catalog.ns1.ns2.tbl"), - ShowTables(UnresolvedNamespace(Seq(catalog, "ns1", "ns2", "tbl")), None)) - comparePlans( - parsePlan(s"SHOW TABLES IN $catalog.ns1.ns2.tbl"), - ShowTables(UnresolvedNamespace(Seq(catalog, "ns1", "ns2", "tbl")), None)) - comparePlans( - parsePlan("SHOW TABLES IN ns1 '*test*'"), - ShowTables(UnresolvedNamespace(Seq("ns1")), Some("*test*"))) - comparePlans( - parsePlan("SHOW TABLES IN ns1 LIKE '*test*'"), - ShowTables(UnresolvedNamespace(Seq("ns1")), Some("*test*"))) - } - - test("show table extended") { - comparePlans( - parsePlan("SHOW TABLE EXTENDED LIKE '*test*'"), - ShowTableStatement(None, "*test*", None)) - comparePlans( - parsePlan(s"SHOW TABLE EXTENDED FROM $catalog.ns1.ns2 LIKE '*test*'"), - ShowTableStatement(Some(Seq(catalog, "ns1", "ns2")), "*test*", None)) - comparePlans( - parsePlan(s"SHOW TABLE EXTENDED IN $catalog.ns1.ns2 LIKE '*test*'"), - ShowTableStatement(Some(Seq(catalog, "ns1", "ns2")), "*test*", None)) - comparePlans( - parsePlan("SHOW TABLE EXTENDED LIKE '*test*' PARTITION(ds='2008-04-09', hr=11)"), - ShowTableStatement(None, "*test*", Some(Map("ds" -> "2008-04-09", "hr" -> "11")))) - comparePlans( - parsePlan(s"SHOW TABLE EXTENDED FROM $catalog.ns1.ns2 LIKE '*test*' " + - "PARTITION(ds='2008-04-09')"), - ShowTableStatement(Some(Seq(catalog, "ns1", "ns2")), "*test*", - Some(Map("ds" -> "2008-04-09")))) - comparePlans( - parsePlan(s"SHOW TABLE EXTENDED IN $catalog.ns1.ns2 LIKE '*test*' " + - "PARTITION(ds='2008-04-09')"), - ShowTableStatement(Some(Seq(catalog, "ns1", "ns2")), "*test*", - Some(Map("ds" -> "2008-04-09")))) - } - test("show an existing table") { val namespace = "test" val table = "people" From 3a3bc57cd6cb21686effc5120589ce076af03a79 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Mon, 9 Nov 2020 22:53:03 +0300 Subject: [PATCH 37/46] Move the SHOW TABLE EXTENDED test out from the common trait --- .../execution/command/ShowTablesSuite.scala | 20 ------------------- .../command/v1/ShowTablesSuite.scala | 19 ++++++++++++++++++ .../command/v2/ShowTablesSuite.scala | 15 +++++++++++++- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index 18f80a0961df4..e5213d547f5c3 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -90,24 +90,4 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { } } } - - // TODO(SPARK-33393): Support SHOW TABLE EXTENDED in DSv2 - test("SHOW TABLE EXTENDED for default") { - withSourceViews { - val expected = Seq(Row("", "source", true), Row("", "source2", true)) - val schema = new StructType() - .add("database", StringType, nullable = false) - .add("tableName", StringType, nullable = false) - .add("isTemporary", BooleanType, nullable = false) - .add("information", StringType, nullable = false) - - val df = sql("SHOW TABLE EXTENDED FROM default LIKE '*source*'") - val result = df.collect() - val resultWithoutInfo = result.map { case Row(db, table, temp, _) => Row(db, table, temp) } - - assert(df.schema === schema) - assert(resultWithoutInfo === expected) - result.foreach { case Row(_, _, _, info: String) => assert(info.nonEmpty) } - } - } } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 4e75cdfb517b4..5e325d3c9b5fa 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -70,4 +70,23 @@ class ShowTablesSuite extends CommonShowTablesSuite { runShowTablesSql("SHOW TABLES LIKE '*2'", Seq(ShowRow("", "source2", true))) } } + + test("SHOW TABLE EXTENDED for default") { + withSourceViews { + val expected = Seq(Row("", "source", true), Row("", "source2", true)) + val schema = new StructType() + .add("database", StringType, nullable = false) + .add("tableName", StringType, nullable = false) + .add("isTemporary", BooleanType, nullable = false) + .add("information", StringType, nullable = false) + + val df = sql("SHOW TABLE EXTENDED FROM default LIKE '*source*'") + val result = df.collect() + val resultWithoutInfo = result.map { case Row(db, table, temp, _) => Row(db, table, temp) } + + assert(df.schema === schema) + assert(resultWithoutInfo === expected) + result.foreach { case Row(_, _, _, info: String) => assert(info.nonEmpty) } + } + } } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 7275e4161d23a..45200c8de8d5c 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -18,12 +18,13 @@ package org.apache.spark.sql.execution.command.v2 import org.apache.spark.SparkConf +import org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException import org.apache.spark.sql.{AnalysisException, QueryTest, Row} import org.apache.spark.sql.connector.InMemoryTableCatalog import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.test.SharedSparkSession -import org.apache.spark.sql.types.{StringType, StructType} +import org.apache.spark.sql.types.{BooleanType, StringType, StructType} class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowTablesSuite { override def catalog: String = "test_catalog_v2" @@ -129,4 +130,16 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT runShowTablesSql("SHOW TABLES", Seq(ShowRow("ns1.ns2", "table", false))) } } + + // TODO(SPARK-33393): Support SHOW TABLE EXTENDED in DSv2 + test("SHOW TABLE EXTENDED: an existing table") { + val table = "people" + withTable(s"$catalog.$table") { + sql(s"CREATE TABLE $catalog.$table (name STRING, id INT) $defaultUsing") + val errMsg = intercept[NoSuchDatabaseException] { + sql(s"SHOW TABLE EXTENDED FROM $catalog LIKE '*$table*'").collect() + }.getMessage + assert(errMsg.contains(s"Database '$catalog' not found")) + } + } } From 1c7f63ca4ca0c76913e0eb2d6e781ac4c66a2cd5 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Mon, 9 Nov 2020 23:22:30 +0300 Subject: [PATCH 38/46] Split the test "namespace is not specified and the default catalog is set" --- .../sql/execution/command/ShowTablesSuite.scala | 2 +- .../sql/execution/command/v1/ShowTablesSuite.scala | 10 ++++++++++ .../sql/execution/command/v2/ShowTablesSuite.scala | 14 +++++--------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index e5213d547f5c3..15431168d6a0c 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -19,7 +19,7 @@ package org.apache.spark.sql.execution.command import org.apache.spark.sql.{QueryTest, Row} import org.apache.spark.sql.test.SharedSparkSession -import org.apache.spark.sql.types.{BooleanType, StringType, StructType} +import org.apache.spark.sql.types.StructType trait ShowTablesSuite extends QueryTest with SharedSparkSession { protected def catalog: String diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 5e325d3c9b5fa..64bce7d9deabf 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -21,6 +21,7 @@ import org.apache.spark.sql.{AnalysisException, Row} import org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException import org.apache.spark.sql.connector.catalog.CatalogManager import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} +import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.types.{BooleanType, StringType, StructType} class ShowTablesSuite extends CommonShowTablesSuite { @@ -46,6 +47,15 @@ class ShowTablesSuite extends CommonShowTablesSuite { assert(msg.contains("Database 'unknown' not found")) } + test("namespace is not specified and the default catalog is set") { + withSQLConf(SQLConf.DEFAULT_CATALOG.key -> catalog) { + withTable("table") { + spark.sql(s"CREATE TABLE table (id bigint, data string) $defaultUsing") + runShowTablesSql("SHOW TABLES", Seq(ShowRow("default", "table", false))) + } + } + } + // `SHOW TABLES` from v2 catalog returns empty result. test("show views from v1 catalog") { withSourceViews { diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 45200c8de8d5c..e2e6518d161e8 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -18,13 +18,13 @@ package org.apache.spark.sql.execution.command.v2 import org.apache.spark.SparkConf -import org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException import org.apache.spark.sql.{AnalysisException, QueryTest, Row} +import org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException import org.apache.spark.sql.connector.InMemoryTableCatalog import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.test.SharedSparkSession -import org.apache.spark.sql.types.{BooleanType, StringType, StructType} +import org.apache.spark.sql.types.{StringType, StructType} class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowTablesSuite { override def catalog: String = "test_catalog_v2" @@ -71,14 +71,10 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT } } - // The test fails for V1 catalog with the error: - // org.apache.spark.sql.AnalysisException: - // The namespace in session catalog must have exactly one name part: spark_catalog.table - test("namespace is not specified and default v2 catalog is set") { + test("namespace is not specified and the default catalog is set") { withSQLConf(SQLConf.DEFAULT_CATALOG.key -> catalog) { - withTable(s"$catalog.table") { - spark.sql(s"CREATE TABLE $catalog.table (id bigint, data string) $defaultUsing") - // v2 catalog is used where default namespace is empty for TestInMemoryTableCatalog. + withTable("table") { + spark.sql(s"CREATE TABLE table (id bigint, data string) $defaultUsing") runShowTablesSql("SHOW TABLES", Seq(ShowRow("", "table", false))) } } From 3d82b4e4bde8c4e8def83b1ca05dad1f14b9ad21 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Mon, 9 Nov 2020 23:27:46 +0300 Subject: [PATCH 39/46] Add TODO(SPARK-33403) --- .../apache/spark/sql/execution/command/v2/ShowTablesSuite.scala | 1 + 1 file changed, 1 insertion(+) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index e2e6518d161e8..aabc20b1370f4 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -75,6 +75,7 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT withSQLConf(SQLConf.DEFAULT_CATALOG.key -> catalog) { withTable("table") { spark.sql(s"CREATE TABLE table (id bigint, data string) $defaultUsing") + // TODO(SPARK-33403): DSv2 SHOW TABLES doesn't show `default` runShowTablesSql("SHOW TABLES", Seq(ShowRow("", "table", false))) } } From d264962f6d6c9134e07b6ef442ebb9cdd64ea1f1 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Mon, 9 Nov 2020 23:34:05 +0300 Subject: [PATCH 40/46] Adjust test title --- .../apache/spark/sql/execution/command/v1/ShowTablesSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 64bce7d9deabf..d1646b63c1099 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -57,7 +57,7 @@ class ShowTablesSuite extends CommonShowTablesSuite { } // `SHOW TABLES` from v2 catalog returns empty result. - test("show views from v1 catalog") { + test("v1 SHOW TABLES list the temp views") { withSourceViews { runShowTablesSql( "SHOW TABLES FROM default", From 286e11ad657a74b1b69f7caac6a1bfcdeb3a65fd Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Mon, 9 Nov 2020 23:40:34 +0300 Subject: [PATCH 41/46] Move withSourceViews() out from the common trait --- .../spark/sql/execution/command/ShowTablesSuite.scala | 10 ---------- .../sql/execution/command/v1/ShowTablesSuite.scala | 10 ++++++++++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index 15431168d6a0c..2cc4a42ee1e95 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -35,16 +35,6 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { assert(df.collect() === getRows(expected)) } - protected def withSourceViews(f: => Unit): Unit = { - withTable("source", "source2") { - val df = spark.createDataFrame(Seq((1L, "a"), (2L, "b"), (3L, "c"))).toDF("id", "data") - df.createOrReplaceTempView("source") - val df2 = spark.createDataFrame(Seq((4L, "d"), (5L, "e"), (6L, "f"))).toDF("id", "data") - df2.createOrReplaceTempView("source2") - f - } - } - test("show an existing table") { val namespace = "test" val table = "people" diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index d1646b63c1099..5537ad372ac25 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -39,6 +39,16 @@ class ShowTablesSuite extends CommonShowTablesSuite { } } + private def withSourceViews(f: => Unit): Unit = { + withTable("source", "source2") { + val df = spark.createDataFrame(Seq((1L, "a"), (2L, "b"), (3L, "c"))).toDF("id", "data") + df.createOrReplaceTempView("source") + val df2 = spark.createDataFrame(Seq((4L, "d"), (5L, "e"), (6L, "f"))).toDF("id", "data") + df2.createOrReplaceTempView("source2") + f + } + } + // `SHOW TABLES` returns empty result in V2 catalog instead of throwing the exception. test("show table in a not existing namespace") { val msg = intercept[NoSuchDatabaseException] { From aafa7f88acf41f13e3b6c833ba7aa1bc575f9d37 Mon Sep 17 00:00:00 2001 From: Wenchen Fan Date: Tue, 10 Nov 2020 16:52:50 +0800 Subject: [PATCH 42/46] refine tests --- .../execution/command/ShowTablesSuite.scala | 84 +++++++++++++------ .../command/v1/ShowTablesSuite.scala | 24 +----- .../command/v2/ShowTablesSuite.scala | 29 +------ 3 files changed, 61 insertions(+), 76 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index 2cc4a42ee1e95..f03af47c4dbf2 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -18,11 +18,14 @@ package org.apache.spark.sql.execution.command import org.apache.spark.sql.{QueryTest, Row} +import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ +import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.test.SharedSparkSession import org.apache.spark.sql.types.StructType trait ShowTablesSuite extends QueryTest with SharedSparkSession { protected def catalog: String + protected def defaultNamespace: Seq[String] protected def defaultUsing: String case class ShowRow(namespace: String, table: String, isTemporary: Boolean) protected def getRows(showRows: Seq[ShowRow]): Seq[Row] @@ -36,47 +39,74 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { } test("show an existing table") { - val namespace = "test" - val table = "people" - withDatabase(s"$catalog.$namespace") { - sql(s"CREATE DATABASE $catalog.$namespace") - withTable(s"$catalog.$namespace.$table") { - sql(s"CREATE TABLE $catalog.$namespace.$table (name STRING, id INT) $defaultUsing") - runShowTablesSql(s"SHOW TABLES IN $catalog.test", Seq(ShowRow(namespace, table, false))) + withNamespace(s"$catalog.ns") { + sql(s"CREATE NAMESPACE $catalog.ns") + withTable(s"$catalog.ns.table") { + sql(s"CREATE TABLE $catalog.ns.table (name STRING, id INT) $defaultUsing") + runShowTablesSql(s"SHOW TABLES IN $catalog.ns", Seq(ShowRow("ns", "table", false))) } } } test("show tables with a pattern") { - withDatabase(s"$catalog.db", s"$catalog.db2") { - sql(s"CREATE DATABASE $catalog.db") - sql(s"CREATE DATABASE $catalog.db2") + withNamespace(s"$catalog.ns1", s"$catalog.ns2") { + sql(s"CREATE NAMESPACE $catalog.ns1") + sql(s"CREATE NAMESPACE $catalog.ns2") withTable( - s"$catalog.db.table", - s"$catalog.db.table_name_1", - s"$catalog.db.table_name_2", - s"$catalog.db2.table_name_2") { - sql(s"CREATE TABLE $catalog.db.table (id bigint, data string) $defaultUsing") - sql(s"CREATE TABLE $catalog.db.table_name_1 (id bigint, data string) $defaultUsing") - sql(s"CREATE TABLE $catalog.db.table_name_2 (id bigint, data string) $defaultUsing") - sql(s"CREATE TABLE $catalog.db2.table_name_2 (id bigint, data string) $defaultUsing") + s"$catalog.ns1.table", + s"$catalog.ns1.table_name_1", + s"$catalog.ns1.table_name_2", + s"$catalog.ns2.table_name_2") { + sql(s"CREATE TABLE $catalog.ns1.table (id bigint, data string) $defaultUsing") + sql(s"CREATE TABLE $catalog.ns1.table_name_1 (id bigint, data string) $defaultUsing") + sql(s"CREATE TABLE $catalog.ns1.table_name_2 (id bigint, data string) $defaultUsing") + sql(s"CREATE TABLE $catalog.ns2.table_name_2 (id bigint, data string) $defaultUsing") runShowTablesSql( - s"SHOW TABLES FROM $catalog.db", + s"SHOW TABLES FROM $catalog.ns1", Seq( - ShowRow("db", "table", false), - ShowRow("db", "table_name_1", false), - ShowRow("db", "table_name_2", false))) + ShowRow("ns1", "table", false), + ShowRow("ns1", "table_name_1", false), + ShowRow("ns1", "table_name_2", false))) runShowTablesSql( - s"SHOW TABLES FROM $catalog.db LIKE '*name*'", + s"SHOW TABLES FROM $catalog.ns1 LIKE '*name*'", Seq( - ShowRow("db", "table_name_1", false), - ShowRow("db", "table_name_2", false))) + ShowRow("ns1", "table_name_1", false), + ShowRow("ns1", "table_name_2", false))) runShowTablesSql( - s"SHOW TABLES FROM $catalog.db LIKE '*2'", - Seq(ShowRow("db", "table_name_2", false))) + s"SHOW TABLES FROM $catalog.ns1 LIKE '*2'", + Seq(ShowRow("ns1", "table_name_2", false))) + } + } + } + + test("show tables with current catalog and namespace") { + withSQLConf(SQLConf.DEFAULT_CATALOG.key -> catalog) { + val tblName = (catalog +: defaultNamespace :+ "table").quoted + withTable(tblName) { + sql(s"CREATE TABLE $tblName (name STRING, id INT) $defaultUsing") + val ns = defaultNamespace.mkString(".") + runShowTablesSql("SHOW TABLES", Seq(ShowRow(ns, "table", false))) + } + } + } + + test("change current catalog and namespace with USE statements") { + withNamespace(s"$catalog.ns") { + sql(s"CREATE NAMESPACE $catalog.ns") + withTable(s"$catalog.ns.table") { + sql(s"CREATE TABLE $catalog.ns.table (name STRING, id INT) $defaultUsing") + + sql(s"USE $catalog") + // No table is matched since the current namespace is not ["ns"] + assert(defaultNamespace != Seq("ns")) + runShowTablesSql("SHOW TABLES", Seq()) + + // Update the current namespace to match "ns.tbl". + sql(s"USE $catalog.ns") + runShowTablesSql("SHOW TABLES", Seq(ShowRow("ns", "table", false))) } } } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 5537ad372ac25..49303b8914e83 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -21,11 +21,11 @@ import org.apache.spark.sql.{AnalysisException, Row} import org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException import org.apache.spark.sql.connector.catalog.CatalogManager import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} -import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.types.{BooleanType, StringType, StructType} class ShowTablesSuite extends CommonShowTablesSuite { override def catalog: String = CatalogManager.SESSION_CATALOG_NAME + override def defaultNamespace: Seq[String] = Seq("default") override protected def defaultUsing: String = "USING parquet" override protected def showSchema: StructType = { new StructType() @@ -57,15 +57,6 @@ class ShowTablesSuite extends CommonShowTablesSuite { assert(msg.contains("Database 'unknown' not found")) } - test("namespace is not specified and the default catalog is set") { - withSQLConf(SQLConf.DEFAULT_CATALOG.key -> catalog) { - withTable("table") { - spark.sql(s"CREATE TABLE table (id bigint, data string) $defaultUsing") - runShowTablesSql("SHOW TABLES", Seq(ShowRow("default", "table", false))) - } - } - } - // `SHOW TABLES` from v2 catalog returns empty result. test("v1 SHOW TABLES list the temp views") { withSourceViews { @@ -75,23 +66,14 @@ class ShowTablesSuite extends CommonShowTablesSuite { } } - test("using v1 catalog, db name with multipartIdentifier ('a.b') is not allowed.") { + test("v1 SHOW TABLES only support single-level namespace") { val exception = intercept[AnalysisException] { runShowTablesSql("SHOW TABLES FROM a.b", Seq()) } assert(exception.getMessage.contains("The database name is not valid: a.b")) } - test("namespace not specified and default v2 catalog not set - fallback to v1") { - withSourceViews { - runShowTablesSql( - "SHOW TABLES", - Seq(ShowRow("", "source", true), ShowRow("", "source2", true))) - runShowTablesSql("SHOW TABLES LIKE '*2'", Seq(ShowRow("", "source2", true))) - } - } - - test("SHOW TABLE EXTENDED for default") { + test("SHOW TABLE EXTENDED from default") { withSourceViews { val expected = Seq(Row("", "source", true), Row("", "source2", true)) val schema = new StructType() diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index aabc20b1370f4..1de09805ec6a5 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -22,12 +22,12 @@ import org.apache.spark.sql.{AnalysisException, QueryTest, Row} import org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException import org.apache.spark.sql.connector.InMemoryTableCatalog import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} -import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.test.SharedSparkSession import org.apache.spark.sql.types.{StringType, StructType} class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowTablesSuite { override def catalog: String = "test_catalog_v2" + override def defaultNamespace: Seq[String] = Nil override protected def defaultUsing: String = "USING _" override protected def showSchema: StructType = { new StructType() @@ -71,16 +71,6 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT } } - test("namespace is not specified and the default catalog is set") { - withSQLConf(SQLConf.DEFAULT_CATALOG.key -> catalog) { - withTable("table") { - spark.sql(s"CREATE TABLE table (id bigint, data string) $defaultUsing") - // TODO(SPARK-33403): DSv2 SHOW TABLES doesn't show `default` - runShowTablesSql("SHOW TABLES", Seq(ShowRow("", "table", false))) - } - } - } - // The test fails for V1 catalog with the error: // org.apache.spark.sql.AnalysisException: // The namespace in session catalog must have exactly one name part: spark_catalog.ns1.ns2.tbl @@ -111,23 +101,6 @@ class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowT } } - // The test fails with the error in V1 session catalog: - // org.apache.spark.sql.AnalysisException: - // The namespace in session catalog must have exactly one name part: spark_catalog.ns1.ns2.table - test("change current catalog and namespace with USE statements") { - withTable(s"$catalog.ns1.ns2.table") { - sql(s"CREATE TABLE $catalog.ns1.ns2.table (id bigint) $defaultUsing") - - // Update the current catalog, and no table is matched since the current namespace is Array(). - sql(s"USE $catalog") - runShowTablesSql("SHOW TABLES", Seq()) - - // Update the current namespace to match ns1.ns2.table. - sql(s"USE $catalog.ns1.ns2") - runShowTablesSql("SHOW TABLES", Seq(ShowRow("ns1.ns2", "table", false))) - } - } - // TODO(SPARK-33393): Support SHOW TABLE EXTENDED in DSv2 test("SHOW TABLE EXTENDED: an existing table") { val table = "people" From 94b337de927c6900f40337d3d828b3ff4c0d123a Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Tue, 10 Nov 2020 15:45:17 +0300 Subject: [PATCH 43/46] Override test() --- .../spark/sql/execution/command/ShowTablesSuite.scala | 9 +++++++++ .../spark/sql/execution/command/v1/ShowTablesSuite.scala | 1 + .../spark/sql/execution/command/v2/ShowTablesSuite.scala | 3 ++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index f03af47c4dbf2..4c392e837c40e 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -17,6 +17,9 @@ package org.apache.spark.sql.execution.command +import org.scalactic.source.Position +import org.scalatest.Tag + import org.apache.spark.sql.{QueryTest, Row} import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ import org.apache.spark.sql.internal.SQLConf @@ -24,6 +27,7 @@ import org.apache.spark.sql.test.SharedSparkSession import org.apache.spark.sql.types.StructType trait ShowTablesSuite extends QueryTest with SharedSparkSession { + protected def version: String protected def catalog: String protected def defaultNamespace: Seq[String] protected def defaultUsing: String @@ -38,6 +42,11 @@ trait ShowTablesSuite extends QueryTest with SharedSparkSession { assert(df.collect() === getRows(expected)) } + override def test(testName: String, testTags: Tag*)(testFun: => Any) + (implicit pos: Position): Unit = { + super.test(s"SHOW TABLES $version: " + testName, testTags: _*)(testFun) + } + test("show an existing table") { withNamespace(s"$catalog.ns") { sql(s"CREATE NAMESPACE $catalog.ns") diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 49303b8914e83..0efcdcad1c708 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -24,6 +24,7 @@ import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTabl import org.apache.spark.sql.types.{BooleanType, StringType, StructType} class ShowTablesSuite extends CommonShowTablesSuite { + override def version: String = "V1" override def catalog: String = CatalogManager.SESSION_CATALOG_NAME override def defaultNamespace: Seq[String] = Seq("default") override protected def defaultUsing: String = "USING parquet" diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 1de09805ec6a5..db0e171bcfe3c 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -26,7 +26,8 @@ import org.apache.spark.sql.test.SharedSparkSession import org.apache.spark.sql.types.{StringType, StructType} class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowTablesSuite { - override def catalog: String = "test_catalog_v2" + override def version: String = "V2" + override def catalog: String = "test_catalog" override def defaultNamespace: Seq[String] = Nil override protected def defaultUsing: String = "USING _" override protected def showSchema: StructType = { From 5f37a4cf7056a3d12d39d12617d660dff29a2f79 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Tue, 10 Nov 2020 15:46:57 +0300 Subject: [PATCH 44/46] Remove some parent classes --- .../apache/spark/sql/execution/command/ShowTablesSuite.scala | 2 +- .../apache/spark/sql/execution/command/v2/ShowTablesSuite.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index 4c392e837c40e..8a5a4f88a90f5 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -26,7 +26,7 @@ import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.test.SharedSparkSession import org.apache.spark.sql.types.StructType -trait ShowTablesSuite extends QueryTest with SharedSparkSession { +trait ShowTablesSuite extends SharedSparkSession { protected def version: String protected def catalog: String protected def defaultNamespace: Seq[String] diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index db0e171bcfe3c..0f22ad7e1244a 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -25,7 +25,7 @@ import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTabl import org.apache.spark.sql.test.SharedSparkSession import org.apache.spark.sql.types.{StringType, StructType} -class ShowTablesSuite extends QueryTest with SharedSparkSession with CommonShowTablesSuite { +class ShowTablesSuite extends CommonShowTablesSuite { override def version: String = "V2" override def catalog: String = "test_catalog" override def defaultNamespace: Seq[String] = Nil From ca2f96adbe106eab3ceaad9064f52f066e427865 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Tue, 10 Nov 2020 15:50:07 +0300 Subject: [PATCH 45/46] Refactoring --- .../spark/sql/execution/command/v1/ShowTablesSuite.scala | 6 +++--- .../spark/sql/execution/command/v2/ShowTablesSuite.scala | 9 ++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala index 0efcdcad1c708..feb3bc623f3fa 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v1/ShowTablesSuite.scala @@ -27,14 +27,14 @@ class ShowTablesSuite extends CommonShowTablesSuite { override def version: String = "V1" override def catalog: String = CatalogManager.SESSION_CATALOG_NAME override def defaultNamespace: Seq[String] = Seq("default") - override protected def defaultUsing: String = "USING parquet" - override protected def showSchema: StructType = { + override def defaultUsing: String = "USING parquet" + override def showSchema: StructType = { new StructType() .add("database", StringType, nullable = false) .add("tableName", StringType, nullable = false) .add("isTemporary", BooleanType, nullable = false) } - override protected def getRows(showRows: Seq[ShowRow]): Seq[Row] = { + override def getRows(showRows: Seq[ShowRow]): Seq[Row] = { showRows.map { case ShowRow(namespace, table, isTemporary) => Row(namespace, table, isTemporary) } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala index 0f22ad7e1244a..668120ae1cada 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/v2/ShowTablesSuite.scala @@ -18,24 +18,23 @@ package org.apache.spark.sql.execution.command.v2 import org.apache.spark.SparkConf -import org.apache.spark.sql.{AnalysisException, QueryTest, Row} +import org.apache.spark.sql.{AnalysisException, Row} import org.apache.spark.sql.catalyst.analysis.NoSuchDatabaseException import org.apache.spark.sql.connector.InMemoryTableCatalog import org.apache.spark.sql.execution.command.{ShowTablesSuite => CommonShowTablesSuite} -import org.apache.spark.sql.test.SharedSparkSession import org.apache.spark.sql.types.{StringType, StructType} class ShowTablesSuite extends CommonShowTablesSuite { override def version: String = "V2" override def catalog: String = "test_catalog" override def defaultNamespace: Seq[String] = Nil - override protected def defaultUsing: String = "USING _" - override protected def showSchema: StructType = { + override def defaultUsing: String = "USING _" + override def showSchema: StructType = { new StructType() .add("namespace", StringType, nullable = false) .add("tableName", StringType, nullable = false) } - override protected def getRows(showRows: Seq[ShowRow]): Seq[Row] = { + override def getRows(showRows: Seq[ShowRow]): Seq[Row] = { showRows.map { case ShowRow(namespace, table, _) => Row(namespace, table) } From 8108cb1b5879b7873e3bfa43058ec3db7feba148 Mon Sep 17 00:00:00 2001 From: Max Gekk Date: Tue, 10 Nov 2020 15:53:04 +0300 Subject: [PATCH 46/46] Remove an unused import --- .../apache/spark/sql/execution/command/ShowTablesSuite.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala index 8a5a4f88a90f5..01720b5723243 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/command/ShowTablesSuite.scala @@ -20,7 +20,7 @@ package org.apache.spark.sql.execution.command import org.scalactic.source.Position import org.scalatest.Tag -import org.apache.spark.sql.{QueryTest, Row} +import org.apache.spark.sql.Row import org.apache.spark.sql.connector.catalog.CatalogV2Implicits._ import org.apache.spark.sql.internal.SQLConf import org.apache.spark.sql.test.SharedSparkSession