Skip to content

Commit d3f6624

Browse files
committed
Merge branch 'main' into expr
2 parents 48be4c9 + 5ba0a84 commit d3f6624

32 files changed

+1261
-168
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ set(ICEBERG_SOURCES
3131
statistics_file.cc
3232
table_metadata.cc
3333
transform.cc
34+
transform_function.cc
3435
type.cc
36+
snapshot.cc
3537
expression.cc)
3638

3739
set(ICEBERG_STATIC_BUILD_INTERFACE_LIBS)

src/iceberg/arrow/arrow_error_transform_internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <arrow/result.h>
2323
#include <arrow/status.h>
2424

25-
#include "iceberg/expected.h"
2625
#include "iceberg/result.h"
2726

2827
namespace iceberg::arrow {

src/iceberg/json_internal.cc

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424

2525
#include <nlohmann/json.hpp>
2626

27-
#include "iceberg/expected.h"
27+
#include "iceberg/partition_spec.h"
2828
#include "iceberg/result.h"
2929
#include "iceberg/schema.h"
3030
#include "iceberg/schema_internal.h"
3131
#include "iceberg/sort_order.h"
3232
#include "iceberg/transform.h"
3333
#include "iceberg/type.h"
34-
#include "iceberg/util/formatter.h"
34+
#include "iceberg/util/formatter.h" // IWYU pragma: keep
3535
#include "iceberg/util/macros.h"
3636

3737
namespace iceberg {
@@ -68,6 +68,9 @@ constexpr std::string_view kRequired = "required";
6868
constexpr std::string_view kElementRequired = "element-required";
6969
constexpr std::string_view kValueRequired = "value-required";
7070

71+
constexpr std::string_view kFieldId = "field-id";
72+
constexpr std::string_view kSpecId = "spec-id";
73+
7174
template <typename T>
7275
Result<T> GetJsonValue(const nlohmann::json& json, std::string_view key) {
7376
if (!json.contains(key)) {
@@ -113,7 +116,7 @@ Result<std::unique_ptr<SortField>> SortFieldFromJson(const nlohmann::json& json)
113116
ICEBERG_ASSIGN_OR_RAISE(auto source_id, GetJsonValue<int32_t>(json, kSourceId));
114117
ICEBERG_ASSIGN_OR_RAISE(
115118
auto transform,
116-
GetJsonValue<std::string>(json, kTransform).and_then(TransformFunctionFromString));
119+
GetJsonValue<std::string>(json, kTransform).and_then(TransformFromString));
117120
ICEBERG_ASSIGN_OR_RAISE(
118121
auto direction,
119122
GetJsonValue<std::string>(json, kDirection).and_then(SortDirectionFromString));
@@ -370,4 +373,50 @@ Result<std::unique_ptr<Schema>> SchemaFromJson(const nlohmann::json& json) {
370373
return FromStructType(std::move(struct_type), schema_id);
371374
}
372375

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+
373422
} // namespace iceberg

src/iceberg/json_internal.h

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
#pragma once
2121

2222
#include <memory>
23-
#include <string_view>
2423

2524
#include <nlohmann/json_fwd.hpp>
2625

27-
#include "iceberg/expected.h"
2826
#include "iceberg/result.h"
2927
#include "iceberg/type_fwd.h"
3028

@@ -108,4 +106,51 @@ Result<std::unique_ptr<Type>> TypeFromJson(const nlohmann::json& json);
108106
/// \return The Iceberg field or an error if the conversion fails.
109107
Result<std::unique_ptr<SchemaField>> FieldFromJson(const nlohmann::json& json);
110108

109+
/// \brief Serializes a `PartitionField` object to JSON.
110+
///
111+
/// This function converts a `PartitionField` object into a JSON representation.
112+
/// The resulting JSON object includes the transform type, source ID, field ID, and
113+
/// name.
114+
///
115+
/// \param partition_field The `PartitionField` object to be serialized.
116+
/// \return A JSON object representing the `PartitionField` in the form of key-value
117+
/// pairs.
118+
nlohmann::json ToJson(const PartitionField& partition_field);
119+
120+
/// \brief Serializes a `PartitionSpec` object to JSON.
121+
///
122+
/// This function converts a `PartitionSpec` object into a JSON representation.
123+
/// The resulting JSON includes the spec ID and a list of `PartitionField` objects.
124+
/// Each `PartitionField` is serialized as described in the `ToJson(PartitionField)`
125+
/// function.
126+
///
127+
/// \param partition_spec The `PartitionSpec` object to be serialized.
128+
/// \return A JSON object representing the `PartitionSpec` with its order ID and fields
129+
/// array.
130+
nlohmann::json ToJson(const PartitionSpec& partition_spec);
131+
132+
/// \brief Deserializes a JSON object into a `PartitionField` object.
133+
///
134+
/// This function parses the provided JSON and creates a `PartitionField` object.
135+
/// It expects the JSON object to contain keys for the transform, source ID, field ID,
136+
/// and name.
137+
///
138+
/// \param json The JSON object representing a `PartitionField`.
139+
/// \return An `expected` value containing either a `PartitionField` object or an error.
140+
/// If the JSON is malformed or missing expected fields, an error will be returned.
141+
Result<std::unique_ptr<PartitionField>> PartitionFieldFromJson(
142+
const nlohmann::json& json);
143+
144+
/// \brief Deserializes a JSON object into a `PartitionSpec` object.
145+
///
146+
/// This function parses the provided JSON and creates a `PartitionSpec` object.
147+
/// It expects the JSON object to contain the spec ID and a list of `PartitionField`
148+
/// objects. Each `PartitionField` will be parsed using the `PartitionFieldFromJson`
149+
/// function.
150+
///
151+
/// \param json The JSON object representing a `PartitionSpec`.
152+
/// \return An `expected` value containing either a `PartitionSpec` object or an error. If
153+
/// the JSON is malformed or missing expected fields, an error will be returned.
154+
Result<std::unique_ptr<PartitionSpec>> PartitionSpecFromJson(
155+
const std::shared_ptr<Schema>& schema, const nlohmann::json& json);
111156
} // namespace iceberg

src/iceberg/partition_field.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323

2424
#include "iceberg/transform.h"
2525
#include "iceberg/type.h"
26-
#include "iceberg/util/formatter.h"
26+
#include "iceberg/util/formatter.h" // IWYU pragma: keep
2727

2828
namespace iceberg {
2929

3030
PartitionField::PartitionField(int32_t source_id, int32_t field_id, std::string name,
31-
std::shared_ptr<TransformFunction> transform)
31+
std::shared_ptr<Transform> transform)
3232
: source_id_(source_id),
3333
field_id_(field_id),
3434
name_(std::move(name)),
@@ -40,9 +40,7 @@ int32_t PartitionField::field_id() const { return field_id_; }
4040

4141
std::string_view PartitionField::name() const { return name_; }
4242

43-
std::shared_ptr<TransformFunction> const& PartitionField::transform() const {
44-
return transform_;
45-
}
43+
std::shared_ptr<Transform> const& PartitionField::transform() const { return transform_; }
4644

4745
std::string PartitionField::ToString() const {
4846
return std::format("{} ({} {}({}))", name_, field_id_, *transform_, source_id_);

src/iceberg/partition_field.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <memory>
2727
#include <string>
2828
#include <string_view>
29-
#include <vector>
3029

3130
#include "iceberg/iceberg_export.h"
3231
#include "iceberg/type_fwd.h"
@@ -43,7 +42,7 @@ class ICEBERG_EXPORT PartitionField : public util::Formattable {
4342
/// \param[in] name The partition field name.
4443
/// \param[in] transform The transform function.
4544
PartitionField(int32_t source_id, int32_t field_id, std::string name,
46-
std::shared_ptr<TransformFunction> transform);
45+
std::shared_ptr<Transform> transform);
4746

4847
/// \brief Get the source field ID.
4948
int32_t source_id() const;
@@ -55,7 +54,7 @@ class ICEBERG_EXPORT PartitionField : public util::Formattable {
5554
std::string_view name() const;
5655

5756
/// \brief Get the transform type.
58-
std::shared_ptr<TransformFunction> const& transform() const;
57+
std::shared_ptr<Transform> const& transform() const;
5958

6059
std::string ToString() const override;
6160

@@ -74,7 +73,7 @@ class ICEBERG_EXPORT PartitionField : public util::Formattable {
7473
int32_t source_id_;
7574
int32_t field_id_;
7675
std::string name_;
77-
std::shared_ptr<TransformFunction> transform_;
76+
std::shared_ptr<Transform> transform_;
7877
};
7978

8079
} // namespace iceberg

src/iceberg/partition_spec.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include "iceberg/schema.h"
2525
#include "iceberg/type.h"
26-
#include "iceberg/util/formatter.h"
26+
#include "iceberg/util/formatter.h" // IWYU pragma: keep
2727

2828
namespace iceberg {
2929

src/iceberg/schema_field.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <format>
2323

2424
#include "iceberg/type.h"
25-
#include "iceberg/util/formatter.h"
25+
#include "iceberg/util/formatter.h" // IWYU pragma: keep
2626

2727
namespace iceberg {
2828

src/iceberg/schema_internal.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <optional>
2525
#include <string>
2626

27-
#include "iceberg/expected.h"
2827
#include "iceberg/schema.h"
2928
#include "iceberg/type.h"
3029

src/iceberg/schema_internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
#include <nanoarrow/nanoarrow.h>
2525

26-
#include "iceberg/expected.h"
2726
#include "iceberg/result.h"
2827
#include "iceberg/type_fwd.h"
2928

0 commit comments

Comments
 (0)