From e21682361c69b010af1c06b60323146970baa43b Mon Sep 17 00:00:00 2001 From: Zhuo Wang Date: Thu, 19 Nov 2026 11:49:53 +0800 Subject: [PATCH] fix: return an error for decimal precision above 38 in TypeFromJson TypeFromJson passed the parsed precision straight to the DecimalType constructor, whose ICEBERG_CHECK_OR_DIE throws IcebergError when the precision exceeds 38. A type string such as "decimal(39,0)", or a schema or table metadata containing one, threw instead of returning an error. Check the precision against DecimalType::kMaxPrecision before constructing the type and return a JsonParseError. Closes #1143. --- src/iceberg/json_serde.cc | 4 ++++ src/iceberg/test/schema_json_test.cc | 31 ++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/iceberg/json_serde.cc b/src/iceberg/json_serde.cc index 37f6c08c94..eaeb50f39d 100644 --- a/src/iceberg/json_serde.cc +++ b/src/iceberg/json_serde.cc @@ -855,6 +855,10 @@ Result> TypeFromJson(const nlohmann::json& json) { StringUtils::ParseNumber(match[1].str())); ICEBERG_ASSIGN_OR_RAISE(auto scale, StringUtils::ParseNumber(match[2].str())); + if (precision > DecimalType::kMaxPrecision) { + return JsonParseError("Invalid decimal type: {}, precision must be at most {}", + type_name, DecimalType::kMaxPrecision); + } return std::make_unique(precision, scale); } return JsonParseError("Invalid decimal type: {}", type_name); diff --git a/src/iceberg/test/schema_json_test.cc b/src/iceberg/test/schema_json_test.cc index 5067191bf9..c3dbbc5c20 100644 --- a/src/iceberg/test/schema_json_test.cc +++ b/src/iceberg/test/schema_json_test.cc @@ -17,6 +17,7 @@ * under the License. */ +#include #include #include @@ -150,6 +151,36 @@ TEST(TypeJsonTest, FromJsonAcceptsBareGeospatialTypes) { ASSERT_EQ(*geography_type, *iceberg::geography()); } +// v1 and v2 share the same set of types. +TEST(TypeJsonTest, InvalidV2Types) { + struct Case { + std::string_view json; + std::string_view error; + }; + constexpr auto kCases = std::to_array({ + {.json = R"json("decimal(39, 0)")json", .error = "precision must be at most 38"}, + {.json = R"json("decimal(10)")json", .error = "Invalid decimal type"}, + {.json = R"json("decimal(a, b)")json", .error = "Invalid decimal type"}, + {.json = R"json("fixed[]")json", .error = "Invalid fixed type"}, + {.json = R"json("fixed[-1]")json", .error = "Invalid fixed type"}, + {.json = R"json("fixed(16)")json", .error = "Invalid fixed type"}, + {.json = R"json("integer")json", .error = "Cannot parse type string"}, + {.json = R"json({"type":"struct"})json", .error = "Missing 'fields'"}, + {.json = R"json({"type":"list","element":"int","element-required":true})json", + .error = "Missing 'element-id'"}, + {.json = + R"json({"type":"map","key":"string","value":"int","value-id":2,"value-required":true})json", + .error = "Missing 'key-id'"}, + {.json = R"json({"type":"set"})json", .error = "Unknown complex type"}, + }); + for (const auto& c : kCases) { + SCOPED_TRACE(c.json); + auto result = TypeFromJson(nlohmann::json::parse(c.json)); + EXPECT_THAT(result, IsError(ErrorKind::kJsonParseError)); + EXPECT_THAT(result, HasErrorMessage(std::string(c.error))); + } +} + TEST(TypeJsonTest, InvalidV3Types) { auto invalid_geometry = TypeFromJson(nlohmann::json::parse("\"geometry()\"")); ASSERT_THAT(invalid_geometry, HasErrorMessage("Invalid geometry type"));