Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Revert "Fix"
This reverts commit 9335b7c.
  • Loading branch information
maropu committed Jun 14, 2018
commit f9992b58faf59f2947ad2ee21627787c8b581b3e
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,13 @@ abstract class JdbcDialect extends Serializable {
*/
def getJDBCType(dt: DataType): Option[JdbcType] = None

protected def doQuoteIdentifier(quoteMark: Char, colName: String): String = {
val q = String.valueOf(quoteMark)
if (colName.startsWith(q) && colName.endsWith(q)) {
colName
} else {
s"$q$colName$q"
}
}

/**
* Quotes the identifier. This is used to put quotes around the identifier in case the column
* name is a reserved keyword, or in case it contains characters that require quotes (e.g. space).
*/
def quoteIdentifier(colName: String): String = doQuoteIdentifier('"', colName)
def quoteIdentifier(colName: String): String = {
s""""$colName""""
}

/**
* Get the SQL query that should be used to find if the given table exists. Dialects can
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ private case object MySQLDialect extends JdbcDialect {
} else None
}

override def quoteIdentifier(colName: String): String = doQuoteIdentifier('`', colName)
override def quoteIdentifier(colName: String): String = {
s"`$colName`"
}

override def getTableExistsQuery(table: String): String = {
s"SELECT 1 FROM $table LIMIT 1"
Expand Down
49 changes: 21 additions & 28 deletions sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1208,34 +1208,27 @@ class JDBCSuite extends SparkFunSuite
}

test("SPARK-24327 quotes a partition column name if `quotePartitionColumnNames` is true") {
// A `isAlreadyQuoted` case is to check if `JDBCDialect` wrongly does not quote
// a partition column name again.
Seq(true, false).foreach { isAlreadyQuoted =>
Seq(true, false).foreach { quotePartitionColumnName =>
val partColName = "THEID"
val quotedPrtColName = testH2Dialect.quoteIdentifier(partColName)
val df = spark.read.format("jdbc")
.option("url", urlWithUserAndPass)
.option("dbtable", "TEST.PEOPLE")
.option("partitionColumn", if (isAlreadyQuoted) quotedPrtColName else partColName)
.option("lowerBound", 1)
.option("upperBound", 4)
.option("numPartitions", 3)
.option("quotePartitionColumnName", quotePartitionColumnName)
.load()
val expectedColName = if (quotePartitionColumnName || isAlreadyQuoted) {
quotedPrtColName
} else {
partColName
}
df.logicalPlan match {
case LogicalRelation(JDBCRelation(parts, _), _, _, _) =>
val whereClauses = parts.map(_.asInstanceOf[JDBCPartition].whereClause).toSet
assert(whereClauses === Set(
s"$expectedColName < 2 or $expectedColName is null",
s"$expectedColName >= 2 AND $expectedColName < 3",
s"$expectedColName >= 3"))
}
Seq(true, false).foreach { quotePartitionColumnName =>
val df = spark.read.format("jdbc")
.option("url", urlWithUserAndPass)
.option("dbtable", "TEST.PEOPLE")
.option("partitionColumn", "THEID")
.option("lowerBound", 1)
.option("upperBound", 4)
.option("numPartitions", 3)
.option("quotePartitionColumnName", quotePartitionColumnName)
.load()
val colName = if (quotePartitionColumnName) {
testH2Dialect.quoteIdentifier("THEID")
} else {
"THEID"
}
df.logicalPlan match {
case LogicalRelation(JDBCRelation(parts, _), _, _, _) =>
val whereClauses = parts.map(_.asInstanceOf[JDBCPartition].whereClause).toSet
assert(whereClauses === Set(
s"$colName < 2 or $colName is null", s"$colName >= 2 AND $colName < 3",
s"$colName >= 3"))
}
}
}
Expand Down