Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
one more comment
  • Loading branch information
gengliangwang committed Sep 26, 2019
commit 75aeda757b10b8601cb386a53a87d888fc6556e1
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ case class PostgreCastStringToBoolean(child: Expression)
}

override def nullSafeEval(input: Any): Any = {
val s = input.asInstanceOf[UTF8String]
val s = input.asInstanceOf[UTF8String].trim().toLowerCase()
if (StringUtils.isTrueString(s)) {
true
} else if (StringUtils.isFalseString(s)) {
Expand All @@ -51,14 +51,16 @@ case class PostgreCastStringToBoolean(child: Expression)
val stringUtils = inline"${StringUtils.getClass.getName.stripSuffix("$")}"
val eval = child.genCode(ctx)
val javaType = JavaCode.javaType(dataType)
val preprocessedString = ctx.freshName("preprocessedString")
val castCode =
code"""
boolean ${ev.isNull} = ${eval.isNull};
$javaType ${ev.value} = false;
if (!${eval.isNull}) {
if ($stringUtils.isTrueString(${eval.value})) {
UTF8String $preprocessedString = ${eval.value}.trim().toLowerCase();
if ($stringUtils.isTrueString($preprocessedString)) {
${ev.value} = true;
} else if ($stringUtils.isFalseString(${eval.value})) {
} else if ($stringUtils.isFalseString($preprocessedString)) {
${ev.value} = false;
} else {
${ev.isNull} = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object StringUtils {
private[this] val falseStrings =
Set("false", "fals", "fal", "fa", "f", "no", "n", "off", "of", "0").map(UTF8String.fromString)

def isTrueString(s: UTF8String): Boolean = trueStrings.contains(s.trim().toLowerCase())
def isTrueString(s: UTF8String): Boolean = trueStrings.contains(s)

def isFalseString(s: UTF8String): Boolean = falseStrings.contains(s.trim().toLowerCase())
def isFalseString(s: UTF8String): Boolean = falseStrings.contains(s)
}