-
Notifications
You must be signed in to change notification settings - Fork 88
feat: add json serde for expressions #553
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,12 +27,18 @@ | |
|
|
||
| #include "iceberg/expression/json_serde_internal.h" | ||
| #include "iceberg/expression/literal.h" | ||
| #include "iceberg/expression/term.h" | ||
| #include "iceberg/transform.h" | ||
| #include "iceberg/util/checked_cast.h" | ||
| #include "iceberg/util/json_util_internal.h" | ||
| #include "iceberg/util/macros.h" | ||
|
|
||
| namespace iceberg { | ||
| namespace { | ||
| // JSON field names | ||
| constexpr std::string_view kType = "type"; | ||
| constexpr std::string_view kTerm = "term"; | ||
| constexpr std::string_view kTransform = "transform"; | ||
| // Expression type strings | ||
| constexpr std::string_view kTypeTrue = "true"; | ||
| constexpr std::string_view kTypeFalse = "false"; | ||
|
|
@@ -123,6 +129,41 @@ nlohmann::json ToJson(Expression::Operation op) { | |
| return json; | ||
| } | ||
|
|
||
| nlohmann::json ToJson(const NamedReference& ref) { return ref.name(); } | ||
|
|
||
| Result<std::shared_ptr<NamedReference>> NamedReferenceFromJson( | ||
| const nlohmann::json& json) { | ||
| if (!json.is_string()) [[unlikely]] { | ||
| return JsonParseError("Expected string for named reference"); | ||
| } | ||
| ICEBERG_ASSIGN_OR_RAISE(auto ref, NamedReference::Make(json.get<std::string>())); | ||
| return std::shared_ptr<NamedReference>(std::move(ref)); | ||
| } | ||
|
|
||
| nlohmann::json ToJson(const UnboundTransform& transform) { | ||
| auto& mutable_transform = const_cast<UnboundTransform&>(transform); | ||
| nlohmann::json json; | ||
| json[kType] = std::string(kTransform); | ||
| json[kTransform] = transform.transform()->ToString(); | ||
| json[kTerm] = mutable_transform.reference()->name(); | ||
| return json; | ||
| } | ||
|
|
||
| Result<std::shared_ptr<UnboundTransform>> UnboundTransformFromJson( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please call this in the |
||
| const nlohmann::json& json) { | ||
| if (json.is_object() && json.contains(kType) && | ||
| json[kType] == std::string(kTransform) && json.contains(kTerm)) { | ||
| ICEBERG_ASSIGN_OR_RAISE(auto transform_str, | ||
| GetJsonValue<std::string>(json, kTransform)); | ||
| ICEBERG_ASSIGN_OR_RAISE(auto transform, TransformFromString(transform_str)); | ||
| ICEBERG_ASSIGN_OR_RAISE(auto ref, NamedReferenceFromJson(json[kTerm])); | ||
| ICEBERG_ASSIGN_OR_RAISE(auto result, | ||
| UnboundTransform::Make(std::move(ref), std::move(transform))); | ||
| return std::shared_ptr<UnboundTransform>(std::move(result)); | ||
| } | ||
| return JsonParseError("Invalid unbound transform json: {}", SafeDumpJson(json)); | ||
| } | ||
|
|
||
| Result<std::shared_ptr<Expression>> ExpressionFromJson(const nlohmann::json& json) { | ||
| // Handle boolean | ||
| if (json.is_boolean()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |||||||||||||||||||
| #include "iceberg/expression/predicate.h" | ||||||||||||||||||||
| #include "iceberg/expression/term.h" | ||||||||||||||||||||
| #include "iceberg/test/matchers.h" | ||||||||||||||||||||
| #include "iceberg/transform.h" | ||||||||||||||||||||
|
|
||||||||||||||||||||
| namespace iceberg { | ||||||||||||||||||||
|
|
||||||||||||||||||||
|
|
@@ -63,4 +64,53 @@ TEST(ExpressionJsonTest, OperationTypeTests) { | |||||||||||||||||||
| EXPECT_FALSE(IsUnaryOperation(Expression::Operation::kTrue)); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| TEST(ExpressionJsonTest, NameReferenceRoundTrip) { | ||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we try to reuse iceberg-cpp/src/iceberg/test/json_serde_test.cc Lines 76 to 84 in b15ac65
|
||||||||||||||||||||
| ICEBERG_UNWRAP_OR_FAIL(auto ref, NamedReference::Make("col_name")); | ||||||||||||||||||||
| auto json = ToJson(*ref); | ||||||||||||||||||||
| EXPECT_EQ(json.get<std::string>(), "col_name"); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ICEBERG_UNWRAP_OR_FAIL(auto parsed, NamedReferenceFromJson(json)); | ||||||||||||||||||||
| EXPECT_EQ(parsed->name(), "col_name"); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| TEST(ExpressionJsonTest, UnboundTransfromRoundTrip) { | ||||||||||||||||||||
| ICEBERG_UNWRAP_OR_FAIL(auto ref, NamedReference::Make("ts")); | ||||||||||||||||||||
| auto transform = Transform::Day(); | ||||||||||||||||||||
| ICEBERG_UNWRAP_OR_FAIL(auto unbound, UnboundTransform::Make(std::move(ref), transform)); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| auto json = ToJson(*unbound); | ||||||||||||||||||||
| EXPECT_EQ(json["type"], "transform"); | ||||||||||||||||||||
| EXPECT_EQ(json["transform"], "day"); | ||||||||||||||||||||
| EXPECT_EQ(json["term"], "ts"); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ICEBERG_UNWRAP_OR_FAIL(auto parsed, UnboundTransformFromJson(json)); | ||||||||||||||||||||
| EXPECT_EQ(parsed->reference()->name(), unbound->reference()->name()); | ||||||||||||||||||||
| EXPECT_EQ(parsed->transform()->transform_type(), | ||||||||||||||||||||
| unbound->transform()->transform_type()); | ||||||||||||||||||||
| EXPECT_EQ(parsed->transform()->ToString(), unbound->transform()->ToString()); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| TEST(ExpressionJsonTest, BucketTransform) { | ||||||||||||||||||||
| ICEBERG_UNWRAP_OR_FAIL(auto ref, NamedReference::Make("id")); | ||||||||||||||||||||
| ICEBERG_UNWRAP_OR_FAIL(auto unbound, | ||||||||||||||||||||
| UnboundTransform::Make(std::move(ref), Transform::Bucket(16))); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| auto json = ToJson(*unbound); | ||||||||||||||||||||
| EXPECT_EQ(json["type"], "transform"); | ||||||||||||||||||||
| EXPECT_EQ(json["transform"], "bucket[16]"); | ||||||||||||||||||||
| EXPECT_EQ(json["term"], "id"); | ||||||||||||||||||||
|
|
||||||||||||||||||||
| ICEBERG_UNWRAP_OR_FAIL(auto parsed, UnboundTransformFromJson(json)); | ||||||||||||||||||||
| EXPECT_EQ(parsed->transform()->transform_type(), | ||||||||||||||||||||
| unbound->transform()->transform_type()); | ||||||||||||||||||||
| EXPECT_EQ(parsed->transform()->ToString(), unbound->transform()->ToString()); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| TEST(ExpressionJsonTest, InvalidInput) { | ||||||||||||||||||||
| EXPECT_THAT(UnboundTransformFromJson(nlohmann::json::object()), | ||||||||||||||||||||
| IsError(ErrorKind::kJsonParseError)); | ||||||||||||||||||||
| EXPECT_THAT(UnboundTransformFromJson(nlohmann::json{{"type", "other"}}), | ||||||||||||||||||||
| IsError(ErrorKind::kJsonParseError)); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| } // namespace iceberg | ||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -131,6 +131,9 @@ class Expression; | |
| class Literal; | ||
| class Term; | ||
| class UnboundPredicate; | ||
| class NamedReference; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's sort them alphabetically. |
||
| class UnboundTransform; | ||
| class Transform; | ||
|
|
||
| /// \brief Evaluator. | ||
| class Evaluator; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.