Skip to content

Commit 94272a9

Browse files
gatorsmilecloud-fan
authored andcommitted
[SPARK-19028][SQL] Fixed non-thread-safe functions used in SessionCatalog
### What changes were proposed in this pull request? Fixed non-thread-safe functions used in SessionCatalog: - refreshTable - lookupRelation ### How was this patch tested? N/A Author: gatorsmile <[email protected]> Closes #16437 from gatorsmile/addSyncToLookUpTable. (cherry picked from commit 35e9740) Signed-off-by: Wenchen Fan <[email protected]>
1 parent d489e1d commit 94272a9

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ class SessionCatalog(
632632
/**
633633
* Refresh the cache entry for a metastore table, if any.
634634
*/
635-
def refreshTable(name: TableIdentifier): Unit = {
635+
def refreshTable(name: TableIdentifier): Unit = synchronized {
636636
// Go through temporary tables and invalidate them.
637637
// If the database is defined, this is definitely not a temp table.
638638
// If the database is not defined, there is a good chance this is a temp table.

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,25 @@ private[sql] class HiveSessionCatalog(
5656
hadoopConf) {
5757

5858
override def lookupRelation(name: TableIdentifier, alias: Option[String]): LogicalPlan = {
59-
val table = formatTableName(name.table)
60-
val db = formatDatabaseName(name.database.getOrElse(currentDb))
61-
if (db == globalTempViewManager.database) {
62-
val relationAlias = alias.getOrElse(table)
63-
globalTempViewManager.get(table).map { viewDef =>
64-
SubqueryAlias(relationAlias, viewDef, Some(name))
65-
}.getOrElse(throw new NoSuchTableException(db, table))
66-
} else if (name.database.isDefined || !tempTables.contains(table)) {
67-
val database = name.database.map(formatDatabaseName)
68-
val newName = name.copy(database = database, table = table)
69-
metastoreCatalog.lookupRelation(newName, alias)
70-
} else {
71-
val relation = tempTables(table)
72-
val tableWithQualifiers = SubqueryAlias(table, relation, None)
73-
// If an alias was specified by the lookup, wrap the plan in a subquery so that
74-
// attributes are properly qualified with this alias.
75-
alias.map(a => SubqueryAlias(a, tableWithQualifiers, None)).getOrElse(tableWithQualifiers)
59+
synchronized {
60+
val table = formatTableName(name.table)
61+
val db = formatDatabaseName(name.database.getOrElse(currentDb))
62+
if (db == globalTempViewManager.database) {
63+
val relationAlias = alias.getOrElse(table)
64+
globalTempViewManager.get(table).map { viewDef =>
65+
SubqueryAlias(relationAlias, viewDef, Some(name))
66+
}.getOrElse(throw new NoSuchTableException(db, table))
67+
} else if (name.database.isDefined || !tempTables.contains(table)) {
68+
val database = name.database.map(formatDatabaseName)
69+
val newName = name.copy(database = database, table = table)
70+
metastoreCatalog.lookupRelation(newName, alias)
71+
} else {
72+
val relation = tempTables(table)
73+
val tableWithQualifiers = SubqueryAlias(table, relation, None)
74+
// If an alias was specified by the lookup, wrap the plan in a subquery so that
75+
// attributes are properly qualified with this alias.
76+
alias.map(a => SubqueryAlias(a, tableWithQualifiers, None)).getOrElse(tableWithQualifiers)
77+
}
7678
}
7779
}
7880

0 commit comments

Comments
 (0)