spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zhichao-li <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-8214][SQL]Add function hex
Date Mon, 29 Jun 2015 01:25:30 GMT
Github user zhichao-li commented on a diff in the pull request:

    https://github.com/apache/spark/pull/6976#discussion_r33429784
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/math.scala
---
    @@ -287,6 +286,88 @@ case class Pow(left: Expression, right: Expression)
       }
     }
     
    +/**
    + * If the argument is an INT or binary, hex returns the number as a STRING in hexadecimal
format.
    + * Otherwise if the number is a STRING,
    + * it converts each character into its hexadecimal representation and returns the resulting
STRING.
    + * Negative numbers would be treated as two's complement.
    + */
    +case class Hex(child: Expression)
    +  extends UnaryExpression with Serializable  {
    +
    +  override def dataType: DataType = StringType
    +
    +  override def checkInputDataTypes(): TypeCheckResult = {
    +    if (child.dataType.isInstanceOf[StringType]
    +      || child.dataType.isInstanceOf[IntegerType]
    +      || child.dataType.isInstanceOf[LongType]
    +      || child .dataType.isInstanceOf[BinaryType]
    +      || child.dataType == NullType) {
    +      TypeCheckResult.TypeCheckSuccess
    +    } else {
    +      TypeCheckResult.TypeCheckFailure(s"hex doesn't accepts ${child.dataType} type")
    +    }
    +  }
    +
    +  override def eval(input: InternalRow): Any = {
    +    val num = child.eval(input)
    +    if (num == null) {
    +      null
    +    } else {
    +      child.dataType match {
    +        case LongType => hex(num.asInstanceOf[Long])
    +        case IntegerType => hex(num.asInstanceOf[Integer].toLong)
    +        case BinaryType => hex(num.asInstanceOf[Array[Byte]])
    +        case StringType => hex(num.asInstanceOf[UTF8String])
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Converts every character in s to two hex digits.
    +   */
    +  private def hex(str: UTF8String): UTF8String = {
    +    doHex(str.toString.getBytes, str.length())
    +  }
    +
    +  private def hex(bytes: Array[Byte]): UTF8String = {
    +    doHex(bytes, bytes.length)
    +  }
    +
    +  private def doHex(bytes: Array[Byte], length: Int): UTF8String = {
    +    var value = new Array[Byte](16)
    +    if (value.length < length * 2) {
    +      value = new Array[Byte](length * 2);
    +    }
    +    var i = 0
    +    while(i < length) {
    +      value(i * 2) = Character.toUpperCase(Character.forDigit(
    +        (bytes(i) & 0xF0) >>> 4, 16)).toByte
    +      value(i * 2 + 1) = Character.toUpperCase(Character.forDigit(
    +        bytes(i) & 0x0F, 16)).toByte
    +      i += 1
    +    }
    +    UTF8String.fromBytes(Arrays.copyOfRange(value, 0, length*2))
    --- End diff --
    
    ok


---
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