Skip to content

Commit 3a9eb01

Browse files
committed
fix
1 parent 4804ccd commit 3a9eb01

File tree

5 files changed

+21
-13
lines changed

5 files changed

+21
-13
lines changed

src/iceberg/json_internal.cc

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ Result<std::unique_ptr<PartitionField>> PartitionFieldFromJson(
533533
}
534534

535535
Result<std::unique_ptr<PartitionSpec>> PartitionSpecFromJson(
536-
const std::shared_ptr<Schema>& schema, const nlohmann::json& json) {
536+
const std::shared_ptr<Schema>& schema, const nlohmann::json& json,
537+
int32_t default_spec_id) {
537538
ICEBERG_ASSIGN_OR_RAISE(auto spec_id, GetJsonValue<int32_t>(json, kSpecId));
538539
ICEBERG_ASSIGN_OR_RAISE(auto fields, GetJsonValue<nlohmann::json>(json, kFields));
539540

@@ -542,9 +543,16 @@ Result<std::unique_ptr<PartitionSpec>> PartitionSpecFromJson(
542543
ICEBERG_ASSIGN_OR_RAISE(auto partition_field, PartitionFieldFromJson(field_json));
543544
partition_fields.push_back(std::move(*partition_field));
544545
}
545-
ICEBERG_ASSIGN_OR_RAISE(
546-
auto spec, PartitionSpec::Make(*schema, spec_id, std::move(partition_fields),
547-
true /*allow_missing_fields*/));
546+
547+
std::unique_ptr<PartitionSpec> spec;
548+
if (default_spec_id == spec_id) {
549+
ICEBERG_ASSIGN_OR_RAISE(
550+
spec, PartitionSpec::Make(*schema, spec_id, std::move(partition_fields),
551+
true /*allow_missing_fields*/));
552+
} else {
553+
ICEBERG_ASSIGN_OR_RAISE(spec,
554+
PartitionSpec::Make(spec_id, std::move(partition_fields)));
555+
}
548556
return spec;
549557
}
550558

@@ -886,8 +894,8 @@ Status ParsePartitionSpecs(const nlohmann::json& json, int8_t format_version,
886894
ICEBERG_ASSIGN_OR_RAISE(default_spec_id, GetJsonValue<int32_t>(json, kDefaultSpecId));
887895

888896
for (const auto& spec_json : spec_array) {
889-
ICEBERG_ASSIGN_OR_RAISE(auto spec,
890-
PartitionSpecFromJson(current_schema, spec_json));
897+
ICEBERG_ASSIGN_OR_RAISE(
898+
auto spec, PartitionSpecFromJson(current_schema, spec_json, default_spec_id));
891899
partition_specs.push_back(std::move(spec));
892900
}
893901
} else {

src/iceberg/json_internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,12 @@ ICEBERG_EXPORT Result<std::string> ToJsonString(const PartitionSpec& partition_s
176176
///
177177
/// \param schema The current schema.
178178
/// \param json The JSON object representing a `PartitionSpec`.
179+
/// \param default_spec_id The default spec ID from the table metadata.
179180
/// \return An `expected` value containing either a `PartitionSpec` object or an error. If
180181
/// the JSON is malformed or missing expected fields, an error will be returned.
181182
ICEBERG_EXPORT Result<std::unique_ptr<PartitionSpec>> PartitionSpecFromJson(
182-
const std::shared_ptr<Schema>& schema, const nlohmann::json& json);
183+
const std::shared_ptr<Schema>& schema, const nlohmann::json& json,
184+
int32_t default_spec_id);
183185

184186
/// \brief Serializes a `SnapshotRef` object to JSON.
185187
///

src/iceberg/test/json_internal_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ TEST(JsonInternalTest, PartitionSpec) {
168168

169169
EXPECT_EQ(json, expected_json);
170170

171-
auto parsed_spec_result = PartitionSpecFromJson(schema, json);
171+
auto parsed_spec_result = PartitionSpecFromJson(schema, json, 1);
172172
ASSERT_TRUE(parsed_spec_result.has_value()) << parsed_spec_result.error().message;
173173
EXPECT_EQ(*spec, *parsed_spec_result.value());
174174
}

src/iceberg/test/manifest_reader_writer_test.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,8 @@ TEST_F(ManifestV1Test, WritePartitionedTest) {
186186
auto identity_transform = Transform::Identity();
187187
std::vector<PartitionField> fields{
188188
PartitionField(1, 1000, "order_ts_hour", identity_transform)};
189-
auto partition_spec_result = PartitionSpec::Make(1, fields);
190-
ASSERT_TRUE(partition_spec_result.has_value());
191-
auto partition_spec =
192-
std::shared_ptr<PartitionSpec>(std::move(partition_spec_result.value()));
189+
ICEBERG_UNWRAP_OR_FAIL(std::shared_ptr<PartitionSpec> partition_spec,
190+
PartitionSpec::Make(*table_schema, 1, fields, false));
193191

194192
auto expected_entries = PreparePartitionedTestData();
195193
auto write_manifest_path = CreateNewTempFilePath();

src/iceberg/test/partition_spec_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ TEST(PartitionSpecTest, PartitionTypeTest) {
138138
std::vector<SchemaField>{field1, field2, field3, field4, field5, field6},
139139
Schema::kInitialSchemaId);
140140

141-
auto parsed_spec_result = PartitionSpecFromJson(schema, json);
141+
auto parsed_spec_result = PartitionSpecFromJson(schema, json, 1);
142142
ASSERT_TRUE(parsed_spec_result.has_value()) << parsed_spec_result.error().message;
143143

144144
auto partition_type = parsed_spec_result.value()->PartitionType(*schema);

0 commit comments

Comments
 (0)