Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into UDAF
Browse files Browse the repository at this point in the history
  • Loading branch information
yhuai committed Jul 22, 2015
2 parents 35b0520 + a4c83cb commit f1d5901
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ object FunctionRegistry {
expression[StringLocate]("locate"),
expression[StringLPad]("lpad"),
expression[StringTrimLeft]("ltrim"),
expression[StringFormat]("printf"),
expression[FormatString]("format_string"),
expression[FormatString]("printf"),
expression[StringRPad]("rpad"),
expression[StringRepeat]("repeat"),
expression[StringReverse]("reverse"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,32 +526,69 @@ case class StringRPad(str: Expression, len: Expression, pad: Expression)
/**
* Returns the input formatted according do printf-style format strings
*/
case class StringFormat(children: Expression*) extends Expression with CodegenFallback {
case class FormatString(children: Expression*) extends Expression with ImplicitCastInputTypes {

require(children.nonEmpty, "printf() should take at least 1 argument")
require(children.nonEmpty, "format_string() should take at least 1 argument")

override def foldable: Boolean = children.forall(_.foldable)
override def nullable: Boolean = children(0).nullable
override def dataType: DataType = StringType
private def format: Expression = children(0)
private def args: Seq[Expression] = children.tail

override def inputTypes: Seq[AbstractDataType] =
StringType :: List.fill(children.size - 1)(AnyDataType)

override def eval(input: InternalRow): Any = {
val pattern = format.eval(input)
val pattern = children(0).eval(input)
if (pattern == null) {
null
} else {
val sb = new StringBuffer()
val formatter = new java.util.Formatter(sb, Locale.US)

val arglist = args.map(_.eval(input).asInstanceOf[AnyRef])
val arglist = children.tail.map(_.eval(input).asInstanceOf[AnyRef])
formatter.format(pattern.asInstanceOf[UTF8String].toString, arglist: _*)

UTF8String.fromString(sb.toString)
}
}

override def prettyName: String = "printf"
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
val pattern = children.head.gen(ctx)

val argListGen = children.tail.map(x => (x.dataType, x.gen(ctx)))
val argListCode = argListGen.map(_._2.code + "\n")

val argListString = argListGen.foldLeft("")((s, v) => {
val nullSafeString =
if (ctx.boxedType(v._1) != ctx.javaType(v._1)) {
// Java primitives get boxed in order to allow null values.
s"(${v._2.isNull}) ? (${ctx.boxedType(v._1)}) null : " +
s"new ${ctx.boxedType(v._1)}(${v._2.primitive})"
} else {
s"(${v._2.isNull}) ? null : ${v._2.primitive}"
}
s + "," + nullSafeString
})

val form = ctx.freshName("formatter")
val formatter = classOf[java.util.Formatter].getName
val sb = ctx.freshName("sb")
val stringBuffer = classOf[StringBuffer].getName
s"""
${pattern.code}
boolean ${ev.isNull} = ${pattern.isNull};
${ctx.javaType(dataType)} ${ev.primitive} = ${ctx.defaultValue(dataType)};
if (!${ev.isNull}) {
${argListCode.mkString}
$stringBuffer $sb = new $stringBuffer();
$formatter $form = new $formatter($sb, ${classOf[Locale].getName}.US);
$form.format(${pattern.primitive}.toString() $argListString);
${ev.primitive} = UTF8String.fromString($sb.toString());
}
"""
}

override def prettyName: String = "format_string"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,18 +351,16 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
}

test("FORMAT") {
val f = 'f.string.at(0)
val d1 = 'd.int.at(1)
val s1 = 's.int.at(2)

val row1 = create_row("aa%d%s", 12, "cc")
val row2 = create_row(null, 12, "cc")
checkEvaluation(StringFormat(Literal("aa%d%s"), Literal(123), Literal("a")), "aa123a", row1)
checkEvaluation(StringFormat(Literal("aa")), "aa", create_row(null))
checkEvaluation(StringFormat(Literal("aa%d%s"), Literal(123), Literal("a")), "aa123a", row1)

checkEvaluation(StringFormat(f, d1, s1), "aa12cc", row1)
checkEvaluation(StringFormat(f, d1, s1), null, row2)
checkEvaluation(FormatString(Literal("aa%d%s"), Literal(123), Literal("a")), "aa123a")
checkEvaluation(FormatString(Literal("aa")), "aa", create_row(null))
checkEvaluation(FormatString(Literal("aa%d%s"), Literal(123), Literal("a")), "aa123a")
checkEvaluation(FormatString(Literal("aa%d%s"), 12, "cc"), "aa12cc")

checkEvaluation(FormatString(Literal.create(null, StringType), 12, "cc"), null)
checkEvaluation(
FormatString(Literal("aa%d%s"), Literal.create(null, IntegerType), "cc"), "aanullcc")
checkEvaluation(
FormatString(Literal("aa%d%s"), 12, Literal.create(null, StringType)), "aa12null")
}

test("INSTR") {
Expand Down
7 changes: 3 additions & 4 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1742,15 +1742,14 @@ object functions {
def rtrim(e: Column): Column = StringTrimRight(e.expr)

/**
* Format strings in printf-style.
* NOTE: `format` is the string value of the formatter, not column name.
* Formats the arguments in printf-style and returns the result as a string column.
*
* @group string_funcs
* @since 1.5.0
*/
@scala.annotation.varargs
def formatString(format: String, arguNames: String*): Column = {
StringFormat(lit(format).expr +: arguNames.map(Column(_).expr): _*)
def format_string(format: String, arguments: Column*): Column = {
FormatString((lit(format) +: arguments).map(_.expr): _*)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class StringFunctionsSuite extends QueryTest {
val df = Seq(("aa%d%s", 123, "cc")).toDF("a", "b", "c")

checkAnswer(
df.select(formatString("aa%d%s", "b", "c")),
df.select(format_string("aa%d%s", $"b", $"c")),
Row("aa123cc"))

checkAnswer(
Expand Down

0 comments on commit f1d5901

Please sign in to comment.