From c24fd591ba6acd777c9972896c0c0f5f9acb3588 Mon Sep 17 00:00:00 2001 From: Zhuo Wang Date: Fri, 4 Dec 2026 12:43:34 +0800 Subject: [PATCH 1/2] fix: accept any JSON number for float and double in LiteralFromJson The spec defines float and double single values as JSON numbers, but LiteralFromJson only accepted floating-point literals, so whole-number literals such as 0 were rejected. Accept any JSON number, and reject values that overflow to infinity. --- src/iceberg/expression/json_serde.cc | 25 ++++++--- src/iceberg/test/expression_json_test.cc | 67 ++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/src/iceberg/expression/json_serde.cc b/src/iceberg/expression/json_serde.cc index c679d2c69e..55bba7fd11 100644 --- a/src/iceberg/expression/json_serde.cc +++ b/src/iceberg/expression/json_serde.cc @@ -17,6 +17,7 @@ * under the License. */ +#include #include #include #include @@ -368,17 +369,29 @@ Result LiteralFromJson(const nlohmann::json& json, const Type* type) { return Literal::Long(val); } - case TypeId::kFloat: - if (!json.is_number_float()) [[unlikely]] { + case TypeId::kFloat: { + if (!json.is_number()) [[unlikely]] { return JsonParseError("Cannot parse {} as a float value", SafeDumpJson(json)); } - return Literal::Float(json.get()); + auto val = json.get(); + if (!std::isfinite(val)) [[unlikely]] { + return JsonParseError("Cannot parse {} as a float value: out of range", + SafeDumpJson(json)); + } + return Literal::Float(val); + } - case TypeId::kDouble: - if (!json.is_number_float()) [[unlikely]] { + case TypeId::kDouble: { + if (!json.is_number()) [[unlikely]] { return JsonParseError("Cannot parse {} as a double value", SafeDumpJson(json)); } - return Literal::Double(json.get()); + auto val = json.get(); + if (!std::isfinite(val)) [[unlikely]] { + return JsonParseError("Cannot parse {} as a double value: out of range", + SafeDumpJson(json)); + } + return Literal::Double(val); + } case TypeId::kString: if (!json.is_string()) [[unlikely]] { diff --git a/src/iceberg/test/expression_json_test.cc b/src/iceberg/test/expression_json_test.cc index 1afecc716a..2a6f5b397f 100644 --- a/src/iceberg/test/expression_json_test.cc +++ b/src/iceberg/test/expression_json_test.cc @@ -540,6 +540,73 @@ TEST(LiteralFromJsonTest, AcceptsNegativeIntegerUntyped) { EXPECT_EQ(lit.ToString(), "-123"); } +TEST(LiteralFromJsonTest, FloatAndDoubleValues) { + struct Case { + std::string json; + Literal expected; + }; + const std::vector float_cases = { + {"0", Literal::Float(0.0f)}, + {"-1", Literal::Float(-1.0f)}, + {"2147483648", Literal::Float(2147483648.0f)}, + {"9223372036854775808", Literal::Float(9223372036854775808.0f)}, + {"16777217", Literal::Float(16777216.0f)}, + {"3.4028235e38", Literal::Float(std::numeric_limits::max())}, + }; + for (const auto& c : float_cases) { + SCOPED_TRACE(c.json); + ICEBERG_UNWRAP_OR_FAIL( + auto lit, LiteralFromJson(nlohmann::json::parse(c.json), float32().get())); + EXPECT_EQ(lit.type()->type_id(), TypeId::kFloat); + EXPECT_EQ(lit, c.expected); + } + + const std::vector double_cases = { + {"0", Literal::Double(0.0)}, + {"-1", Literal::Double(-1.0)}, + {"2147483648", Literal::Double(2147483648.0)}, + {"9223372036854775808", Literal::Double(9223372036854775808.0)}, + {"9007199254740993", Literal::Double(9007199254740992.0)}, + {"1" + std::string(39, '0'), Literal::Double(1e39)}, + }; + for (const auto& c : double_cases) { + SCOPED_TRACE(c.json); + ICEBERG_UNWRAP_OR_FAIL( + auto lit, LiteralFromJson(nlohmann::json::parse(c.json), float64().get())); + EXPECT_EQ(lit.type()->type_id(), TypeId::kDouble); + EXPECT_EQ(lit, c.expected); + } + + ICEBERG_UNWRAP_OR_FAIL(auto one, LiteralFromJson(nlohmann::json(1), float64().get())); + ICEBERG_UNWRAP_OR_FAIL(auto one_json, ToJson(one)); + EXPECT_EQ(one_json.dump(), "1.0"); +} + +TEST(LiteralFromJsonTest, RejectsInvalidFloatAndDouble) { + for (const auto& json : {nlohmann::json::parse("1e39"), nlohmann::json::parse("-1e39"), + nlohmann::json::parse("1" + std::string(39, '0')), + nlohmann::json(std::numeric_limits::infinity())}) { + SCOPED_TRACE(json.dump()); + EXPECT_THAT(LiteralFromJson(json, float32().get()), HasErrorMessage("out of range")); + } + for (const auto& json : {nlohmann::json(std::numeric_limits::infinity()), + nlohmann::json(-std::numeric_limits::infinity())}) { + SCOPED_TRACE(json.dump()); + EXPECT_THAT(LiteralFromJson(json, float64().get()), HasErrorMessage("out of range")); + } + for (const std::shared_ptr& type : + {std::shared_ptr(float32()), + std::shared_ptr(float64())}) { + for (const auto& json : + {nlohmann::json("1.5"), nlohmann::json("1"), nlohmann::json(true)}) { + SCOPED_TRACE(type->ToString() + " " + json.dump()); + EXPECT_THAT(LiteralFromJson(json, type.get()), + HasErrorMessage("Cannot parse " + json.dump() + " as a " + + type->ToString() + " value")); + } + } +} + struct SchemaAwarePredicateParam { std::string name; std::string field_name; From 6c1cff34a961c9741d2c1d7ad028e29702281d34 Mon Sep 17 00:00:00 2001 From: Zhuo Wang Date: Fri, 4 Dec 2026 13:56:57 +0800 Subject: [PATCH 2/2] test: use designated initializers in LiteralFromJson float tests --- src/iceberg/test/expression_json_test.cc | 25 ++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/iceberg/test/expression_json_test.cc b/src/iceberg/test/expression_json_test.cc index 2a6f5b397f..01c09f7f23 100644 --- a/src/iceberg/test/expression_json_test.cc +++ b/src/iceberg/test/expression_json_test.cc @@ -546,12 +546,13 @@ TEST(LiteralFromJsonTest, FloatAndDoubleValues) { Literal expected; }; const std::vector float_cases = { - {"0", Literal::Float(0.0f)}, - {"-1", Literal::Float(-1.0f)}, - {"2147483648", Literal::Float(2147483648.0f)}, - {"9223372036854775808", Literal::Float(9223372036854775808.0f)}, - {"16777217", Literal::Float(16777216.0f)}, - {"3.4028235e38", Literal::Float(std::numeric_limits::max())}, + {.json = "0", .expected = Literal::Float(0.0f)}, + {.json = "-1", .expected = Literal::Float(-1.0f)}, + {.json = "2147483648", .expected = Literal::Float(2147483648.0f)}, + {.json = "9223372036854775808", .expected = Literal::Float(9223372036854775808.0f)}, + {.json = "16777217", .expected = Literal::Float(16777216.0f)}, + {.json = "3.4028235e38", + .expected = Literal::Float(std::numeric_limits::max())}, }; for (const auto& c : float_cases) { SCOPED_TRACE(c.json); @@ -562,12 +563,12 @@ TEST(LiteralFromJsonTest, FloatAndDoubleValues) { } const std::vector double_cases = { - {"0", Literal::Double(0.0)}, - {"-1", Literal::Double(-1.0)}, - {"2147483648", Literal::Double(2147483648.0)}, - {"9223372036854775808", Literal::Double(9223372036854775808.0)}, - {"9007199254740993", Literal::Double(9007199254740992.0)}, - {"1" + std::string(39, '0'), Literal::Double(1e39)}, + {.json = "0", .expected = Literal::Double(0.0)}, + {.json = "-1", .expected = Literal::Double(-1.0)}, + {.json = "2147483648", .expected = Literal::Double(2147483648.0)}, + {.json = "9223372036854775808", .expected = Literal::Double(9223372036854775808.0)}, + {.json = "9007199254740993", .expected = Literal::Double(9007199254740992.0)}, + {.json = "1" + std::string(39, '0'), .expected = Literal::Double(1e39)}, }; for (const auto& c : double_cases) { SCOPED_TRACE(c.json);