This is an automated email from the ASF dual-hosted git repository.
wgtmac pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-cpp.git
The following commit(s) were added to refs/heads/main by this push:
new 2bf4a305 fix: return an error for decimal precision above 38 in
TypeFromJson (#1147)
2bf4a305 is described below
commit 2bf4a30585897c935d0a234001f415fd2f01df68
Author: wzhuo <[email protected]>
AuthorDate: Wed Nov 25 22:25:39 2026 +0800
fix: return an error for decimal precision above 38 in TypeFromJson (#1147)
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 13259f10..1ea14690 100644
--- a/src/iceberg/json_serde.cc
+++ b/src/iceberg/json_serde.cc
@@ -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);
diff --git a/src/iceberg/test/schema_json_test.cc
b/src/iceberg/test/schema_json_test.cc
index 48fce03b..ae8ba7f6 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 <array>
#include <memory>
#include <string>
@@ -158,6 +159,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"));