You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by we...@apache.org on 2018/09/07 02:12:31 UTC
spark git commit: [SPARK-25208][SQL][FOLLOW-UP] Reduce code size.
Repository: spark
Updated Branches:
refs/heads/master da6fa3828 -> 1b1711e05
[SPARK-25208][SQL][FOLLOW-UP] Reduce code size.
## What changes were proposed in this pull request?
This is a follow-up pr of #22200.
When casting to decimal type, if `Cast.canNullSafeCastToDecimal()`, overflow won't happen, so we don't need to check the result of `Decimal.changePrecision()`.
## How was this patch tested?
Existing tests.
Closes #22352 from ueshin/issues/SPARK-25208/reduce_code_size.
Authored-by: Takuya UESHIN <ue...@databricks.com>
Signed-off-by: Wenchen Fan <we...@databricks.com>
Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/1b1711e0
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/1b1711e0
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/1b1711e0
Branch: refs/heads/master
Commit: 1b1711e0532b1a1521054ef3b5980cdb3d70cdeb
Parents: da6fa38
Author: Takuya UESHIN <ue...@databricks.com>
Authored: Fri Sep 7 10:12:20 2018 +0800
Committer: Wenchen Fan <we...@databricks.com>
Committed: Fri Sep 7 10:12:20 2018 +0800
----------------------------------------------------------------------
.../spark/sql/catalyst/expressions/Cast.scala | 37 ++++++++++++--------
1 file changed, 23 insertions(+), 14 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/spark/blob/1b1711e0/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
----------------------------------------------------------------------
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
index 0053503..8f77799 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala
@@ -924,27 +924,36 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
}
private[this] def changePrecision(d: ExprValue, decimalType: DecimalType,
- evPrim: ExprValue, evNull: ExprValue): Block =
- code"""
- if ($d.changePrecision(${decimalType.precision}, ${decimalType.scale})) {
- $evPrim = $d;
- } else {
- $evNull = true;
- }
- """
+ evPrim: ExprValue, evNull: ExprValue, canNullSafeCast: Boolean): Block = {
+ if (canNullSafeCast) {
+ code"""
+ |$d.changePrecision(${decimalType.precision}, ${decimalType.scale});
+ |$evPrim = $d;
+ """.stripMargin
+ } else {
+ code"""
+ |if ($d.changePrecision(${decimalType.precision}, ${decimalType.scale})) {
+ | $evPrim = $d;
+ |} else {
+ | $evNull = true;
+ |}
+ """.stripMargin
+ }
+ }
private[this] def castToDecimalCode(
from: DataType,
target: DecimalType,
ctx: CodegenContext): CastFunction = {
val tmp = ctx.freshVariable("tmpDecimal", classOf[Decimal])
+ val canNullSafeCast = Cast.canNullSafeCastToDecimal(from, target)
from match {
case StringType =>
(c, evPrim, evNull) =>
code"""
try {
Decimal $tmp = Decimal.apply(new java.math.BigDecimal($c.toString()));
- ${changePrecision(tmp, target, evPrim, evNull)}
+ ${changePrecision(tmp, target, evPrim, evNull, canNullSafeCast)}
} catch (java.lang.NumberFormatException e) {
$evNull = true;
}
@@ -953,7 +962,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
(c, evPrim, evNull) =>
code"""
Decimal $tmp = $c ? Decimal.apply(1) : Decimal.apply(0);
- ${changePrecision(tmp, target, evPrim, evNull)}
+ ${changePrecision(tmp, target, evPrim, evNull, canNullSafeCast)}
"""
case DateType =>
// date can't cast to decimal in Hive
@@ -964,19 +973,19 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
code"""
Decimal $tmp = Decimal.apply(
scala.math.BigDecimal.valueOf(${timestampToDoubleCode(c)}));
- ${changePrecision(tmp, target, evPrim, evNull)}
+ ${changePrecision(tmp, target, evPrim, evNull, canNullSafeCast)}
"""
case DecimalType() =>
(c, evPrim, evNull) =>
code"""
Decimal $tmp = $c.clone();
- ${changePrecision(tmp, target, evPrim, evNull)}
+ ${changePrecision(tmp, target, evPrim, evNull, canNullSafeCast)}
"""
case x: IntegralType =>
(c, evPrim, evNull) =>
code"""
Decimal $tmp = Decimal.apply((long) $c);
- ${changePrecision(tmp, target, evPrim, evNull)}
+ ${changePrecision(tmp, target, evPrim, evNull, canNullSafeCast)}
"""
case x: FractionalType =>
// All other numeric types can be represented precisely as Doubles
@@ -984,7 +993,7 @@ case class Cast(child: Expression, dataType: DataType, timeZoneId: Option[String
code"""
try {
Decimal $tmp = Decimal.apply(scala.math.BigDecimal.valueOf((double) $c));
- ${changePrecision(tmp, target, evPrim, evNull)}
+ ${changePrecision(tmp, target, evPrim, evNull, canNullSafeCast)}
} catch (java.lang.NumberFormatException e) {
$evNull = true;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org