Skip to content

Commit bcb7506

Browse files
committed
feat: expression interface apply review suggestions.
1 parent d191747 commit bcb7506

File tree

10 files changed

+324
-277
lines changed

10 files changed

+324
-277
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ set(ICEBERG_INCLUDES "$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/src>"
2020
set(ICEBERG_SOURCES
2121
arrow_c_data_internal.cc
2222
demo.cc
23-
expression.cc
2423
json_internal.cc
2524
schema.cc
2625
schema_field.cc
@@ -32,7 +31,10 @@ set(ICEBERG_SOURCES
3231
statistics_file.cc
3332
table_metadata.cc
3433
transform.cc
35-
type.cc)
34+
type.cc
35+
expressions/true.cc
36+
expressions/false.cc
37+
expressions/and.cc)
3638

3739
set(ICEBERG_STATIC_BUILD_INTERFACE_LIBS)
3840
set(ICEBERG_SHARED_BUILD_INTERFACE_LIBS)

src/iceberg/expression.cc

Lines changed: 0 additions & 129 deletions
This file was deleted.

src/iceberg/expression.h

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
#pragma once
2121

2222
/// \file iceberg/expression.h
23-
/// Boolean expression tree for Iceberg table operations.
23+
/// Expression interface for Iceberg table operations.
2424

25-
#include <string>
26-
27-
#include "iceberg/error.h"
2825
#include "iceberg/expected.h"
2926
#include "iceberg/iceberg_export.h"
27+
#include "iceberg/result.h"
3028

3129
namespace iceberg {
3230

@@ -63,39 +61,23 @@ class ICEBERG_EXPORT Expression {
6361
virtual ~Expression() = default;
6462

6563
/// \brief Returns the operation for an expression node.
66-
[[nodiscard]] virtual Operation Op() const = 0;
64+
virtual Operation Op() const = 0;
6765

6866
/// \brief Returns the negation of this expression, equivalent to not(this).
69-
[[nodiscard]] virtual expected<std::shared_ptr<Expression>, Error> Negate() const {
70-
return unexpected<Error>(
71-
{ErrorKind::kInvalidExpression, "This expression cannot be negated"});
67+
virtual Result<std::shared_ptr<Expression>> Negate() const {
68+
return unexpected(
69+
Error(ErrorKind::kInvalidExpression, "Expression cannot be negated"));
7270
}
7371

7472
/// \brief Returns whether this expression will accept the same values as another.
75-
///
76-
/// If this returns true, the expressions are guaranteed to return the same evaluation
77-
/// for the same input. However, if this returns false the expressions may return the
78-
/// same evaluation for the same input. That is, expressions may be equivalent even if
79-
/// this returns false.
80-
///
81-
/// For best results, rewrite not and bind expressions before calling this method.
82-
///
8373
/// \param other another expression
8474
/// \return true if the expressions are equivalent
85-
[[nodiscard]] virtual bool IsEquivalentTo(const Expression& other) const {
75+
virtual bool IsEquivalentTo(const Expression& other) const {
8676
// only bound predicates can be equivalent
8777
return false;
8878
}
8979

90-
/// \brief Convert operation string to enum
91-
static expected<Operation, Error> FromString(const std::string& operation_type);
92-
93-
/// \brief Returns the operation used when this is negated.
94-
static expected<Operation, Error> Negate(Operation op);
95-
96-
/// \brief Returns the equivalent operation when the left and right operands are
97-
/// exchanged.
98-
static expected<Operation, Error> FlipLR(Operation op);
80+
virtual std::string ToString() const { return "Expression"; }
9981
};
10082

10183
} // namespace iceberg

src/iceberg/expressions/and.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include "and.h"
21+
22+
namespace iceberg {
23+
24+
And::And(std::shared_ptr<Expression> left, std::shared_ptr<Expression> right)
25+
: left_(std::move(left)), right_(std::move(right)) {}
26+
27+
bool And::IsEquivalentTo(const Expression& expr) const {
28+
if (expr.Op() == Operation::kAnd) {
29+
const auto& other = static_cast<const And&>(expr);
30+
return (left_->IsEquivalentTo(*other.left()) &&
31+
right_->IsEquivalentTo(*other.right())) ||
32+
(left_->IsEquivalentTo(*other.right()) &&
33+
right_->IsEquivalentTo(*other.left()));
34+
}
35+
return false;
36+
}
37+
38+
} // namespace iceberg

src/iceberg/expressions/and.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
22+
#include <format>
23+
#include <memory>
24+
#include <string>
25+
26+
#include "iceberg/expression.h"
27+
28+
namespace iceberg {
29+
30+
/// \brief An Expression that represents a logical AND operation between two expressions.
31+
///
32+
/// This expression evaluates to true if and only if both of its child expressions
33+
/// evaluate to true.
34+
class ICEBERG_EXPORT And : public Expression {
35+
public:
36+
/// \brief Constructs an And expression from two sub-expressions.
37+
///
38+
/// @param left The left operand of the AND expression
39+
/// @param right The right operand of the AND expression
40+
And(std::shared_ptr<Expression> left, std::shared_ptr<Expression> right);
41+
42+
/// \brief Returns the left operand of the AND expression.
43+
///
44+
/// @return The left operand of the AND expression
45+
const std::shared_ptr<Expression>& left() const { return left_; }
46+
47+
/// \brief Returns the right operand of the AND expression.
48+
///
49+
/// @return The right operand of the AND expression
50+
const std::shared_ptr<Expression>& right() const { return right_; }
51+
52+
Operation Op() const override { return Operation::kAnd; }
53+
54+
std::string ToString() const override {
55+
return std::format("({} and {})", left_->ToString(), right_->ToString());
56+
}
57+
58+
// implement Negate later
59+
// expected<std::shared_ptr<Expression>, Error> Negate() const override;
60+
61+
bool IsEquivalentTo(const Expression& other) const override;
62+
63+
private:
64+
std::shared_ptr<Expression> left_;
65+
std::shared_ptr<Expression> right_;
66+
};
67+
68+
} // namespace iceberg

src/iceberg/expressions/false.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include "false.h"
21+
22+
#include "true.h"
23+
24+
namespace iceberg {
25+
26+
expected<std::shared_ptr<Expression>, Error> False::Negate() const {
27+
return True::shared_instance();
28+
}
29+
30+
} // namespace iceberg

0 commit comments

Comments
 (0)