Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ case class UnBase64(child: Expression)
* If either argument is null, the result will also be null.
*/
case class Decode(bin: Expression, charset: Expression)
extends BinaryExpression with ImplicitCastInputTypes with CodegenFallback {
extends BinaryExpression with ImplicitCastInputTypes {

override def left: Expression = bin
override def right: Expression = charset
Expand All @@ -802,6 +802,17 @@ case class Decode(bin: Expression, charset: Expression)
val fromCharset = input2.asInstanceOf[UTF8String].toString
UTF8String.fromString(new String(input1.asInstanceOf[Array[Byte]], fromCharset))
}

override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
nullSafeCodeGen(ctx, ev, (bytes, charset) =>
s"""
try {
${ev.primitive} = UTF8String.fromString(new String($bytes, $charset.toString()));
} catch (java.io.UnsupportedEncodingException e) {
org.apache.spark.unsafe.PlatformDependent.throwException(e);
}
""")
}
}

/**
Expand All @@ -810,7 +821,7 @@ case class Decode(bin: Expression, charset: Expression)
* If either argument is null, the result will also be null.
*/
case class Encode(value: Expression, charset: Expression)
extends BinaryExpression with ImplicitCastInputTypes with CodegenFallback {
extends BinaryExpression with ImplicitCastInputTypes {

override def left: Expression = value
override def right: Expression = charset
Expand All @@ -821,6 +832,16 @@ case class Encode(value: Expression, charset: Expression)
val toCharset = input2.asInstanceOf[UTF8String].toString
input1.asInstanceOf[UTF8String].toString.getBytes(toCharset)
}

override def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {
nullSafeCodeGen(ctx, ev, (string, charset) =>
s"""
try {
${ev.primitive} = $string.toString().getBytes($charset.toString());
} catch (java.io.UnsupportedEncodingException e) {
org.apache.spark.unsafe.PlatformDependent.throwException(e);
}""")
}
}

/**
Expand Down