Skip to content

Commit

Permalink
use checkInputTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
zhichao-li committed Jul 1, 2015
1 parent bffd37f commit 607d7a3
Showing 1 changed file with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
package org.apache.spark.sql.catalyst.expressions

import java.lang.{Long => JLong}
import java.nio.charset.{StandardCharsets, Charset}
import java.util.Arrays

import org.apache.commons.codec.DecoderException
import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
import org.apache.spark.sql.catalyst.expressions.codegen._
import org.apache.spark.sql.types._
Expand Down Expand Up @@ -361,27 +359,43 @@ case class Pow(left: Expression, right: Expression)
* Resulting characters are returned as a byte array.
*/
case class UnHex(child: Expression)
extends UnaryExpression with AutoCastInputTypes with Serializable {

override def expectedChildTypes: Seq[DataType] = Seq(StringType)
extends UnaryExpression with Serializable {

override def dataType: DataType = BinaryType

override def checkInputDataTypes(): TypeCheckResult = {
if (child.dataType.isInstanceOf[StringType] || child.dataType == NullType) {
TypeCheckResult.TypeCheckSuccess
} else {
TypeCheckResult.TypeCheckFailure(s"$unHex accepts String type, not ${child.dataType}")
}
}


override def eval(input: InternalRow): Any = {
val num = child.eval(input)
if (num == null) {
null
} else {
unhex(num.asInstanceOf[UTF8String])
unhex(num.asInstanceOf[UTF8String].toString)
}
}

private def unhex(utf8Str: UTF8String): Array[Byte] = {
try {
new org.apache.commons.codec.binary.Hex(StandardCharsets.UTF_8).decode(utf8Str.getBytes)
} catch {
case _: DecoderException => null
private def unhex(s: String): Array[Byte] = {
// append a leading 0 if needed
val str = if (s.length % 2 == 1) {"0" + s} else {s}
val result = new Array[Byte](str.length / 2)
var i = 0
while (i < str.length()) {
try {
result(i / 2) = Integer.parseInt(str.substring(i, i + 2), 16).asInstanceOf[Byte]
} catch {
// invalid character present, return null
case _: NumberFormatException => return null
}
i += 2
}
result
}
}

Expand Down

0 comments on commit 607d7a3

Please sign in to comment.