Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
private[this] def castToLong(from: DataType): Any => Any = from match {
case StringType =>
val result = new LongWrapper()
buildCast[UTF8String](_, s => if (s.toLong(result)) result.value else null)
buildCast[UTF8String](_, s => if (s.trim().toLong(result)) result.value else null)
case BooleanType =>
buildCast[Boolean](_, b => if (b) 1L else 0L)
case DateType =>
Expand All @@ -501,7 +501,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
private[this] def castToInt(from: DataType): Any => Any = from match {
case StringType =>
val result = new IntWrapper()
buildCast[UTF8String](_, s => if (s.toInt(result)) result.value else null)
buildCast[UTF8String](_, s => if (s.trim().toInt(result)) result.value else null)
case BooleanType =>
buildCast[Boolean](_, b => if (b) 1 else 0)
case DateType =>
Expand Down Expand Up @@ -1418,7 +1418,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
(c, evPrim, evNull) =>
code"""
UTF8String.IntWrapper $wrapper = new UTF8String.IntWrapper();
if ($c.toInt($wrapper)) {
if ($c.trim().toInt($wrapper)) {
$evPrim = $wrapper.value;
} else {
$evNull = true;
Expand Down Expand Up @@ -1447,7 +1447,7 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
(c, evPrim, evNull) =>
code"""
UTF8String.LongWrapper $wrapper = new UTF8String.LongWrapper();
if ($c.toLong($wrapper)) {
if ($c.trim().toLong($wrapper)) {
$evPrim = $wrapper.value;
} else {
$evNull = true;
Expand Down
4 changes: 2 additions & 2 deletions sql/core/benchmarks/CastBenchmark-results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Java HotSpot(TM) 64-Bit Server VM 1.8.0_231-b11 on Mac OS X 10.15.1
Intel(R) Core(TM) i5-5287U CPU @ 2.90GHz
Benchmark trim the string: Best Time(ms) Avg Time(ms) Stdev(ms) Rate(M/s) Per Row(ns) Relative
------------------------------------------------------------------------------------------------------------------------
cast(str as int) as c_int 2478 3669 1046 1.7 604.9 1.0X
cast(str as long) as c_long 1439 1548 94 2.8 351.4 1.7X
cast(str as int) as c_int 3169 3530 610 1.3 773.6 1.0X
cast(str as long) as c_long 1812 1881 60 2.3 442.4 1.7X
Copy link
Member Author

Choose a reason for hiding this comment

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

@cloud-fan this is master branch to original trim result.