@@ -155,7 +155,7 @@ case class Cast(child: Expression, dataType: DataType)
155155 case ByteType =>
156156 buildCast[Byte ](_, _ != 0 )
157157 case DecimalType () =>
158- buildCast[Decimal ](_, _ != Decimal . ZERO )
158+ buildCast[Decimal ](_, ! _.isZero )
159159 case DoubleType =>
160160 buildCast[Double ](_, _ != 0 )
161161 case FloatType =>
@@ -315,13 +315,13 @@ case class Cast(child: Expression, dataType: DataType)
315315 case TimestampType =>
316316 // Note that we lose precision here.
317317 buildCast[Long ](_, t => changePrecision(Decimal (timestampToDouble(t)), target))
318- case DecimalType () =>
318+ case dt : DecimalType =>
319319 b => changePrecision(b.asInstanceOf [Decimal ].clone(), target)
320- case LongType =>
321- b => changePrecision(Decimal (b. asInstanceOf [Long ] ), target)
322- case x : NumericType => // All other numeric types can be represented precisely as Doubles
320+ case t : IntegralType =>
321+ b => changePrecision(Decimal (t.integral. asInstanceOf [Integral [ Any ]].toLong(b) ), target)
322+ case x : FractionalType =>
323323 b => try {
324- changePrecision(Decimal (x.numeric .asInstanceOf [Numeric [Any ]].toDouble(b)), target)
324+ changePrecision(Decimal (x.fractional .asInstanceOf [Fractional [Any ]].toDouble(b)), target)
325325 } catch {
326326 case _ : NumberFormatException => null
327327 }
@@ -534,10 +534,7 @@ case class Cast(child: Expression, dataType: DataType)
534534 (c, evPrim, evNull) =>
535535 s """
536536 try {
537- org.apache.spark.sql.types.Decimal tmpDecimal =
538- new org.apache.spark.sql.types.Decimal().set(
539- new scala.math.BigDecimal(
540- new java.math.BigDecimal( $c.toString())));
537+ Decimal tmpDecimal = Decimal.apply(new java.math.BigDecimal( $c.toString()));
541538 ${changePrecision(" tmpDecimal" , target, evPrim, evNull)}
542539 } catch (java.lang.NumberFormatException e) {
543540 $evNull = true;
@@ -546,12 +543,7 @@ case class Cast(child: Expression, dataType: DataType)
546543 case BooleanType =>
547544 (c, evPrim, evNull) =>
548545 s """
549- org.apache.spark.sql.types.Decimal tmpDecimal = null;
550- if ( $c) {
551- tmpDecimal = new org.apache.spark.sql.types.Decimal().set(1);
552- } else {
553- tmpDecimal = new org.apache.spark.sql.types.Decimal().set(0);
554- }
546+ Decimal tmpDecimal = $c ? Decimal.apply(1) : Decimal.apply(0);
555547 ${changePrecision(" tmpDecimal" , target, evPrim, evNull)}
556548 """
557549 case DateType =>
@@ -561,32 +553,28 @@ case class Cast(child: Expression, dataType: DataType)
561553 // Note that we lose precision here.
562554 (c, evPrim, evNull) =>
563555 s """
564- org.apache.spark.sql.types.Decimal tmpDecimal =
565- new org.apache.spark.sql.types.Decimal().set(
566- scala.math.BigDecimal.valueOf( ${timestampToDoubleCode(c)}));
556+ Decimal tmpDecimal = Decimal.apply(
557+ scala.math.BigDecimal.valueOf( ${timestampToDoubleCode(c)}));
567558 ${changePrecision(" tmpDecimal" , target, evPrim, evNull)}
568559 """
569560 case DecimalType () =>
570561 (c, evPrim, evNull) =>
571562 s """
572- org.apache.spark.sql.types. Decimal tmpDecimal = $c.clone();
563+ Decimal tmpDecimal = $c.clone();
573564 ${changePrecision(" tmpDecimal" , target, evPrim, evNull)}
574565 """
575- case LongType =>
566+ case x : IntegralType =>
576567 (c, evPrim, evNull) =>
577568 s """
578- org.apache.spark.sql.types.Decimal tmpDecimal =
579- new org.apache.spark.sql.types.Decimal().set( $c);
569+ Decimal tmpDecimal = Decimal.apply((long) $c);
580570 ${changePrecision(" tmpDecimal" , target, evPrim, evNull)}
581571 """
582- case x : NumericType =>
572+ case x : FractionalType =>
583573 // All other numeric types can be represented precisely as Doubles
584574 (c, evPrim, evNull) =>
585575 s """
586576 try {
587- org.apache.spark.sql.types.Decimal tmpDecimal =
588- new org.apache.spark.sql.types.Decimal().set(
589- scala.math.BigDecimal.valueOf((double) $c));
577+ Decimal tmpDecimal = Decimal.apply(scala.math.BigDecimal.valueOf((double) $c));
590578 ${changePrecision(" tmpDecimal" , target, evPrim, evNull)}
591579 } catch (java.lang.NumberFormatException e) {
592580 $evNull = true;
0 commit comments