diff --git a/src/iceberg/json_serde.cc b/src/iceberg/json_serde.cc index 37f6c08c9..eaeb50f39 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 5067191bf..c3dbbc5c2 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"));