Skip to content

Commit dac7dbf

Browse files
tarekbeckerrxin
authored andcommitted
[SPARK-9160][SQL] codegen encode, decode
Jira: https://issues.apache.org/jira/browse/SPARK-9160 Author: Tarek Auel <[email protected]> Closes #7543 from tarekauel/SPARK-9160 and squashes the following commits: 7528f0e [Tarek Auel] [SPARK-9160][SQL] codegen encode, decode
1 parent c9db8ea commit dac7dbf

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/stringOperations.scala

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ case class UnBase64(child: Expression) extends UnaryExpression with ImplicitCast
818818
* If either argument is null, the result will also be null.
819819
*/
820820
case class Decode(bin: Expression, charset: Expression)
821-
extends BinaryExpression with ImplicitCastInputTypes with CodegenFallback {
821+
extends BinaryExpression with ImplicitCastInputTypes {
822822

823823
override def left: Expression = bin
824824
override def right: Expression = charset
@@ -829,6 +829,17 @@ case class Decode(bin: Expression, charset: Expression)
829829
val fromCharset = input2.asInstanceOf[UTF8String].toString
830830
UTF8String.fromString(new String(input1.asInstanceOf[Array[Byte]], fromCharset))
831831
}
832+
833+
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
834+
nullSafeCodeGen(ctx, ev, (bytes, charset) =>
835+
s"""
836+
try {
837+
${ev.primitive} = UTF8String.fromString(new String($bytes, $charset.toString()));
838+
} catch (java.io.UnsupportedEncodingException e) {
839+
org.apache.spark.unsafe.PlatformDependent.throwException(e);
840+
}
841+
""")
842+
}
832843
}
833844

834845
/**
@@ -837,7 +848,7 @@ case class Decode(bin: Expression, charset: Expression)
837848
* If either argument is null, the result will also be null.
838849
*/
839850
case class Encode(value: Expression, charset: Expression)
840-
extends BinaryExpression with ImplicitCastInputTypes with CodegenFallback {
851+
extends BinaryExpression with ImplicitCastInputTypes {
841852

842853
override def left: Expression = value
843854
override def right: Expression = charset
@@ -848,6 +859,16 @@ case class Encode(value: Expression, charset: Expression)
848859
val toCharset = input2.asInstanceOf[UTF8String].toString
849860
input1.asInstanceOf[UTF8String].toString.getBytes(toCharset)
850861
}
862+
863+
override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
864+
nullSafeCodeGen(ctx, ev, (string, charset) =>
865+
s"""
866+
try {
867+
${ev.primitive} = $string.toString().getBytes($charset.toString());
868+
} catch (java.io.UnsupportedEncodingException e) {
869+
org.apache.spark.unsafe.PlatformDependent.throwException(e);
870+
}""")
871+
}
851872
}
852873

853874
/**

0 commit comments

Comments
 (0)