Skip to content

Commit ad43a5f

Browse files
author
Andrew Or
committed
Expand test scope + clean up test code
1 parent 08969cd commit ad43a5f

File tree

4 files changed

+231
-105
lines changed

4 files changed

+231
-105
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/InMemoryCatalog.scala

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,20 @@ class InMemoryCatalog extends ExternalCatalog {
6969

7070
private def requireFunctionExists(db: String, funcName: String): Unit = {
7171
if (!existsFunction(db, funcName)) {
72-
throw new AnalysisException(s"Function $funcName does not exist in $db database")
72+
throw new AnalysisException(s"Function '$funcName' does not exist in database '$db'")
7373
}
7474
}
7575

7676
private def requireTableExists(db: String, table: String): Unit = {
7777
if (!existsTable(db, table)) {
78-
throw new AnalysisException(s"Table $table does not exist in $db database")
78+
throw new AnalysisException(s"Table '$table' does not exist in database '$db'")
7979
}
8080
}
8181

8282
private def requirePartitionExists(db: String, table: String, spec: TablePartitionSpec): Unit = {
8383
if (!existsPartition(db, table, spec)) {
84-
throw new AnalysisException(s"Partition does not exist in database $db table $table: $spec")
84+
throw new AnalysisException(
85+
s"Partition does not exist in database '$db' table '$table': '$spec'")
8586
}
8687
}
8788

@@ -94,7 +95,7 @@ class InMemoryCatalog extends ExternalCatalog {
9495
ignoreIfExists: Boolean): Unit = synchronized {
9596
if (catalog.contains(dbDefinition.name)) {
9697
if (!ignoreIfExists) {
97-
throw new AnalysisException(s"Database ${dbDefinition.name} already exists.")
98+
throw new AnalysisException(s"Database '${dbDefinition.name}' already exists.")
9899
}
99100
} else {
100101
catalog.put(dbDefinition.name, new DatabaseDesc(dbDefinition))
@@ -109,17 +110,17 @@ class InMemoryCatalog extends ExternalCatalog {
109110
if (!cascade) {
110111
// If cascade is false, make sure the database is empty.
111112
if (catalog(db).tables.nonEmpty) {
112-
throw new AnalysisException(s"Database $db is not empty. One or more tables exist.")
113+
throw new AnalysisException(s"Database '$db' is not empty. One or more tables exist.")
113114
}
114115
if (catalog(db).functions.nonEmpty) {
115-
throw new AnalysisException(s"Database $db is not empty. One or more functions exist.")
116+
throw new AnalysisException(s"Database '$db' is not empty. One or more functions exist.")
116117
}
117118
}
118119
// Remove the database.
119120
catalog.remove(db)
120121
} else {
121122
if (!ignoreIfNotExists) {
122-
throw new AnalysisException(s"Database $db does not exist")
123+
throw new AnalysisException(s"Database '$db' does not exist")
123124
}
124125
}
125126
}
@@ -160,7 +161,7 @@ class InMemoryCatalog extends ExternalCatalog {
160161
val table = tableDefinition.name.table
161162
if (existsTable(db, table)) {
162163
if (!ignoreIfExists) {
163-
throw new AnalysisException(s"Table $table already exists in $db database")
164+
throw new AnalysisException(s"Table '$table' already exists in database '$db'")
164165
}
165166
} else {
166167
catalog(db).tables.put(table, new TableDesc(tableDefinition))
@@ -224,8 +225,8 @@ class InMemoryCatalog extends ExternalCatalog {
224225
val dupSpecs = parts.collect { case p if existingParts.contains(p.spec) => p.spec }
225226
if (dupSpecs.nonEmpty) {
226227
val dupSpecsStr = dupSpecs.mkString("\n===\n")
227-
throw new AnalysisException(
228-
s"The following partitions already exist in database $db table $table:\n$dupSpecsStr")
228+
throw new AnalysisException("The following partitions already exist in database " +
229+
s"'$db' table '$table':\n$dupSpecsStr")
229230
}
230231
}
231232
parts.foreach { p => existingParts.put(p.spec, p) }
@@ -242,8 +243,8 @@ class InMemoryCatalog extends ExternalCatalog {
242243
val missingSpecs = partSpecs.collect { case s if !existingParts.contains(s) => s }
243244
if (missingSpecs.nonEmpty) {
244245
val missingSpecsStr = missingSpecs.mkString("\n===\n")
245-
throw new AnalysisException(
246-
s"The following partitions do not exist in database $db table $table:\n$missingSpecsStr")
246+
throw new AnalysisException("The following partitions do not exist in database " +
247+
s"'$db' table '$table':\n$missingSpecsStr")
247248
}
248249
}
249250
partSpecs.foreach(existingParts.remove)
@@ -295,7 +296,7 @@ class InMemoryCatalog extends ExternalCatalog {
295296
override def createFunction(db: String, func: CatalogFunction): Unit = synchronized {
296297
requireDbExists(db)
297298
if (existsFunction(db, func.name.funcName)) {
298-
throw new AnalysisException(s"Function $func already exists in $db database")
299+
throw new AnalysisException(s"Function '$func' already exists in '$db' database")
299300
} else {
300301
catalog(db).functions.put(func.name.funcName, func)
301302
}

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalog.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class SessionCatalog(externalCatalog: ExternalCatalog) {
8080
def getCurrentDatabase: String = currentDb
8181

8282
def setCurrentDatabase(db: String): Unit = {
83+
if (!databaseExists(db)) {
84+
throw new AnalysisException(s"cannot set current database to non-existent '$db'")
85+
}
8386
currentDb = db
8487
}
8588

@@ -223,7 +226,7 @@ class SessionCatalog(externalCatalog: ExternalCatalog) {
223226
*/
224227
def listTables(db: String, pattern: String): Seq[TableIdentifier] = {
225228
val dbTables =
226-
externalCatalog.listTables(db, pattern).map { t => TableIdentifier(t, Some(currentDb)) }
229+
externalCatalog.listTables(db, pattern).map { t => TableIdentifier(t, Some(db)) }
227230
val regex = pattern.replaceAll("\\*", ".*").r
228231
val _tempTables = tempTables.keys().asScala
229232
.filter { t => regex.pattern.matcher(t).matches() }

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,11 @@ abstract class CatalogTestUtils {
544544
CatalogDatabase(name, name + " description", newUriForDatabase(), Map.empty)
545545
}
546546

547-
def newTable(name: String, db: String): CatalogTable = {
547+
def newTable(name: String, db: String): CatalogTable = newTable(name, Some(db))
548+
549+
def newTable(name: String, database: Option[String] = None): CatalogTable = {
548550
CatalogTable(
549-
name = TableIdentifier(name, Some(db)),
551+
name = TableIdentifier(name, database),
550552
tableType = CatalogTableType.EXTERNAL_TABLE,
551553
storage = storageFormat,
552554
schema = Seq(CatalogColumn("col1", "int"), CatalogColumn("col2", "string")),

0 commit comments

Comments
 (0)