Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,15 @@ object StringUtils extends Logging {
"(?s)" + out.result() // (?s) enables dotall mode, causing "." to match new lines
}

private[this] val trueStrings = Set("t", "true", "y", "yes", "1").map(UTF8String.fromString)
private[this] val falseStrings = Set("f", "false", "n", "no", "0").map(UTF8String.fromString)
private[this] val trueStrings =
Set("t", "true", "y", "yes", "1", "on").map(UTF8String.fromString)

private[this] val falseStrings =
Set("f", "false", "n", "no", "0", "off").map(UTF8String.fromString)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems only PostgreSQL accepts on and off?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I guess so. Do you know other common string representattion used in other databases?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But PostgreSQL also acceptsof, tru, fals, ...:

postgres=# select cast('of' as boolean), cast('tru' as boolean), cast('fals' as boolean);
 bool | bool | bool
------+------+------
 f    | t    | f
(1 row)

postgres/postgres@9729c93

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay. Let me add that too. Thank you


// scalastyle:off caselocale
def isTrueString(s: UTF8String): Boolean = trueStrings.contains(s.toLowerCase)
def isFalseString(s: UTF8String): Boolean = falseStrings.contains(s.toLowerCase)
def isTrueString(s: UTF8String): Boolean = trueStrings.contains(s.toLowerCase.trim())
def isFalseString(s: UTF8String): Boolean = falseStrings.contains(s.toLowerCase.trim())
// scalastyle:on caselocale

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -825,13 +825,17 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
checkCast("y", true)
checkCast("yes", true)
checkCast("1", true)
checkCast("on", true)
checkCast(" true ", true)

checkCast("f", false)
checkCast("false", false)
checkCast("FAlsE", false)
checkCast("n", false)
checkCast("no", false)
checkCast("0", false)
checkCast("off", false)
checkCast(" off ", false)

checkEvaluation(cast("abc", BooleanType), null)
checkEvaluation(cast("", BooleanType), null)
Expand Down