3030
3131namespace iceberg {
3232
33+ // / Operation types for expressions
34+ enum class Operation {
35+ kTrue ,
36+ kFalse ,
37+ kIsNull ,
38+ kNotNull ,
39+ kIsNan ,
40+ kNotNan ,
41+ kLt ,
42+ kLtEq ,
43+ kGt ,
44+ kGtEq ,
45+ kEq ,
46+ kNotEq ,
47+ kIn ,
48+ kNotIn ,
49+ kNot ,
50+ kAnd ,
51+ kOr ,
52+ kStartsWith ,
53+ kNotStartsWith ,
54+ kCount ,
55+ kCountStar ,
56+ kMax ,
57+ kMin
58+ };
59+
60+ // / \brief Returns whether the operation is a predicate operation.
61+ constexpr bool IsPredicate (Operation op) {
62+ switch (op) {
63+ case Operation::kTrue :
64+ case Operation::kFalse :
65+ case Operation::kIsNull :
66+ case Operation::kNotNull :
67+ case Operation::kIsNan :
68+ case Operation::kNotNan :
69+ case Operation::kLt :
70+ case Operation::kLtEq :
71+ case Operation::kGt :
72+ case Operation::kGtEq :
73+ case Operation::kEq :
74+ case Operation::kNotEq :
75+ case Operation::kIn :
76+ case Operation::kNotIn :
77+ case Operation::kNot :
78+ case Operation::kAnd :
79+ case Operation::kOr :
80+ case Operation::kStartsWith :
81+ case Operation::kNotStartsWith :
82+ return true ;
83+ case Operation::kCount :
84+ case Operation::kCountStar :
85+ case Operation::kMax :
86+ case Operation::kMin :
87+ return false ;
88+ }
89+ return false ;
90+ }
91+
3392// / \brief Represents a boolean expression tree.
3493class ICEBERG_EXPORT Expression {
3594 public:
36- // / Operation types for expressions
37- enum class Operation {
38- kTrue ,
39- kFalse ,
40- kIsNull ,
41- kNotNull ,
42- kIsNan ,
43- kNotNan ,
44- kLt ,
45- kLtEq ,
46- kGt ,
47- kGtEq ,
48- kEq ,
49- kNotEq ,
50- kIn ,
51- kNotIn ,
52- kNot ,
53- kAnd ,
54- kOr ,
55- kStartsWith ,
56- kNotStartsWith ,
57- kCount ,
58- kCountStar ,
59- kMax ,
60- kMin
61- };
62-
6395 virtual ~Expression () = default ;
6496
6597 // / \brief Returns the operation for an expression node.
6698 virtual Operation op () const = 0;
6799
68- // / \brief Returns the negation of this expression, equivalent to not(this).
69- virtual std::shared_ptr<Expression> Negate () const {
70- throw IcebergError (" Expression cannot be negated" );
71- }
72-
73100 // / \brief Returns whether this expression will accept the same values as another.
74101 // / \param other another expression
75102 // / \return true if the expressions are equivalent
@@ -78,13 +105,19 @@ class ICEBERG_EXPORT Expression {
78105 return false ;
79106 }
80107
81- virtual std::string ToString () const { return " Expression" ; }
108+ virtual std::string ToString () const = 0;
109+ };
110+
111+ class ICEBERG_EXPORT Predicate : public Expression {
112+ public:
113+ // / \brief Returns a negated version of this predicate.
114+ virtual std::shared_ptr<Predicate> Negate () const = 0;
82115};
83116
84117// / \brief An Expression that is always true.
85118// /
86119// / Represents a boolean predicate that always evaluates to true.
87- class ICEBERG_EXPORT True : public Expression {
120+ class ICEBERG_EXPORT True : public Predicate {
88121 public:
89122 // / \brief Returns the singleton instance
90123 static const std::shared_ptr<True>& Instance ();
@@ -93,7 +126,7 @@ class ICEBERG_EXPORT True : public Expression {
93126
94127 std::string ToString () const override { return " true" ; }
95128
96- std::shared_ptr<Expression > Negate () const override ;
129+ std::shared_ptr<Predicate > Negate () const override ;
97130
98131 bool Equals (const Expression& other) const override {
99132 return other.op () == Operation::kTrue ;
@@ -104,7 +137,7 @@ class ICEBERG_EXPORT True : public Expression {
104137};
105138
106139// / \brief An expression that is always false.
107- class ICEBERG_EXPORT False : public Expression {
140+ class ICEBERG_EXPORT False : public Predicate {
108141 public:
109142 // / \brief Returns the singleton instance
110143 static const std::shared_ptr<False>& Instance ();
@@ -113,7 +146,7 @@ class ICEBERG_EXPORT False : public Expression {
113146
114147 std::string ToString () const override { return " false" ; }
115148
116- std::shared_ptr<Expression > Negate () const override ;
149+ std::shared_ptr<Predicate > Negate () const override ;
117150
118151 bool Equals (const Expression& other) const override {
119152 return other.op () == Operation::kFalse ;
@@ -127,70 +160,70 @@ class ICEBERG_EXPORT False : public Expression {
127160// /
128161// / This expression evaluates to true if and only if both of its child expressions
129162// / evaluate to true.
130- class ICEBERG_EXPORT And : public Expression {
163+ class ICEBERG_EXPORT And : public Predicate {
131164 public:
132165 // / \brief Constructs an And expression from two sub-expressions.
133166 // /
134167 // / \param left The left operand of the AND expression
135168 // / \param right The right operand of the AND expression
136- And (std::shared_ptr<Expression > left, std::shared_ptr<Expression > right);
169+ And (std::shared_ptr<Predicate > left, std::shared_ptr<Predicate > right);
137170
138171 // / \brief Returns the left operand of the AND expression.
139172 // /
140173 // / \return The left operand of the AND expression
141- const std::shared_ptr<Expression >& left () const { return left_; }
174+ const std::shared_ptr<Predicate >& left () const { return left_; }
142175
143176 // / \brief Returns the right operand of the AND expression.
144177 // /
145178 // / \return The right operand of the AND expression
146- const std::shared_ptr<Expression >& right () const { return right_; }
179+ const std::shared_ptr<Predicate >& right () const { return right_; }
147180
148181 Operation op () const override { return Operation::kAnd ; }
149182
150183 std::string ToString () const override ;
151184
152- std::shared_ptr<Expression > Negate () const override ;
185+ std::shared_ptr<Predicate > Negate () const override ;
153186
154187 bool Equals (const Expression& other) const override ;
155188
156189 private:
157- std::shared_ptr<Expression > left_;
158- std::shared_ptr<Expression > right_;
190+ std::shared_ptr<Predicate > left_;
191+ std::shared_ptr<Predicate > right_;
159192};
160193
161194// / \brief An Expression that represents a logical OR operation between two expressions.
162195// /
163196// / This expression evaluates to true if at least one of its child expressions
164197// / evaluates to true.
165- class ICEBERG_EXPORT Or : public Expression {
198+ class ICEBERG_EXPORT Or : public Predicate {
166199 public:
167200 // / \brief Constructs an Or expression from two sub-expressions.
168201 // /
169202 // / \param left The left operand of the OR expression
170203 // / \param right The right operand of the OR expression
171- Or (std::shared_ptr<Expression > left, std::shared_ptr<Expression > right);
204+ Or (std::shared_ptr<Predicate > left, std::shared_ptr<Predicate > right);
172205
173206 // / \brief Returns the left operand of the OR expression.
174207 // /
175208 // / \return The left operand of the OR expression
176- const std::shared_ptr<Expression >& left () const { return left_; }
209+ const std::shared_ptr<Predicate >& left () const { return left_; }
177210
178211 // / \brief Returns the right operand of the OR expression.
179212 // /
180213 // / \return The right operand of the OR expression
181- const std::shared_ptr<Expression >& right () const { return right_; }
214+ const std::shared_ptr<Predicate >& right () const { return right_; }
182215
183216 Operation op () const override { return Operation::kOr ; }
184217
185218 std::string ToString () const override ;
186219
187- std::shared_ptr<Expression > Negate () const override ;
220+ std::shared_ptr<Predicate > Negate () const override ;
188221
189222 bool Equals (const Expression& other) const override ;
190223
191224 private:
192- std::shared_ptr<Expression > left_;
193- std::shared_ptr<Expression > right_;
225+ std::shared_ptr<Predicate > left_;
226+ std::shared_ptr<Predicate > right_;
194227};
195228
196229} // namespace iceberg
0 commit comments