-
Notifications
You must be signed in to change notification settings - Fork 103
[Expr Serde] [PR 2/X] Operation type enum conversion helper #534
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
Closed
Closed
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9a0875b
initial commit added json serializer for expressions
ef343ad
scope initial change to boolean support only
dbda110
clang-format
52e2501
cleanup
549b9ae
Added helper for operation enum conversion
3feca74
review comment
04ff61b
set or unary operation helper
bd77101
updated tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include "iceberg/expression/json_internal.h" | ||
|
|
||
| #include <format> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #include "iceberg/expression/expressions.h" | ||
| #include "iceberg/expression/literal.h" | ||
| #include "iceberg/util/json_util_internal.h" | ||
| #include "iceberg/util/macros.h" | ||
|
|
||
| namespace iceberg { | ||
| namespace { | ||
| // Expression type strings | ||
| constexpr std::string_view kTypeTrue = "true"; | ||
| constexpr std::string_view kTypeFalse = "false"; | ||
| constexpr std::string_view kTypeEq = "eq"; | ||
| constexpr std::string_view kTypeAnd = "and"; | ||
| constexpr std::string_view kTypeOr = "or"; | ||
| constexpr std::string_view kTypeNot = "not"; | ||
| constexpr std::string_view kTypeIn = "in"; | ||
| constexpr std::string_view kTypeNotIn = "not-in"; | ||
| constexpr std::string_view kTypeLt = "lt"; | ||
| constexpr std::string_view kTypeLtEq = "lt-eq"; | ||
| constexpr std::string_view kTypeGt = "gt"; | ||
| constexpr std::string_view kTypeGtEq = "gt-eq"; | ||
| constexpr std::string_view kTypeNotEq = "not-eq"; | ||
| constexpr std::string_view kTypeStartsWith = "starts-with"; | ||
| constexpr std::string_view kTypeNotStartsWith = "not-starts-with"; | ||
| constexpr std::string_view kTypeIsNull = "is-null"; | ||
| constexpr std::string_view kTypeNotNull = "not-null"; | ||
| constexpr std::string_view kTypeIsNan = "is-nan"; | ||
| constexpr std::string_view kTypeNotNan = "not-nan"; | ||
| } // namespace | ||
|
|
||
| /// \brief Converts a JSON type string to an Expression::Operation. | ||
| /// | ||
| /// \param type_str The JSON type string | ||
| /// \return The corresponding Operation or an error if unknown | ||
| Result<Expression::Operation> OperationTypeFromString(const std::string_view type_str) { | ||
| if (type_str == kTypeTrue) return Expression::Operation::kTrue; | ||
| if (type_str == kTypeFalse) return Expression::Operation::kFalse; | ||
| if (type_str == kTypeAnd) return Expression::Operation::kAnd; | ||
| if (type_str == kTypeOr) return Expression::Operation::kOr; | ||
| if (type_str == kTypeNot) return Expression::Operation::kNot; | ||
| if (type_str == kTypeEq) return Expression::Operation::kEq; | ||
| if (type_str == kTypeNotEq) return Expression::Operation::kNotEq; | ||
| if (type_str == kTypeLt) return Expression::Operation::kLt; | ||
| if (type_str == kTypeLtEq) return Expression::Operation::kLtEq; | ||
| if (type_str == kTypeGt) return Expression::Operation::kGt; | ||
| if (type_str == kTypeGtEq) return Expression::Operation::kGtEq; | ||
| if (type_str == kTypeIn) return Expression::Operation::kIn; | ||
| if (type_str == kTypeNotIn) return Expression::Operation::kNotIn; | ||
| if (type_str == kTypeIsNull) return Expression::Operation::kIsNull; | ||
| if (type_str == kTypeNotNull) return Expression::Operation::kNotNull; | ||
| if (type_str == kTypeIsNan) return Expression::Operation::kIsNan; | ||
| if (type_str == kTypeNotNan) return Expression::Operation::kNotNan; | ||
| if (type_str == kTypeStartsWith) return Expression::Operation::kStartsWith; | ||
| if (type_str == kTypeNotStartsWith) return Expression::Operation::kNotStartsWith; | ||
|
|
||
| return JsonParseError("Unknown expression type: {}", type_str); | ||
| } | ||
|
|
||
| /// \brief Converts an Expression::Operation to its JSON string representation. | ||
| /// | ||
| /// \param op The operation to convert | ||
| /// \return The JSON type string (e.g., "eq", "lt-eq", "is-null") | ||
| std::string_view ToStringOperationType(Expression::Operation op) { | ||
| switch (op) { | ||
| case Expression::Operation::kTrue: | ||
| return kTypeTrue; | ||
| case Expression::Operation::kFalse: | ||
| return kTypeFalse; | ||
| case Expression::Operation::kAnd: | ||
| return kTypeAnd; | ||
| case Expression::Operation::kOr: | ||
| return kTypeOr; | ||
| case Expression::Operation::kNot: | ||
| return kTypeNot; | ||
| case Expression::Operation::kEq: | ||
| return kTypeEq; | ||
| case Expression::Operation::kNotEq: | ||
| return kTypeNotEq; | ||
| case Expression::Operation::kLt: | ||
| return kTypeLt; | ||
| case Expression::Operation::kLtEq: | ||
| return kTypeLtEq; | ||
| case Expression::Operation::kGt: | ||
| return kTypeGt; | ||
| case Expression::Operation::kGtEq: | ||
| return kTypeGtEq; | ||
| case Expression::Operation::kIn: | ||
| return kTypeIn; | ||
| case Expression::Operation::kNotIn: | ||
| return kTypeNotIn; | ||
| case Expression::Operation::kIsNull: | ||
| return kTypeIsNull; | ||
| case Expression::Operation::kNotNull: | ||
| return kTypeNotNull; | ||
| case Expression::Operation::kIsNan: | ||
| return kTypeIsNan; | ||
| case Expression::Operation::kNotNan: | ||
| return kTypeNotNan; | ||
| case Expression::Operation::kStartsWith: | ||
| return kTypeStartsWith; | ||
| case Expression::Operation::kNotStartsWith: | ||
| return kTypeNotStartsWith; | ||
| default: | ||
| return "unknown"; | ||
| } | ||
| } | ||
|
|
||
| Result<std::shared_ptr<Expression>> ExpressionFromJson(const nlohmann::json& json) { | ||
| // Handle boolean | ||
| if (json.is_boolean()) { | ||
| return json.get<bool>() ? std::static_pointer_cast<Expression>(True::Instance()) | ||
| : std::static_pointer_cast<Expression>(False::Instance()); | ||
| } | ||
| return JsonParseError("Only booleans are currently supported"); | ||
| } | ||
|
|
||
| nlohmann::json ToJson(const Expression& expr) { | ||
| switch (expr.op()) { | ||
| case Expression::Operation::kTrue: | ||
| return true; | ||
|
|
||
| case Expression::Operation::kFalse: | ||
| return false; | ||
| default: | ||
| throw std::logic_error("Only booleans are currently supported"); | ||
| } | ||
| } | ||
|
|
||
| #define ICEBERG_DEFINE_FROM_JSON(Model) \ | ||
| template <> \ | ||
| Result<std::shared_ptr<Model>> FromJson<Model>(const nlohmann::json& json) { \ | ||
| return Model##FromJson(json); \ | ||
| } | ||
|
|
||
| ICEBERG_DEFINE_FROM_JSON(Expression) | ||
|
|
||
| } // namespace iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <nlohmann/json_fwd.hpp> | ||
|
|
||
| #include "iceberg/expression/expression.h" | ||
| #include "iceberg/iceberg_export.h" | ||
| #include "iceberg/result.h" | ||
|
|
||
| /// \file iceberg/expression/json_internal.h | ||
| /// JSON serialization and deserialization for expressions. | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| template <typename Model> | ||
| Result<Model> FromJson(const nlohmann::json& json); | ||
|
|
||
| #define ICEBERG_DECLARE_JSON_SERDE(Model) \ | ||
| ICEBERG_EXPORT Result<std::shared_ptr<Model>> Model##FromJson( \ | ||
| const nlohmann::json& json); \ | ||
| \ | ||
| template <typename Model> \ | ||
| ICEBERG_EXPORT Result<std::shared_ptr<Model>> FromJson(const nlohmann::json& json); \ | ||
| \ | ||
| ICEBERG_EXPORT nlohmann::json ToJson(const Model& model); | ||
|
|
||
| /// \note Don't forget to add `ICEBERG_DEFINE_FROM_JSON` to the end of | ||
| /// `json_internal.cc` to define the `FromJson` function for the model. | ||
| ICEBERG_DECLARE_JSON_SERDE(Expression) | ||
|
|
||
| #undef ICEBERG_DECLARE_JSON_SERDE | ||
|
|
||
| /// \brief Converts an operation type string to an Expression::Operation. | ||
| /// | ||
| /// \param type_str The operation type string | ||
| /// \return The corresponding Operation or an error if unknown | ||
| ICEBERG_EXPORT Result<Expression::Operation> OperationTypeFromString( | ||
| const std::string_view type_str); | ||
|
|
||
| /// \brief Converts an Expression::Operation to its string representation. | ||
| /// | ||
| /// \param op The operation to convert | ||
| /// \return The operation type string (e.g., "eq", "lt-eq", "is-null") | ||
| ICEBERG_EXPORT std::string_view ToStringOperationType(Expression::Operation op); | ||
|
|
||
| } // namespace iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include <memory> | ||
| #include <vector> | ||
|
|
||
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #include "iceberg/expression/expression.h" | ||
| #include "iceberg/expression/expressions.h" | ||
| #include "iceberg/expression/json_internal.h" | ||
| #include "iceberg/expression/literal.h" | ||
| #include "iceberg/expression/predicate.h" | ||
| #include "iceberg/expression/term.h" | ||
| #include "iceberg/test/matchers.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| class ExpressionJsonTest : public ::testing::Test { | ||
| protected: | ||
| // Helper to test round-trip serialization | ||
| // Uses string comparison since expressions may have different internal identity | ||
| // but the same semantic meaning (i.e., ToString() output matches) | ||
| void TestRoundTrip(const Expression& expr) { | ||
|
Collaborator
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. You add this helper function but not used anywhere. |
||
| auto json = ToJson(expr); | ||
| auto result = ExpressionFromJson(json); | ||
| ASSERT_THAT(result, IsOk()) << "Failed to parse JSON: " << json.dump(); | ||
| EXPECT_EQ(expr.ToString(), result.value()->ToString()) | ||
| << "Round-trip failed.\nJSON: " << json.dump(); | ||
| } | ||
| }; | ||
|
|
||
| // Test boolean constant expressions | ||
| TEST_F(ExpressionJsonTest, TrueExpression) { | ||
| auto expr = True::Instance(); | ||
| auto json = ToJson(*expr); | ||
|
|
||
| // True should serialize as JSON boolean true | ||
| EXPECT_TRUE(json.is_boolean()); | ||
| EXPECT_TRUE(json.get<bool>()); | ||
|
|
||
| // Parse back | ||
| auto result = ExpressionFromJson(json); | ||
| ASSERT_THAT(result, IsOk()); | ||
| EXPECT_EQ(result.value()->op(), Expression::Operation::kTrue); | ||
| } | ||
|
|
||
| TEST_F(ExpressionJsonTest, FalseExpression) { | ||
| auto expr = False::Instance(); | ||
| auto json = ToJson(*expr); | ||
|
|
||
| // False should serialize as JSON boolean false | ||
| EXPECT_TRUE(json.is_boolean()); | ||
| EXPECT_FALSE(json.get<bool>()); | ||
|
|
||
| // Parse back | ||
| auto result = ExpressionFromJson(json); | ||
| ASSERT_THAT(result, IsOk()); | ||
| EXPECT_EQ(result.value()->op(), Expression::Operation::kFalse); | ||
| } | ||
|
|
||
| TEST_F(ExpressionJsonTest, OpToString) { | ||
| EXPECT_EQ(OperationTypeFromString("true"), Expression::Operation::kTrue); | ||
| EXPECT_EQ("true", ToStringOperationType(Expression::Operation::kTrue)); | ||
| } | ||
|
|
||
| } // namespace iceberg | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
By convention, this file should be named json.cc, as
internalis typically reserved for header files. I'm aware that we already have ajson_internal.cc, but that’s legacy. I thinkjson_serde.cc/ json_serde_internal.hwould be a better choice. What do you think, @wgtmac?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.
Agreed