You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "evindj (via GitHub)" <gi...@apache.org> on 2026/01/24 20:55:38 UTC
[PR] [PR 1/X] basic plumbing for serde support for expressions [iceberg-cpp]
evindj opened a new pull request, #532:
URL: https://github.com/apache/iceberg-cpp/pull/532
### Summary
This is the first PR aimed a addressing https://github.com/apache/iceberg-cpp/issues/331
Because doing it in one go will be more error prone and will make review harder, I am splitting the change across several PR.
This first one covers:
- Template definition to help generate ToJson and FromJson implementations.
- basic support for only boolean expressions.
- Unit tests for the case that we was added.
### Test
- Added some unit tests that cover the change and ensured the pass
~/Documents/my_iceberg/iceberg-cpp bool_expressio_serde ./build/src/iceberg/test/expression_test --gtest_filter="ExpressionJson*" ✔ 1297 13:53:47
Running main() from gmock_main.cc
Note: Google Test filter = ExpressionJson*
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from ExpressionJsonTest
[ RUN ] ExpressionJsonTest.TrueExpression
[ OK ] ExpressionJsonTest.TrueExpression (0 ms)
[ RUN ] ExpressionJsonTest.FalseExpression
[ OK ] ExpressionJsonTest.FalseExpression (0 ms)
[----------] 2 tests from ExpressionJsonTest (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (0 ms total)
[ PASSED ] 2 tests.
idjiofack@Innocents-MacBook-Pro-2 ~/Documents/my_iceberg/iceberg-cpp bool_expressio_serde
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] [Expr Serde] [PR 1/X] basic plumbing for serde support for expressions [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #532:
URL: https://github.com/apache/iceberg-cpp/pull/532#discussion_r2734687291
##########
src/iceberg/expression/json_serde_internal.h:
##########
@@ -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"
Review Comment:
We don't need this if we include 'iceberg/type_fwd.h'
##########
src/iceberg/expression/json_serde_internal.h:
##########
@@ -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"
Review Comment:
We don't need this if we include `iceberg/type_fwd.h`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add serde for expressions [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #532:
URL: https://github.com/apache/iceberg-cpp/pull/532#discussion_r2749151326
##########
src/iceberg/expression/json_serde.cc:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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 <ranges>
+#include <string>
+#include <utility>
+#include <vector>
+
+#include <nlohmann/json.hpp>
+
+#include "iceberg/expression/json_serde_internal.h"
+#include "iceberg/expression/literal.h"
+#include "iceberg/util/checked_cast.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";
+constexpr std::string_view kCount = "count";
Review Comment:
Why the above use `kType` prefix but below does not?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] [Expr Serde] [PR 1/X] basic plumbing for serde support for expressions [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #532:
URL: https://github.com/apache/iceberg-cpp/pull/532#discussion_r2734664179
##########
src/iceberg/expression/json_serde_internal.h:
##########
@@ -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"
+#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) \
+ 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.
Review Comment:
```suggestion
/// `json_serde.cc` to define the `FromJson` function for the model.
```
##########
src/iceberg/expression/meson.build:
##########
@@ -24,6 +24,7 @@ install_headers(
'expression_visitor.h',
'expressions.h',
'inclusive_metrics_evaluator.h',
+ 'json_serde_internal.h',
Review Comment:
```suggestion
```
We don't install header files with internal suffix. This is automatically handled in the CMake script.
##########
src/iceberg/expression/json_serde_internal.h:
##########
@@ -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"
+#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) \
Review Comment:
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 `iceberg/json_internal.h` to define ToJson and XXXFromJson explicitly?
##########
src/iceberg/test/expression_json_test.cc:
##########
@@ -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);
Review Comment:
I think `CheckBoolean` is unlikely to be reused by other test cases. What about:
1. Remove explicit class definition of `ExpressionJsonTest` and use `TEST(ExpressionJsonTest, BooleanExpression)` instead.
2. Make `CheckBoolean` as lambda function inside this test case.
##########
src/iceberg/expression/json_serde.cc:
##########
@@ -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())
+ : 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");
Review Comment:
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.
##########
src/iceberg/expression/json_serde.cc:
##########
@@ -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())
+ : 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");
Review Comment:
Please note that we cannot throw in any case. It is fine to throw `IcebergException` with an error message indicating a unimplemented state for now.
##########
src/iceberg/expression/json_serde.cc:
##########
@@ -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())
Review Comment:
Can we use `internal::checked_pointer_cast` here?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add json serde for expression operations [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac merged PR #532:
URL: https://github.com/apache/iceberg-cpp/pull/532
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] [Expr Serde] [PR 1/X] basic plumbing for serde support for expressions [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on PR #532:
URL: https://github.com/apache/iceberg-cpp/pull/532#issuecomment-3816013499
Thanks for the update! Let me know when ready to review again.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add serde for expressions [iceberg-cpp]
Posted by "evindj (via GitHub)" <gi...@apache.org>.
evindj commented on PR #532:
URL: https://github.com/apache/iceberg-cpp/pull/532#issuecomment-3821731106
@wgtmac I think this ok to have another look.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] [Expr Serde] [PR 1/X] basic plumbing for serde support for expressions [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #532:
URL: https://github.com/apache/iceberg-cpp/pull/532#discussion_r2730129553
##########
src/iceberg/expression/json_serde_internal.h:
##########
@@ -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"
+#include "iceberg/iceberg_export.h"
+#include "iceberg/result.h"
+
+/// \file iceberg/expression/json_internal.h
Review Comment:
Let's keep the comment in sync
##########
src/iceberg/expression/json_serde_internal.h:
##########
@@ -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"
+#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.
Review Comment:
ditto
##########
src/iceberg/expression/json_serde.cc:
##########
@@ -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::static_pointer_cast<Expression>(True::Instance())
Review Comment:
nit: use checked_pointer_cast
##########
src/iceberg/CMakeLists.txt:
##########
@@ -31,6 +31,7 @@ set(ICEBERG_SOURCES
expression/expression.cc
expression/expressions.cc
expression/inclusive_metrics_evaluator.cc
+ expression/json_serde.cc
Review Comment:
We also need to update `meson.build` for any new file.
##########
src/iceberg/test/expression_json_test.cc:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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 {};
+
+// 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) {
Review Comment:
These two cases share a lot of similarities. Can we use a single test?
##########
src/iceberg/test/CMakeLists.txt:
##########
@@ -88,6 +88,7 @@ add_iceberg_test(table_test
add_iceberg_test(expression_test
SOURCES
aggregate_test.cc
+ expression_json_test.cc
Review Comment:
Same comment for `meson.build`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] [Expr Serde] [PR 1/X] basic plumbing for serde support for expressions [iceberg-cpp]
Posted by "evindj (via GitHub)" <gi...@apache.org>.
evindj commented on code in PR #532:
URL: https://github.com/apache/iceberg-cpp/pull/532#discussion_r2734762597
##########
src/iceberg/expression/json_serde.cc:
##########
@@ -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())
+ : 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");
Review Comment:
> My concern is large refactoring afterwards if we don't have a good design beforehand
You are correct, I will cleanup my notes and shared as design. but I don't expect too much refactoring.
##########
src/iceberg/expression/json_serde_internal.h:
##########
@@ -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"
+#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) \
Review Comment:
Yeah, as I am looking at other literals, predicates, and terms it does seem like it would be better not to have the macro.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org
Re: [PR] feat: add serde for expressions [iceberg-cpp]
Posted by "wgtmac (via GitHub)" <gi...@apache.org>.
wgtmac commented on code in PR #532:
URL: https://github.com/apache/iceberg-cpp/pull/532#discussion_r2748692773
##########
src/iceberg/expression/json_serde_internal.h:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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/expressions.h"
+#include "iceberg/iceberg_export.h"
+#include "iceberg/result.h"
+#include "iceberg/type_fwd.h"
+
+/// \file iceberg/expression/json_serde_internal.h
+/// JSON serialization and deserialization for expressions.
+
+namespace iceberg {
+
+/// \brief Converts an operation type string to an Expression::Operation.
+///
+/// \param typeStr The operation type string
+/// \return The corresponding Operation or an error if unknown
+ICEBERG_EXPORT Result<Expression::Operation> OperationTypeFromString(
+ const std::string_view typeStr);
+
+/// \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);
+
+/// \brief Deserializes a JSON object into an Expression.
+///
+/// \param json A JSON object representing an expression
+/// \return A shared pointer to the deserialized Expression or an error
+ICEBERG_EXPORT Result<std::shared_ptr<Expression>> ExpressionFromJson(
+ const nlohmann::json& json);
+
+/// \brief Serializes an Expression into its JSON representation.
+///
+/// \param expr The expression to serialize
+/// \return A JSON object representing the expression
+ICEBERG_EXPORT nlohmann::json ExpressionToJson(const Expression& expr);
Review Comment:
```suggestion
ICEBERG_EXPORT nlohmann::json ToJson(const Expression& expr);
```
This is consistent with serde impls.
##########
src/iceberg/expression/json_serde.cc:
##########
@@ -0,0 +1,170 @@
+/*
+ * 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/json_serde_internal.h"
+#include "iceberg/expression/literal.h"
+#include "iceberg/util/checked_cast.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
+
+bool IsUnaryOperation(Expression::Operation op) {
+ switch (op) {
+ case Expression::Operation::kIsNull:
+ case Expression::Operation::kNotNull:
+ case Expression::Operation::kIsNan:
+ case Expression::Operation::kNotNan:
+ return true;
+ default:
+ return false;
+ }
+}
+
+bool IsSetOperation(Expression::Operation op) {
+ switch (op) {
+ case Expression::Operation::kIn:
+ case Expression::Operation::kNotIn:
+ return true;
+ default:
+ return false;
+ }
+}
+
+Result<Expression::Operation> OperationTypeFromString(const std::string_view typeStr) {
+ if (typeStr == kTypeTrue) return Expression::Operation::kTrue;
+ if (typeStr == kTypeFalse) return Expression::Operation::kFalse;
+ if (typeStr == kTypeAnd) return Expression::Operation::kAnd;
+ if (typeStr == kTypeOr) return Expression::Operation::kOr;
+ if (typeStr == kTypeNot) return Expression::Operation::kNot;
+ if (typeStr == kTypeEq) return Expression::Operation::kEq;
+ if (typeStr == kTypeNotEq) return Expression::Operation::kNotEq;
+ if (typeStr == kTypeLt) return Expression::Operation::kLt;
+ if (typeStr == kTypeLtEq) return Expression::Operation::kLtEq;
+ if (typeStr == kTypeGt) return Expression::Operation::kGt;
+ if (typeStr == kTypeGtEq) return Expression::Operation::kGtEq;
+ if (typeStr == kTypeIn) return Expression::Operation::kIn;
+ if (typeStr == kTypeNotIn) return Expression::Operation::kNotIn;
+ if (typeStr == kTypeIsNull) return Expression::Operation::kIsNull;
+ if (typeStr == kTypeNotNull) return Expression::Operation::kNotNull;
+ if (typeStr == kTypeIsNan) return Expression::Operation::kIsNan;
+ if (typeStr == kTypeNotNan) return Expression::Operation::kNotNan;
+ if (typeStr == kTypeStartsWith) return Expression::Operation::kStartsWith;
+ if (typeStr == kTypeNotStartsWith) return Expression::Operation::kNotStartsWith;
+
+ return JsonParseError("Unknown expression type: {}", typeStr);
+}
+
+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:
+ ICEBERG_CHECK_OR_DIE(false, "Unknown expression operation.");
+ }
Review Comment:
```suggestion
}
std::unreachable();
```
It is safer to remove `default` branch which enables the compiler to check missing new cases.
The convention in this codebase is to use status and not use exception (except expressions.h which provides fluent api to create expression for users).
##########
src/iceberg/expression/json_serde_internal.h:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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/expressions.h"
Review Comment:
```suggestion
```
Let's avoid using this.
##########
src/iceberg/expression/json_serde_internal.h:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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/expressions.h"
+#include "iceberg/iceberg_export.h"
+#include "iceberg/result.h"
+#include "iceberg/type_fwd.h"
+
+/// \file iceberg/expression/json_serde_internal.h
+/// JSON serialization and deserialization for expressions.
+
+namespace iceberg {
+
+/// \brief Converts an operation type string to an Expression::Operation.
+///
+/// \param typeStr The operation type string
+/// \return The corresponding Operation or an error if unknown
+ICEBERG_EXPORT Result<Expression::Operation> OperationTypeFromString(
+ const std::string_view typeStr);
+
+/// \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);
Review Comment:
```suggestion
ICEBERG_EXPORT std::string_view ToString(Expression::Operation op);
```
Less is more :)
##########
src/iceberg/expression/json_serde.cc:
##########
@@ -0,0 +1,170 @@
+/*
+ * 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/json_serde_internal.h"
+#include "iceberg/expression/literal.h"
+#include "iceberg/util/checked_cast.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
+
+bool IsUnaryOperation(Expression::Operation op) {
+ switch (op) {
+ case Expression::Operation::kIsNull:
+ case Expression::Operation::kNotNull:
+ case Expression::Operation::kIsNan:
+ case Expression::Operation::kNotNan:
+ return true;
+ default:
+ return false;
+ }
+}
+
+bool IsSetOperation(Expression::Operation op) {
+ switch (op) {
+ case Expression::Operation::kIn:
+ case Expression::Operation::kNotIn:
+ return true;
+ default:
+ return false;
+ }
+}
+
+Result<Expression::Operation> OperationTypeFromString(const std::string_view typeStr) {
+ if (typeStr == kTypeTrue) return Expression::Operation::kTrue;
+ if (typeStr == kTypeFalse) return Expression::Operation::kFalse;
+ if (typeStr == kTypeAnd) return Expression::Operation::kAnd;
+ if (typeStr == kTypeOr) return Expression::Operation::kOr;
+ if (typeStr == kTypeNot) return Expression::Operation::kNot;
+ if (typeStr == kTypeEq) return Expression::Operation::kEq;
+ if (typeStr == kTypeNotEq) return Expression::Operation::kNotEq;
+ if (typeStr == kTypeLt) return Expression::Operation::kLt;
+ if (typeStr == kTypeLtEq) return Expression::Operation::kLtEq;
+ if (typeStr == kTypeGt) return Expression::Operation::kGt;
+ if (typeStr == kTypeGtEq) return Expression::Operation::kGtEq;
+ if (typeStr == kTypeIn) return Expression::Operation::kIn;
+ if (typeStr == kTypeNotIn) return Expression::Operation::kNotIn;
+ if (typeStr == kTypeIsNull) return Expression::Operation::kIsNull;
+ if (typeStr == kTypeNotNull) return Expression::Operation::kNotNull;
+ if (typeStr == kTypeIsNan) return Expression::Operation::kIsNan;
+ if (typeStr == kTypeNotNan) return Expression::Operation::kNotNan;
+ if (typeStr == kTypeStartsWith) return Expression::Operation::kStartsWith;
+ if (typeStr == kTypeNotStartsWith) return Expression::Operation::kNotStartsWith;
+
+ return JsonParseError("Unknown expression type: {}", typeStr);
+}
+
+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:
+ ICEBERG_CHECK_OR_DIE(false, "Unknown expression operation.");
+ }
+}
+
+Result<std::shared_ptr<Expression>> ExpressionFromJson(const nlohmann::json& json) {
+ // Handle boolean
+ if (json.is_boolean()) {
+ return json.get<bool>()
+ ? internal::checked_pointer_cast<Expression>(True::Instance())
+ : internal::checked_pointer_cast<Expression>(False::Instance());
+ }
+ return JsonParseError("Only booleans are currently supported.");
+}
+
+nlohmann::json ExpressionToJson(const Expression& expr) {
+ switch (expr.op()) {
+ case Expression::Operation::kTrue:
+ return true;
+
+ case Expression::Operation::kFalse:
+ return false;
+ default:
Review Comment:
Please add a TODO comment to remove `ICEBERG_CHECK_OR_DIE` in the future because we cannot really throw here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org