Skip to content

Commit 88935c5

Browse files
author
bomeng
committed
fix issue of column name contains space
1 parent 953ff89 commit 88935c5

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JdbcUtils.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ object JdbcUtils extends Logging {
9797
* Returns a PreparedStatement that inserts a row into table via conn.
9898
*/
9999
def insertStatement(conn: Connection, table: String, rddSchema: StructType): PreparedStatement = {
100-
val columns = rddSchema.fields.map(_.name).mkString(",")
100+
val columns = rddSchema.fields.map(f => escapeColumnName(f.name)).mkString(",")
101101
val placeholders = rddSchema.fields.map(_ => "?").mkString(",")
102102
val sql = s"INSERT INTO $table ($columns) VALUES ($placeholders)"
103103
conn.prepareStatement(sql)
@@ -245,14 +245,23 @@ object JdbcUtils extends Logging {
245245
Array[Byte]().iterator
246246
}
247247

248+
/**
249+
* The utility to add backtick if the column name has space
250+
* @param columnName the input column name
251+
* @return the escaped column name, add backtick if name contains space
252+
*/
253+
private def escapeColumnName(columnName: String): String = {
254+
if (columnName.contains(" ")) s"`$columnName`" else columnName
255+
}
256+
248257
/**
249258
* Compute the schema string for this RDD.
250259
*/
251260
def schemaString(df: DataFrame, url: String): String = {
252261
val sb = new StringBuilder()
253262
val dialect = JdbcDialects.get(url)
254263
df.schema.fields foreach { field => {
255-
val name = field.name
264+
val name = escapeColumnName(field.name)
256265
val typ: String = getJdbcType(field.dataType, dialect).databaseTypeDefinition
257266
val nullable = if (field.nullable) "" else "NOT NULL"
258267
sb.append(s", $name $typ $nullable")

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ class JDBCWriteSuite extends SharedSQLContext with BeforeAndAfter {
5252
conn1.prepareStatement("insert into test.people values ('mary', 2)").executeUpdate()
5353
conn1.prepareStatement("drop table if exists test.people1").executeUpdate()
5454
conn1.prepareStatement(
55-
"create table test.people1 (name TEXT(32) NOT NULL, theid INTEGER NOT NULL)").executeUpdate()
55+
"create table test.people1 (name TEXT(32) NOT NULL, `the id` INTEGER NOT NULL)")
56+
.executeUpdate()
5657
conn1.commit()
5758

5859
sql(
@@ -151,4 +152,10 @@ class JDBCWriteSuite extends SharedSQLContext with BeforeAndAfter {
151152
assert(2 === sqlContext.read.jdbc(url1, "TEST.PEOPLE1", properties).count)
152153
assert(2 === sqlContext.read.jdbc(url1, "TEST.PEOPLE1", properties).collect()(0).length)
153154
}
155+
156+
test("SPARK-14460: Insert into table with column containing space") {
157+
val df = sqlContext.createDataFrame(sparkContext.parallelize(arr2x2), schema2)
158+
df.write.insertInto("PEOPLE1")
159+
assert(2 === sqlContext.read.jdbc(url1, "TEST.PEOPLE1", properties).count)
160+
}
154161
}

0 commit comments

Comments
 (0)