-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathfunction_step.cc
More file actions
359 lines (303 loc) · 12.6 KB
/
Copy pathfunction_step.cc
File metadata and controls
359 lines (303 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include "eval/eval/function_step.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "google/api/expr/v1alpha1/syntax.pb.h"
#include "google/protobuf/arena.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
#include "base/function.h"
#include "base/function_descriptor.h"
#include "base/handle.h"
#include "base/kind.h"
#include "base/value.h"
#include "base/values/error_value.h"
#include "eval/eval/attribute_trail.h"
#include "eval/eval/evaluator_core.h"
#include "eval/eval/expression_step_base.h"
#include "eval/internal/errors.h"
#include "eval/internal/interop.h"
#include "eval/public/cel_function.h"
#include "eval/public/cel_function_registry.h"
#include "eval/public/cel_value.h"
#include "eval/public/unknown_set.h"
#include "extensions/protobuf/memory_manager.h"
#include "internal/status_macros.h"
#include "runtime/activation_interface.h"
#include "runtime/function_overload_reference.h"
#include "runtime/function_provider.h"
namespace google::api::expr::runtime {
namespace {
using ::cel::FunctionEvaluationContext;
using ::cel::Handle;
using ::cel::Value;
// Determine if the overload should be considered. Overloads that can consume
// errors or unknown sets must be allowed as a non-strict function.
bool ShouldAcceptOverload(const cel::FunctionDescriptor& descriptor,
absl::Span<const cel::Handle<cel::Value>> arguments) {
for (size_t i = 0; i < arguments.size(); i++) {
if (arguments[i]->Is<cel::UnknownValue>() ||
arguments[i]->Is<cel::ErrorValue>()) {
return !descriptor.is_strict();
}
}
return true;
}
bool ArgumentKindsMatch(const cel::FunctionDescriptor& descriptor,
absl::Span<const cel::Handle<cel::Value>> arguments) {
auto types_size = descriptor.types().size();
if (types_size != arguments.size()) {
return false;
}
for (size_t i = 0; i < types_size; i++) {
const auto& arg = arguments[i];
cel::Kind param_kind = descriptor.types()[i];
if (arg->kind() != param_kind && param_kind != CelValue::Type::kAny) {
return false;
}
}
return true;
}
// Convert partially unknown arguments to unknowns before passing to the
// function.
// TODO(issues/52): See if this can be refactored to remove the eager
// arguments copy.
// Argument and attribute spans are expected to be equal length.
std::vector<cel::Handle<cel::Value>> CheckForPartialUnknowns(
ExecutionFrame* frame, absl::Span<const cel::Handle<cel::Value>> args,
absl::Span<const AttributeTrail> attrs) {
std::vector<cel::Handle<cel::Value>> result;
result.reserve(args.size());
for (size_t i = 0; i < args.size(); i++) {
auto attr_set = frame->attribute_utility().CheckForUnknowns(
attrs.subspan(i, 1), /*use_partial=*/true);
if (!attr_set.empty()) {
auto unknown_set = google::protobuf::Arena::Create<UnknownSet>(
cel::extensions::ProtoMemoryManager::CastToProtoArena(
frame->memory_manager()),
std::move(attr_set));
result.push_back(
cel::interop_internal::CreateUnknownValueFromView(unknown_set));
} else {
result.push_back(args.at(i));
}
}
return result;
}
bool IsUnknownFunctionResultError(const Handle<Value>& result) {
if (!result->Is<cel::ErrorValue>()) {
return false;
}
const auto& status = result.As<cel::ErrorValue>()->value();
if (status.code() != absl::StatusCode::kUnavailable) {
return false;
}
auto payload = status.GetPayload(
cel::interop_internal::kPayloadUrlUnknownFunctionResult);
return payload.has_value() && payload.value() == "true";
}
// Simple wrapper around a function resolution result. A function call should
// resolve to a single function implementation and a descriptor or none.
using ResolveResult = absl::optional<cel::FunctionOverloadReference>;
// Implementation of ExpressionStep that finds suitable CelFunction overload and
// invokes it. Abstract base class standardizes behavior between lazy and eager
// function bindings. Derived classes provide ResolveFunction behavior.
class AbstractFunctionStep : public ExpressionStepBase {
public:
// Constructs FunctionStep that uses overloads specified.
AbstractFunctionStep(const std::string& name, size_t num_arguments,
int64_t expr_id)
: ExpressionStepBase(expr_id),
name_(name),
num_arguments_(num_arguments) {}
absl::Status Evaluate(ExecutionFrame* frame) const override;
// Handles overload resolution and updating result appropriately.
// Shouldn't update frame state.
//
// A non-ok result is an unrecoverable error, either from an illegal
// evaluation state or forwarded from an extension function. Errors where
// evaluation can reasonably condition are returned in the result as a
// cel::ErrorValue.
absl::StatusOr<Handle<Value>> DoEvaluate(ExecutionFrame* frame) const;
virtual absl::StatusOr<ResolveResult> ResolveFunction(
absl::Span<const cel::Handle<cel::Value>> args,
const ExecutionFrame* frame) const = 0;
protected:
std::string name_;
size_t num_arguments_;
};
absl::StatusOr<Handle<Value>> AbstractFunctionStep::DoEvaluate(
ExecutionFrame* frame) const {
// Create Span object that contains input arguments to the function.
auto input_args = frame->value_stack().GetSpan(num_arguments_);
std::vector<cel::Handle<cel::Value>> unknowns_args;
// Preprocess args. If an argument is partially unknown, convert it to an
// unknown attribute set.
if (frame->enable_unknowns()) {
auto input_attrs = frame->value_stack().GetAttributeSpan(num_arguments_);
unknowns_args = CheckForPartialUnknowns(frame, input_args, input_attrs);
input_args = absl::MakeConstSpan(unknowns_args);
}
// Derived class resolves to a single function overload or none.
CEL_ASSIGN_OR_RETURN(ResolveResult matched_function,
ResolveFunction(input_args, frame));
// Overload found and is allowed to consume the arguments.
if (matched_function.has_value() &&
ShouldAcceptOverload(matched_function->descriptor, input_args)) {
FunctionEvaluationContext context(frame->value_factory());
CEL_ASSIGN_OR_RETURN(
Handle<Value> result,
matched_function->implementation.Invoke(context, input_args));
if (frame->enable_unknown_function_results() &&
IsUnknownFunctionResultError(result)) {
auto unknown_set = frame->attribute_utility().CreateUnknownSet(
matched_function->descriptor, id(), input_args);
return cel::interop_internal::CreateUnknownValueFromView(unknown_set);
}
return result;
}
// No matching overloads.
// Such absence can be caused by presence of CelError in arguments.
// To enable behavior of functions that accept CelError( &&, || ), CelErrors
// should be propagated along execution path.
for (const auto& arg : input_args) {
if (arg->Is<cel::ErrorValue>()) {
return arg;
}
}
if (frame->enable_unknowns()) {
// Already converted partial unknowns to unknown sets so just merge.
auto unknown_set =
frame->attribute_utility().MergeUnknowns(input_args, nullptr);
if (unknown_set != nullptr) {
return cel::interop_internal::CreateUnknownValueFromView(unknown_set);
}
}
std::string arg_types;
for (const auto& arg : input_args) {
if (!arg_types.empty()) {
absl::StrAppend(&arg_types, ", ");
}
absl::StrAppend(&arg_types, CelValue::TypeName(arg->kind()));
}
// If no errors or unknowns in input args, create new CelError for missing
// overlaod.
return cel::interop_internal::CreateErrorValueFromView(
cel::interop_internal::CreateNoMatchingOverloadError(
frame->memory_manager(), absl::StrCat(name_, "(", arg_types, ")")));
}
absl::Status AbstractFunctionStep::Evaluate(ExecutionFrame* frame) const {
if (!frame->value_stack().HasEnough(num_arguments_)) {
return absl::Status(absl::StatusCode::kInternal, "Value stack underflow");
}
// DoEvaluate may return a status for non-recoverable errors (e.g.
// unexpected typing, illegal expression state). Application errors that can
// reasonably be handled as a cel error will appear in the result value.
CEL_ASSIGN_OR_RETURN(auto result, DoEvaluate(frame));
frame->value_stack().Pop(num_arguments_);
frame->value_stack().Push(std::move(result));
return absl::OkStatus();
}
class EagerFunctionStep : public AbstractFunctionStep {
public:
EagerFunctionStep(std::vector<cel::FunctionOverloadReference> overloads,
const std::string& name, size_t num_args, int64_t expr_id)
: AbstractFunctionStep(name, num_args, expr_id),
overloads_(std::move(overloads)) {}
absl::StatusOr<ResolveResult> ResolveFunction(
absl::Span<const cel::Handle<cel::Value>> input_args,
const ExecutionFrame* frame) const override;
private:
std::vector<cel::FunctionOverloadReference> overloads_;
};
absl::StatusOr<ResolveResult> EagerFunctionStep::ResolveFunction(
absl::Span<const cel::Handle<cel::Value>> input_args,
const ExecutionFrame* frame) const {
ResolveResult result = absl::nullopt;
for (const auto& overload : overloads_) {
if (ArgumentKindsMatch(overload.descriptor, input_args)) {
// More than one overload matches our arguments.
if (result.has_value()) {
return absl::Status(absl::StatusCode::kInternal,
"Cannot resolve overloads");
}
result.emplace(overload);
}
}
return result;
}
class LazyFunctionStep : public AbstractFunctionStep {
public:
// Constructs LazyFunctionStep that attempts to lookup function implementation
// at runtime.
LazyFunctionStep(const std::string& name, size_t num_args,
bool receiver_style,
std::vector<CelFunctionRegistry::LazyOverload> providers,
int64_t expr_id)
: AbstractFunctionStep(name, num_args, expr_id),
receiver_style_(receiver_style),
providers_(std::move(providers)) {}
absl::StatusOr<ResolveResult> ResolveFunction(
absl::Span<const cel::Handle<cel::Value>> input_args,
const ExecutionFrame* frame) const override;
private:
bool receiver_style_;
std::vector<CelFunctionRegistry::LazyOverload> providers_;
};
absl::StatusOr<ResolveResult> LazyFunctionStep::ResolveFunction(
absl::Span<const cel::Handle<cel::Value>> input_args,
const ExecutionFrame* frame) const {
ResolveResult result = absl::nullopt;
std::vector<CelValue::Type> arg_types(num_arguments_);
std::transform(
input_args.begin(), input_args.end(), arg_types.begin(),
[](const cel::Handle<cel::Value>& value) { return value->kind(); });
CelFunctionDescriptor matcher{name_, receiver_style_, arg_types};
const cel::ActivationInterface& activation = frame->modern_activation();
for (auto provider : providers_) {
// The LazyFunctionStep has so far only resolved by function shape, check
// that the runtime argument kinds agree with the specific descriptor for
// the provider candidates.
if (!ArgumentKindsMatch(provider.descriptor, input_args)) {
continue;
}
CEL_ASSIGN_OR_RETURN(auto overload,
provider.provider.GetFunction(matcher, activation));
if (overload.has_value()) {
// More than one overload matches our arguments.
if (result.has_value()) {
return absl::Status(absl::StatusCode::kInternal,
"Cannot resolve overloads");
}
result.emplace(overload.value());
}
}
return result;
}
} // namespace
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateFunctionStep(
const cel::ast::internal::Call& call_expr, int64_t expr_id,
std::vector<CelFunctionRegistry::LazyOverload> lazy_overloads) {
bool receiver_style = call_expr.has_target();
size_t num_args = call_expr.args().size() + (receiver_style ? 1 : 0);
const std::string& name = call_expr.function();
return std::make_unique<LazyFunctionStep>(name, num_args, receiver_style,
std::move(lazy_overloads), expr_id);
}
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateFunctionStep(
const cel::ast::internal::Call& call_expr, int64_t expr_id,
std::vector<cel::FunctionOverloadReference> overloads) {
bool receiver_style = call_expr.has_target();
size_t num_args = call_expr.args().size() + (receiver_style ? 1 : 0);
const std::string& name = call_expr.function();
return std::make_unique<EagerFunctionStep>(std::move(overloads), name,
num_args, expr_id);
}
} // namespace google::api::expr::runtime