Skip to content

Commit bdfa1bf

Browse files
Krishna Paifacebook-github-bot
authored andcommitted
fix(json): Fix casting of NaN's in json (facebookincubator#12825)
Summary: We use folly::tryTo to cast json strings . This is quite permissive for certain strings such as "nan"'s etc which are allowed in folly but not in presto java. Differential Revision: D71936263
1 parent 44f0f20 commit bdfa1bf

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

velox/functions/prestosql/tests/JsonCastTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,6 +1034,18 @@ TEST_F(JsonCastTest, toDouble) {
10341034
DOUBLE(),
10351035
{"NaN"_sv},
10361036
"The JSON document has an improper structure");
1037+
1038+
testThrow<JsonNativeType>(
1039+
JSON(),
1040+
REAL(),
1041+
{"\"nan\""_sv},
1042+
"The JSON element does not have the requested type");
1043+
1044+
testThrow<JsonNativeType>(
1045+
JSON(),
1046+
DOUBLE(),
1047+
{"\"nan\""_sv},
1048+
"The JSON element does not have the requested type");
10371049
}
10381050

10391051
TEST_F(JsonCastTest, toBoolean) {

velox/functions/prestosql/types/JsonCastOperator.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,16 @@ simdjson::simdjson_result<T> fromString(const std::string_view& s) {
593593
if (result.hasError()) {
594594
return simdjson::INCORRECT_TYPE;
595595
}
596+
597+
if constexpr (std::is_floating_point_v<T>) {
598+
// Only "NaN" is allowed to be converted to NaN. "nan" is not allowed.
599+
if (FOLLY_UNLIKELY(std::isnan(*result))) {
600+
if (s != "NaN" && s != "-NaN") {
601+
return simdjson::INCORRECT_TYPE;
602+
}
603+
}
604+
}
605+
596606
return std::move(*result);
597607
}
598608

0 commit comments

Comments
 (0)