Skip to content

Commit 5eb6733

Browse files
committed
More tests
1 parent c41e7bc commit 5eb6733

File tree

2 files changed

+90
-42
lines changed

2 files changed

+90
-42
lines changed

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,11 +1212,11 @@ class SessionCatalogSuite extends PlanTest {
12121212
new SimpleFunctionRegistry,
12131213
CatalystSqlParser)
12141214
assert(original ne clone)
1215-
assert(clone.getTempView("copytest1") == Option(tempTable1))
1215+
assert(clone.getTempView("copytest1") == Some(tempTable1))
12161216

12171217
// check if clone and original independent
12181218
clone.dropTable(TableIdentifier("copytest1"), ignoreIfNotExists = false, purge = false)
1219-
assert(original.getTempView("copytest1") == Option(tempTable1))
1219+
assert(original.getTempView("copytest1") == Some(tempTable1))
12201220

12211221
val tempTable2 = Range(1, 20, 2, 10)
12221222
original.createTempView("copytest2", tempTable2, overrideIfExists = false)
@@ -1225,14 +1225,15 @@ class SessionCatalogSuite extends PlanTest {
12251225

12261226
test("clone SessionCatalog - current db") {
12271227
val externalCatalog = newEmptyCatalog()
1228-
externalCatalog.createDatabase(newDb("copytest1"), true)
1229-
externalCatalog.createDatabase(newDb("copytest2"), true)
1230-
externalCatalog.createDatabase(newDb("copytest3"), true)
1228+
val db1 = "db1"
1229+
val db2 = "db2"
1230+
val db3 = "db3"
1231+
1232+
externalCatalog.createDatabase(newDb(db1), ignoreIfExists = true)
1233+
externalCatalog.createDatabase(newDb(db2), ignoreIfExists = true)
1234+
externalCatalog.createDatabase(newDb(db3), ignoreIfExists = true)
12311235

12321236
val original = new SessionCatalog(externalCatalog)
1233-
val tempTable1 = Range(1, 10, 1, 10)
1234-
val db1 = "copytest1"
1235-
original.createTempView(db1, tempTable1, overrideIfExists = false)
12361237
original.setCurrentDatabase(db1)
12371238

12381239
// check if current db copied over
@@ -1245,15 +1246,8 @@ class SessionCatalogSuite extends PlanTest {
12451246
assert(clone.getCurrentDatabase == db1)
12461247

12471248
// check if clone and original independent
1248-
val db2 = "copytest2"
1249-
val tempTable2 = Range(1, 20, 2, 20)
1250-
clone.createTempView(db2, tempTable2, overrideIfExists = false)
12511249
clone.setCurrentDatabase(db2)
12521250
assert(original.getCurrentDatabase == db1)
1253-
1254-
val db3 = "copytest3"
1255-
val tempTable3 = Range(1, 30, 2, 30)
1256-
original.createTempView(db3, tempTable3, overrideIfExists = false)
12571251
original.setCurrentDatabase(db3)
12581252
assert(clone.getCurrentDatabase == db2)
12591253
}

sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveSessionCatalogSuite.scala

Lines changed: 81 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,96 @@
1717

1818
package org.apache.spark.sql.hive
1919

20+
import java.net.URI
21+
2022
import org.apache.hadoop.conf.Configuration
2123

22-
import org.apache.spark.SparkFunSuite
23-
import org.apache.spark.sql.SparkSession
2424
import org.apache.spark.sql.catalyst.TableIdentifier
2525
import org.apache.spark.sql.catalyst.analysis.SimpleFunctionRegistry
26+
import org.apache.spark.sql.catalyst.catalog.CatalogDatabase
2627
import org.apache.spark.sql.catalyst.parser.CatalystSqlParser
2728
import org.apache.spark.sql.catalyst.plans.logical.Range
29+
import org.apache.spark.sql.hive.test.TestHiveSingleton
2830
import org.apache.spark.sql.internal.SQLConf
31+
import org.apache.spark.util.Utils
2932

30-
class HiveSessionCatalogSuite extends SparkFunSuite {
33+
class HiveSessionCatalogSuite extends TestHiveSingleton {
3134

3235
test("clone HiveSessionCatalog") {
33-
val hiveSession = SparkSession.builder().master("local").enableHiveSupport().getOrCreate()
34-
assert(hiveSession.sessionState.catalog.isInstanceOf[HiveSessionCatalog])
35-
val original = hiveSession.sessionState.catalog.asInstanceOf[HiveSessionCatalog]
36-
37-
val tempTable1 = Range(1, 10, 1, 10)
38-
original.createTempView("copytest1", tempTable1, overrideIfExists = false)
39-
40-
// check if tables copied over
41-
val clone = original.newSessionCatalogWith(
42-
hiveSession,
43-
new SQLConf,
44-
new Configuration(),
45-
new SimpleFunctionRegistry,
46-
CatalystSqlParser)
47-
assert(original ne clone)
48-
assert(clone.getTempView("copytest1") == Option(tempTable1))
49-
50-
// check if clone and original independent
51-
clone.dropTable(TableIdentifier("copytest1"), ignoreIfNotExists = false, purge = false)
52-
assert(original.getTempView("copytest1") == Option(tempTable1))
53-
54-
val tempTable2 = Range(1, 20, 2, 10)
55-
original.createTempView("copytest2", tempTable2, overrideIfExists = false)
56-
assert(clone.getTempView("copytest2").isEmpty)
36+
val original = spark.sessionState.catalog.asInstanceOf[HiveSessionCatalog]
37+
38+
val tempTableName1 = "copytest1"
39+
val tempTableName2 = "copytest2"
40+
try {
41+
val tempTable1 = Range(1, 10, 1, 10)
42+
original.createTempView(tempTableName1, tempTable1, overrideIfExists = false)
43+
44+
// check if tables copied over
45+
val clone = original.newSessionCatalogWith(
46+
spark,
47+
new SQLConf,
48+
new Configuration(),
49+
new SimpleFunctionRegistry,
50+
CatalystSqlParser)
51+
assert(original ne clone)
52+
assert(clone.getTempView(tempTableName1) == Some(tempTable1))
53+
54+
// check if clone and original independent
55+
clone.dropTable(TableIdentifier(tempTableName1), ignoreIfNotExists = false, purge = false)
56+
assert(original.getTempView(tempTableName1) == Some(tempTable1))
57+
58+
val tempTable2 = Range(1, 20, 2, 10)
59+
original.createTempView(tempTableName2, tempTable2, overrideIfExists = false)
60+
assert(clone.getTempView(tempTableName2).isEmpty)
61+
} finally {
62+
// Drop the created temp views from the global singleton HiveSession.
63+
original.dropTable(TableIdentifier(tempTableName1), ignoreIfNotExists = true, purge = true)
64+
original.dropTable(TableIdentifier(tempTableName2), ignoreIfNotExists = true, purge = true)
65+
}
66+
}
67+
68+
test("clone SessionCatalog - current db") {
69+
val original = spark.sessionState.catalog.asInstanceOf[HiveSessionCatalog]
70+
val originalCurrentDatabase = original.getCurrentDatabase
71+
val db1 = "db1"
72+
val db2 = "db2"
73+
val db3 = "db3"
74+
try {
75+
original.createDatabase(newDb(db1), ignoreIfExists = true)
76+
original.createDatabase(newDb(db2), ignoreIfExists = true)
77+
original.createDatabase(newDb(db3), ignoreIfExists = true)
78+
79+
original.setCurrentDatabase(db1)
80+
81+
// check if tables copied over
82+
val clone = original.newSessionCatalogWith(
83+
spark,
84+
new SQLConf,
85+
new Configuration(),
86+
new SimpleFunctionRegistry,
87+
CatalystSqlParser)
88+
89+
// check if current db copied over
90+
assert(original ne clone)
91+
assert(clone.getCurrentDatabase == db1)
92+
93+
// check if clone and original independent
94+
clone.setCurrentDatabase(db2)
95+
assert(original.getCurrentDatabase == db1)
96+
original.setCurrentDatabase(db3)
97+
assert(clone.getCurrentDatabase == db2)
98+
} finally {
99+
// Drop the created databases from the global singleton HiveSession.
100+
original.dropDatabase(db1, ignoreIfNotExists = true, cascade = true)
101+
original.dropDatabase(db2, ignoreIfNotExists = true, cascade = true)
102+
original.dropDatabase(db3, ignoreIfNotExists = true, cascade = true)
103+
original.setCurrentDatabase(originalCurrentDatabase)
104+
}
105+
}
106+
107+
def newUriForDatabase(): URI = new URI(Utils.createTempDir().toURI.toString.stripSuffix("/"))
108+
109+
def newDb(name: String): CatalogDatabase = {
110+
CatalogDatabase(name, name + " description", newUriForDatabase(), Map.empty)
57111
}
58112
}

0 commit comments

Comments
 (0)