Skip to content

Commit

Permalink
Accept only BinaryType for Md5
Browse files Browse the repository at this point in the history
  • Loading branch information
qiansl127 committed Jun 17, 2015
1 parent 1df0b5b commit 12c61f4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ import org.apache.spark.unsafe.types.UTF8String

/**
* A function that calculates an MD5 128-bit checksum and returns it as a hex string
* For input of type [[StringType]] or [[BinaryType]]
* For input of type [[BinaryType]]
*/
case class Md5(child: Expression) extends UnaryExpression {
case class Md5(child: Expression)
extends UnaryExpression with ExpectsInputTypes {

override def dataType: DataType = StringType

override def expectedChildTypes: Seq[DataType] = Seq(BinaryType)

override def checkInputDataTypes(): TypeCheckResult =
if (child.dataType == StringType || child.dataType == BinaryType) {
if (child.dataType == BinaryType) {
TypeCheckResult.TypeCheckSuccess
} else {
TypeCheckResult.TypeCheckFailure(
s"types error in ${this.getClass.getSimpleName} " +
s"get (${child.dataType}, expect StringType or BinaryType).")
s"get (${child.dataType}, expect BinaryType).")
}

override def children: Seq[Expression] = child :: Nil
Expand All @@ -47,10 +50,8 @@ case class Md5(child: Expression) extends UnaryExpression {
val value = child.eval(input)
if (value == null) {
null
} else if (child.dataType == BinaryType) {
} else{
UTF8String.fromString(DigestUtils.md5Hex(value.asInstanceOf[Array[Byte]]))
} else {
UTF8String.fromString(DigestUtils.md5Hex(value.asInstanceOf[UTF8String].getBytes))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ import org.apache.spark.sql.types.{StringType, BinaryType}
class MiscFunctionsSuite extends SparkFunSuite with ExpressionEvalHelper {

test("md5") {
checkEvaluation(Md5(Literal("ABC")), "902fbdd2b1df0c4f70b4a5d23525e932")
checkEvaluation(Md5(Literal("ABC".getBytes)), "902fbdd2b1df0c4f70b4a5d23525e932")
checkEvaluation(Md5(Literal.create(Array[Byte](1, 2, 3, 4, 5, 6), BinaryType)),
"6ac1e56bc78f031059be7be854522c4c")
checkEvaluation(Md5(Literal.create(null, BinaryType)), null)
checkEvaluation(Md5(Literal.create(null, StringType)), null)
}

}

0 comments on commit 12c61f4

Please sign in to comment.