Skip to content

Commit fba01b9

Browse files
Feng Liugatorsmile
authored andcommitted
[SPARK-23378][SQL] move setCurrentDatabase from HiveExternalCatalog to 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.
1 parent 0c66fe4 commit fba01b9

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveExternalCatalog.scala

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,11 +1107,6 @@ private[spark] class HiveExternalCatalog(conf: SparkConf, hadoopConf: Configurat
11071107
}
11081108
}
11091109

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

sql/hive/src/main/scala/org/apache/spark/sql/hive/client/HiveClientImpl.scala

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,18 @@ private[hive] class HiveClientImpl(
291291
state.err = stream
292292
}
293293

294-
override def setCurrentDatabase(databaseName: String): Unit = withHiveState {
295-
if (databaseExists(databaseName)) {
296-
state.setCurrentDatabase(databaseName)
294+
private def setCurrentDatabaseRaw(db: String): Unit = {
295+
if (databaseExists(db)) {
296+
state.setCurrentDatabase(db)
297297
} else {
298-
throw new NoSuchDatabaseException(databaseName)
298+
throw new NoSuchDatabaseException(db)
299299
}
300300
}
301301

302+
override def setCurrentDatabase(databaseName: String): Unit = withHiveState {
303+
setCurrentDatabaseRaw(databaseName)
304+
}
305+
302306
override def createDatabase(
303307
database: CatalogDatabase,
304308
ignoreIfExists: Boolean): Unit = withHiveState {
@@ -598,8 +602,18 @@ private[hive] class HiveClientImpl(
598602
db: String,
599603
table: String,
600604
newParts: Seq[CatalogTablePartition]): Unit = withHiveState {
601-
val hiveTable = toHiveTable(getTable(db, table), Some(userName))
602-
shim.alterPartitions(client, table, newParts.map { p => toHivePartition(p, hiveTable) }.asJava)
605+
// Note: Before altering table partitions in Hive, you *must* set the current database
606+
// to the one that contains the table of interest. Otherwise you will end up with the
607+
// most helpful error message ever: "Unable to alter partition. alter is not possible."
608+
// See HIVE-2742 for more detail.
609+
val original = state.getCurrentDatabase
610+
try {
611+
setCurrentDatabaseRaw(db)
612+
val hiveTable = toHiveTable(getTable(db, table), Some(userName))
613+
shim.alterPartitions(client, table, newParts.map { toHivePartition(_, hiveTable) }.asJava)
614+
} finally {
615+
state.setCurrentDatabase(original)
616+
}
603617
}
604618

605619
/**

0 commit comments

Comments
 (0)