Skip to content

Commit 3e4c7db

Browse files
JoshRosenhvanhovell
authored andcommitted
[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 <[email protected]> Closes #14777 from JoshRosen/SPARK-17205.
1 parent a133057 commit 3e4c7db

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,21 @@ case class Literal (value: Any, dataType: DataType) extends LeafExpression with
251251
case (v: Short, ShortType) => v + "S"
252252
case (v: Long, LongType) => v + "L"
253253
// Float type doesn't have a suffix
254-
case (v: Float, FloatType) => s"CAST($v AS ${FloatType.sql})"
255-
case (v: Double, DoubleType) => v + "D"
254+
case (v: Float, FloatType) =>
255+
val castedValue = v match {
256+
case _ if v.isNaN => "'NaN'"
257+
case Float.PositiveInfinity => "'Infinity'"
258+
case Float.NegativeInfinity => "'-Infinity'"
259+
case _ => v
260+
}
261+
s"CAST($castedValue AS ${FloatType.sql})"
262+
case (v: Double, DoubleType) =>
263+
v match {
264+
case _ if v.isNaN => s"CAST('NaN' AS ${DoubleType.sql})"
265+
case Double.PositiveInfinity => s"CAST('Infinity' AS ${DoubleType.sql})"
266+
case Double.NegativeInfinity => s"CAST('-Infinity' AS ${DoubleType.sql})"
267+
case _ => v + "D"
268+
}
256269
case (v: Decimal, t: DecimalType) => s"CAST($v AS ${t.sql})"
257270
case (v: Int, DateType) => s"DATE '${DateTimeUtils.toJavaDate(v)}'"
258271
case (v: Long, TimestampType) => s"TIMESTAMP('${DateTimeUtils.toJavaTimestamp(v)}')"

sql/hive/src/test/scala/org/apache/spark/sql/catalyst/ExpressionSQLBuilderSuite.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ class ExpressionSQLBuilderSuite extends SQLBuilderTest {
3232
checkSQL(Literal(4: Int), "4")
3333
checkSQL(Literal(8: Long), "8L")
3434
checkSQL(Literal(1.5F), "CAST(1.5 AS FLOAT)")
35+
checkSQL(Literal(Float.PositiveInfinity), "CAST('Infinity' AS FLOAT)")
36+
checkSQL(Literal(Float.NegativeInfinity), "CAST('-Infinity' AS FLOAT)")
37+
checkSQL(Literal(Float.NaN), "CAST('NaN' AS FLOAT)")
3538
checkSQL(Literal(2.5D), "2.5D")
39+
checkSQL(Literal(Double.PositiveInfinity), "CAST('Infinity' AS DOUBLE)")
40+
checkSQL(Literal(Double.NegativeInfinity), "CAST('-Infinity' AS DOUBLE)")
41+
checkSQL(Literal(Double.NaN), "CAST('NaN' AS DOUBLE)")
3642
checkSQL(
3743
Literal(Timestamp.valueOf("2016-01-01 00:00:00")), "TIMESTAMP('2016-01-01 00:00:00.0')")
3844
// TODO tests for decimals

0 commit comments

Comments
 (0)