Skip to content

Commit b4edfef

Browse files
author
chendingchao.cdc
committed
improve code
Signed-off-by: chendingchao.cdc <[email protected]>
1 parent d1712a3 commit b4edfef

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

src/iceberg/json_internal.cc

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -668,13 +668,12 @@ nlohmann::json ToJson(const PartitionSpec& partition_spec) {
668668
}
669669

670670
Result<std::unique_ptr<PartitionField>> PartitionFieldFromJson(
671-
const nlohmann::json& json, bool allow_field_id_missing) {
671+
const nlohmann::json& json, std::optional<int32_t> partition_field_id) {
672672
ICEBERG_ASSIGN_OR_RAISE(auto source_id, GetJsonValue<int32_t>(json, kSourceId));
673673
int32_t field_id;
674-
if (allow_field_id_missing) {
675-
// Partition field id in v1 is not tracked, so we use -1 to indicate that.
676-
ICEBERG_ASSIGN_OR_RAISE(field_id, GetJsonValueOrDefault<int32_t>(
677-
json, kFieldId, SchemaField::kInvalidFieldId));
674+
if (partition_field_id.has_value()) {
675+
// Partition field id in v1 is not tracked, so we use partition_field_id directly.
676+
field_id = partition_field_id.value();
678677
} else {
679678
ICEBERG_ASSIGN_OR_RAISE(field_id, GetJsonValue<int32_t>(json, kFieldId));
680679
}
@@ -1053,14 +1052,10 @@ Status ParsePartitionSpecs(const nlohmann::json& json, int8_t format_version,
10531052
int32_t next_partition_field_id = PartitionSpec::kLegacyPartitionDataIdStart;
10541053
std::vector<PartitionField> fields;
10551054
for (const auto& entry_json : partition_spec_json) {
1056-
ICEBERG_ASSIGN_OR_RAISE(auto field, PartitionFieldFromJson(entry_json, true));
1057-
int32_t field_id = field->field_id();
1058-
if (field_id == SchemaField::kInvalidFieldId) {
1059-
// If the field ID is not set, we need to assign a new one
1060-
field_id = next_partition_field_id++;
1061-
}
1062-
fields.emplace_back(field->source_id(), field_id, std::string(field->name()),
1063-
std::move(field->transform()));
1055+
ICEBERG_ASSIGN_OR_RAISE(
1056+
auto field, PartitionFieldFromJson(entry_json, next_partition_field_id));
1057+
next_partition_field_id++;
1058+
fields.push_back(std::move(*field));
10641059
}
10651060

10661061
auto spec = std::make_unique<PartitionSpec>(

src/iceberg/json_internal.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,13 @@ nlohmann::json ToJson(const PartitionField& partition_field);
126126
/// and name.
127127
///
128128
/// \param json The JSON object representing a `PartitionField`.
129-
/// \param allow_field_id_missing Whether the field ID is allowed to be missing. This can
130-
/// happen when deserializing partition fields from V1 metadata files.
129+
/// \param partition_field_id If partition_field_id has value, we assign partition fields'
130+
/// id from partition_field_id. This can happen when deserializing partition fields
131+
/// from V1 metadata files.
131132
/// \return An `expected` value containing either a `PartitionField` object or an error.
132133
/// If the JSON is malformed or missing expected fields, an error will be returned.
133134
Result<std::unique_ptr<PartitionField>> PartitionFieldFromJson(
134-
const nlohmann::json& json, bool allow_field_id_missing = false);
135+
const nlohmann::json& json, std::optional<int32_t> partition_field_id = std::nullopt);
135136

136137
/// \brief Serializes a `PartitionSpec` object to JSON.
137138
///

0 commit comments

Comments
 (0)