@@ -503,9 +503,9 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
503
503
}
504
504
505
505
test(" cast from array" ) {
506
- val array = Literal .create(Seq (" 123" , " abc " , " " , null ),
506
+ val array = Literal .create(Seq (" 123" , " true " , " f " , null ),
507
507
ArrayType (StringType , containsNull = true ))
508
- val array_notNull = Literal .create(Seq (" 123" , " abc " , " " ),
508
+ val array_notNull = Literal .create(Seq (" 123" , " true " , " f " ),
509
509
ArrayType (StringType , containsNull = false ))
510
510
511
511
checkNullCast(ArrayType (StringType ), ArrayType (IntegerType ))
@@ -522,7 +522,7 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
522
522
{
523
523
val ret = cast(array, ArrayType (BooleanType , containsNull = true ))
524
524
assert(ret.resolved === true )
525
- checkEvaluation(ret, Seq (true , true , false , null ))
525
+ checkEvaluation(ret, Seq (null , true , false , null ))
526
526
}
527
527
{
528
528
val ret = cast(array, ArrayType (BooleanType , containsNull = false ))
@@ -541,12 +541,12 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
541
541
{
542
542
val ret = cast(array_notNull, ArrayType (BooleanType , containsNull = true ))
543
543
assert(ret.resolved === true )
544
- checkEvaluation(ret, Seq (true , true , false ))
544
+ checkEvaluation(ret, Seq (null , true , false ))
545
545
}
546
546
{
547
547
val ret = cast(array_notNull, ArrayType (BooleanType , containsNull = false ))
548
548
assert(ret.resolved === true )
549
- checkEvaluation(ret, Seq (true , true , false ))
549
+ checkEvaluation(ret, Seq (null , true , false ))
550
550
}
551
551
552
552
{
@@ -557,10 +557,10 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
557
557
558
558
test(" cast from map" ) {
559
559
val map = Literal .create(
560
- Map (" a" -> " 123" , " b" -> " abc " , " c" -> " " , " d" -> null ),
560
+ Map (" a" -> " 123" , " b" -> " true " , " c" -> " f " , " d" -> null ),
561
561
MapType (StringType , StringType , valueContainsNull = true ))
562
562
val map_notNull = Literal .create(
563
- Map (" a" -> " 123" , " b" -> " abc " , " c" -> " " ),
563
+ Map (" a" -> " 123" , " b" -> " true " , " c" -> " f " ),
564
564
MapType (StringType , StringType , valueContainsNull = false ))
565
565
566
566
checkNullCast(MapType (StringType , IntegerType ), MapType (StringType , StringType ))
@@ -577,7 +577,7 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
577
577
{
578
578
val ret = cast(map, MapType (StringType , BooleanType , valueContainsNull = true ))
579
579
assert(ret.resolved === true )
580
- checkEvaluation(ret, Map (" a" -> true , " b" -> true , " c" -> false , " d" -> null ))
580
+ checkEvaluation(ret, Map (" a" -> null , " b" -> true , " c" -> false , " d" -> null ))
581
581
}
582
582
{
583
583
val ret = cast(map, MapType (StringType , BooleanType , valueContainsNull = false ))
@@ -600,12 +600,12 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
600
600
{
601
601
val ret = cast(map_notNull, MapType (StringType , BooleanType , valueContainsNull = true ))
602
602
assert(ret.resolved === true )
603
- checkEvaluation(ret, Map (" a" -> true , " b" -> true , " c" -> false ))
603
+ checkEvaluation(ret, Map (" a" -> null , " b" -> true , " c" -> false ))
604
604
}
605
605
{
606
606
val ret = cast(map_notNull, MapType (StringType , BooleanType , valueContainsNull = false ))
607
607
assert(ret.resolved === true )
608
- checkEvaluation(ret, Map (" a" -> true , " b" -> true , " c" -> false ))
608
+ checkEvaluation(ret, Map (" a" -> null , " b" -> true , " c" -> false ))
609
609
}
610
610
{
611
611
val ret = cast(map_notNull, MapType (IntegerType , StringType , valueContainsNull = true ))
@@ -630,8 +630,8 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
630
630
val struct = Literal .create(
631
631
InternalRow (
632
632
UTF8String .fromString(" 123" ),
633
- UTF8String .fromString(" abc " ),
634
- UTF8String .fromString(" " ),
633
+ UTF8String .fromString(" true " ),
634
+ UTF8String .fromString(" f " ),
635
635
null ),
636
636
StructType (Seq (
637
637
StructField (" a" , StringType , nullable = true ),
@@ -641,8 +641,8 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
641
641
val struct_notNull = Literal .create(
642
642
InternalRow (
643
643
UTF8String .fromString(" 123" ),
644
- UTF8String .fromString(" abc " ),
645
- UTF8String .fromString(" " )),
644
+ UTF8String .fromString(" true " ),
645
+ UTF8String .fromString(" f " )),
646
646
StructType (Seq (
647
647
StructField (" a" , StringType , nullable = false ),
648
648
StructField (" b" , StringType , nullable = false ),
@@ -672,7 +672,7 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
672
672
StructField (" c" , BooleanType , nullable = true ),
673
673
StructField (" d" , BooleanType , nullable = true ))))
674
674
assert(ret.resolved === true )
675
- checkEvaluation(ret, InternalRow (true , true , false , null ))
675
+ checkEvaluation(ret, InternalRow (null , true , false , null ))
676
676
}
677
677
{
678
678
val ret = cast(struct, StructType (Seq (
@@ -704,15 +704,15 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
704
704
StructField (" b" , BooleanType , nullable = true ),
705
705
StructField (" c" , BooleanType , nullable = true ))))
706
706
assert(ret.resolved === true )
707
- checkEvaluation(ret, InternalRow (true , true , false ))
707
+ checkEvaluation(ret, InternalRow (null , true , false ))
708
708
}
709
709
{
710
710
val ret = cast(struct_notNull, StructType (Seq (
711
711
StructField (" a" , BooleanType , nullable = true ),
712
712
StructField (" b" , BooleanType , nullable = true ),
713
713
StructField (" c" , BooleanType , nullable = false ))))
714
714
assert(ret.resolved === true )
715
- checkEvaluation(ret, InternalRow (true , true , false ))
715
+ checkEvaluation(ret, InternalRow (null , true , false ))
716
716
}
717
717
718
718
{
@@ -731,8 +731,8 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
731
731
test(" complex casting" ) {
732
732
val complex = Literal .create(
733
733
Row (
734
- Seq (" 123" , " abc " , " " ),
735
- Map (" a" -> " 123" , " b" -> " abc " , " c" -> " " ),
734
+ Seq (" 123" , " true " , " f " ),
735
+ Map (" a" -> " 123" , " b" -> " true " , " c" -> " f " ),
736
736
Row (0 )),
737
737
StructType (Seq (
738
738
StructField (" a" ,
@@ -755,11 +755,11 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
755
755
assert(ret.resolved === true )
756
756
checkEvaluation(ret, Row (
757
757
Seq (123 , null , null ),
758
- Map (" a" -> true , " b" -> true , " c" -> false ),
758
+ Map (" a" -> null , " b" -> true , " c" -> false ),
759
759
Row (0L )))
760
760
}
761
761
762
- test(" case between string and interval" ) {
762
+ test(" cast between string and interval" ) {
763
763
import org .apache .spark .unsafe .types .CalendarInterval
764
764
765
765
checkEvaluation(Cast (Literal (" interval -3 month 7 hours" ), CalendarIntervalType ),
@@ -769,4 +769,23 @@ class CastSuite extends SparkFunSuite with ExpressionEvalHelper {
769
769
StringType ),
770
770
" interval 1 years 3 months -3 days" )
771
771
}
772
+
773
+ test(" cast string to boolean" ) {
774
+ checkCast(" t" , true )
775
+ checkCast(" true" , true )
776
+ checkCast(" tRUe" , true )
777
+ checkCast(" y" , true )
778
+ checkCast(" yes" , true )
779
+ checkCast(" 1" , true )
780
+
781
+ checkCast(" f" , false )
782
+ checkCast(" false" , false )
783
+ checkCast(" FAlsE" , false )
784
+ checkCast(" n" , false )
785
+ checkCast(" no" , false )
786
+ checkCast(" 0" , false )
787
+
788
+ checkEvaluation(cast(" abc" , BooleanType ), null )
789
+ checkEvaluation(cast(" " , BooleanType ), null )
790
+ }
772
791
}
0 commit comments