|
22 | 22 | /// \file iceberg/expression.h |
23 | 23 | /// Expression interface for Iceberg table operations. |
24 | 24 |
|
| 25 | +#include <memory> |
| 26 | +#include <string> |
| 27 | + |
25 | 28 | #include "iceberg/expected.h" |
26 | 29 | #include "iceberg/iceberg_export.h" |
27 | 30 | #include "iceberg/result.h" |
28 | 31 |
|
29 | 32 | namespace iceberg { |
30 | 33 |
|
| 34 | +/// \brief Type alias for shared pointer to Expression |
| 35 | +class Expression; |
| 36 | +using ExpressionPtr = std::shared_ptr<Expression>; |
| 37 | + |
31 | 38 | /// \brief Represents a boolean expression tree. |
32 | 39 | class ICEBERG_EXPORT Expression { |
33 | 40 | public: |
@@ -61,23 +68,100 @@ class ICEBERG_EXPORT Expression { |
61 | 68 | virtual ~Expression() = default; |
62 | 69 |
|
63 | 70 | /// \brief Returns the operation for an expression node. |
64 | | - virtual Operation Op() const = 0; |
| 71 | + virtual Operation op() const = 0; |
65 | 72 |
|
66 | 73 | /// \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 { |
68 | 75 | return unexpected( |
69 | 76 | Error(ErrorKind::kInvalidExpression, "Expression cannot be negated")); |
70 | 77 | } |
71 | 78 |
|
72 | 79 | /// \brief Returns whether this expression will accept the same values as another. |
73 | 80 | /// \param other another expression |
74 | 81 | /// \return true if the expressions are equivalent |
75 | | - virtual bool IsEquivalentTo(const Expression& other) const { |
| 82 | + virtual bool Equals(const Expression& other) const { |
76 | 83 | // only bound predicates can be equivalent |
77 | 84 | return false; |
78 | 85 | } |
79 | 86 |
|
80 | 87 | virtual std::string ToString() const { return "Expression"; } |
81 | 88 | }; |
82 | 89 |
|
| 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 | + |
83 | 167 | } // namespace iceberg |
0 commit comments