Github user zhichao-li commented on a diff in the pull request:

    https://github.com/apache/spark/pull/6976#discussion_r33429654
  
    --- 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);
    --- End diff --
    
    The idea is to give the value a lager buffer in case if the input string is 
too long since we would convert every character into  two hex digits here for 
String input


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to