1717
1818package org .apache .spark .sql .catalyst .catalog
1919
20- import java .util .concurrent .ConcurrentHashMap
21-
22- import scala .collection .JavaConverters ._
20+ import scala .collection .mutable
2321
2422import org .apache .spark .sql .AnalysisException
2523import org .apache .spark .sql .catalyst .{CatalystConf , SimpleCatalystConf }
@@ -31,6 +29,8 @@ import org.apache.spark.sql.catalyst.plans.logical.{LogicalPlan, SubqueryAlias}
3129 * An internal catalog that is used by a Spark Session. This internal catalog serves as a
3230 * proxy to the underlying metastore (e.g. Hive Metastore) and it also manages temporary
3331 * tables and functions of the Spark Session that it belongs to.
32+ *
33+ * This class is not thread-safe.
3434 */
3535class SessionCatalog (externalCatalog : ExternalCatalog , conf : CatalystConf ) {
3636 import ExternalCatalog ._
@@ -39,8 +39,8 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
3939 this (externalCatalog, new SimpleCatalystConf (true ))
4040 }
4141
42- protected [this ] val tempTables = new ConcurrentHashMap [String , LogicalPlan ]
43- protected [this ] val tempFunctions = new ConcurrentHashMap [String , CatalogFunction ]
42+ protected [this ] val tempTables = new mutable. HashMap [String , LogicalPlan ]
43+ protected [this ] val tempFunctions = new mutable. HashMap [String , CatalogFunction ]
4444
4545 // Note: we track current database here because certain operations do not explicitly
4646 // specify the database (e.g. DROP TABLE my_table). In these cases we must first
@@ -122,9 +122,9 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
122122 * If no such database is specified, create it in the current database.
123123 */
124124 def createTable (tableDefinition : CatalogTable , ignoreIfExists : Boolean ): Unit = {
125- val db = tableDefinition.name .database.getOrElse(currentDb)
126- val table = formatTableName(tableDefinition.name .table)
127- val newTableDefinition = tableDefinition.copy(name = TableIdentifier (table, Some (db)))
125+ val db = tableDefinition.identifier .database.getOrElse(currentDb)
126+ val table = formatTableName(tableDefinition.identifier .table)
127+ val newTableDefinition = tableDefinition.copy(identifier = TableIdentifier (table, Some (db)))
128128 externalCatalog.createTable(db, newTableDefinition, ignoreIfExists)
129129 }
130130
@@ -138,9 +138,9 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
138138 * this becomes a no-op.
139139 */
140140 def alterTable (tableDefinition : CatalogTable ): Unit = {
141- val db = tableDefinition.name .database.getOrElse(currentDb)
142- val table = formatTableName(tableDefinition.name .table)
143- val newTableDefinition = tableDefinition.copy(name = TableIdentifier (table, Some (db)))
141+ val db = tableDefinition.identifier .database.getOrElse(currentDb)
142+ val table = formatTableName(tableDefinition.identifier .table)
143+ val newTableDefinition = tableDefinition.copy(identifier = TableIdentifier (table, Some (db)))
144144 externalCatalog.alterTable(db, newTableDefinition)
145145 }
146146
@@ -164,9 +164,9 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
164164 def createTempTable (
165165 name : String ,
166166 tableDefinition : LogicalPlan ,
167- ignoreIfExists : Boolean ): Unit = {
167+ overrideIfExists : Boolean ): Unit = {
168168 val table = formatTableName(name)
169- if (tempTables.containsKey (table) && ! ignoreIfExists ) {
169+ if (tempTables.contains (table) && ! overrideIfExists ) {
170170 throw new AnalysisException (s " Temporary table ' $name' already exists. " )
171171 }
172172 tempTables.put(table, tableDefinition)
@@ -188,10 +188,11 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
188188 val db = oldName.database.getOrElse(currentDb)
189189 val oldTableName = formatTableName(oldName.table)
190190 val newTableName = formatTableName(newName.table)
191- if (oldName.database.isDefined || ! tempTables.containsKey (oldTableName)) {
191+ if (oldName.database.isDefined || ! tempTables.contains (oldTableName)) {
192192 externalCatalog.renameTable(db, oldTableName, newTableName)
193193 } else {
194- val table = tempTables.remove(oldTableName)
194+ val table = tempTables(oldTableName)
195+ tempTables.remove(oldTableName)
195196 tempTables.put(newTableName, table)
196197 }
197198 }
@@ -206,7 +207,7 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
206207 def dropTable (name : TableIdentifier , ignoreIfNotExists : Boolean ): Unit = {
207208 val db = name.database.getOrElse(currentDb)
208209 val table = formatTableName(name.table)
209- if (name.database.isDefined || ! tempTables.containsKey (table)) {
210+ if (name.database.isDefined || ! tempTables.contains (table)) {
210211 externalCatalog.dropTable(db, table, ignoreIfNotExists)
211212 } else {
212213 tempTables.remove(table)
@@ -224,11 +225,11 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
224225 val db = name.database.getOrElse(currentDb)
225226 val table = formatTableName(name.table)
226227 val relation =
227- if (name.database.isDefined || ! tempTables.containsKey (table)) {
228+ if (name.database.isDefined || ! tempTables.contains (table)) {
228229 val metadata = externalCatalog.getTable(db, table)
229230 CatalogRelation (db, metadata, alias)
230231 } else {
231- tempTables.get (table)
232+ tempTables(table)
232233 }
233234 val qualifiedTable = SubqueryAlias (table, relation)
234235 // If an alias was specified by the lookup, wrap the plan in a subquery so that
@@ -247,7 +248,7 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
247248 def tableExists (name : TableIdentifier ): Boolean = {
248249 val db = name.database.getOrElse(currentDb)
249250 val table = formatTableName(name.table)
250- if (name.database.isDefined || ! tempTables.containsKey (table)) {
251+ if (name.database.isDefined || ! tempTables.contains (table)) {
251252 externalCatalog.tableExists(db, table)
252253 } else {
253254 true // it's a temporary table
@@ -266,7 +267,7 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
266267 val dbTables =
267268 externalCatalog.listTables(db, pattern).map { t => TableIdentifier (t, Some (db)) }
268269 val regex = pattern.replaceAll(" \\ *" , " .*" ).r
269- val _tempTables = tempTables.keys().asScala
270+ val _tempTables = tempTables.keys.toSeq
270271 .filter { t => regex.pattern.matcher(t).matches() }
271272 .map { t => TableIdentifier (t) }
272273 dbTables ++ _tempTables
@@ -290,7 +291,7 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
290291 * For testing only.
291292 */
292293 private [catalog] def getTempTable (name : String ): Option [LogicalPlan ] = {
293- Option ( tempTables.get(name) )
294+ tempTables.get(name)
294295 }
295296
296297 // ----------------------------------------------------------------------------
@@ -399,9 +400,9 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
399400 * If no such database is specified, create it in the current database.
400401 */
401402 def createFunction (funcDefinition : CatalogFunction ): Unit = {
402- val db = funcDefinition.name .database.getOrElse(currentDb)
403+ val db = funcDefinition.identifier .database.getOrElse(currentDb)
403404 val newFuncDefinition = funcDefinition.copy(
404- name = FunctionIdentifier (funcDefinition.name .funcName, Some (db)))
405+ identifier = FunctionIdentifier (funcDefinition.identifier .funcName, Some (db)))
405406 externalCatalog.createFunction(db, newFuncDefinition)
406407 }
407408
@@ -424,9 +425,9 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
424425 * this becomes a no-op.
425426 */
426427 def alterFunction (funcDefinition : CatalogFunction ): Unit = {
427- val db = funcDefinition.name .database.getOrElse(currentDb)
428+ val db = funcDefinition.identifier .database.getOrElse(currentDb)
428429 val newFuncDefinition = funcDefinition.copy(
429- name = FunctionIdentifier (funcDefinition.name .funcName, Some (db)))
430+ identifier = FunctionIdentifier (funcDefinition.identifier .funcName, Some (db)))
430431 externalCatalog.alterFunction(db, newFuncDefinition)
431432 }
432433
@@ -439,10 +440,10 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
439440 * This assumes no database is specified in `funcDefinition`.
440441 */
441442 def createTempFunction (funcDefinition : CatalogFunction , ignoreIfExists : Boolean ): Unit = {
442- require(funcDefinition.name .database.isEmpty,
443+ require(funcDefinition.identifier .database.isEmpty,
443444 " attempted to create a temporary function while specifying a database" )
444- val name = funcDefinition.name .funcName
445- if (tempFunctions.containsKey (name) && ! ignoreIfExists) {
445+ val name = funcDefinition.identifier .funcName
446+ if (tempFunctions.contains (name) && ! ignoreIfExists) {
446447 throw new AnalysisException (s " Temporary function ' $name' already exists. " )
447448 }
448449 tempFunctions.put(name, funcDefinition)
@@ -455,7 +456,7 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
455456 // Hive has DROP FUNCTION and DROP TEMPORARY FUNCTION. We may want to consolidate
456457 // dropFunction and dropTempFunction.
457458 def dropTempFunction (name : String , ignoreIfNotExists : Boolean ): Unit = {
458- if (! tempFunctions.containsKey (name) && ! ignoreIfNotExists) {
459+ if (! tempFunctions.contains (name) && ! ignoreIfNotExists) {
459460 throw new AnalysisException (
460461 s " Temporary function ' $name' cannot be dropped because it does not exist! " )
461462 }
@@ -476,11 +477,12 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
476477 throw new AnalysisException (" rename does not support moving functions across databases" )
477478 }
478479 val db = oldName.database.getOrElse(currentDb)
479- if (oldName.database.isDefined || ! tempFunctions.containsKey (oldName.funcName)) {
480+ if (oldName.database.isDefined || ! tempFunctions.contains (oldName.funcName)) {
480481 externalCatalog.renameFunction(db, oldName.funcName, newName.funcName)
481482 } else {
482- val func = tempFunctions.remove(oldName.funcName)
483- val newFunc = func.copy(name = func.name.copy(funcName = newName.funcName))
483+ val func = tempFunctions(oldName.funcName)
484+ val newFunc = func.copy(identifier = func.identifier.copy(funcName = newName.funcName))
485+ tempFunctions.remove(oldName.funcName)
484486 tempFunctions.put(newName.funcName, newFunc)
485487 }
486488 }
@@ -494,10 +496,10 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
494496 */
495497 def getFunction (name : FunctionIdentifier ): CatalogFunction = {
496498 val db = name.database.getOrElse(currentDb)
497- if (name.database.isDefined || ! tempFunctions.containsKey (name.funcName)) {
499+ if (name.database.isDefined || ! tempFunctions.contains (name.funcName)) {
498500 externalCatalog.getFunction(db, name.funcName)
499501 } else {
500- tempFunctions.get (name.funcName)
502+ tempFunctions(name.funcName)
501503 }
502504 }
503505
@@ -510,7 +512,7 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
510512 val dbFunctions =
511513 externalCatalog.listFunctions(db, pattern).map { f => FunctionIdentifier (f, Some (db)) }
512514 val regex = pattern.replaceAll(" \\ *" , " .*" ).r
513- val _tempFunctions = tempFunctions.keys().asScala
515+ val _tempFunctions = tempFunctions.keys.toSeq
514516 .filter { f => regex.pattern.matcher(f).matches() }
515517 .map { f => FunctionIdentifier (f) }
516518 dbFunctions ++ _tempFunctions
@@ -520,7 +522,7 @@ class SessionCatalog(externalCatalog: ExternalCatalog, conf: CatalystConf) {
520522 * Return a temporary function. For testing only.
521523 */
522524 private [catalog] def getTempFunction (name : String ): Option [CatalogFunction ] = {
523- Option ( tempFunctions.get(name) )
525+ tempFunctions.get(name)
524526 }
525527
526528}
0 commit comments