Skip to content

Commit

Permalink
[SPARK-23036][SQL][TEST] Add withGlobalTempView for testing
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?

Add withGlobalTempView when create global temp view, like withTempView and withView.
And correct some improper usage.
Please see jira.
There are other similar place like that. I will fix it if community need. Please confirm it.
## How was this patch tested?

no new test.

Author: xubo245 <601450868@qq.com>

Closes #20228 from xubo245/DropTempView.
  • Loading branch information
xubo245 authored and gatorsmile committed Jan 13, 2018
1 parent fc6fe8a commit bd4a21b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GlobalTempViewSuite extends QueryTest with SharedSQLContext {

test("basic semantic") {
val expectedErrorMsg = "not found"
try {
withGlobalTempView("src") {
sql("CREATE GLOBAL TEMP VIEW src AS SELECT 1, 'a'")

// If there is no database in table name, we should try local temp view first, if not found,
Expand Down Expand Up @@ -79,19 +79,15 @@ class GlobalTempViewSuite extends QueryTest with SharedSQLContext {
// We can also use Dataset API to replace global temp view
Seq(2 -> "b").toDF("i", "j").createOrReplaceGlobalTempView("src")
checkAnswer(spark.table(s"$globalTempDB.src"), Row(2, "b"))
} finally {
spark.catalog.dropGlobalTempView("src")
}
}

test("global temp view is shared among all sessions") {
try {
withGlobalTempView("src") {
sql("CREATE GLOBAL TEMP VIEW src AS SELECT 1, 2")
checkAnswer(spark.table(s"$globalTempDB.src"), Row(1, 2))
val newSession = spark.newSession()
checkAnswer(newSession.table(s"$globalTempDB.src"), Row(1, 2))
} finally {
spark.catalog.dropGlobalTempView("src")
}
}

Expand All @@ -105,27 +101,25 @@ class GlobalTempViewSuite extends QueryTest with SharedSQLContext {

test("CREATE GLOBAL TEMP VIEW USING") {
withTempPath { path =>
try {
withGlobalTempView("src") {
Seq(1 -> "a").toDF("i", "j").write.parquet(path.getAbsolutePath)
sql(s"CREATE GLOBAL TEMP VIEW src USING parquet OPTIONS (PATH '${path.toURI}')")
checkAnswer(spark.table(s"$globalTempDB.src"), Row(1, "a"))
sql(s"INSERT INTO $globalTempDB.src SELECT 2, 'b'")
checkAnswer(spark.table(s"$globalTempDB.src"), Row(1, "a") :: Row(2, "b") :: Nil)
} finally {
spark.catalog.dropGlobalTempView("src")
}
}
}

test("CREATE TABLE LIKE should work for global temp view") {
try {
sql("CREATE GLOBAL TEMP VIEW src AS SELECT 1 AS a, '2' AS b")
sql(s"CREATE TABLE cloned LIKE $globalTempDB.src")
val tableMeta = spark.sessionState.catalog.getTableMetadata(TableIdentifier("cloned"))
assert(tableMeta.schema == new StructType().add("a", "int", false).add("b", "string", false))
} finally {
spark.catalog.dropGlobalTempView("src")
sql("DROP TABLE default.cloned")
withTable("cloned") {
withGlobalTempView("src") {
sql("CREATE GLOBAL TEMP VIEW src AS SELECT 1 AS a, '2' AS b")
sql(s"CREATE TABLE cloned LIKE $globalTempDB.src")
val tableMeta = spark.sessionState.catalog.getTableMetadata(TableIdentifier("cloned"))
assert(tableMeta.schema == new StructType()
.add("a", "int", false).add("b", "string", false))
}
}
}

Expand All @@ -146,26 +140,25 @@ class GlobalTempViewSuite extends QueryTest with SharedSQLContext {
}

test("should lookup global temp view if and only if global temp db is specified") {
try {
sql("CREATE GLOBAL TEMP VIEW same_name AS SELECT 3, 4")
sql("CREATE TEMP VIEW same_name AS SELECT 1, 2")
withTempView("same_name") {
withGlobalTempView("same_name") {
sql("CREATE GLOBAL TEMP VIEW same_name AS SELECT 3, 4")
sql("CREATE TEMP VIEW same_name AS SELECT 1, 2")

checkAnswer(sql("SELECT * FROM same_name"), Row(1, 2))
checkAnswer(sql("SELECT * FROM same_name"), Row(1, 2))

// we never lookup global temp views if database is not specified in table name
spark.catalog.dropTempView("same_name")
intercept[AnalysisException](sql("SELECT * FROM same_name"))
// we never lookup global temp views if database is not specified in table name
spark.catalog.dropTempView("same_name")
intercept[AnalysisException](sql("SELECT * FROM same_name"))

// Use qualified name to lookup a global temp view.
checkAnswer(sql(s"SELECT * FROM $globalTempDB.same_name"), Row(3, 4))
} finally {
spark.catalog.dropTempView("same_name")
spark.catalog.dropGlobalTempView("same_name")
// Use qualified name to lookup a global temp view.
checkAnswer(sql(s"SELECT * FROM $globalTempDB.same_name"), Row(3, 4))
}
}
}

test("public Catalog should recognize global temp view") {
try {
withGlobalTempView("src") {
sql("CREATE GLOBAL TEMP VIEW src AS SELECT 1, 2")

assert(spark.catalog.tableExists(globalTempDB, "src"))
Expand All @@ -175,8 +168,6 @@ class GlobalTempViewSuite extends QueryTest with SharedSQLContext {
description = null,
tableType = "TEMPORARY",
isTemporary = true).toString)
} finally {
spark.catalog.dropGlobalTempView("src")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,25 @@ abstract class SQLViewSuite extends QueryTest with SQLTestUtils {
}

test("create a permanent view on a temp view") {
withView("jtv1", "temp_jtv1", "global_temp_jtv1") {
sql("CREATE TEMPORARY VIEW temp_jtv1 AS SELECT * FROM jt WHERE id > 3")
var e = intercept[AnalysisException] {
sql("CREATE VIEW jtv1 AS SELECT * FROM temp_jtv1 WHERE id < 6")
}.getMessage
assert(e.contains("Not allowed to create a permanent view `jtv1` by " +
"referencing a temporary view `temp_jtv1`"))

val globalTempDB = spark.sharedState.globalTempViewManager.database
sql("CREATE GLOBAL TEMP VIEW global_temp_jtv1 AS SELECT * FROM jt WHERE id > 0")
e = intercept[AnalysisException] {
sql(s"CREATE VIEW jtv1 AS SELECT * FROM $globalTempDB.global_temp_jtv1 WHERE id < 6")
}.getMessage
assert(e.contains(s"Not allowed to create a permanent view `jtv1` by referencing " +
s"a temporary view `global_temp`.`global_temp_jtv1`"))
withView("jtv1") {
withTempView("temp_jtv1") {
withGlobalTempView("global_temp_jtv1") {
sql("CREATE TEMPORARY VIEW temp_jtv1 AS SELECT * FROM jt WHERE id > 3")
var e = intercept[AnalysisException] {
sql("CREATE VIEW jtv1 AS SELECT * FROM temp_jtv1 WHERE id < 6")
}.getMessage
assert(e.contains("Not allowed to create a permanent view `jtv1` by " +
"referencing a temporary view `temp_jtv1`"))

val globalTempDB = spark.sharedState.globalTempViewManager.database
sql("CREATE GLOBAL TEMP VIEW global_temp_jtv1 AS SELECT * FROM jt WHERE id > 0")
e = intercept[AnalysisException] {
sql(s"CREATE VIEW jtv1 AS SELECT * FROM $globalTempDB.global_temp_jtv1 WHERE id < 6")
}.getMessage
assert(e.contains(s"Not allowed to create a permanent view `jtv1` by referencing " +
s"a temporary view `global_temp`.`global_temp_jtv1`"))
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,26 @@ private[sql] trait SQLTestUtilsBase
}

/**
* Drops temporary table `tableName` after calling `f`.
* Drops temporary view `viewNames` after calling `f`.
*/
protected def withTempView(tableNames: String*)(f: => Unit): Unit = {
protected def withTempView(viewNames: String*)(f: => Unit): Unit = {
try f finally {
// If the test failed part way, we don't want to mask the failure by failing to remove
// temp tables that never got created.
try tableNames.foreach(spark.catalog.dropTempView) catch {
// temp views that never got created.
try viewNames.foreach(spark.catalog.dropTempView) catch {
case _: NoSuchTableException =>
}
}
}

/**
* Drops global temporary view `viewNames` after calling `f`.
*/
protected def withGlobalTempView(viewNames: String*)(f: => Unit): Unit = {
try f finally {
// If the test failed part way, we don't want to mask the failure by failing to remove
// global temp views that never got created.
try viewNames.foreach(spark.catalog.dropGlobalTempView) catch {
case _: NoSuchTableException =>
}
}
Expand Down

0 comments on commit bd4a21b

Please sign in to comment.