@@ -1393,25 +1393,19 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
1393
1393
"""
1394
1394
}
1395
1395
1396
- private [this ] def lowerAndUpperBound (
1397
- fractionType : String ,
1398
- integralType : String ): (String , String ) = {
1399
- assert(fractionType == " float" || fractionType == " double" )
1400
- val typeIndicator = fractionType.charAt(0 )
1401
- val (min, max) = integralType.toLowerCase(Locale .ROOT ) match {
1402
- case " long" => (Long .MinValue , Long .MaxValue )
1403
- case " int" => (Int .MinValue , Int .MaxValue )
1404
- case " short" => (Short .MinValue , Short .MaxValue )
1405
- case " byte" => (Byte .MinValue , Byte .MaxValue )
1396
+ private [this ] def lowerAndUpperBound (integralType : String ): (String , String ) = {
1397
+ val (min, max, typeIndicator) = integralType.toLowerCase(Locale .ROOT ) match {
1398
+ case " long" => (Long .MinValue , Long .MaxValue , " L" )
1399
+ case " int" => (Int .MinValue , Int .MaxValue , " " )
1400
+ case " short" => (Short .MinValue , Short .MaxValue , " " )
1401
+ case " byte" => (Byte .MinValue , Byte .MaxValue , " " )
1406
1402
}
1407
1403
(min.toString + typeIndicator, max.toString + typeIndicator)
1408
1404
}
1409
1405
1410
- private [this ] def castFractionToIntegralTypeCode (
1411
- fractionType : String ,
1412
- integralType : String ): CastFunction = {
1406
+ private [this ] def castFractionToIntegralTypeCode (integralType : String ): CastFunction = {
1413
1407
assert(ansiEnabled)
1414
- val (min, max) = lowerAndUpperBound(fractionType, integralType)
1408
+ val (min, max) = lowerAndUpperBound(integralType)
1415
1409
val mathClass = classOf [Math ].getName
1416
1410
// When casting floating values to integral types, Spark uses the method `Numeric.toInt`
1417
1411
// Or `Numeric.toLong` directly. For positive floating values, it is equivalent to `Math.floor`;
@@ -1449,12 +1443,10 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
1449
1443
(c, evPrim, evNull) => code " $evNull = true; "
1450
1444
case TimestampType => castTimestampToIntegralTypeCode(ctx, " byte" )
1451
1445
case DecimalType () => castDecimalToIntegralTypeCode(ctx, " byte" )
1452
- case _ : ShortType | _ : IntegerType | _ : LongType if ansiEnabled =>
1446
+ case ShortType | IntegerType | LongType if ansiEnabled =>
1453
1447
castIntegralTypeToIntegralTypeExactCode(" byte" )
1454
- case _ : FloatType if ansiEnabled =>
1455
- castFractionToIntegralTypeCode(" float" , " byte" )
1456
- case _ : DoubleType if ansiEnabled =>
1457
- castFractionToIntegralTypeCode(" double" , " byte" )
1448
+ case FloatType | DoubleType if ansiEnabled =>
1449
+ castFractionToIntegralTypeCode(" byte" )
1458
1450
case x : NumericType =>
1459
1451
(c, evPrim, evNull) => code " $evPrim = (byte) $c; "
1460
1452
}
@@ -1482,12 +1474,10 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
1482
1474
(c, evPrim, evNull) => code " $evNull = true; "
1483
1475
case TimestampType => castTimestampToIntegralTypeCode(ctx, " short" )
1484
1476
case DecimalType () => castDecimalToIntegralTypeCode(ctx, " short" )
1485
- case _ : IntegerType | _ : LongType if ansiEnabled =>
1477
+ case IntegerType | LongType if ansiEnabled =>
1486
1478
castIntegralTypeToIntegralTypeExactCode(" short" )
1487
- case _ : FloatType if ansiEnabled =>
1488
- castFractionToIntegralTypeCode(" float" , " short" )
1489
- case _ : DoubleType if ansiEnabled =>
1490
- castFractionToIntegralTypeCode(" double" , " short" )
1479
+ case FloatType | DoubleType if ansiEnabled =>
1480
+ castFractionToIntegralTypeCode(" short" )
1491
1481
case x : NumericType =>
1492
1482
(c, evPrim, evNull) => code " $evPrim = (short) $c; "
1493
1483
}
@@ -1513,11 +1503,9 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
1513
1503
(c, evPrim, evNull) => code " $evNull = true; "
1514
1504
case TimestampType => castTimestampToIntegralTypeCode(ctx, " int" )
1515
1505
case DecimalType () => castDecimalToIntegralTypeCode(ctx, " int" )
1516
- case _ : LongType if ansiEnabled => castIntegralTypeToIntegralTypeExactCode(" int" )
1517
- case _ : FloatType if ansiEnabled =>
1518
- castFractionToIntegralTypeCode(" float" , " int" )
1519
- case _ : DoubleType if ansiEnabled =>
1520
- castFractionToIntegralTypeCode(" double" , " int" )
1506
+ case LongType if ansiEnabled => castIntegralTypeToIntegralTypeExactCode(" int" )
1507
+ case FloatType | DoubleType if ansiEnabled =>
1508
+ castFractionToIntegralTypeCode(" int" )
1521
1509
case x : NumericType =>
1522
1510
(c, evPrim, evNull) => code " $evPrim = (int) $c; "
1523
1511
}
@@ -1544,10 +1532,8 @@ abstract class CastBase extends UnaryExpression with TimeZoneAwareExpression wit
1544
1532
case TimestampType =>
1545
1533
(c, evPrim, evNull) => code " $evPrim = (long) ${timestampToLongCode(c)}; "
1546
1534
case DecimalType () => castDecimalToIntegralTypeCode(ctx, " long" )
1547
- case _ : FloatType if ansiEnabled =>
1548
- castFractionToIntegralTypeCode(" float" , " long" )
1549
- case _ : DoubleType if ansiEnabled =>
1550
- castFractionToIntegralTypeCode(" double" , " long" )
1535
+ case FloatType | DoubleType if ansiEnabled =>
1536
+ castFractionToIntegralTypeCode(" long" )
1551
1537
case x : NumericType =>
1552
1538
(c, evPrim, evNull) => code " $evPrim = (long) $c; "
1553
1539
}
0 commit comments