Github user cloud-fan commented on a diff in the pull request:
https://github.com/apache/spark/pull/6843#discussion_r32844594
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala
---
@@ -307,9 +307,176 @@ case class StringLength(child: Expression) extends
UnaryExpression with ExpectsI
if (string == null) null else string.asInstanceOf[UTF8String].length
}
- override def toString: String = s"length($child)"
+ override def toString: String = s"LENGTH($child)"
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode):
String = {
defineCodeGen(ctx, ev, c => s"($c).length()")
}
}
+
+/**
+ * Returns the numeric value of the first character of str.
+ */
+case class Ascii(child: Expression) extends UnaryExpression {
+ override def dataType: DataType = IntegerType
+ override def checkInputDataTypes(): TypeCheckResult = {
+ if (child.dataType != StringType) {
+ TypeCheckResult.TypeCheckFailure(
+ s"type of child expression in ASCII should be string, not
${child.dataType}")
+ } else {
+ TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ 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 {
+ override def dataType: DataType = StringType
+ override def checkInputDataTypes(): TypeCheckResult = {
+ if (child.dataType != BinaryType) {
+ TypeCheckResult.TypeCheckFailure(
+ s"type of child expression in BASE64 should be binary, not
${child.dataType}")
+ } else {
+ TypeCheckResult.TypeCheckSuccess
+ }
+ }
+
+ 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 {
+ override def dataType: DataType = BinaryType
+ override def checkInputDataTypes(): TypeCheckResult = {
+ if (child.dataType != StringType) {
+ TypeCheckResult.TypeCheckFailure(
+ s"type of child expression in UNBASE64 should be string, not
${child.dataType}")
--- End diff --
Maybe we should simplify type checking code for single type restrict like
this one. Create a new `ExpectsInputTypes` is a good idea for me.
---
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]