spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davies <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-8227][SQL]Add function unhex
Date Wed, 01 Jul 2015 15:22:48 GMT
Github user davies commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7113#discussion_r33689483
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/math.scala
---
    @@ -354,6 +354,67 @@ case class Pow(left: Expression, right: Expression)
       }
     }
     
    +/**
    + * Performs the inverse operation of HEX.
    + * Resulting characters are returned as a byte array.
    + */
    +case class UnHex(child: Expression) 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].getBytes)
    +    }
    +  }
    +
    +  private val hexDigits = {
    +    val array = Array.fill[Byte](128)(-1)
    +    (0 to 9).foreach(i => array('0' + i) = i.toByte)
    +    (0 to 5).foreach(i => array('A' + i) = (i + 10).toByte)
    +    (0 to 5).foreach(i => array('a' + i) = (i + 10).toByte)
    +    array
    +  }
    +
    +  private def toDigit(b: Byte): Byte = {
    +    val digit = hexDigits(b)
    +    if (digit == -1) {
    +      throw new NumberFormatException(s"invalid hex number $b")
    +    }
    +    digit
    +  }
    +
    +  private def unhex(inputBytes: Array[Byte]): Array[Byte] = {
    +    var bytes = inputBytes
    +    if ((bytes.length & 0x01) != 0) {
    +      bytes = 48.toByte +: bytes // padding with '0'
    +    }
    +    val out = new Array[Byte](bytes.length >> 1)
    +    // two characters form the hex value.
    +    var i = 0
    +    while (i < bytes.length) {
    +      try {
    +        out(i / 2) = ((toDigit(bytes(i)) << 4) | toDigit(bytes(i + 1)) & 0xFF).toByte
    +        i += 2
    +      } catch {
    +        case _: NumberFormatException => return null
    --- End diff --
    
    We could check the value from `toDigit` here without throwing exceptions (Using exception
as control flow is slow).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message