Skip to content

Commit a4c83cb

Browse files
committed
[SPARK-9154][SQL] Rename formatString to format_string.
Also make format_string the canonical form, rather than printf. Author: Reynold Xin <[email protected]> Closes apache#7579 from rxin/format_strings and squashes the following commits: 53ee54f [Reynold Xin] Fixed unit tests. 52357e1 [Reynold Xin] Add format_string alias. b40a42a [Reynold Xin] [SPARK-9154][SQL] Rename formatString to format_string.
1 parent d4c7a7a commit a4c83cb

File tree

5 files changed

+18
-42
lines changed

5 files changed

+18
-42
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/FunctionRegistry.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ object FunctionRegistry {
168168
expression[StringLocate]("locate"),
169169
expression[StringLPad]("lpad"),
170170
expression[StringTrimLeft]("ltrim"),
171-
expression[StringFormat]("printf"),
171+
expression[FormatString]("format_string"),
172+
expression[FormatString]("printf"),
172173
expression[StringRPad]("rpad"),
173174
expression[StringRepeat]("repeat"),
174175
expression[StringReverse]("reverse"),

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -526,29 +526,26 @@ case class StringRPad(str: Expression, len: Expression, pad: Expression)
526526
/**
527527
* Returns the input formatted according do printf-style format strings
528528
*/
529-
case class StringFormat(children: Expression*) extends Expression with ImplicitCastInputTypes {
529+
case class FormatString(children: Expression*) extends Expression with ImplicitCastInputTypes {
530530

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

533533
override def foldable: Boolean = children.forall(_.foldable)
534534
override def nullable: Boolean = children(0).nullable
535535
override def dataType: DataType = StringType
536-
private def format: Expression = children(0)
537-
private def args: Seq[Expression] = children.tail
538536

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

542-
543540
override def eval(input: InternalRow): Any = {
544-
val pattern = format.eval(input)
541+
val pattern = children(0).eval(input)
545542
if (pattern == null) {
546543
null
547544
} else {
548545
val sb = new StringBuffer()
549546
val formatter = new java.util.Formatter(sb, Locale.US)
550547

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

554551
UTF8String.fromString(sb.toString)
@@ -591,7 +588,7 @@ case class StringFormat(children: Expression*) extends Expression with ImplicitC
591588
"""
592589
}
593590

594-
override def prettyName: String = "printf"
591+
override def prettyName: String = "format_string"
595592
}
596593

597594
/**

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,16 @@ class StringExpressionsSuite extends SparkFunSuite with ExpressionEvalHelper {
351351
}
352352

353353
test("FORMAT") {
354-
checkEvaluation(StringFormat(Literal("aa%d%s"), Literal(123), Literal("a")), "aa123a")
355-
checkEvaluation(StringFormat(Literal("aa")), "aa", create_row(null))
356-
checkEvaluation(StringFormat(Literal("aa%d%s"), Literal(123), Literal("a")), "aa123a")
357-
checkEvaluation(StringFormat(Literal("aa%d%s"), 12, "cc"), "aa12cc")
354+
checkEvaluation(FormatString(Literal("aa%d%s"), Literal(123), Literal("a")), "aa123a")
355+
checkEvaluation(FormatString(Literal("aa")), "aa", create_row(null))
356+
checkEvaluation(FormatString(Literal("aa%d%s"), Literal(123), Literal("a")), "aa123a")
357+
checkEvaluation(FormatString(Literal("aa%d%s"), 12, "cc"), "aa12cc")
358358

359-
checkEvaluation(StringFormat(Literal.create(null, StringType), 12, "cc"), null)
359+
checkEvaluation(FormatString(Literal.create(null, StringType), 12, "cc"), null)
360360
checkEvaluation(
361-
StringFormat(Literal("aa%d%s"), Literal.create(null, IntegerType), "cc"), "aanullcc")
361+
FormatString(Literal("aa%d%s"), Literal.create(null, IntegerType), "cc"), "aanullcc")
362362
checkEvaluation(
363-
StringFormat(Literal("aa%d%s"), 12, Literal.create(null, StringType)), "aa12null")
363+
FormatString(Literal("aa%d%s"), 12, Literal.create(null, StringType)), "aa12null")
364364
}
365365

366366
test("INSTR") {

sql/core/src/main/scala/org/apache/spark/sql/functions.scala

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,26 +1742,14 @@ object functions {
17421742
def rtrim(e: Column): Column = StringTrimRight(e.expr)
17431743

17441744
/**
1745-
* Format strings in printf-style.
1745+
* Formats the arguments in printf-style and returns the result as a string column.
17461746
*
17471747
* @group string_funcs
17481748
* @since 1.5.0
17491749
*/
17501750
@scala.annotation.varargs
1751-
def formatString(format: Column, arguments: Column*): Column = {
1752-
StringFormat((format +: arguments).map(_.expr): _*)
1753-
}
1754-
1755-
/**
1756-
* Format strings in printf-style.
1757-
* NOTE: `format` is the string value of the formatter, not column name.
1758-
*
1759-
* @group string_funcs
1760-
* @since 1.5.0
1761-
*/
1762-
@scala.annotation.varargs
1763-
def formatString(format: String, arguNames: String*): Column = {
1764-
StringFormat(lit(format).expr +: arguNames.map(Column(_).expr): _*)
1751+
def format_string(format: String, arguments: Column*): Column = {
1752+
FormatString((lit(format) +: arguments).map(_.expr): _*)
17651753
}
17661754

17671755
/**

sql/core/src/test/scala/org/apache/spark/sql/StringFunctionsSuite.scala

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,12 @@ class StringFunctionsSuite extends QueryTest {
126126
val df = Seq(("aa%d%s", 123, "cc")).toDF("a", "b", "c")
127127

128128
checkAnswer(
129-
df.select(formatString("aa%d%s", "b", "c")),
129+
df.select(format_string("aa%d%s", $"b", $"c")),
130130
Row("aa123cc"))
131131

132132
checkAnswer(
133133
df.selectExpr("printf(a, b, c)"),
134134
Row("aa123cc"))
135-
136-
val df2 = Seq(("aa%d%s".getBytes, 123, "cc")).toDF("a", "b", "c")
137-
138-
checkAnswer(
139-
df2.select(formatString($"a", $"b", $"c"), formatString("aa%d%s", "b", "c")),
140-
Row("aa123cc", "aa123cc"))
141-
142-
checkAnswer(
143-
df2.selectExpr("printf(a, b, c)"),
144-
Row("aa123cc"))
145135
}
146136

147137
test("string instr function") {

0 commit comments

Comments
 (0)