Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update as feedback
  • Loading branch information
chenghao-intel committed Jul 16, 2015
commit 3ebe288e6f659deb9b4a1de7cc11ea1fc7f5db2b
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,6 @@ case class Length(child: Expression) extends UnaryExpression with ExpectsInputTy
child.dataType match {
case StringType => defineCodeGen(ctx, ev, c => s"($c).numChars()")
case BinaryType => defineCodeGen(ctx, ev, c => s"($c).length")
case NullType => defineCodeGen(ctx, ev, c => s"-1")
}
}

Expand Down Expand Up @@ -685,8 +684,6 @@ case class FormatNumber(x: Expression, d: Expression)
override def right: Expression = d
override def dataType: DataType = StringType
override def inputTypes: Seq[AbstractDataType] = Seq(NumericType, IntegerType)
override def foldable: Boolean = x.foldable && d.foldable
override def nullable: Boolean = x.nullable || d.nullable

@transient
private var lastDValue: Int = -100
Expand All @@ -706,8 +703,7 @@ case class FormatNumber(x: Expression, d: Expression)
val dObject = d.eval(input)

if (dObject == null || dObject.asInstanceOf[Int] < 0) {
throw new IllegalArgumentException(
s"Argument 2 of function FORMAT_NUMBER must be >= 0, but $dObject was found")
return null
}
val dValue = dObject.asInstanceOf[Int]

Expand Down Expand Up @@ -742,5 +738,7 @@ case class FormatNumber(x: Expression, d: Expression)
UTF8String.fromString(numberFormat.format(xObject.asInstanceOf[Decimal].toJavaBigDecimal))
}
}

override def prettyName: String = "format_number"
}

Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,6 @@ class StringFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {

checkEvaluation(Length(Literal.create(null, StringType)), null, create_row(string))
checkEvaluation(Length(Literal.create(null, BinaryType)), null, create_row(bytes))

checkEvaluation(Length(Literal.create(null, NullType)), null, create_row(null))
}

test("number format") {
Expand All @@ -453,6 +451,7 @@ class StringFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {
checkEvaluation(FormatNumber(Literal(12831273.23481d), Literal(3)), "12,831,273.235")
checkEvaluation(FormatNumber(Literal(12831273.83421d), Literal(0)), "12,831,274")
checkEvaluation(FormatNumber(Literal(123123324123L), Literal(3)), "123,123,324,123.000")
checkEvaluation(FormatNumber(Literal(123123324123L), Literal(-1)), null)
checkEvaluation(
FormatNumber(
Literal(Decimal(123123324123L) * Decimal(123123.21234d)), Literal(4)),
Expand Down
24 changes: 13 additions & 11 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1685,41 +1685,43 @@ object functions {
//////////////////////////////////////////////////////////////////////////////////////////////

/**
* Computes the length of a given string / binary value
* Computes the length of a given string / binary value.
*
* @group string_funcs
* @since 1.5.0
*/
def length(e: Column): Column = Length(e.expr)

/**
* Computes the length of a given string / binary column
* Computes the length of a given string / binary column.
*
* @group string_funcs
* @since 1.5.0
*/
def length(columnName: String): Column = length(Column(columnName))

/**
* Formats the number X to a format like '#,###,###.##', rounded to D decimal places,
* and returns the result as a string. If D is 0, the result has no decimal point or
* fractional part.
* Formats the number X to a format like '#,###,###.##', rounded to d decimal places,
* and returns the result as a string.
* If d is 0, the result has no decimal point or fractional part.
* If d < 0, the result will be null.
*
* @group string_funcs
* @since 1.5.0
*/
def formatNumber(x: Column, d: Column): Column = FormatNumber(x.expr, d.expr)
def format_number(x: Column, d: Int): Column = FormatNumber(x.expr, lit(d).expr)

/**
* Formats the number X to a format like '#,###,###.##', rounded to D decimal places,
* and returns the result as a string. If D is 0, the result has no decimal point or
* fractional part.
* Formats the number X to a format like '#,###,###.##', rounded to d decimal places,
* and returns the result as a string.
* If d is 0, the result has no decimal point or fractional part.
* If d < 0, the result will be null.
*
* @group string_funcs
* @since 1.5.0
*/
def formatNumber(columnXName: String, columnDName: String): Column = {
formatNumber(Column(columnXName), Column(columnDName))
def format_number(columnXName: String, d: Int): Column = {
format_number(Column(columnXName), d)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ class DataFrameFunctionsSuite extends QueryTest {

checkAnswer(
df.select(
formatNumber($"f", $"e"),
formatNumber("f", "e")),
format_number($"f", 4),
format_number("f", 4)),
Row("5.0000", "5.0000"))

checkAnswer(
Expand Down