@@ -42,11 +42,9 @@ trait Catalog {
4242
4343 val conf : CatalystConf
4444
45- def tableExists (tableIdentifier : Seq [ String ] ): Boolean
45+ def tableExists (tableIdent : TableIdentifier ): Boolean
4646
47- def lookupRelation (
48- tableIdentifier : Seq [String ],
49- alias : Option [String ] = None ): LogicalPlan
47+ def lookupRelation (tableIdent : TableIdentifier , alias : Option [String ] = None ): LogicalPlan
5048
5149 /**
5250 * Returns tuples of (tableName, isTemporary) for all tables in the given database.
@@ -56,101 +54,67 @@ trait Catalog {
5654
5755 def refreshTable (tableIdent : TableIdentifier ): Unit
5856
59- // TODO: Refactor it in the work of SPARK-10104
60- def registerTable (tableIdentifier : Seq [String ], plan : LogicalPlan ): Unit
57+ def registerTable (tableIdent : TableIdentifier , plan : LogicalPlan ): Unit
6158
62- // TODO: Refactor it in the work of SPARK-10104
63- def unregisterTable (tableIdentifier : Seq [String ]): Unit
59+ def unregisterTable (tableIdent : TableIdentifier ): Unit
6460
6561 def unregisterAllTables (): Unit
6662
67- // TODO: Refactor it in the work of SPARK-10104
68- protected def processTableIdentifier (tableIdentifier : Seq [String ]): Seq [String ] = {
69- if (conf.caseSensitiveAnalysis) {
70- tableIdentifier
71- } else {
72- tableIdentifier.map(_.toLowerCase)
73- }
74- }
75-
76- // TODO: Refactor it in the work of SPARK-10104
77- protected def getDbTableName (tableIdent : Seq [String ]): String = {
78- val size = tableIdent.size
79- if (size <= 2 ) {
80- tableIdent.mkString(" ." )
81- } else {
82- tableIdent.slice(size - 2 , size).mkString(" ." )
83- }
84- }
85-
86- // TODO: Refactor it in the work of SPARK-10104
87- protected def getDBTable (tableIdent : Seq [String ]) : (Option [String ], String ) = {
88- (tableIdent.lift(tableIdent.size - 2 ), tableIdent.last)
89- }
90-
9163 /**
92- * It is not allowed to specifiy database name for tables stored in [[SimpleCatalog ]].
93- * We use this method to check it.
64+ * Get the table name of TableIdentifier for temporary tables.
9465 */
95- protected def checkTableIdentifier (tableIdentifier : Seq [String ]): Unit = {
96- if (tableIdentifier.length > 1 ) {
66+ protected def getTableName (tableIdent : TableIdentifier ): String = {
67+ // It is not allowed to specify database name for temporary tables.
68+ // We check it here and throw exception if database is defined.
69+ if (tableIdent.database.isDefined) {
9770 throw new AnalysisException (" Specifying database name or other qualifiers are not allowed " +
9871 " for temporary tables. If the table name has dots (.) in it, please quote the " +
9972 " table name with backticks (`)." )
10073 }
74+ if (conf.caseSensitiveAnalysis) {
75+ tableIdent.table
76+ } else {
77+ tableIdent.table.toLowerCase
78+ }
10179 }
10280}
10381
10482class SimpleCatalog (val conf : CatalystConf ) extends Catalog {
105- val tables = new ConcurrentHashMap [String , LogicalPlan ]
106-
107- override def registerTable (
108- tableIdentifier : Seq [String ],
109- plan : LogicalPlan ): Unit = {
110- checkTableIdentifier(tableIdentifier)
111- val tableIdent = processTableIdentifier(tableIdentifier)
112- tables.put(getDbTableName(tableIdent), plan)
83+ private [this ] val tables = new ConcurrentHashMap [String , LogicalPlan ]
84+
85+ override def registerTable (tableIdent : TableIdentifier , plan : LogicalPlan ): Unit = {
86+ tables.put(getTableName(tableIdent), plan)
11387 }
11488
115- override def unregisterTable (tableIdentifier : Seq [String ]): Unit = {
116- checkTableIdentifier(tableIdentifier)
117- val tableIdent = processTableIdentifier(tableIdentifier)
118- tables.remove(getDbTableName(tableIdent))
89+ override def unregisterTable (tableIdent : TableIdentifier ): Unit = {
90+ tables.remove(getTableName(tableIdent))
11991 }
12092
12193 override def unregisterAllTables (): Unit = {
12294 tables.clear()
12395 }
12496
125- override def tableExists (tableIdentifier : Seq [String ]): Boolean = {
126- checkTableIdentifier(tableIdentifier)
127- val tableIdent = processTableIdentifier(tableIdentifier)
128- tables.containsKey(getDbTableName(tableIdent))
97+ override def tableExists (tableIdent : TableIdentifier ): Boolean = {
98+ tables.containsKey(getTableName(tableIdent))
12999 }
130100
131101 override def lookupRelation (
132- tableIdentifier : Seq [ String ] ,
102+ tableIdent : TableIdentifier ,
133103 alias : Option [String ] = None ): LogicalPlan = {
134- checkTableIdentifier(tableIdentifier)
135- val tableIdent = processTableIdentifier(tableIdentifier)
136- val tableFullName = getDbTableName(tableIdent)
137- val table = tables.get(tableFullName)
104+ val tableName = getTableName(tableIdent)
105+ val table = tables.get(tableName)
138106 if (table == null ) {
139- sys.error( s " Table Not Found: $tableFullName " )
107+ throw new NoSuchTableException
140108 }
141- val tableWithQualifiers = Subquery (tableIdent.last , table)
109+ val tableWithQualifiers = Subquery (tableName , table)
142110
143111 // If an alias was specified by the lookup, wrap the plan in a subquery so that attributes are
144112 // properly qualified with this alias.
145113 alias.map(a => Subquery (a, tableWithQualifiers)).getOrElse(tableWithQualifiers)
146114 }
147115
148116 override def getTables (databaseName : Option [String ]): Seq [(String , Boolean )] = {
149- val result = ArrayBuffer .empty[(String , Boolean )]
150- for (name <- tables.keySet().asScala) {
151- result += ((name, true ))
152- }
153- result
117+ tables.keySet().asScala.map(_ -> true ).toSeq
154118 }
155119
156120 override def refreshTable (tableIdent : TableIdentifier ): Unit = {
@@ -165,68 +129,50 @@ class SimpleCatalog(val conf: CatalystConf) extends Catalog {
165129 * lost when the JVM exits.
166130 */
167131trait OverrideCatalog extends Catalog {
132+ private [this ] val overrides = new ConcurrentHashMap [String , LogicalPlan ]
168133
169- // TODO: This doesn't work when the database changes...
170- val overrides = new mutable.HashMap [(Option [String ], String ), LogicalPlan ]()
171-
172- abstract override def tableExists (tableIdentifier : Seq [String ]): Boolean = {
173- val tableIdent = processTableIdentifier(tableIdentifier)
174- // A temporary tables only has a single part in the tableIdentifier.
175- val overriddenTable = if (tableIdentifier.length > 1 ) {
176- None : Option [LogicalPlan ]
134+ private def getOverriddenTable (tableIdent : TableIdentifier ): Option [LogicalPlan ] = {
135+ if (tableIdent.database.isDefined) {
136+ None
177137 } else {
178- overrides.get(getDBTable (tableIdent))
138+ Option ( overrides.get(getTableName (tableIdent) ))
179139 }
180- overriddenTable match {
140+ }
141+
142+ abstract override def tableExists (tableIdent : TableIdentifier ): Boolean = {
143+ getOverriddenTable(tableIdent) match {
181144 case Some (_) => true
182- case None => super .tableExists(tableIdentifier )
145+ case None => super .tableExists(tableIdent )
183146 }
184147 }
185148
186149 abstract override def lookupRelation (
187- tableIdentifier : Seq [ String ] ,
150+ tableIdent : TableIdentifier ,
188151 alias : Option [String ] = None ): LogicalPlan = {
189- val tableIdent = processTableIdentifier(tableIdentifier)
190- // A temporary tables only has a single part in the tableIdentifier.
191- val overriddenTable = if (tableIdentifier.length > 1 ) {
192- None : Option [LogicalPlan ]
193- } else {
194- overrides.get(getDBTable(tableIdent))
195- }
196- val tableWithQualifers = overriddenTable.map(r => Subquery (tableIdent.last, r))
152+ getOverriddenTable(tableIdent) match {
153+ case Some (table) =>
154+ val tableName = getTableName(tableIdent)
155+ val tableWithQualifiers = Subquery (tableName, table)
197156
198- // If an alias was specified by the lookup, wrap the plan in a subquery so that attributes are
199- // properly qualified with this alias.
200- val withAlias =
201- tableWithQualifers.map(r => alias.map(a => Subquery (a, r)).getOrElse(r))
157+ // If an alias was specified by the lookup, wrap the plan in a sub-query so that attributes
158+ // are properly qualified with this alias.
159+ alias.map(a => Subquery (a, tableWithQualifiers)).getOrElse(tableWithQualifiers)
202160
203- withAlias.getOrElse(super .lookupRelation(tableIdentifier, alias))
161+ case None => super .lookupRelation(tableIdent, alias)
162+ }
204163 }
205164
206165 abstract override def getTables (databaseName : Option [String ]): Seq [(String , Boolean )] = {
207- // We always return all temporary tables.
208- val temporaryTables = overrides.map {
209- case ((_, tableName), _) => (tableName, true )
210- }.toSeq
211-
212- temporaryTables ++ super .getTables(databaseName)
166+ overrides.keySet().asScala.map(_ -> true ).toSeq ++ super .getTables(databaseName)
213167 }
214168
215- override def registerTable (
216- tableIdentifier : Seq [String ],
217- plan : LogicalPlan ): Unit = {
218- checkTableIdentifier(tableIdentifier)
219- val tableIdent = processTableIdentifier(tableIdentifier)
220- overrides.put(getDBTable(tableIdent), plan)
169+ override def registerTable (tableIdent : TableIdentifier , plan : LogicalPlan ): Unit = {
170+ overrides.put(getTableName(tableIdent), plan)
221171 }
222172
223- override def unregisterTable (tableIdentifier : Seq [String ]): Unit = {
224- // A temporary tables only has a single part in the tableIdentifier.
225- // If tableIdentifier has more than one parts, it is not a temporary table
226- // and we do not need to do anything at here.
227- if (tableIdentifier.length == 1 ) {
228- val tableIdent = processTableIdentifier(tableIdentifier)
229- overrides.remove(getDBTable(tableIdent))
173+ override def unregisterTable (tableIdent : TableIdentifier ): Unit = {
174+ if (tableIdent.database.isEmpty) {
175+ overrides.remove(getTableName(tableIdent))
230176 }
231177 }
232178
@@ -243,12 +189,12 @@ object EmptyCatalog extends Catalog {
243189
244190 override val conf : CatalystConf = EmptyConf
245191
246- override def tableExists (tableIdentifier : Seq [ String ] ): Boolean = {
192+ override def tableExists (tableIdent : TableIdentifier ): Boolean = {
247193 throw new UnsupportedOperationException
248194 }
249195
250196 override def lookupRelation (
251- tableIdentifier : Seq [ String ] ,
197+ tableIdent : TableIdentifier ,
252198 alias : Option [String ] = None ): LogicalPlan = {
253199 throw new UnsupportedOperationException
254200 }
@@ -257,15 +203,17 @@ object EmptyCatalog extends Catalog {
257203 throw new UnsupportedOperationException
258204 }
259205
260- override def registerTable (tableIdentifier : Seq [ String ] , plan : LogicalPlan ): Unit = {
206+ override def registerTable (tableIdent : TableIdentifier , plan : LogicalPlan ): Unit = {
261207 throw new UnsupportedOperationException
262208 }
263209
264- override def unregisterTable (tableIdentifier : Seq [ String ] ): Unit = {
210+ override def unregisterTable (tableIdent : TableIdentifier ): Unit = {
265211 throw new UnsupportedOperationException
266212 }
267213
268- override def unregisterAllTables (): Unit = {}
214+ override def unregisterAllTables (): Unit = {
215+ throw new UnsupportedOperationException
216+ }
269217
270218 override def refreshTable (tableIdent : TableIdentifier ): Unit = {
271219 throw new UnsupportedOperationException
0 commit comments