Coverage Report

Created: 2026-11-21 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
806k
    const Value& rhs, const AttributeTrail& rhs_attr, bool negation) {
48
806k
  if (lhs.IsError()) {
49
679k
    return lhs;
50
679k
  }
51
52
126k
  if (rhs.IsError()) {
53
46.3k
    return rhs;
54
46.3k
  }
55
56
80.4k
  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
160k
  CEL_ASSIGN_OR_RETURN(auto is_equal,
67
160k
                       ValueEqualImpl(lhs, rhs, frame.descriptor_pool(),
68
160k
                                      frame.message_factory(), frame.arena()));
69
160k
  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
80.4k
  return negation ? BoolValue(!*is_equal) : BoolValue(*is_equal);
76
160k
}
77
78
absl::StatusOr<Value> EvaluateInMap(ExecutionFrameBase& frame,
79
                                    const Value& item,
80
10.0k
                                    const MapValue& container) {
81
10.0k
  switch (item.kind()) {
82
614
    case ValueKind::kBool:
83
2.18k
    case ValueKind::kString:
84
2.93k
    case ValueKind::kInt:
85
3.76k
    case ValueKind::kUint:
86
8.52k
    case ValueKind::kDouble:
87
8.52k
      break;
88
1.54k
    default:
89
1.54k
      return cel::ErrorValue::From(
90
1.54k
          cel::runtime_internal::CreateNoMatchingOverloadError(
91
1.54k
              cel::builtin::kIn),
92
1.54k
          frame.arena());
93
10.0k
  }
94
8.52k
  Value result;
95
8.52k
  CEL_RETURN_IF_ERROR(container.Has(item, frame.descriptor_pool(),
96
8.52k
                                    frame.message_factory(), frame.arena(),
97
8.52k
                                    &result));
98
99
8.52k
  if (result.IsTrue()) {
100
328
    return result;
101
328
  }
102
103
8.20k
  if (item.IsDouble() || item.IsUint()) {
104
5.59k
    Number number = item.IsDouble()
105
5.59k
                        ? Number::FromDouble(item.GetDouble().NativeValue())
106
5.59k
                        : Number::FromUint64(item.GetUint().NativeValue());
107
5.59k
    if (number.LosslessConvertibleToInt()) {
108
4.05k
      CEL_RETURN_IF_ERROR(
109
4.05k
          container.Has(IntValue(number.AsInt()), frame.descriptor_pool(),
110
4.05k
                        frame.message_factory(), frame.arena(), &result));
111
4.05k
      if (result.IsTrue()) {
112
428
        return result;
113
428
      }
114
4.05k
    }
115
5.59k
  }
116
117
7.77k
  if (item.IsDouble() || item.IsInt()) {
118
5.46k
    Number number = item.IsDouble()
119
5.46k
                        ? Number::FromDouble(item.GetDouble().NativeValue())
120
5.46k
                        : Number::FromInt64(item.GetInt().NativeValue());
121
5.46k
    if (number.LosslessConvertibleToUint()) {
122
3.76k
      CEL_RETURN_IF_ERROR(
123
3.76k
          container.Has(UintValue(number.AsUint()), frame.descriptor_pool(),
124
3.76k
                        frame.message_factory(), frame.arena(), &result));
125
3.76k
      if (result.IsTrue()) {
126
107
        return result;
127
107
      }
128
3.76k
    }
129
5.46k
  }
130
131
7.66k
  return BoolValue(false);
132
7.77k
}
133
134
absl::StatusOr<Value> EvaluateIn(ExecutionFrameBase& frame, const Value& item,
135
                                 const AttributeTrail& item_attr,
136
                                 const Value& container,
137
230k
                                 const AttributeTrail& container_attr) {
138
230k
  if (item.IsError()) {
139
7.71k
    return item;
140
7.71k
  }
141
223k
  if (container.IsError()) {
142
8.55k
    return container;
143
8.55k
  }
144
145
214k
  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
214k
  if (container.IsList()) {
155
202k
    return container.GetList().Contains(item, frame.descriptor_pool(),
156
202k
                                        frame.message_factory(), frame.arena());
157
202k
  }
158
11.9k
  if (container.IsMap()) {
159
10.0k
    return EvaluateInMap(frame, item, container.GetMap());
160
10.0k
  }
161
1.92k
  return cel::ErrorValue::From(
162
1.92k
      cel::runtime_internal::CreateNoMatchingOverloadError(cel::builtin::kIn),
163
1.92k
      frame.arena());
164
11.9k
}
165
166
}  // namespace
167
168
806k
void EvaluateFastEqualStep(bool negation, ExecutionFrame& frame) {
169
806k
  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
806k
  absl::Span<const Value> args = frame.value_stack().GetSpan(2);
175
806k
  absl::Span<const AttributeTrail> attrs =
176
806k
      frame.value_stack().GetAttributeSpan(2);
177
178
806k
  absl::StatusOr<Value> result =
179
806k
      EvaluateEquality(frame, args[0], attrs[0], args[1], attrs[1], negation);
180
806k
  if (!result.ok()) {
181
0
    frame.Abort(std::move(result).status());
182
0
    return;
183
0
  }
184
806k
  frame.value_stack().PopAndPush(2, *std::move(result));
185
806k
}
186
187
230k
void EvaluateFastInStep(ExecutionFrame& frame) {
188
230k
  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
230k
  absl::Span<const Value> args = frame.value_stack().GetSpan(2);
194
230k
  absl::Span<const AttributeTrail> attrs =
195
230k
      frame.value_stack().GetAttributeSpan(2);
196
197
230k
  absl::StatusOr<Value> result =
198
230k
      EvaluateIn(frame, args[0], attrs[0], args[1], attrs[1]);
199
230k
  if (!result.ok()) {
200
0
    frame.Abort(std::move(result).status());
201
0
    return;
202
0
  }
203
230k
  frame.value_stack().PopAndPush(2, *std::move(result));
204
230k
}
205
206
}  // namespace google::api::expr::runtime