|
24 | 24 |
|
25 | 25 | #include <nlohmann/json.hpp> |
26 | 26 |
|
27 | | -#include "iceberg/expected.h" |
| 27 | +#include "iceberg/partition_spec.h" |
28 | 28 | #include "iceberg/result.h" |
29 | 29 | #include "iceberg/schema.h" |
30 | 30 | #include "iceberg/schema_internal.h" |
31 | 31 | #include "iceberg/sort_order.h" |
32 | 32 | #include "iceberg/transform.h" |
33 | 33 | #include "iceberg/type.h" |
34 | | -#include "iceberg/util/formatter.h" |
| 34 | +#include "iceberg/util/formatter.h" // IWYU pragma: keep |
35 | 35 | #include "iceberg/util/macros.h" |
36 | 36 |
|
37 | 37 | namespace iceberg { |
@@ -68,6 +68,9 @@ constexpr std::string_view kRequired = "required"; |
68 | 68 | constexpr std::string_view kElementRequired = "element-required"; |
69 | 69 | constexpr std::string_view kValueRequired = "value-required"; |
70 | 70 |
|
| 71 | +constexpr std::string_view kFieldId = "field-id"; |
| 72 | +constexpr std::string_view kSpecId = "spec-id"; |
| 73 | + |
71 | 74 | template <typename T> |
72 | 75 | Result<T> GetJsonValue(const nlohmann::json& json, std::string_view key) { |
73 | 76 | if (!json.contains(key)) { |
@@ -113,7 +116,7 @@ Result<std::unique_ptr<SortField>> SortFieldFromJson(const nlohmann::json& json) |
113 | 116 | ICEBERG_ASSIGN_OR_RAISE(auto source_id, GetJsonValue<int32_t>(json, kSourceId)); |
114 | 117 | ICEBERG_ASSIGN_OR_RAISE( |
115 | 118 | auto transform, |
116 | | - GetJsonValue<std::string>(json, kTransform).and_then(TransformFunctionFromString)); |
| 119 | + GetJsonValue<std::string>(json, kTransform).and_then(TransformFromString)); |
117 | 120 | ICEBERG_ASSIGN_OR_RAISE( |
118 | 121 | auto direction, |
119 | 122 | GetJsonValue<std::string>(json, kDirection).and_then(SortDirectionFromString)); |
@@ -370,4 +373,50 @@ Result<std::unique_ptr<Schema>> SchemaFromJson(const nlohmann::json& json) { |
370 | 373 | return FromStructType(std::move(struct_type), schema_id); |
371 | 374 | } |
372 | 375 |
|
| 376 | +nlohmann::json ToJson(const PartitionField& partition_field) { |
| 377 | + nlohmann::json json; |
| 378 | + json[kSourceId] = partition_field.source_id(); |
| 379 | + json[kFieldId] = partition_field.field_id(); |
| 380 | + json[kTransform] = std::format("{}", *partition_field.transform()); |
| 381 | + json[kName] = partition_field.name(); |
| 382 | + return json; |
| 383 | +} |
| 384 | + |
| 385 | +nlohmann::json ToJson(const PartitionSpec& partition_spec) { |
| 386 | + nlohmann::json json; |
| 387 | + json[kSpecId] = partition_spec.spec_id(); |
| 388 | + |
| 389 | + nlohmann::json fields_json = nlohmann::json::array(); |
| 390 | + for (const auto& field : partition_spec.fields()) { |
| 391 | + fields_json.push_back(ToJson(field)); |
| 392 | + } |
| 393 | + json[kFields] = fields_json; |
| 394 | + return json; |
| 395 | +} |
| 396 | + |
| 397 | +Result<std::unique_ptr<PartitionField>> PartitionFieldFromJson( |
| 398 | + const nlohmann::json& json) { |
| 399 | + ICEBERG_ASSIGN_OR_RAISE(auto source_id, GetJsonValue<int32_t>(json, kSourceId)); |
| 400 | + ICEBERG_ASSIGN_OR_RAISE(auto field_id, GetJsonValue<int32_t>(json, kFieldId)); |
| 401 | + ICEBERG_ASSIGN_OR_RAISE( |
| 402 | + auto transform, |
| 403 | + GetJsonValue<std::string>(json, kTransform).and_then(TransformFromString)); |
| 404 | + ICEBERG_ASSIGN_OR_RAISE(auto name, GetJsonValue<std::string>(json, kName)); |
| 405 | + return std::make_unique<PartitionField>(source_id, field_id, name, |
| 406 | + std::move(transform)); |
| 407 | +} |
| 408 | + |
| 409 | +Result<std::unique_ptr<PartitionSpec>> PartitionSpecFromJson( |
| 410 | + const std::shared_ptr<Schema>& schema, const nlohmann::json& json) { |
| 411 | + ICEBERG_ASSIGN_OR_RAISE(auto spec_id, GetJsonValue<int32_t>(json, kSpecId)); |
| 412 | + ICEBERG_ASSIGN_OR_RAISE(auto fields, GetJsonValue<nlohmann::json>(json, kFields)); |
| 413 | + |
| 414 | + std::vector<PartitionField> partition_fields; |
| 415 | + for (const auto& field_json : fields) { |
| 416 | + ICEBERG_ASSIGN_OR_RAISE(auto partition_field, PartitionFieldFromJson(field_json)); |
| 417 | + partition_fields.push_back(std::move(*partition_field)); |
| 418 | + } |
| 419 | + return std::make_unique<PartitionSpec>(schema, spec_id, std::move(partition_fields)); |
| 420 | +} |
| 421 | + |
373 | 422 | } // namespace iceberg |
0 commit comments