From 4a9e2feb7d5498a48299e0c134e6faaa1bbaa9af Mon Sep 17 00:00:00 2001 From: Zhuo Wang Date: Thu, 19 Nov 2026 11:48:38 +0800 Subject: [PATCH] fix: write decimal types in the spec's canonical "decimal(P, S)" form Appendix C of the spec serializes a decimal type as "decimal(

, )", with "decimal(9, 2)" as its example, since apache/iceberg#18145. ToJson wrote "decimal(9,2)". DecimalType::ToString and Java already write the spaced form. TypeFromJson accepts both spellings, so existing metadata still reads. Closes #1142. --- src/iceberg/json_serde.cc | 2 +- src/iceberg/test/schema_json_test.cc | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/iceberg/json_serde.cc b/src/iceberg/json_serde.cc index 37f6c08c94..63dee8311c 100644 --- a/src/iceberg/json_serde.cc +++ b/src/iceberg/json_serde.cc @@ -663,7 +663,7 @@ Result ToJson(const Type& type) { return "double"; case TypeId::kDecimal: { const auto& decimal_type = internal::checked_cast(type); - return std::format("decimal({},{})", decimal_type.precision(), + return std::format("decimal({}, {})", decimal_type.precision(), decimal_type.scale()); } case TypeId::kDate: diff --git a/src/iceberg/test/schema_json_test.cc b/src/iceberg/test/schema_json_test.cc index 5067191bf9..3118545412 100644 --- a/src/iceberg/test/schema_json_test.cc +++ b/src/iceberg/test/schema_json_test.cc @@ -78,7 +78,7 @@ INSTANTIATE_TEST_SUITE_P( SchemaJsonParam{.json = "\"geography(srid:4326, karney)\"", .type = iceberg::geography("srid:4326", EdgeAlgorithm::kKarney)}, SchemaJsonParam{.json = "\"fixed[8]\"", .type = iceberg::fixed(8)}, - SchemaJsonParam{.json = "\"decimal(10,2)\"", .type = iceberg::decimal(10, 2)}, + SchemaJsonParam{.json = "\"decimal(10, 2)\"", .type = iceberg::decimal(10, 2)}, SchemaJsonParam{.json = "\"date\"", .type = iceberg::date()}, SchemaJsonParam{.json = "\"time\"", .type = iceberg::time()}, SchemaJsonParam{.json = "\"timestamp\"", .type = iceberg::timestamp()}, @@ -123,6 +123,15 @@ TEST(TypeJsonTest, FromJsonWithSpaces) { ASSERT_EQ(decimal->scale(), 2); } +TEST(TypeJsonTest, DecimalWithoutSpaceWritesCanonicalForm) { + // Older writers omit the space; the spec's canonical form has it. + ICEBERG_UNWRAP_OR_FAIL(auto type, + TypeFromJson(nlohmann::json::parse("\"decimal(10,2)\""))); + ASSERT_EQ(*type, *iceberg::decimal(10, 2)); + ICEBERG_UNWRAP_OR_FAIL(auto json, ToJson(*type)); + ASSERT_EQ(json, "decimal(10, 2)"); +} + TEST(TypeJsonTest, FromJsonV3TypesWithSpacesAndCase) { auto variant_result = TypeFromJson(nlohmann::json::parse("\"Variant\"")); ASSERT_TRUE(variant_result.has_value());