Skip to content

Commit caa4013

Browse files
author
Andrew Or
committed
Clean up duplicate code in Table/FunctionIdentifier
1 parent 90ccdbb commit caa4013

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/TableIdentifier.scala renamed to sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/identifiers.scala

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,48 @@
1717

1818
package org.apache.spark.sql.catalyst
1919

20+
2021
/**
21-
* Identifies a table in a database.
22-
* If `database` is not defined, the current database is used.
22+
* An identifier that optionally specifies a database.
23+
*
24+
* Format (unquoted): "name" or "db.name"
25+
* Format (quoted): "`name`" or "`db`.`name`"
2326
*/
24-
private[sql] case class TableIdentifier(table: String, database: Option[String]) {
25-
def this(table: String) = this(table, None)
26-
27+
private[sql] abstract class IdentifierWithDatabase(name: String) {
28+
def database: Option[String]
29+
def quotedString: String = database.map(db => s"`$db`.`$name`").getOrElse(s"`$name`")
30+
def unquotedString: String = database.map(db => s"$db.$name").getOrElse(name)
2731
override def toString: String = quotedString
32+
}
33+
2834

29-
def quotedString: String = database.map(db => s"`$db`.`$table`").getOrElse(s"`$table`")
35+
/**
36+
* Identifies a table in a database.
37+
* If `database` is not defined, the current database is used.
38+
*/
39+
private[sql] case class TableIdentifier(
40+
table: String,
41+
database: Option[String])
42+
extends IdentifierWithDatabase(table) {
3043

31-
def unquotedString: String = database.map(db => s"$db.$table").getOrElse(table)
44+
def this(name: String) = this(name, None)
3245
}
3346

3447
private[sql] object TableIdentifier {
35-
def apply(tableName: String): TableIdentifier = new TableIdentifier(tableName)
48+
def apply(tableName: String): TableIdentifier = TableIdentifier(tableName)
3649
}
3750

51+
3852
/**
3953
* Identifies a function in a database.
4054
* If `database` is not defined, the current database is used.
4155
*/
42-
// TODO: reuse some code with TableIdentifier.
43-
private[sql] case class FunctionIdentifier(funcName: String, database: Option[String]) {
44-
def this(name: String) = this(name, None)
56+
private[sql] case class FunctionIdentifier(
57+
funcName: String,
58+
database: Option[String])
59+
extends IdentifierWithDatabase(funcName) {
4560

46-
override def toString: String = quotedString
47-
48-
def quotedString: String = database.map(db => s"`$db`.`$funcName`").getOrElse(s"`$funcName`")
49-
50-
def unquotedString: String = database.map(db => s"$db.$funcName").getOrElse(funcName)
61+
def this(name: String) = this(name, None)
5162
}
5263

5364
private[sql] object FunctionIdentifier {

0 commit comments

Comments
 (0)