Skip to content

Commit 93a0510

Browse files
gatorsmileyhuai
authored andcommitted
[SPARK-12138][SQL] Escape \u in the generated comments of codegen
When \u appears in a comment block (i.e. in /**/), code gen will break. So, in Expression and CodegenFallback, we escape \u to \\u. yhuai Please review it. I did reproduce it and it works after the fix. Thanks! Author: gatorsmile <gatorsmile@gmail.com> Closes apache#10155 from gatorsmile/escapeU. (cherry picked from commit 49efd03) Signed-off-by: Yin Huai <yhuai@databricks.com>
1 parent 8bbb3cd commit 93a0510

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Expression.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ abstract class Expression extends TreeNode[Expression] {
180180
* Returns the string representation of this expression that is safe to be put in
181181
* code comments of generated code.
182182
*/
183-
protected def toCommentSafeString: String = this.toString.replace("*/", "\\*\\/")
183+
protected def toCommentSafeString: String = this.toString
184+
.replace("*/", "\\*\\/")
185+
.replace("\\u", "\\\\u")
184186
}
185187

186188

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CodeGenerationSuite.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,13 @@ class CodeGenerationSuite extends SparkFunSuite with ExpressionEvalHelper {
143143
true,
144144
InternalRow(UTF8String.fromString("*/")))
145145
}
146+
147+
test("\\u in the data") {
148+
// When \ u appears in a comment block (i.e. in /**/), code gen will break.
149+
// So, in Expression and CodegenFallback, we escape \ u to \\u.
150+
checkEvaluation(
151+
EqualTo(BoundReference(0, StringType, false), Literal.create("\\u", StringType)),
152+
true,
153+
InternalRow(UTF8String.fromString("\\u")))
154+
}
146155
}

0 commit comments

Comments
 (0)