/proc/self/cwd/eval/eval/equality_steps.cc
Line | Count | Source |
1 | | // Copyright 2025 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | #include "eval/eval/equality_steps.h" |
15 | | |
16 | | #include <utility> |
17 | | |
18 | | #include "absl/status/status.h" |
19 | | #include "absl/status/statusor.h" |
20 | | #include "absl/types/span.h" |
21 | | #include "base/builtins.h" |
22 | | #include "common/value.h" |
23 | | #include "common/value_kind.h" |
24 | | #include "eval/eval/attribute_trail.h" |
25 | | #include "eval/eval/evaluator_core.h" |
26 | | #include "internal/number.h" |
27 | | #include "internal/status_macros.h" |
28 | | #include "runtime/internal/errors.h" |
29 | | #include "runtime/standard/equality_functions.h" |
30 | | |
31 | | namespace google::api::expr::runtime { |
32 | | |
33 | | namespace { |
34 | | |
35 | | using ::cel::BoolValue; |
36 | | using ::cel::IntValue; |
37 | | using ::cel::MapValue; |
38 | | using ::cel::UintValue; |
39 | | using ::cel::Value; |
40 | | |
41 | | using ::cel::ValueKind; |
42 | | using ::cel::internal::Number; |
43 | | using ::cel::runtime_internal::ValueEqualImpl; |
44 | | |
45 | | absl::StatusOr<Value> EvaluateEquality( |
46 | | ExecutionFrameBase& frame, const Value& lhs, const AttributeTrail& lhs_attr, |
47 | 666k | const Value& rhs, const AttributeTrail& rhs_attr, bool negation) { |
48 | 666k | if (lhs.IsError()) { |
49 | 544k | return lhs; |
50 | 544k | } |
51 | | |
52 | 121k | if (rhs.IsError()) { |
53 | 64.3k | return rhs; |
54 | 64.3k | } |
55 | | |
56 | 57.1k | if (frame.unknown_processing_enabled()) { |
57 | 0 | UnknownAccumulator accumulator(frame.unknown_tree()); |
58 | 0 | accumulator.MaybeAdd(lhs, lhs_attr); |
59 | 0 | accumulator.MaybeAdd(rhs, rhs_attr); |
60 | 0 | if (auto accumulated = std::move(accumulator).Accumulate(); |
61 | 0 | accumulated.has_value()) { |
62 | 0 | return *accumulated; |
63 | 0 | } |
64 | 0 | } |
65 | | |
66 | 114k | CEL_ASSIGN_OR_RETURN(auto is_equal, |
67 | 114k | ValueEqualImpl(lhs, rhs, frame.descriptor_pool(), |
68 | 114k | frame.message_factory(), frame.arena())); |
69 | 114k | if (!is_equal.has_value()) { |
70 | 0 | return cel::ErrorValue::From( |
71 | 0 | cel::runtime_internal::CreateNoMatchingOverloadError( |
72 | 0 | negation ? cel::builtin::kInequal : cel::builtin::kEqual), |
73 | 0 | frame.arena()); |
74 | 0 | } |
75 | 57.1k | return negation ? BoolValue(!*is_equal) : BoolValue(*is_equal); |
76 | 114k | } |
77 | | |
78 | | absl::StatusOr<Value> EvaluateInMap(ExecutionFrameBase& frame, |
79 | | const Value& item, |
80 | 22.8k | const MapValue& container) { |
81 | 22.8k | switch (item.kind()) { |
82 | 599 | case ValueKind::kBool: |
83 | 11.1k | case ValueKind::kString: |
84 | 11.8k | case ValueKind::kInt: |
85 | 12.2k | case ValueKind::kUint: |
86 | 12.8k | case ValueKind::kDouble: |
87 | 12.8k | break; |
88 | 9.94k | default: |
89 | 9.94k | return cel::ErrorValue::From( |
90 | 9.94k | cel::runtime_internal::CreateNoMatchingOverloadError( |
91 | 9.94k | cel::builtin::kIn), |
92 | 9.94k | frame.arena()); |
93 | 22.8k | } |
94 | 12.8k | Value result; |
95 | 12.8k | CEL_RETURN_IF_ERROR(container.Has(item, frame.descriptor_pool(), |
96 | 12.8k | frame.message_factory(), frame.arena(), |
97 | 12.8k | &result)); |
98 | | |
99 | 12.8k | if (result.IsTrue()) { |
100 | 243 | return result; |
101 | 243 | } |
102 | | |
103 | 12.6k | if (item.IsDouble() || item.IsUint()) { |
104 | 1.04k | Number number = item.IsDouble() |
105 | 1.04k | ? Number::FromDouble(item.GetDouble().NativeValue()) |
106 | 1.04k | : Number::FromUint64(item.GetUint().NativeValue()); |
107 | 1.04k | if (number.LosslessConvertibleToInt()) { |
108 | 773 | CEL_RETURN_IF_ERROR( |
109 | 773 | container.Has(IntValue(number.AsInt()), frame.descriptor_pool(), |
110 | 773 | frame.message_factory(), frame.arena(), &result)); |
111 | 773 | if (result.IsTrue()) { |
112 | 268 | return result; |
113 | 268 | } |
114 | 773 | } |
115 | 1.04k | } |
116 | | |
117 | 12.3k | if (item.IsDouble() || item.IsInt()) { |
118 | 1.25k | Number number = item.IsDouble() |
119 | 1.25k | ? Number::FromDouble(item.GetDouble().NativeValue()) |
120 | 1.25k | : Number::FromInt64(item.GetInt().NativeValue()); |
121 | 1.25k | if (number.LosslessConvertibleToUint()) { |
122 | 906 | CEL_RETURN_IF_ERROR( |
123 | 906 | container.Has(UintValue(number.AsUint()), frame.descriptor_pool(), |
124 | 906 | frame.message_factory(), frame.arena(), &result)); |
125 | 906 | if (result.IsTrue()) { |
126 | 222 | return result; |
127 | 222 | } |
128 | 906 | } |
129 | 1.25k | } |
130 | | |
131 | 12.1k | return BoolValue(false); |
132 | 12.3k | } |
133 | | |
134 | | absl::StatusOr<Value> EvaluateIn(ExecutionFrameBase& frame, const Value& item, |
135 | | const AttributeTrail& item_attr, |
136 | | const Value& container, |
137 | 222k | const AttributeTrail& container_attr) { |
138 | 222k | if (item.IsError()) { |
139 | 8.84k | return item; |
140 | 8.84k | } |
141 | 213k | if (container.IsError()) { |
142 | 6.77k | return container; |
143 | 6.77k | } |
144 | | |
145 | 206k | if (frame.unknown_processing_enabled()) { |
146 | 0 | UnknownAccumulator accumulator(frame.unknown_tree()); |
147 | 0 | accumulator.MaybeAdd(item, item_attr); |
148 | 0 | accumulator.MaybeAdd(container, container_attr); |
149 | 0 | if (auto accumulated = std::move(accumulator).Accumulate(); |
150 | 0 | accumulated.has_value()) { |
151 | 0 | return *accumulated; |
152 | 0 | } |
153 | 0 | } |
154 | 206k | if (container.IsList()) { |
155 | 183k | return container.GetList().Contains(item, frame.descriptor_pool(), |
156 | 183k | frame.message_factory(), frame.arena()); |
157 | 183k | } |
158 | 23.4k | if (container.IsMap()) { |
159 | 22.8k | return EvaluateInMap(frame, item, container.GetMap()); |
160 | 22.8k | } |
161 | 620 | return cel::ErrorValue::From( |
162 | 620 | cel::runtime_internal::CreateNoMatchingOverloadError(cel::builtin::kIn), |
163 | 620 | frame.arena()); |
164 | 23.4k | } |
165 | | |
166 | | } // namespace |
167 | | |
168 | 666k | void EvaluateFastEqualStep(bool negation, ExecutionFrame& frame) { |
169 | 666k | if (!frame.value_stack().HasEnough(2)) { |
170 | 0 | frame.Abort( |
171 | 0 | absl::Status(absl::StatusCode::kInternal, "Value stack underflow")); |
172 | 0 | return; |
173 | 0 | } |
174 | 666k | absl::Span<const Value> args = frame.value_stack().GetSpan(2); |
175 | 666k | absl::Span<const AttributeTrail> attrs = |
176 | 666k | frame.value_stack().GetAttributeSpan(2); |
177 | | |
178 | 666k | absl::StatusOr<Value> result = |
179 | 666k | EvaluateEquality(frame, args[0], attrs[0], args[1], attrs[1], negation); |
180 | 666k | if (!result.ok()) { |
181 | 0 | frame.Abort(std::move(result).status()); |
182 | 0 | return; |
183 | 0 | } |
184 | 666k | frame.value_stack().PopAndPush(2, *std::move(result)); |
185 | 666k | } |
186 | | |
187 | 222k | void EvaluateFastInStep(ExecutionFrame& frame) { |
188 | 222k | if (!frame.value_stack().HasEnough(2)) { |
189 | 0 | frame.Abort( |
190 | 0 | absl::Status(absl::StatusCode::kInternal, "Value stack underflow")); |
191 | 0 | return; |
192 | 0 | } |
193 | 222k | absl::Span<const Value> args = frame.value_stack().GetSpan(2); |
194 | 222k | absl::Span<const AttributeTrail> attrs = |
195 | 222k | frame.value_stack().GetAttributeSpan(2); |
196 | | |
197 | 222k | absl::StatusOr<Value> result = |
198 | 222k | EvaluateIn(frame, args[0], attrs[0], args[1], attrs[1]); |
199 | 222k | if (!result.ok()) { |
200 | 0 | frame.Abort(std::move(result).status()); |
201 | 0 | return; |
202 | 0 | } |
203 | 222k | frame.value_stack().PopAndPush(2, *std::move(result)); |
204 | 222k | } |
205 | | |
206 | | } // namespace google::api::expr::runtime |