-
Notifications
You must be signed in to change notification settings - Fork 84
[Expr Serde] [PR 1/X] basic plumbing for serde support for expressions #532
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 |
|---|---|---|
| @@ -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. | ||
| */ | ||
|
|
||
| #include <format> | ||
| #include <string> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #include "iceberg/expression/expressions.h" | ||
| #include "iceberg/expression/json_serde_internal.h" | ||
| #include "iceberg/expression/literal.h" | ||
| #include "iceberg/util/json_util_internal.h" | ||
| #include "iceberg/util/macros.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| Result<std::shared_ptr<Expression>> ExpressionFromJson(const nlohmann::json& json) { | ||
| // Handle boolean | ||
| if (json.is_boolean()) { | ||
| return json.get<bool>() ? std::dynamic_pointer_cast<Expression>(True::Instance()) | ||
|
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 use |
||
| : std::dynamic_pointer_cast<Expression>(False::Instance()); | ||
| } | ||
| return JsonParseError("Only boolean 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"); | ||
|
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. Please note that we cannot throw in any case. It is fine to throw
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. BTW, what is the plan of work item breakdown? This PR is trivial enough that I cannot see how do we want to work with different operations, predicates, literals, etc. My concern is large refactoring afterwards if we don't have a good design beforehand.
Author
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 are correct, I will cleanup my notes and shared as design. but I don't expect too much refactoring. |
||
| } | ||
| } | ||
|
|
||
| #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 | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,51 @@ | ||||||
| /* | ||||||
| * 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" | ||||||
|
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. We don't need this if we include |
||||||
| #include "iceberg/iceberg_export.h" | ||||||
| #include "iceberg/result.h" | ||||||
|
|
||||||
| /// \file iceberg/expression/json_serde_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) \ | ||||||
|
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. Do we need this macro definition actually? I think the rest models have a lot of variants so we define the macro for convenience. Expressions do not have that much so perhaps we can just follow the style in
Author
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. Yeah, as I am looking at other literals, predicates, and terms it does seem like it would be better not to have the macro. |
||||||
| 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_serde_internal.cc` to define the `FromJson` function for the model. | ||||||
|
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.
Suggested change
|
||||||
| ICEBERG_DECLARE_JSON_SERDE(Expression) | ||||||
|
|
||||||
| #undef ICEBERG_DECLARE_JSON_SERDE | ||||||
|
|
||||||
| } // namespace iceberg | ||||||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -24,6 +24,7 @@ install_headers( | |||
| 'expression_visitor.h', | ||||
| 'expressions.h', | ||||
| 'inclusive_metrics_evaluator.h', | ||||
| 'json_serde_internal.h', | ||||
|
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.
Suggested change
We don't install header files with internal suffix. This is automatically handled in the CMake script. |
||||
| 'literal.h', | ||||
| 'manifest_evaluator.h', | ||||
| 'predicate.h', | ||||
|
|
||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,7 @@ add_iceberg_test(table_test | |
| add_iceberg_test(expression_test | ||
| SOURCES | ||
| aggregate_test.cc | ||
| expression_json_test.cc | ||
|
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. Same comment for |
||
| expression_test.cc | ||
| expression_visitor_test.cc | ||
| inclusive_metrics_evaluator_test.cc | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| /* | ||
| * 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_serde_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: | ||
| void CheckBoolean(std::shared_ptr<Expression> expr, bool value) { | ||
| auto json = ToJson(*expr); | ||
| EXPECT_TRUE(json.is_boolean()); | ||
| EXPECT_EQ(json.get<bool>(), value); | ||
|
|
||
| auto result = ExpressionFromJson(json); | ||
| ASSERT_THAT(result, IsOk()); | ||
| if (value) { | ||
| EXPECT_EQ(result.value()->op(), Expression::Operation::kTrue); | ||
| } else { | ||
| EXPECT_EQ(result.value()->op(), Expression::Operation::kFalse); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // Test boolean constant expressions | ||
| TEST_F(ExpressionJsonTest, CheckBooleanExpression) { | ||
| CheckBoolean(True::Instance(), true); | ||
|
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. I think
|
||
| CheckBoolean(False::Instance(), false); | ||
| } | ||
|
|
||
| } // namespace iceberg | ||
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.
We also need to update
meson.buildfor any new file.