diff options
author | Josh Rosen <joshrosen@databricks.com> | 2016-08-26 00:15:01 +0200 |
---|---|---|
committer | Herman van Hovell <hvanhovell@databricks.com> | 2016-08-26 00:15:01 +0200 |
commit | 3e4c7db4d11c474457e7886a5501108ebab0cf6d (patch) | |
tree | a2fa523867b5b90a47b7a03b6d55da5f9fc1c8dc /sql/catalyst/src/main | |
parent | a133057ce5817f834babe9f25023092aec3c321d (diff) | |
download | spark-3e4c7db4d11c474457e7886a5501108ebab0cf6d.tar.gz spark-3e4c7db4d11c474457e7886a5501108ebab0cf6d.tar.bz2 spark-3e4c7db4d11c474457e7886a5501108ebab0cf6d.zip |
[SPARK-17205] Literal.sql should handle Infinity and NaN
This patch updates `Literal.sql` to properly generate SQL for `NaN` and `Infinity` float and double literals: these special values need to be handled differently from regular values, since simply appending a suffix to the value's `toString()` representation will not work for these values.
Author: Josh Rosen <joshrosen@databricks.com>
Closes #14777 from JoshRosen/SPARK-17205.
Diffstat (limited to 'sql/catalyst/src/main')
-rw-r--r-- | sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala index 55fd9c0834..730a7f62e0 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala @@ -251,8 +251,21 @@ case class Literal (value: Any, dataType: DataType) extends LeafExpression with case (v: Short, ShortType) => v + "S" case (v: Long, LongType) => v + "L" // Float type doesn't have a suffix - case (v: Float, FloatType) => s"CAST($v AS ${FloatType.sql})" - case (v: Double, DoubleType) => v + "D" + case (v: Float, FloatType) => + val castedValue = v match { + case _ if v.isNaN => "'NaN'" + case Float.PositiveInfinity => "'Infinity'" + case Float.NegativeInfinity => "'-Infinity'" + case _ => v + } + s"CAST($castedValue AS ${FloatType.sql})" + case (v: Double, DoubleType) => + v match { + case _ if v.isNaN => s"CAST('NaN' AS ${DoubleType.sql})" + case Double.PositiveInfinity => s"CAST('Infinity' AS ${DoubleType.sql})" + case Double.NegativeInfinity => s"CAST('-Infinity' AS ${DoubleType.sql})" + case _ => v + "D" + } case (v: Decimal, t: DecimalType) => s"CAST($v AS ${t.sql})" case (v: Int, DateType) => s"DATE '${DateTimeUtils.toJavaDate(v)}'" case (v: Long, TimestampType) => s"TIMESTAMP('${DateTimeUtils.toJavaTimestamp(v)}')" |