Skip to content

Commit cd8322b

Browse files
committed
[SPARK-9154][SQL] codegen string format
1 parent 086caba commit cd8322b

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,11 @@ case class StringFormat(children: Expression*) extends Expression with CodegenFa
503503

504504
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
505505
val pattern = children.head.gen(ctx)
506-
val argListGen = children.tail.map(x => (x.dataType, x.gen(ctx)))
507-
val argListCode = argListGen.map(_._2.code + "\n")
508-
val argListString = argListGen.foldLeft("")((s, x) => s + s", ${x._2.primitive}" + (if (!ctx.isPrimitiveType(x._1)) ".toString()" else ""))
506+
507+
val argListGen = children.tail.map(_.gen(ctx))
508+
val argListCode = argListGen.map(_.code + "\n")
509+
val argListString = argListGen.foldLeft("")((s, v) => s + s", ${v.primitive}")
510+
509511
val form = ctx.freshName("formatter")
510512
val formatter = classOf[java.util.Formatter].getName
511513
val sb = ctx.freshName("sb")

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,14 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
353353
test("FORMAT") {
354354
val f = 'f.string.at(0)
355355
val d1 = 'd.int.at(1)
356-
val s1 = 's.int.at(2)
356+
val s1 = 's.string.at(2)
357357

358358
val row1 = create_row("aa%d%s", 12, "cc")
359359
val row2 = create_row(null, 12, "cc")
360360
checkEvaluation(StringFormat(Literal("aa%d%s"), Literal(123), Literal("a")), "aa123a", row1)
361361
checkEvaluation(StringFormat(Literal("aa")), "aa", create_row(null))
362362
checkEvaluation(StringFormat(Literal("aa%d%s"), Literal(123), Literal("a")), "aa123a", row1)
363363

364-
println(StringFormat(f, d1, s1).eval(row1))
365364
checkEvaluation(StringFormat(f, d1, s1), "aa12cc", row1)
366365
checkEvaluation(StringFormat(f, d1, s1), null, row2)
367366
}

0 commit comments

Comments
 (0)