Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[SPARK-18772][SQL] NaN/Infinite float parsing in JSON is inconsistent
  • Loading branch information
Nathan Howell authored and HyukjinKwon committed May 12, 2017
commit f83eec7a4c8fce223c5fd28b411fe2d1ae1da8dd
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ class JacksonParser(
}
}

private object SpecialDouble {
def unapply(value: String): Option[Double] = {
value.toLowerCase match {
case "nan" => Some(Double.NaN)
case "infinity" | "+infinity" | "inf" | "+inf" => Some(Double.PositiveInfinity)
case "-infinity" | "-inf" => Some(Double.NegativeInfinity)
case _ => None
}
}
}

/**
* Create a converter which converts the JSON documents held by the `JsonParser`
* to a value according to a desired schema.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,37 @@ class JsonSuite extends QueryTest with SharedSQLContext with TestJsonData {
.collect
}.getMessage
assert(errMsg.startsWith("The field for corrupt records must be string type and nullable"))

test("SPARK-18772: Special floats") {
val records = sparkContext
.parallelize(
"""{"a": "NaN"}""" ::
"""{"a": "nAn"}""" ::
"""{"a": "-iNf"}""" ::
"""{"a": "inF"}""" ::
"""{"a": "+Inf"}""" ::
"""{"a": "-iNfInity"}""" ::
"""{"a": "InFiNiTy"}""" ::
"""{"a": "+InfiNitY"}""" ::
"""{"a": "+Infi"}""" ::
Nil)

for (dt <- Seq(FloatType, DoubleType)) {
val res = spark.read
.schema(StructType(Seq(StructField("a", dt))))
.json(records)
.select($"a".cast(DoubleType).as[java.lang.Double])
.collect()
assert(res.length === 9)
assert(res(0).isNaN)
assert(res(1).isNaN)
assert(res(2).toDouble.isNegInfinity)
assert(res(3).toDouble.isPosInfinity)
assert(res(4).toDouble.isPosInfinity)
assert(res(5).toDouble.isNegInfinity)
assert(res(6).toDouble.isPosInfinity)
assert(res(7).toDouble.isPosInfinity)
assert(res(8) eq null)
}
}
}