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 " iceberg/expression.h"
21+
22+ #include < unordered_map>
23+
24+ namespace iceberg {
25+
26+ expected<Expression::Operation, Error> Expression::FromString (
27+ const std::string& operation_type) {
28+ std::string lowercase_op = operation_type;
29+ std::transform (lowercase_op.begin (), lowercase_op.end (), lowercase_op.begin (),
30+ [](unsigned char c) { return std::tolower (c); });
31+
32+ static const std::unordered_map<std::string, Operation> op_map = {
33+ {" true" , Operation::kTrue },
34+ {" false" , Operation::kFalse },
35+ {" is_null" , Operation::kIsNull },
36+ {" not_null" , Operation::kNotNull },
37+ {" is_nan" , Operation::kIsNan },
38+ {" not_nan" , Operation::kNotNan },
39+ {" lt" , Operation::kLt },
40+ {" lt_eq" , Operation::kLtEq },
41+ {" gt" , Operation::kGt },
42+ {" gt_eq" , Operation::kGtEq },
43+ {" eq" , Operation::kEq },
44+ {" not_eq" , Operation::kNotEq },
45+ {" in" , Operation::kIn },
46+ {" not_in" , Operation::kNotIn },
47+ {" not" , Operation::kNot },
48+ {" and" , Operation::kAnd },
49+ {" or" , Operation::kOr },
50+ {" starts_with" , Operation::kStartsWith },
51+ {" not_starts_with" , Operation::kNotStartsWith },
52+ {" count" , Operation::kCount },
53+ {" count_star" , Operation::kCountStar },
54+ {" max" , Operation::kMax },
55+ {" min" , Operation::kMin }};
56+
57+ auto it = op_map.find (lowercase_op);
58+ if (it == op_map.end ()) {
59+ return unexpected<Error>(
60+ {ErrorKind::kInvalidOperatorType , " Unknown operation type: " + operation_type});
61+ }
62+ return it->second ;
63+ }
64+
65+ expected<Expression::Operation, Error> Expression::Negate (Operation op) {
66+ switch (op) {
67+ case Operation::kTrue :
68+ return Operation::kFalse ;
69+ case Operation::kFalse :
70+ return Operation::kTrue ;
71+ case Operation::kIsNull :
72+ return Operation::kNotNull ;
73+ case Operation::kNotNull :
74+ return Operation::kIsNull ;
75+ case Operation::kIsNan :
76+ return Operation::kNotNan ;
77+ case Operation::kNotNan :
78+ return Operation::kIsNan ;
79+ case Operation::kLt :
80+ return Operation::kGtEq ;
81+ case Operation::kLtEq :
82+ return Operation::kGt ;
83+ case Operation::kGt :
84+ return Operation::kLtEq ;
85+ case Operation::kGtEq :
86+ return Operation::kLt ;
87+ case Operation::kEq :
88+ return Operation::kNotEq ;
89+ case Operation::kNotEq :
90+ return Operation::kEq ;
91+ case Operation::kIn :
92+ return Operation::kNotIn ;
93+ case Operation::kNotIn :
94+ return Operation::kIn ;
95+ case Operation::kStartsWith :
96+ return Operation::kNotStartsWith ;
97+ case Operation::kNotStartsWith :
98+ return Operation::kStartsWith ;
99+ default :
100+ return unexpected<Error>(
101+ {ErrorKind::kInvalidOperatorType , " No negation defined for operation" });
102+ }
103+ }
104+
105+ expected<Expression::Operation, Error> Expression::FlipLR (Operation op) {
106+ switch (op) {
107+ case Operation::kLt :
108+ return Operation::kGt ;
109+ case Operation::kLtEq :
110+ return Operation::kGtEq ;
111+ case Operation::kGt :
112+ return Operation::kLt ;
113+ case Operation::kGtEq :
114+ return Operation::kLtEq ;
115+ case Operation::kEq :
116+ return Operation::kEq ;
117+ case Operation::kNotEq :
118+ return Operation::kNotEq ;
119+ case Operation::kAnd :
120+ return Operation::kAnd ;
121+ case Operation::kOr :
122+ return Operation::kOr ;
123+ default :
124+ return unexpected<Error>(
125+ {ErrorKind::kInvalidOperatorType , " No left-right flip for operation" });
126+ }
127+ }
128+
129+ } // namespace iceberg
0 commit comments