2020#include " iceberg/expression/expression.h"
2121
2222#include < format>
23+ #include < utility>
24+
25+ #include " iceberg/util/formatter_internal.h"
26+ #include " iceberg/util/macros.h"
2327
2428namespace iceberg {
2529
@@ -29,15 +33,15 @@ const std::shared_ptr<True>& True::Instance() {
2933 return instance;
3034}
3135
32- std::shared_ptr<Expression> True::Negate () const { return False::Instance (); }
36+ Result< std::shared_ptr<Expression> > True::Negate () const { return False::Instance (); }
3337
3438// False implementation
3539const std::shared_ptr<False>& False::Instance () {
3640 static const std::shared_ptr<False> instance = std::shared_ptr<False>(new False ());
3741 return instance;
3842}
3943
40- std::shared_ptr<Expression> False::Negate () const { return True::Instance (); }
44+ Result< std::shared_ptr<Expression> > False::Negate () const { return True::Instance (); }
4145
4246// And implementation
4347And::And (std::shared_ptr<Expression> left, std::shared_ptr<Expression> right)
@@ -47,11 +51,11 @@ std::string And::ToString() const {
4751 return std::format (" ({} and {})" , left_->ToString (), right_->ToString ());
4852}
4953
50- std::shared_ptr<Expression> And::Negate () const {
54+ Result< std::shared_ptr<Expression> > And::Negate () const {
5155 // De Morgan's law: not(A and B) = (not A) or (not B)
52- auto left_negated = left_->Negate ();
53- auto right_negated = right_->Negate ();
54- return std::make_shared<Or>(left_negated, right_negated);
56+ ICEBERG_ASSIGN_OR_RAISE ( auto left_negated, left_->Negate () );
57+ ICEBERG_ASSIGN_OR_RAISE ( auto right_negated, right_->Negate () );
58+ return std::make_shared<Or>(std::move ( left_negated), std::move ( right_negated) );
5559}
5660
5761bool And::Equals (const Expression& expr) const {
@@ -71,11 +75,11 @@ std::string Or::ToString() const {
7175 return std::format (" ({} or {})" , left_->ToString (), right_->ToString ());
7276}
7377
74- std::shared_ptr<Expression> Or::Negate () const {
78+ Result< std::shared_ptr<Expression> > Or::Negate () const {
7579 // De Morgan's law: not(A or B) = (not A) and (not B)
76- auto left_negated = left_->Negate ();
77- auto right_negated = right_->Negate ();
78- return std::make_shared<And>(left_negated, right_negated);
80+ ICEBERG_ASSIGN_OR_RAISE ( auto left_negated, left_->Negate () );
81+ ICEBERG_ASSIGN_OR_RAISE ( auto right_negated, right_->Negate () );
82+ return std::make_shared<And>(std::move ( left_negated), std::move ( right_negated) );
7983}
8084
8185bool Or::Equals (const Expression& expr) const {
@@ -87,4 +91,104 @@ bool Or::Equals(const Expression& expr) const {
8791 return false ;
8892}
8993
94+ std::string_view ToString (Expression::Operation op) {
95+ switch (op) {
96+ case Expression::Operation::kAnd :
97+ return " AND" ;
98+ case Expression::Operation::kOr :
99+ return " OR" ;
100+ case Expression::Operation::kTrue :
101+ return " TRUE" ;
102+ case Expression::Operation::kFalse :
103+ return " FALSE" ;
104+ case Expression::Operation::kIsNull :
105+ return " IS_NULL" ;
106+ case Expression::Operation::kNotNull :
107+ return " NOT_NULL" ;
108+ case Expression::Operation::kIsNan :
109+ return " IS_NAN" ;
110+ case Expression::Operation::kNotNan :
111+ return " NOT_NAN" ;
112+ case Expression::Operation::kLt :
113+ return " LT" ;
114+ case Expression::Operation::kLtEq :
115+ return " LT_EQ" ;
116+ case Expression::Operation::kGt :
117+ return " GT" ;
118+ case Expression::Operation::kGtEq :
119+ return " GT_EQ" ;
120+ case Expression::Operation::kEq :
121+ return " EQ" ;
122+ case Expression::Operation::kNotEq :
123+ return " NOT_EQ" ;
124+ case Expression::Operation::kIn :
125+ return " IN" ;
126+ case Expression::Operation::kNotIn :
127+ return " NOT_IN" ;
128+ case Expression::Operation::kStartsWith :
129+ return " STARTS_WITH" ;
130+ case Expression::Operation::kNotStartsWith :
131+ return " NOT_STARTS_WITH" ;
132+ case Expression::Operation::kCount :
133+ return " COUNT" ;
134+ case Expression::Operation::kNot :
135+ return " NOT" ;
136+ case Expression::Operation::kCountStar :
137+ return " COUNT_STAR" ;
138+ case Expression::Operation::kMax :
139+ return " MAX" ;
140+ case Expression::Operation::kMin :
141+ return " MIN" ;
142+ }
143+ std::unreachable ();
144+ }
145+
146+ Result<Expression::Operation> Negate (Expression::Operation op) {
147+ switch (op) {
148+ case Expression::Operation::kIsNull :
149+ return Expression::Operation::kNotNull ;
150+ case Expression::Operation::kNotNull :
151+ return Expression::Operation::kIsNull ;
152+ case Expression::Operation::kIsNan :
153+ return Expression::Operation::kNotNan ;
154+ case Expression::Operation::kNotNan :
155+ return Expression::Operation::kIsNan ;
156+ case Expression::Operation::kLt :
157+ return Expression::Operation::kGtEq ;
158+ case Expression::Operation::kLtEq :
159+ return Expression::Operation::kGt ;
160+ case Expression::Operation::kGt :
161+ return Expression::Operation::kLtEq ;
162+ case Expression::Operation::kGtEq :
163+ return Expression::Operation::kLt ;
164+ case Expression::Operation::kEq :
165+ return Expression::Operation::kNotEq ;
166+ case Expression::Operation::kNotEq :
167+ return Expression::Operation::kEq ;
168+ case Expression::Operation::kIn :
169+ return Expression::Operation::kNotIn ;
170+ case Expression::Operation::kNotIn :
171+ return Expression::Operation::kIn ;
172+ case Expression::Operation::kStartsWith :
173+ return Expression::Operation::kNotStartsWith ;
174+ case Expression::Operation::kNotStartsWith :
175+ return Expression::Operation::kStartsWith ;
176+ case Expression::Operation::kTrue :
177+ return Expression::Operation::kFalse ;
178+ case Expression::Operation::kFalse :
179+ return Expression::Operation::kTrue ;
180+ case Expression::Operation::kAnd :
181+ return Expression::Operation::kOr ;
182+ case Expression::Operation::kOr :
183+ return Expression::Operation::kAnd ;
184+ case Expression::Operation::kNot :
185+ case Expression::Operation::kCountStar :
186+ case Expression::Operation::kMax :
187+ case Expression::Operation::kMin :
188+ case Expression::Operation::kCount :
189+ return InvalidArgument (" No negation for operation: {}" , op);
190+ }
191+ std::unreachable ();
192+ }
193+
90194} // namespace iceberg
0 commit comments