Skip to content
Closed
Show file tree
Hide file tree
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 @@ -346,6 +346,23 @@ public static UTF8String quote(UTF8String str) {
return UTF8String.fromString(qtChar + sp + qtChar);
}

/**
* Returns the single-character string for the {@code chr} expression: the
* ASCII/Latin-1 character for {@code longVal & 0xFF}. A negative argument
* yields the empty string. Shared by the eval and codegen paths so the
* generated Java is a single call rather than an inline if/else chain.
*/
public static UTF8String chr(long longVal) {
if (longVal < 0) {
return UTF8String.EMPTY_UTF8;
} else if ((longVal & 0xFF) == 0) {
return UTF8String.fromString(String.valueOf(Character.MIN_VALUE));
} else {
char c = (char) (longVal & 0xFF);
return UTF8String.fromString(String.valueOf(c));
}
}

/**
* Compiles {@code regex} with the given {@code flags} for the regexp expression
* family, translating a {@link PatternSyntaxException} into the user-facing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2822,31 +2822,14 @@ case class Chr(child: Expression)
override def inputTypes: Seq[DataType] = Seq(LongType)

protected override def nullSafeEval(lon: Any): Any = {
val longVal = lon.asInstanceOf[Long]
if (longVal < 0) {
UTF8String.EMPTY_UTF8
} else if ((longVal & 0xFF) == 0) {
UTF8String.fromString(Character.MIN_VALUE.toString)
} else {
UTF8String.fromString((longVal & 0xFF).toChar.toString)
}
ExpressionImplUtils.chr(lon.asInstanceOf[Long])
}

override def contextIndependentFoldable: Boolean = child.contextIndependentFoldable

override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = {
nullSafeCodeGen(ctx, ev, lon => {
s"""
if ($lon < 0) {
${ev.value} = UTF8String.EMPTY_UTF8;
} else if (($lon & 0xFF) == 0) {
${ev.value} = UTF8String.fromString(String.valueOf(Character.MIN_VALUE));
} else {
char c = (char)($lon & 0xFF);
${ev.value} = UTF8String.fromString(String.valueOf(c));
}
"""
})
val utils = classOf[ExpressionImplUtils].getName
nullSafeCodeGen(ctx, ev, lon => s"${ev.value} = $utils.chr($lon);")
}

override protected def withNewChildInternal(newChild: Expression): Chr = copy(child = newChild)
Expand Down