Skip to content

Commit

Permalink
[SPARK-23378][SQL] move setCurrentDatabase from HiveExternalCatalog t…
Browse files Browse the repository at this point in the history
…o HiveClientImpl

## What changes were proposed in this pull request?

This removes the special case that `alterPartitions` call from `HiveExternalCatalog` can reset the current database in the hive client as a side effect.

## How was this patch tested?

(Please explain how this patch was tested. E.g. unit tests, integration tests, manual tests)
(If this patch involves UI changes, please attach a screenshot; otherwise, remove this)

Please review http://spark.apache.org/contributing.html before opening a pull request.

Author: Feng Liu <[email protected]>

Closes #20564 from liufengdb/move.
  • Loading branch information
Feng Liu authored and gatorsmile committed Feb 12, 2018
1 parent 0c66fe4 commit fba01b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1107,11 +1107,6 @@ private[spark] class HiveExternalCatalog(conf: SparkConf, hadoopConf: Configurat
}
}

// Note: Before altering table partitions in Hive, you *must* set the current database
// to the one that contains the table of interest. Otherwise you will end up with the
// most helpful error message ever: "Unable to alter partition. alter is not possible."
// See HIVE-2742 for more detail.
client.setCurrentDatabase(db)
client.alterPartitions(db, table, withStatsProps)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,18 @@ private[hive] class HiveClientImpl(
state.err = stream
}

override def setCurrentDatabase(databaseName: String): Unit = withHiveState {
if (databaseExists(databaseName)) {
state.setCurrentDatabase(databaseName)
private def setCurrentDatabaseRaw(db: String): Unit = {
if (databaseExists(db)) {
state.setCurrentDatabase(db)
} else {
throw new NoSuchDatabaseException(databaseName)
throw new NoSuchDatabaseException(db)
}
}

override def setCurrentDatabase(databaseName: String): Unit = withHiveState {
setCurrentDatabaseRaw(databaseName)
}

override def createDatabase(
database: CatalogDatabase,
ignoreIfExists: Boolean): Unit = withHiveState {
Expand Down Expand Up @@ -598,8 +602,18 @@ private[hive] class HiveClientImpl(
db: String,
table: String,
newParts: Seq[CatalogTablePartition]): Unit = withHiveState {
val hiveTable = toHiveTable(getTable(db, table), Some(userName))
shim.alterPartitions(client, table, newParts.map { p => toHivePartition(p, hiveTable) }.asJava)
// Note: Before altering table partitions in Hive, you *must* set the current database
// to the one that contains the table of interest. Otherwise you will end up with the
// most helpful error message ever: "Unable to alter partition. alter is not possible."
// See HIVE-2742 for more detail.
val original = state.getCurrentDatabase
try {
setCurrentDatabaseRaw(db)
val hiveTable = toHiveTable(getTable(db, table), Some(userName))
shim.alterPartitions(client, table, newParts.map { toHivePartition(_, hiveTable) }.asJava)
} finally {
state.setCurrentDatabase(original)
}
}

/**
Expand Down

0 comments on commit fba01b9

Please sign in to comment.