Skip to content

Commit b9c046f

Browse files
committed
[SPARK-8004][SQL] Quote identifier in JDBC data source.
This is a follow-up patch to #6577 to replace columnEnclosing to quoteIdentifier. I also did some minor cleanup to the JdbcDialect file. Author: Reynold Xin <[email protected]> Closes #6689 from rxin/jdbc-quote and squashes the following commits: bad365f [Reynold Xin] Fixed test compilation... e39e14e [Reynold Xin] Fixed compilation. db9a8e0 [Reynold Xin] [SPARK-8004][SQL] Quote identifier in JDBC data source. (cherry picked from commit d6d601a) Signed-off-by: Reynold Xin <[email protected]>
1 parent ff26767 commit b9c046f

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

sql/core/src/main/scala/org/apache/spark/sql/jdbc/JDBCRDD.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,13 @@ private[sql] object JDBCRDD extends Logging {
212212
filters: Array[Filter],
213213
parts: Array[Partition]): RDD[Row] = {
214214
val dialect = JdbcDialects.get(url)
215-
val enclosedColumns = requiredColumns.map(dialect.columnEnclosing(_))
215+
val quotedColumns = requiredColumns.map(colName => dialect.quoteIdentifier(colName))
216216
new JDBCRDD(
217217
sc,
218218
getConnector(driver, url, properties),
219219
pruneSchema(schema, requiredColumns),
220220
fqTable,
221-
enclosedColumns,
221+
quotedColumns,
222222
filters,
223223
parts,
224224
properties)

sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717

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

20+
import java.sql.Types
21+
2022
import org.apache.spark.sql.types._
2123
import org.apache.spark.annotation.DeveloperApi
2224

23-
import java.sql.Types
24-
2525
/**
2626
* :: DeveloperApi ::
2727
* A database type definition coupled with the jdbc type needed to send null
@@ -82,11 +82,10 @@ abstract class JdbcDialect {
8282
def getJDBCType(dt: DataType): Option[JdbcType] = None
8383

8484
/**
85-
* Enclose column name
86-
* @param colName The coulmn name
87-
* @return Enclosed column name
85+
* Quotes the identifier. This is used to put quotes around the identifier in case the column
86+
* name is a reserved keyword, or in case it contains characters that require quotes (e.g. space).
8887
*/
89-
def columnEnclosing(colName: String): String = {
88+
def quoteIdentifier(colName: String): String = {
9089
s""""$colName""""
9190
}
9291
}
@@ -150,18 +149,19 @@ object JdbcDialects {
150149
@DeveloperApi
151150
class AggregatedDialect(dialects: List[JdbcDialect]) extends JdbcDialect {
152151

153-
require(!dialects.isEmpty)
152+
require(dialects.nonEmpty)
154153

155-
def canHandle(url : String): Boolean =
154+
override def canHandle(url : String): Boolean =
156155
dialects.map(_.canHandle(url)).reduce(_ && _)
157156

158157
override def getCatalystType(
159-
sqlType: Int, typeName: String, size: Int, md: MetadataBuilder): Option[DataType] =
160-
dialects.map(_.getCatalystType(sqlType, typeName, size, md)).flatten.headOption
161-
162-
override def getJDBCType(dt: DataType): Option[JdbcType] =
163-
dialects.map(_.getJDBCType(dt)).flatten.headOption
158+
sqlType: Int, typeName: String, size: Int, md: MetadataBuilder): Option[DataType] = {
159+
dialects.flatMap(_.getCatalystType(sqlType, typeName, size, md)).headOption
160+
}
164161

162+
override def getJDBCType(dt: DataType): Option[JdbcType] = {
163+
dialects.flatMap(_.getJDBCType(dt)).headOption
164+
}
165165
}
166166

167167
/**
@@ -170,7 +170,7 @@ class AggregatedDialect(dialects: List[JdbcDialect]) extends JdbcDialect {
170170
*/
171171
@DeveloperApi
172172
case object NoopDialect extends JdbcDialect {
173-
def canHandle(url : String): Boolean = true
173+
override def canHandle(url : String): Boolean = true
174174
}
175175

176176
/**
@@ -179,7 +179,7 @@ case object NoopDialect extends JdbcDialect {
179179
*/
180180
@DeveloperApi
181181
case object PostgresDialect extends JdbcDialect {
182-
def canHandle(url: String): Boolean = url.startsWith("jdbc:postgresql")
182+
override def canHandle(url: String): Boolean = url.startsWith("jdbc:postgresql")
183183
override def getCatalystType(
184184
sqlType: Int, typeName: String, size: Int, md: MetadataBuilder): Option[DataType] = {
185185
if (sqlType == Types.BIT && typeName.equals("bit") && size != 1) {
@@ -205,7 +205,7 @@ case object PostgresDialect extends JdbcDialect {
205205
*/
206206
@DeveloperApi
207207
case object MySQLDialect extends JdbcDialect {
208-
def canHandle(url : String): Boolean = url.startsWith("jdbc:mysql")
208+
override def canHandle(url : String): Boolean = url.startsWith("jdbc:mysql")
209209
override def getCatalystType(
210210
sqlType: Int, typeName: String, size: Int, md: MetadataBuilder): Option[DataType] = {
211211
if (sqlType == Types.VARBINARY && typeName.equals("BIT") && size != 1) {
@@ -218,7 +218,7 @@ case object MySQLDialect extends JdbcDialect {
218218
} else None
219219
}
220220

221-
override def columnEnclosing(colName: String): String = {
221+
override def quoteIdentifier(colName: String): String = {
222222
s"`$colName`"
223223
}
224224
}

sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,13 @@ class JDBCSuite extends SparkFunSuite with BeforeAndAfter {
411411
assert(JdbcDialects.get("test.invalid") == NoopDialect)
412412
}
413413

414-
test("Enclosing column names by jdbc dialect") {
414+
test("quote column names by jdbc dialect") {
415415
val MySQL = JdbcDialects.get("jdbc:mysql://127.0.0.1/db")
416416
val Postgres = JdbcDialects.get("jdbc:postgresql://127.0.0.1/db")
417417

418418
val columns = Seq("abc", "key")
419-
val MySQLColumns = columns.map(MySQL.columnEnclosing(_))
420-
val PostgresColumns = columns.map(Postgres.columnEnclosing(_))
419+
val MySQLColumns = columns.map(MySQL.quoteIdentifier(_))
420+
val PostgresColumns = columns.map(Postgres.quoteIdentifier(_))
421421
assert(MySQLColumns === Seq("`abc`", "`key`"))
422422
assert(PostgresColumns === Seq(""""abc"""", """"key""""))
423423
}

0 commit comments

Comments
 (0)