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.trim().toLong(result)) result.value else null)
buildCast[UTF8String](_, s => if (s.nonCopyTrim().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.trim().toInt(result)) result.value else null)
buildCast[UTF8String](_, s => if (s.nonCopyTrim().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.trim().toInt($wrapper)) {
if ($c.nonCopyTrim().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.trim().toLong($wrapper)) {
if ($c.nonCopyTrim().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 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
cast(str as int) as c_int 2208 4341 1848 1.9 539.0 1.0X
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the result of master branch?

cast(str as long) as c_long 2039 3450 2146 2.0 497.8 1.1X
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 the result seems not as we expected, I'd increase the cardinality and do another test round. Can you help me to see if I missed something?



4 changes: 4 additions & 0 deletions sql/core/src/test/resources/sql-tests/inputs/cast.sql
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ DESC FUNCTION EXTENDED boolean;
-- cast string to interval and interval to string
SELECT CAST('interval 3 month 1 hour' AS interval);
SELECT CAST(interval 3 month 1 hour AS string);

select cast(' 1' as int);
select cast(' 1' as bigint);
select cast(' 1' as double);
26 changes: 25 additions & 1 deletion sql/core/src/test/resources/sql-tests/results/cast.sql.out
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Automatically generated by SQLQueryTestSuite
-- Number of queries: 35
-- Number of queries: 38


-- !query 0
Expand Down Expand Up @@ -287,3 +287,27 @@ SELECT CAST(interval 3 month 1 hour AS string)
struct<CAST(INTERVAL '3 months 1 hours' AS STRING):string>
-- !query 34 output
3 months 1 hours


-- !query 35
select cast(' 1' as int)
-- !query 35 schema
struct<CAST( 1 AS INT):int>
-- !query 35 output
1


-- !query 36
select cast(' 1' as bigint)
-- !query 36 schema
struct<CAST( 1 AS BIGINT):bigint>
-- !query 36 output
1


-- !query 37
select cast(' 1' as double)
-- !query 37 schema
struct<CAST( 1 AS DOUBLE):double>
-- !query 37 output
1.0