Github user adrian-wang commented on a diff in the pull request:

    https://github.com/apache/spark/pull/6843#discussion_r32672177
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
 ---
    @@ -313,3 +313,131 @@ case class StringLength(child: Expression) extends 
UnaryExpression with ExpectsI
         defineCodeGen(ctx, ev, c => s"($c).length()")
       }
     }
    +
    +/**
    + * Returns the numeric value of the first character of str.
    + */
    +case class Ascii(child: Expression) extends UnaryExpression with 
ExpectsInputTypes {
    +  override def dataType: DataType = IntegerType
    +  override def expectedChildTypes: Seq[DataType] = Seq(StringType)
    +
    +  override def eval(input: InternalRow): Any = {
    +    val string = child.eval(input)
    +    if (string == null) {
    +      null
    +    } else {
    +      val bytes = string.asInstanceOf[UTF8String].getBytes
    +      if (bytes.length > 0) {
    +        bytes(0).asInstanceOf[Int]
    +      } else {
    +        0
    +      }
    +    }
    +  }
    +
    +  override def toString: String = s"ascii($child)"
    +}
    +
    +/**
    + * Converts the argument from binary to a base 64 string.
    + */
    +case class Base64(child: Expression) extends UnaryExpression with 
ExpectsInputTypes {
    +  override def dataType: DataType = StringType
    +  override def expectedChildTypes: Seq[DataType] = Seq(BinaryType)
    +
    +  override def eval(input: InternalRow): Any = {
    +    val bytes = child.eval(input)
    +    if (bytes == null) {
    +      null
    +    } else {
    +      UTF8String.fromBytes(
    +        org.apache.commons.codec.binary.Base64.encodeBase64(
    +          bytes.asInstanceOf[Array[Byte]]))
    +    }
    +  }
    +
    +  override def toString: String = s"base64($child)"
    +}
    +
    +/**
    + * Converts the argument from a base 64 string to BINARY.
    + */
    +case class UnBase64(child: Expression) extends UnaryExpression with 
ExpectsInputTypes {
    +  override def dataType: DataType = BinaryType
    +  override def expectedChildTypes: Seq[DataType] = Seq(StringType)
    +
    +  override def eval(input: InternalRow): Any = {
    +    val string = child.eval(input)
    +    if (string == null) {
    +      null
    +    } else {
    +      
org.apache.commons.codec.binary.Base64.decodeBase64(string.asInstanceOf[UTF8String].toString)
    +    }
    +  }
    +
    +  override def toString: String = s"unbase64($child)"
    +}
    +
    +/**
    + * Decodes the first argument into a String using the provided character 
set
    + * (one of 'US-ASCII', 'ISO-8859-1', 'UTF-8', 'UTF-16BE', 'UTF-16LE', 
'UTF-16').
    + * If either argument is null, the result will also be null. (As of Hive 
0.12.0.).
    + */
    +case class Decode(bin: Expression, charset: Expression)
    +  extends Expression with ExpectsInputTypes {
    +  override def children: Seq[Expression] = bin :: charset :: Nil
    +  override def foldable: Boolean = bin.foldable && charset.foldable
    +  override def nullable: Boolean = bin.nullable || charset.nullable
    +  override def dataType: DataType = StringType
    +  override def expectedChildTypes: Seq[DataType] = Seq(BinaryType, 
StringType)
    +
    +  override def eval(input: InternalRow): Any = {
    +    val l = bin.eval(input)
    +    if (l == null) {
    +      null
    +    } else {
    +      val r = charset.eval(input)
    +      if (r == null) {
    +        null
    +      } else {
    +        val fromCharset = r.asInstanceOf[UTF8String].toString
    +        UTF8String.fromString(new String(l.asInstanceOf[Array[Byte]], 
fromCharset))
    +      }
    +    }
    +  }
    +
    +  override def toString: String = s"decode($bin, $charset)"
    +}
    +
    +/**
    +* Encodes the first argument into a BINARY using the provided character set
    --- End diff --
    
    indent


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