Skip to content

Commit 48be4c9

Browse files
committed
feat:expressin apply review comments.
1 parent bcb7506 commit 48be4c9

File tree

11 files changed

+166
-302
lines changed

11 files changed

+166
-302
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ set(ICEBERG_SOURCES
3232
table_metadata.cc
3333
transform.cc
3434
type.cc
35-
expressions/true.cc
36-
expressions/false.cc
37-
expressions/and.cc)
35+
expression.cc)
3836

3937
set(ICEBERG_STATIC_BUILD_INTERFACE_LIBS)
4038
set(ICEBERG_SHARED_BUILD_INTERFACE_LIBS)

src/iceberg/expression.cc

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 "expression.h"
21+
22+
#include <format>
23+
24+
namespace iceberg {
25+
26+
// True implementation
27+
const std::shared_ptr<True>& True::Instance() {
28+
static const std::shared_ptr<True> instance = std::shared_ptr<True>(new True());
29+
return instance;
30+
}
31+
32+
Result<ExpressionPtr> True::Negate() const { return False::Instance(); }
33+
34+
// False implementation
35+
const std::shared_ptr<False>& False::Instance() {
36+
static const std::shared_ptr<False> instance = std::shared_ptr<False>(new False());
37+
return instance;
38+
}
39+
40+
Result<ExpressionPtr> False::Negate() const { return True::Instance(); }
41+
42+
// And implementation
43+
And::And(ExpressionPtr left, ExpressionPtr right)
44+
: left_(std::move(left)), right_(std::move(right)) {}
45+
46+
std::string And::ToString() const {
47+
return std::format("({} and {})", left_->ToString(), right_->ToString());
48+
}
49+
50+
Result<ExpressionPtr> And::Negate() const {
51+
// TODO(yingcai-cy): Implement Or expression
52+
return unexpected(
53+
Error(ErrorKind::kInvalidExpression, "And negation not yet implemented"));
54+
}
55+
56+
bool And::Equals(const Expression& expr) const {
57+
if (expr.op() == Operation::kAnd) {
58+
const auto& other = static_cast<const And&>(expr);
59+
return (left_->Equals(*other.left()) && right_->Equals(*other.right())) ||
60+
(left_->Equals(*other.right()) && right_->Equals(*other.left()));
61+
}
62+
return false;
63+
}
64+
65+
} // namespace iceberg

src/iceberg/expression.h

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@
2222
/// \file iceberg/expression.h
2323
/// Expression interface for Iceberg table operations.
2424

25+
#include <memory>
26+
#include <string>
27+
2528
#include "iceberg/expected.h"
2629
#include "iceberg/iceberg_export.h"
2730
#include "iceberg/result.h"
2831

2932
namespace iceberg {
3033

34+
/// \brief Type alias for shared pointer to Expression
35+
class Expression;
36+
using ExpressionPtr = std::shared_ptr<Expression>;
37+
3138
/// \brief Represents a boolean expression tree.
3239
class ICEBERG_EXPORT Expression {
3340
public:
@@ -61,23 +68,100 @@ class ICEBERG_EXPORT Expression {
6168
virtual ~Expression() = default;
6269

6370
/// \brief Returns the operation for an expression node.
64-
virtual Operation Op() const = 0;
71+
virtual Operation op() const = 0;
6572

6673
/// \brief Returns the negation of this expression, equivalent to not(this).
67-
virtual Result<std::shared_ptr<Expression>> Negate() const {
74+
virtual Result<ExpressionPtr> Negate() const {
6875
return unexpected(
6976
Error(ErrorKind::kInvalidExpression, "Expression cannot be negated"));
7077
}
7178

7279
/// \brief Returns whether this expression will accept the same values as another.
7380
/// \param other another expression
7481
/// \return true if the expressions are equivalent
75-
virtual bool IsEquivalentTo(const Expression& other) const {
82+
virtual bool Equals(const Expression& other) const {
7683
// only bound predicates can be equivalent
7784
return false;
7885
}
7986

8087
virtual std::string ToString() const { return "Expression"; }
8188
};
8289

90+
/// \brief An Expression that is always true.
91+
///
92+
/// Represents a boolean predicate that always evaluates to true.
93+
class ICEBERG_EXPORT True : public Expression {
94+
public:
95+
/// \brief Returns the singleton instance
96+
static const std::shared_ptr<True>& Instance();
97+
98+
Operation op() const override { return Operation::kTrue; }
99+
100+
std::string ToString() const override { return "true"; }
101+
102+
Result<ExpressionPtr> Negate() const override;
103+
104+
bool Equals(const Expression& other) const override {
105+
return other.op() == Operation::kTrue;
106+
}
107+
108+
private:
109+
constexpr True() = default;
110+
};
111+
112+
/// \brief An expression that is always false.
113+
class ICEBERG_EXPORT False : public Expression {
114+
public:
115+
/// \brief Returns the singleton instance
116+
static const std::shared_ptr<False>& Instance();
117+
118+
Operation op() const override { return Operation::kFalse; }
119+
120+
std::string ToString() const override { return "false"; }
121+
122+
Result<ExpressionPtr> Negate() const override;
123+
124+
bool Equals(const Expression& other) const override {
125+
return other.op() == Operation::kFalse;
126+
}
127+
128+
private:
129+
constexpr False() = default;
130+
};
131+
132+
/// \brief An Expression that represents a logical AND operation between two expressions.
133+
///
134+
/// This expression evaluates to true if and only if both of its child expressions
135+
/// evaluate to true.
136+
class ICEBERG_EXPORT And : public Expression {
137+
public:
138+
/// \brief Constructs an And expression from two sub-expressions.
139+
///
140+
/// @param left The left operand of the AND expression
141+
/// @param right The right operand of the AND expression
142+
And(ExpressionPtr left, ExpressionPtr right);
143+
144+
/// \brief Returns the left operand of the AND expression.
145+
///
146+
/// @return The left operand of the AND expression
147+
const ExpressionPtr& left() const { return left_; }
148+
149+
/// \brief Returns the right operand of the AND expression.
150+
///
151+
/// @return The right operand of the AND expression
152+
const ExpressionPtr& right() const { return right_; }
153+
154+
Operation op() const override { return Operation::kAnd; }
155+
156+
std::string ToString() const override;
157+
158+
Result<ExpressionPtr> Negate() const override;
159+
160+
bool Equals(const Expression& other) const override;
161+
162+
private:
163+
ExpressionPtr left_;
164+
ExpressionPtr right_;
165+
};
166+
83167
} // namespace iceberg

src/iceberg/expressions/and.cc

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

src/iceberg/expressions/and.h

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

src/iceberg/expressions/false.cc

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

src/iceberg/expressions/false.h

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

0 commit comments

Comments
 (0)