Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions src/iceberg/json_serde.cc
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,10 @@ Result<std::unique_ptr<Type>> TypeFromJson(const nlohmann::json& json) {
StringUtils::ParseNumber<int32_t>(match[1].str()));
ICEBERG_ASSIGN_OR_RAISE(auto scale,
StringUtils::ParseNumber<int32_t>(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<DecimalType>(precision, scale);
}
return JsonParseError("Invalid decimal type: {}", type_name);
Expand Down
31 changes: 31 additions & 0 deletions src/iceberg/test/schema_json_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

#include <array>
#include <memory>
#include <string>

Expand Down Expand Up @@ -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<Case>({
{.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"));
Expand Down
Loading