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 @@ -695,18 +695,16 @@ case class Cast(

// UDFToBoolean
private[this] def castToBoolean(from: DataType): Any => Any = from match {
case _: StringType if ansiEnabled =>
buildCast[UTF8String](_, s => UTF8StringUtils.toBooleanExact(s, getContextOrNull()))
case _: StringType =>
buildCast[UTF8String](_, s => {
if (StringUtils.isTrueString(s)) {
true
} else if (StringUtils.isFalseString(s)) {
false
} else {
if (ansiEnabled) {
throw QueryExecutionErrors.invalidInputSyntaxForBooleanError(s, getContextOrNull())
} else {
null
}
null
}
})
case TimestampType =>
Expand Down Expand Up @@ -1881,22 +1879,20 @@ case class Cast(
private[this] def castToBooleanCode(
from: DataType,
ctx: CodegenContext): CastFunction = from match {
case _: StringType if ansiEnabled =>
val stringUtils = UTF8StringUtils.getClass.getCanonicalName.stripSuffix("$")
val errorContext = getContextOrNullCode(ctx)
(c, evPrim, _) => code"$evPrim = $stringUtils.toBooleanExact($c, $errorContext);"
case _: StringType =>
val stringUtils = inline"${StringUtils.getClass.getName.stripSuffix("$")}"
(c, evPrim, evNull) =>
val castFailureCode = if (ansiEnabled) {
val errorContext = getContextOrNullCode(ctx)
s"throw QueryExecutionErrors.invalidInputSyntaxForBooleanError($c, $errorContext);"
} else {
s"$evNull = true;"
}
code"""
if ($stringUtils.isTrueString($c)) {
$evPrim = true;
} else if ($stringUtils.isFalseString($c)) {
$evPrim = false;
} else {
$castFailureCode
$evNull = true;
}
"""
case TimestampType =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.apache.spark.sql.types.{ByteType, DataType, IntegerType, LongType, Sh
import org.apache.spark.unsafe.types.UTF8String

/**
* Helper functions for casting string to numeric values.
* Helper functions for casting string to primitive values under ANSI mode.
*/
object UTF8StringUtils {

Expand All @@ -39,6 +39,12 @@ object UTF8StringUtils {
def toByteExact(s: UTF8String, context: QueryContext): Byte =
withException(s.toByteExact, context, ByteType, s)

def toBooleanExact(s: UTF8String, context: QueryContext): Boolean = {
if (StringUtils.isTrueString(s)) true
else if (StringUtils.isFalseString(s)) false
else throw QueryExecutionErrors.invalidInputSyntaxForBooleanError(s, context)
}

private def withException[A](
f: => A,
context: QueryContext,
Expand Down