Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/iceberg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(ICEBERG_SOURCES
expression/expression.cc
expression/expressions.cc
expression/inclusive_metrics_evaluator.cc
expression/json_serde.cc
expression/literal.cc
expression/manifest_evaluator.cc
expression/predicate.cc
Expand Down
64 changes: 64 additions & 0 deletions src/iceberg/expression/json_serde.cc
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::static_pointer_cast<Expression>(True::Instance())
: std::static_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");
}
}

#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
51 changes: 51 additions & 0 deletions src/iceberg/expression/json_serde_internal.h
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"
#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

} // namespace iceberg
1 change: 1 addition & 0 deletions src/iceberg/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ add_iceberg_test(table_test
add_iceberg_test(expression_test
SOURCES
aggregate_test.cc
expression_json_test.cc
expression_test.cc
expression_visitor_test.cc
inclusive_metrics_evaluator_test.cc
Expand Down
68 changes: 68 additions & 0 deletions src/iceberg/test/expression_json_test.cc
Original file line number Diff line number Diff line change
@@ -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) {
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);
}

} // namespace iceberg
Loading