You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by rx...@apache.org on 2015/07/09 09:26:29 UTC
spark git commit: [SPARK-8942][SQL] use double not decimal when cast
double and float to timestamp
Repository: spark
Updated Branches:
refs/heads/master 851e247ca -> 09cb0d9c2
[SPARK-8942][SQL] use double not decimal when cast double and float to timestamp
Author: Wenchen Fan <cl...@outlook.com>
Closes #7312 from cloud-fan/minor and squashes the following commits:
a4589fa [Wenchen Fan] use double not decimal when cast double and float to timestamp
Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/09cb0d9c
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/09cb0d9c
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/09cb0d9c
Branch: refs/heads/master
Commit: 09cb0d9c2dcb83818ced22ff9bd6a51688ea7ffe
Parents: 851e247
Author: Wenchen Fan <cl...@outlook.com>
Authored: Thu Jul 9 00:26:25 2015 -0700
Committer: Reynold Xin <rx...@databricks.com>
Committed: Thu Jul 9 00:26:25 2015 -0700
----------------------------------------------------------------------
.../spark/sql/catalyst/expressions/Cast.scala | 18 ++++++------------
1 file changed, 6 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/spark/blob/09cb0d9c/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 567feca..7f2383d 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
@@ -192,23 +192,18 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w
buildCast[Decimal](_, d => decimalToTimestamp(d))
// TimestampWritable.doubleToTimestamp
case DoubleType =>
- buildCast[Double](_, d => try {
- decimalToTimestamp(Decimal(d))
- } catch {
- case _: NumberFormatException => null
- })
+ buildCast[Double](_, d => doubleToTimestamp(d))
// TimestampWritable.floatToTimestamp
case FloatType =>
- buildCast[Float](_, f => try {
- decimalToTimestamp(Decimal(f))
- } catch {
- case _: NumberFormatException => null
- })
+ buildCast[Float](_, f => doubleToTimestamp(f.toDouble))
}
private[this] def decimalToTimestamp(d: Decimal): Long = {
(d.toBigDecimal * 1000000L).longValue()
}
+ private[this] def doubleToTimestamp(d: Double): Any = {
+ if (d.isNaN || d.isInfinite) null else (d * 1000000L).toLong
+ }
// converting milliseconds to us
private[this] def longToTimestamp(t: Long): Long = t * 1000L
@@ -396,8 +391,7 @@ case class Cast(child: Expression, dataType: DataType) extends UnaryExpression w
buildCast[InternalRow](_, row => {
var i = 0
while (i < row.length) {
- val v = row(i)
- newRow.update(i, if (v == null) null else casts(i)(v))
+ newRow.update(i, if (row.isNullAt(i)) null else casts(i)(row(i)))
i += 1
}
newRow.copy()
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org