Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ class SessionCatalog(
* Create a metastore table in the database specified in `tableDefinition`.
* If no such database is specified, create it in the current database.
*/
def createTable(tableDefinition: CatalogTable, ignoreIfExists: Boolean): Unit = {
def createTable(
tableDefinition: CatalogTable,
ignoreIfExists: Boolean,
validateLocation: Boolean = true): Unit = {
val db = formatDatabaseName(tableDefinition.identifier.database.getOrElse(getCurrentDatabase))
val table = formatTableName(tableDefinition.identifier.table)
val tableIdentifier = TableIdentifier(table, Some(db))
Expand All @@ -305,7 +308,11 @@ class SessionCatalog(
}

requireDbExists(db)
if (!ignoreIfExists) {
if (tableExists(newTableDefinition.identifier)) {
if (!ignoreIfExists) {
throw new TableAlreadyExistsException(db = db, table = table)
}
} else if (validateLocation) {
validateTableLocation(newTableDefinition)
}
externalCatalog.createTable(newTableDefinition, ignoreIfExists)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ case class CreateDataSourceTableAsSelectCommand(
// provider (for example, see org.apache.spark.sql.parquet.DefaultSource).
schema = result.schema)
// Table location is already validated. No need to check it again during table creation.
sessionState.catalog.createTable(newTable, ignoreIfExists = true)
sessionState.catalog.createTable(newTable, ignoreIfExists = false, validateLocation = false)

result match {
case fs: HadoopFsRelation if table.partitionColumnNames.nonEmpty &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,15 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
sql(s"CREATE TABLE tab1 (col1 int, col2 string) USING ${dataSource}")
}.getMessage
assert(ex.contains(exMsgWithDefaultDB))

// Always check location of managed table, with or without (IF NOT EXISTS)
withTable("tab2") {
sql(s"CREATE TABLE tab2 (col1 int, col2 string) USING ${dataSource}")
ex = intercept[AnalysisException] {
sql(s"CREATE TABLE IF NOT EXISTS tab1 LIKE tab2")
}.getMessage
assert(ex.contains(exMsgWithDefaultDB))
}
} finally {
waitForTasksToFinish()
Utils.deleteRecursively(tableLoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ class HiveDDLSuite
assert(e2.getMessage.contains(forbiddenPrefix + "foo"))

val e3 = intercept[AnalysisException] {
sql(s"CREATE TABLE tbl (a INT) TBLPROPERTIES ('${forbiddenPrefix}foo'='anything')")
sql(s"CREATE TABLE tbl2 (a INT) TBLPROPERTIES ('${forbiddenPrefix}foo'='anything')")
}
assert(e3.getMessage.contains(forbiddenPrefix + "foo"))
}
Expand Down