Github user windpiger commented on a diff in the pull request:
https://github.com/apache/spark/pull/17287#discussion_r106336735
--- Diff:
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/catalog/SessionCatalogSuite.scala
---
@@ -76,468 +102,500 @@ class SessionCatalogSuite extends PlanTest {
}
test("create databases using invalid names") {
- val catalog = new SessionCatalog(newEmptyCatalog())
- testInvalidName(name => catalog.createDatabase(newDb(name),
ignoreIfExists = true))
+ withEmptyCatalog { catalog =>
+ testInvalidName(
+ name => catalog.createDatabase(newDb(name), ignoreIfExists = true))
+ }
}
test("get database when a database exists") {
- val catalog = new SessionCatalog(newBasicCatalog())
- val db1 = catalog.getDatabaseMetadata("db1")
- assert(db1.name == "db1")
- assert(db1.description.contains("db1"))
+ withBasicCatalog { catalog =>
+ val db1 = catalog.getDatabaseMetadata("db1")
+ assert(db1.name == "db1")
+ assert(db1.description.contains("db1"))
+ }
}
test("get database should throw exception when the database does not
exist") {
- val catalog = new SessionCatalog(newBasicCatalog())
- intercept[NoSuchDatabaseException] {
- catalog.getDatabaseMetadata("db_that_does_not_exist")
+ withBasicCatalog { catalog =>
+ intercept[NoSuchDatabaseException] {
+ catalog.getDatabaseMetadata("db_that_does_not_exist")
+ }
}
}
test("list databases without pattern") {
- val catalog = new SessionCatalog(newBasicCatalog())
- assert(catalog.listDatabases().toSet == Set("default", "db1", "db2",
"db3"))
+ withBasicCatalog { catalog =>
+ assert(catalog.listDatabases().toSet == Set("default", "db1", "db2",
"db3"))
+ }
}
test("list databases with pattern") {
- val catalog = new SessionCatalog(newBasicCatalog())
- assert(catalog.listDatabases("db").toSet == Set.empty)
- assert(catalog.listDatabases("db*").toSet == Set("db1", "db2", "db3"))
- assert(catalog.listDatabases("*1").toSet == Set("db1"))
- assert(catalog.listDatabases("db2").toSet == Set("db2"))
+ withBasicCatalog { catalog =>
+ assert(catalog.listDatabases("db").toSet == Set.empty)
+ assert(catalog.listDatabases("db*").toSet == Set("db1", "db2",
"db3"))
+ assert(catalog.listDatabases("*1").toSet == Set("db1"))
+ assert(catalog.listDatabases("db2").toSet == Set("db2"))
+ }
}
test("drop database") {
- val catalog = new SessionCatalog(newBasicCatalog())
- catalog.dropDatabase("db1", ignoreIfNotExists = false, cascade = false)
- assert(catalog.listDatabases().toSet == Set("default", "db2", "db3"))
+ withBasicCatalog { catalog =>
+ catalog.dropDatabase("db1", ignoreIfNotExists = false, cascade =
false)
+ assert(catalog.listDatabases().toSet == Set("default", "db2", "db3"))
+ }
}
test("drop database when the database is not empty") {
// Throw exception if there are functions left
- val externalCatalog1 = newBasicCatalog()
- val sessionCatalog1 = new SessionCatalog(externalCatalog1)
- externalCatalog1.dropTable("db2", "tbl1", ignoreIfNotExists = false,
purge = false)
- externalCatalog1.dropTable("db2", "tbl2", ignoreIfNotExists = false,
purge = false)
- intercept[AnalysisException] {
- sessionCatalog1.dropDatabase("db2", ignoreIfNotExists = false,
cascade = false)
+ withBasicCatalog { catalog =>
+ catalog.externalCatalog.dropTable("db2", "tbl1", ignoreIfNotExists =
false, purge = false)
+ catalog.externalCatalog.dropTable("db2", "tbl2", ignoreIfNotExists =
false, purge = false)
+ intercept[AnalysisException] {
+ catalog.dropDatabase("db2", ignoreIfNotExists = false, cascade =
false)
+ }
}
-
- // Throw exception if there are tables left
- val externalCatalog2 = newBasicCatalog()
- val sessionCatalog2 = new SessionCatalog(externalCatalog2)
- externalCatalog2.dropFunction("db2", "func1")
- intercept[AnalysisException] {
- sessionCatalog2.dropDatabase("db2", ignoreIfNotExists = false,
cascade = false)
+ withBasicCatalog { catalog =>
+ // Throw exception if there are tables left
+ catalog.externalCatalog.dropFunction("db2", "func1")
+ intercept[AnalysisException] {
+ catalog.dropDatabase("db2", ignoreIfNotExists = false, cascade =
false)
+ }
}
- // When cascade is true, it should drop them
- val externalCatalog3 = newBasicCatalog()
- val sessionCatalog3 = new SessionCatalog(externalCatalog3)
- externalCatalog3.dropDatabase("db2", ignoreIfNotExists = false,
cascade = true)
- assert(sessionCatalog3.listDatabases().toSet == Set("default", "db1",
"db3"))
+ withBasicCatalog { catalog =>
+ // When cascade is true, it should drop them
+ catalog.externalCatalog.dropDatabase("db2", ignoreIfNotExists =
false, cascade = true)
+ assert(catalog.listDatabases().toSet == Set("default", "db1", "db3"))
+ }
}
test("drop database when the database does not exist") {
- val catalog = new SessionCatalog(newBasicCatalog())
- intercept[NoSuchDatabaseException] {
- catalog.dropDatabase("db_that_does_not_exist", ignoreIfNotExists =
false, cascade = false)
+ withBasicCatalog { catalog =>
+ // TODO: fix this inconsistent between HiveExternalCatalog and
InMemoryCatalog
+ if (isHiveExternalCatalog) {
+ val e = intercept[AnalysisException] {
+ catalog.dropDatabase("db_that_does_not_exist", ignoreIfNotExists
= false, cascade = false)
+ }.getMessage
+ assert(e.contains(
+ "org.apache.hadoop.hive.metastore.api.NoSuchObjectException:
db_that_does_not_exist"))
+ } else {
+ intercept[NoSuchDatabaseException] {
+ catalog.dropDatabase("db_that_does_not_exist", ignoreIfNotExists
= false, cascade = false)
+ }
+ }
+ catalog.dropDatabase("db_that_does_not_exist", ignoreIfNotExists =
true, cascade = false)
}
- catalog.dropDatabase("db_that_does_not_exist", ignoreIfNotExists =
true, cascade = false)
}
test("drop current database and drop default database") {
- val catalog = new SessionCatalog(newBasicCatalog())
- catalog.setCurrentDatabase("db1")
- assert(catalog.getCurrentDatabase == "db1")
- catalog.dropDatabase("db1", ignoreIfNotExists = false, cascade = true)
- intercept[NoSuchDatabaseException] {
- catalog.createTable(newTable("tbl1", "db1"), ignoreIfExists = false)
- }
- catalog.setCurrentDatabase("default")
- assert(catalog.getCurrentDatabase == "default")
- intercept[AnalysisException] {
- catalog.dropDatabase("default", ignoreIfNotExists = false, cascade =
true)
+ withBasicCatalog { catalog =>
+ catalog.setCurrentDatabase("db1")
+ assert(catalog.getCurrentDatabase == "db1")
+ catalog.dropDatabase("db1", ignoreIfNotExists = false, cascade =
true)
+ intercept[NoSuchDatabaseException] {
+ catalog.createTable(newTable("tbl1", "db1"), ignoreIfExists =
false)
+ }
+ catalog.setCurrentDatabase("default")
+ assert(catalog.getCurrentDatabase == "default")
+ intercept[AnalysisException] {
+ catalog.dropDatabase("default", ignoreIfNotExists = false, cascade
= true)
+ }
}
}
test("alter database") {
- val catalog = new SessionCatalog(newBasicCatalog())
- val db1 = catalog.getDatabaseMetadata("db1")
- // Note: alter properties here because Hive does not support altering
other fields
- catalog.alterDatabase(db1.copy(properties = Map("k" -> "v3", "good" ->
"true")))
- val newDb1 = catalog.getDatabaseMetadata("db1")
- assert(db1.properties.isEmpty)
- assert(newDb1.properties.size == 2)
- assert(newDb1.properties.get("k") == Some("v3"))
- assert(newDb1.properties.get("good") == Some("true"))
+ withBasicCatalog { catalog =>
+ val db1 = catalog.getDatabaseMetadata("db1")
+ // Note: alter properties here because Hive does not support
altering other fields
+ catalog.alterDatabase(db1.copy(properties = Map("k" -> "v3", "good"
-> "true")))
+ val newDb1 = catalog.getDatabaseMetadata("db1")
+ assert(db1.properties.isEmpty)
+ assert(newDb1.properties.size == 2)
+ assert(newDb1.properties.get("k") == Some("v3"))
+ assert(newDb1.properties.get("good") == Some("true"))
+ }
}
test("alter database should throw exception when the database does not
exist") {
- val catalog = new SessionCatalog(newBasicCatalog())
- intercept[NoSuchDatabaseException] {
- catalog.alterDatabase(newDb("unknown_db"))
+ withBasicCatalog { catalog =>
+ intercept[NoSuchDatabaseException] {
+ catalog.alterDatabase(newDb("unknown_db"))
+ }
}
}
test("get/set current database") {
- val catalog = new SessionCatalog(newBasicCatalog())
- assert(catalog.getCurrentDatabase == "default")
- catalog.setCurrentDatabase("db2")
- assert(catalog.getCurrentDatabase == "db2")
- intercept[NoSuchDatabaseException] {
+ withBasicCatalog { catalog =>
+ assert(catalog.getCurrentDatabase == "default")
+ catalog.setCurrentDatabase("db2")
+ assert(catalog.getCurrentDatabase == "db2")
+ intercept[NoSuchDatabaseException] {
+ catalog.setCurrentDatabase("deebo")
+ }
+ catalog.createDatabase(newDb("deebo"), ignoreIfExists = false)
catalog.setCurrentDatabase("deebo")
+ assert(catalog.getCurrentDatabase == "deebo")
}
- catalog.createDatabase(newDb("deebo"), ignoreIfExists = false)
- catalog.setCurrentDatabase("deebo")
- assert(catalog.getCurrentDatabase == "deebo")
}
//
--------------------------------------------------------------------------
// Tables
//
--------------------------------------------------------------------------
test("create table") {
- val externalCatalog = newBasicCatalog()
- val sessionCatalog = new SessionCatalog(externalCatalog)
- assert(externalCatalog.listTables("db1").isEmpty)
- assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl2"))
- sessionCatalog.createTable(newTable("tbl3", "db1"), ignoreIfExists =
false)
- sessionCatalog.createTable(newTable("tbl3", "db2"), ignoreIfExists =
false)
- assert(externalCatalog.listTables("db1").toSet == Set("tbl3"))
- assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl2",
"tbl3"))
- // Create table without explicitly specifying database
- sessionCatalog.setCurrentDatabase("db1")
- sessionCatalog.createTable(newTable("tbl4"), ignoreIfExists = false)
- assert(externalCatalog.listTables("db1").toSet == Set("tbl3", "tbl4"))
- assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl2",
"tbl3"))
+ withBasicCatalog { catalog =>
+ assert(catalog.externalCatalog.listTables("db1").isEmpty)
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl1", "tbl2"))
+ catalog.createTable(newTable("tbl3", "db1"), ignoreIfExists = false)
+ catalog.createTable(newTable("tbl3", "db2"), ignoreIfExists = false)
+ assert(catalog.externalCatalog.listTables("db1").toSet ==
Set("tbl3"))
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl1", "tbl2", "tbl3"))
+ // Create table without explicitly specifying database
+ catalog.setCurrentDatabase("db1")
+ catalog.createTable(newTable("tbl4"), ignoreIfExists = false)
+ assert(catalog.externalCatalog.listTables("db1").toSet ==
Set("tbl3", "tbl4"))
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl1", "tbl2", "tbl3"))
+ }
}
test("create tables using invalid names") {
- val catalog = new SessionCatalog(newEmptyCatalog())
- testInvalidName(name => catalog.createTable(newTable(name, "db1"),
ignoreIfExists = false))
+ withEmptyCatalog { catalog =>
+ testInvalidName(name => catalog.createTable(newTable(name, "db1"),
ignoreIfExists = false))
+ }
}
test("create table when database does not exist") {
- val catalog = new SessionCatalog(newBasicCatalog())
- // Creating table in non-existent database should always fail
- intercept[NoSuchDatabaseException] {
- catalog.createTable(newTable("tbl1", "does_not_exist"),
ignoreIfExists = false)
- }
- intercept[NoSuchDatabaseException] {
- catalog.createTable(newTable("tbl1", "does_not_exist"),
ignoreIfExists = true)
- }
- // Table already exists
- intercept[TableAlreadyExistsException] {
- catalog.createTable(newTable("tbl1", "db2"), ignoreIfExists = false)
+ withBasicCatalog { catalog =>
+ // Creating table in non-existent database should always fail
+ intercept[NoSuchDatabaseException] {
+ catalog.createTable(newTable("tbl1", "does_not_exist"),
ignoreIfExists = false)
+ }
+ intercept[NoSuchDatabaseException] {
+ catalog.createTable(newTable("tbl1", "does_not_exist"),
ignoreIfExists = true)
+ }
+ // Table already exists
+ intercept[TableAlreadyExistsException] {
+ catalog.createTable(newTable("tbl1", "db2"), ignoreIfExists =
false)
+ }
+ catalog.createTable(newTable("tbl1", "db2"), ignoreIfExists = true)
}
- catalog.createTable(newTable("tbl1", "db2"), ignoreIfExists = true)
}
test("create temp table") {
- val catalog = new SessionCatalog(newBasicCatalog())
- val tempTable1 = Range(1, 10, 1, 10)
- val tempTable2 = Range(1, 20, 2, 10)
- catalog.createTempView("tbl1", tempTable1, overrideIfExists = false)
- catalog.createTempView("tbl2", tempTable2, overrideIfExists = false)
- assert(catalog.getTempView("tbl1") == Option(tempTable1))
- assert(catalog.getTempView("tbl2") == Option(tempTable2))
- assert(catalog.getTempView("tbl3").isEmpty)
- // Temporary table already exists
- intercept[TempTableAlreadyExistsException] {
+ withBasicCatalog { catalog =>
+ val tempTable1 = Range(1, 10, 1, 10)
+ val tempTable2 = Range(1, 20, 2, 10)
catalog.createTempView("tbl1", tempTable1, overrideIfExists = false)
+ catalog.createTempView("tbl2", tempTable2, overrideIfExists = false)
+ assert(catalog.getTempView("tbl1") == Option(tempTable1))
+ assert(catalog.getTempView("tbl2") == Option(tempTable2))
+ assert(catalog.getTempView("tbl3").isEmpty)
+ // Temporary table already exists
+ intercept[TempTableAlreadyExistsException] {
+ catalog.createTempView("tbl1", tempTable1, overrideIfExists =
false)
+ }
+ // Temporary table already exists but we override it
+ catalog.createTempView("tbl1", tempTable2, overrideIfExists = true)
+ assert(catalog.getTempView("tbl1") == Option(tempTable2))
}
- // Temporary table already exists but we override it
- catalog.createTempView("tbl1", tempTable2, overrideIfExists = true)
- assert(catalog.getTempView("tbl1") == Option(tempTable2))
}
test("drop table") {
- val externalCatalog = newBasicCatalog()
- val sessionCatalog = new SessionCatalog(externalCatalog)
- assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl2"))
- sessionCatalog.dropTable(TableIdentifier("tbl1", Some("db2")),
ignoreIfNotExists = false,
- purge = false)
- assert(externalCatalog.listTables("db2").toSet == Set("tbl2"))
- // Drop table without explicitly specifying database
- sessionCatalog.setCurrentDatabase("db2")
- sessionCatalog.dropTable(TableIdentifier("tbl2"), ignoreIfNotExists =
false, purge = false)
- assert(externalCatalog.listTables("db2").isEmpty)
+ withBasicCatalog { catalog =>
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl1", "tbl2"))
+ catalog.dropTable(TableIdentifier("tbl1", Some("db2")),
ignoreIfNotExists = false,
+ purge = false)
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl2"))
+ // Drop table without explicitly specifying database
+ catalog.setCurrentDatabase("db2")
+ catalog.dropTable(TableIdentifier("tbl2"), ignoreIfNotExists =
false, purge = false)
+ assert(catalog.externalCatalog.listTables("db2").isEmpty)
+ }
}
test("drop table when database/table does not exist") {
- val catalog = new SessionCatalog(newBasicCatalog())
- // Should always throw exception when the database does not exist
- intercept[NoSuchDatabaseException] {
- catalog.dropTable(TableIdentifier("tbl1", Some("unknown_db")),
ignoreIfNotExists = false,
- purge = false)
- }
- intercept[NoSuchDatabaseException] {
- catalog.dropTable(TableIdentifier("tbl1", Some("unknown_db")),
ignoreIfNotExists = true,
- purge = false)
- }
- intercept[NoSuchTableException] {
- catalog.dropTable(TableIdentifier("unknown_table", Some("db2")),
ignoreIfNotExists = false,
+ withBasicCatalog { catalog =>
+ // Should always throw exception when the database does not exist
+ intercept[NoSuchDatabaseException] {
+ catalog.dropTable(TableIdentifier("tbl1", Some("unknown_db")),
ignoreIfNotExists = false,
+ purge = false)
+ }
+ intercept[NoSuchDatabaseException] {
+ catalog.dropTable(TableIdentifier("tbl1", Some("unknown_db")),
ignoreIfNotExists = true,
+ purge = false)
+ }
+ intercept[NoSuchTableException] {
+ catalog.dropTable(TableIdentifier("unknown_table", Some("db2")),
ignoreIfNotExists = false,
+ purge = false)
+ }
+ catalog.dropTable(TableIdentifier("unknown_table", Some("db2")),
ignoreIfNotExists = true,
purge = false)
}
- catalog.dropTable(TableIdentifier("unknown_table", Some("db2")),
ignoreIfNotExists = true,
- purge = false)
}
test("drop temp table") {
- val externalCatalog = newBasicCatalog()
- val sessionCatalog = new SessionCatalog(externalCatalog)
- val tempTable = Range(1, 10, 2, 10)
- sessionCatalog.createTempView("tbl1", tempTable, overrideIfExists =
false)
- sessionCatalog.setCurrentDatabase("db2")
- assert(sessionCatalog.getTempView("tbl1") == Some(tempTable))
- assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl2"))
- // If database is not specified, temp table should be dropped first
- sessionCatalog.dropTable(TableIdentifier("tbl1"), ignoreIfNotExists =
false, purge = false)
- assert(sessionCatalog.getTempView("tbl1") == None)
- assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl2"))
- // If temp table does not exist, the table in the current database
should be dropped
- sessionCatalog.dropTable(TableIdentifier("tbl1"), ignoreIfNotExists =
false, purge = false)
- assert(externalCatalog.listTables("db2").toSet == Set("tbl2"))
- // If database is specified, temp tables are never dropped
- sessionCatalog.createTempView("tbl1", tempTable, overrideIfExists =
false)
- sessionCatalog.createTable(newTable("tbl1", "db2"), ignoreIfExists =
false)
- sessionCatalog.dropTable(TableIdentifier("tbl1", Some("db2")),
ignoreIfNotExists = false,
- purge = false)
- assert(sessionCatalog.getTempView("tbl1") == Some(tempTable))
- assert(externalCatalog.listTables("db2").toSet == Set("tbl2"))
+ withBasicCatalog { catalog =>
+ val tempTable = Range(1, 10, 2, 10)
+ catalog.createTempView("tbl1", tempTable, overrideIfExists = false)
+ catalog.setCurrentDatabase("db2")
+ assert(catalog.getTempView("tbl1") == Some(tempTable))
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl1", "tbl2"))
+ // If database is not specified, temp table should be dropped first
+ catalog.dropTable(TableIdentifier("tbl1"), ignoreIfNotExists =
false, purge = false)
+ assert(catalog.getTempView("tbl1") == None)
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl1", "tbl2"))
+ // If temp table does not exist, the table in the current database
should be dropped
+ catalog.dropTable(TableIdentifier("tbl1"), ignoreIfNotExists =
false, purge = false)
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl2"))
+ // If database is specified, temp tables are never dropped
+ catalog.createTempView("tbl1", tempTable, overrideIfExists = false)
+ catalog.createTable(newTable("tbl1", "db2"), ignoreIfExists = false)
+ catalog.dropTable(TableIdentifier("tbl1", Some("db2")),
ignoreIfNotExists = false,
+ purge = false)
+ assert(catalog.getTempView("tbl1") == Some(tempTable))
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl2"))
+ }
}
test("rename table") {
- val externalCatalog = newBasicCatalog()
- val sessionCatalog = new SessionCatalog(externalCatalog)
- assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl2"))
- sessionCatalog.renameTable(TableIdentifier("tbl1", Some("db2")),
TableIdentifier("tblone"))
- assert(externalCatalog.listTables("db2").toSet == Set("tblone",
"tbl2"))
- sessionCatalog.renameTable(TableIdentifier("tbl2", Some("db2")),
TableIdentifier("tbltwo"))
- assert(externalCatalog.listTables("db2").toSet == Set("tblone",
"tbltwo"))
- // Rename table without explicitly specifying database
- sessionCatalog.setCurrentDatabase("db2")
- sessionCatalog.renameTable(TableIdentifier("tbltwo"),
TableIdentifier("table_two"))
- assert(externalCatalog.listTables("db2").toSet == Set("tblone",
"table_two"))
- // Renaming "db2.tblone" to "db1.tblones" should fail because
databases don't match
- intercept[AnalysisException] {
- sessionCatalog.renameTable(
- TableIdentifier("tblone", Some("db2")), TableIdentifier("tblones",
Some("db1")))
- }
- // The new table already exists
- intercept[TableAlreadyExistsException] {
- sessionCatalog.renameTable(
- TableIdentifier("tblone", Some("db2")),
- TableIdentifier("table_two"))
+ withBasicCatalog { catalog =>
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl1", "tbl2"))
+ catalog.renameTable(TableIdentifier("tbl1", Some("db2")),
TableIdentifier("tblone"))
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tblone", "tbl2"))
+ catalog.renameTable(TableIdentifier("tbl2", Some("db2")),
TableIdentifier("tbltwo"))
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tblone", "tbltwo"))
+ // Rename table without explicitly specifying database
+ catalog.setCurrentDatabase("db2")
+ catalog.renameTable(TableIdentifier("tbltwo"),
TableIdentifier("table_two"))
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tblone", "table_two"))
+ // Renaming "db2.tblone" to "db1.tblones" should fail because
databases don't match
+ intercept[AnalysisException] {
+ catalog.renameTable(
+ TableIdentifier("tblone", Some("db2")),
TableIdentifier("tblones", Some("db1")))
+ }
+ // The new table already exists
+ intercept[TableAlreadyExistsException] {
+ catalog.renameTable(
+ TableIdentifier("tblone", Some("db2")),
+ TableIdentifier("table_two"))
+ }
}
}
test("rename tables to an invalid name") {
- val catalog = new SessionCatalog(newBasicCatalog())
- testInvalidName(
- name => catalog.renameTable(TableIdentifier("tbl1", Some("db2")),
TableIdentifier(name)))
+ withBasicCatalog { catalog =>
+ testInvalidName(
+ name => catalog.renameTable(TableIdentifier("tbl1", Some("db2")),
TableIdentifier(name)))
+ }
}
test("rename table when database/table does not exist") {
- val catalog = new SessionCatalog(newBasicCatalog())
- intercept[NoSuchDatabaseException] {
- catalog.renameTable(TableIdentifier("tbl1", Some("unknown_db")),
TableIdentifier("tbl2"))
- }
- intercept[NoSuchTableException] {
- catalog.renameTable(TableIdentifier("unknown_table", Some("db2")),
TableIdentifier("tbl2"))
+ withBasicCatalog { catalog =>
+ intercept[NoSuchDatabaseException] {
+ catalog.renameTable(TableIdentifier("tbl1", Some("unknown_db")),
TableIdentifier("tbl2"))
+ }
+ intercept[NoSuchTableException] {
+ catalog.renameTable(TableIdentifier("unknown_table", Some("db2")),
TableIdentifier("tbl2"))
+ }
}
}
test("rename temp table") {
- val externalCatalog = newBasicCatalog()
- val sessionCatalog = new SessionCatalog(externalCatalog)
- val tempTable = Range(1, 10, 2, 10)
- sessionCatalog.createTempView("tbl1", tempTable, overrideIfExists =
false)
- sessionCatalog.setCurrentDatabase("db2")
- assert(sessionCatalog.getTempView("tbl1") == Option(tempTable))
- assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl2"))
- // If database is not specified, temp table should be renamed first
- sessionCatalog.renameTable(TableIdentifier("tbl1"),
TableIdentifier("tbl3"))
- assert(sessionCatalog.getTempView("tbl1").isEmpty)
- assert(sessionCatalog.getTempView("tbl3") == Option(tempTable))
- assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl2"))
- // If database is specified, temp tables are never renamed
- sessionCatalog.renameTable(TableIdentifier("tbl2", Some("db2")),
TableIdentifier("tbl4"))
- assert(sessionCatalog.getTempView("tbl3") == Option(tempTable))
- assert(sessionCatalog.getTempView("tbl4").isEmpty)
- assert(externalCatalog.listTables("db2").toSet == Set("tbl1", "tbl4"))
+ withBasicCatalog { catalog =>
+ val tempTable = Range(1, 10, 2, 10)
+ catalog.createTempView("tbl1", tempTable, overrideIfExists = false)
+ catalog.setCurrentDatabase("db2")
+ assert(catalog.getTempView("tbl1") == Option(tempTable))
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl1", "tbl2"))
+ // If database is not specified, temp table should be renamed first
+ catalog.renameTable(TableIdentifier("tbl1"), TableIdentifier("tbl3"))
+ assert(catalog.getTempView("tbl1").isEmpty)
+ assert(catalog.getTempView("tbl3") == Option(tempTable))
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl1", "tbl2"))
+ // If database is specified, temp tables are never renamed
+ catalog.renameTable(TableIdentifier("tbl2", Some("db2")),
TableIdentifier("tbl4"))
+ assert(catalog.getTempView("tbl3") == Option(tempTable))
+ assert(catalog.getTempView("tbl4").isEmpty)
+ assert(catalog.externalCatalog.listTables("db2").toSet ==
Set("tbl1", "tbl4"))
+ }
}
test("alter table") {
- val externalCatalog = newBasicCatalog()
- val sessionCatalog = new SessionCatalog(externalCatalog)
- val tbl1 = externalCatalog.getTable("db2", "tbl1")
- sessionCatalog.alterTable(tbl1.copy(properties = Map("toh" -> "frem")))
- val newTbl1 = externalCatalog.getTable("db2", "tbl1")
- assert(!tbl1.properties.contains("toh"))
- assert(newTbl1.properties.size == tbl1.properties.size + 1)
- assert(newTbl1.properties.get("toh") == Some("frem"))
- // Alter table without explicitly specifying database
- sessionCatalog.setCurrentDatabase("db2")
- sessionCatalog.alterTable(tbl1.copy(identifier =
TableIdentifier("tbl1")))
- val newestTbl1 = externalCatalog.getTable("db2", "tbl1")
- assert(newestTbl1 == tbl1)
+ withBasicCatalog { catalog =>
+ val tbl1 = catalog.externalCatalog.getTable("db2", "tbl1")
+ catalog.alterTable(tbl1.copy(properties = Map("toh" -> "frem")))
+ val newTbl1 = catalog.externalCatalog.getTable("db2", "tbl1")
+ assert(!tbl1.properties.contains("toh"))
+ assert(newTbl1.properties.size == tbl1.properties.size + 1)
+ assert(newTbl1.properties.get("toh") == Some("frem"))
+ // Alter table without explicitly specifying database
+ catalog.setCurrentDatabase("db2")
+ catalog.alterTable(tbl1.copy(identifier = TableIdentifier("tbl1")))
+ val newestTbl1 = catalog.externalCatalog.getTable("db2", "tbl1")
+ assert(newestTbl1.copy(properties = Map.empty) ==
tbl1.copy(properties = Map.empty))
--- End diff --
hive table properties will have transient_lastDdltime modified, so here we
set it to empty
and there is only this key exists in properties
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]