forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_step.cc
More file actions
537 lines (453 loc) · 18.6 KB
/
Copy pathfunction_step.cc
File metadata and controls
537 lines (453 loc) · 18.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
#include "eval/eval/function_step.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/container/inlined_vector.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
#include "common/casting.h"
#include "common/expr.h"
#include "common/function_descriptor.h"
#include "common/kind.h"
#include "common/value.h"
#include "common/value_kind.h"
#include "eval/eval/attribute_trail.h"
#include "eval/eval/direct_expression_step.h"
#include "eval/eval/evaluator_core.h"
#include "eval/eval/expression_step_base.h"
#include "eval/internal/errors.h"
#include "internal/status_macros.h"
#include "runtime/activation_interface.h"
#include "runtime/function.h"
#include "runtime/function_overload_reference.h"
#include "runtime/function_provider.h"
#include "runtime/function_registry.h"
#include "runtime/internal/errors.h"
namespace google::api::expr::runtime {
namespace {
using ::cel::ErrorValue;
using ::cel::UnknownValue;
using ::cel::Value;
using ::cel::ValueKindToKind;
using ::cel::runtime_internal::CreateNoMatchingOverloadError;
// 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::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::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 != cel::Kind::kAny) {
return false;
}
}
return true;
}
// Adjust new type names to legacy equivalent. int -> int64.
// Temporary fix to migrate value types without breaking clients.
// TODO(uncreated-issue/46): Update client tests that depend on this value.
std::string ToLegacyKindName(absl::string_view type_name) {
if (type_name == "int" || type_name == "uint") {
return absl::StrCat(type_name, "64");
}
return std::string(type_name);
}
std::string CallArgTypeString(absl::Span<const cel::Value> args) {
std::string call_sig_string = "";
for (size_t i = 0; i < args.size(); i++) {
const auto& arg = args[i];
if (!call_sig_string.empty()) {
absl::StrAppend(&call_sig_string, ", ");
}
absl::StrAppend(
&call_sig_string,
ToLegacyKindName(cel::KindToString(ValueKindToKind(arg->kind()))));
}
return absl::StrCat("(", call_sig_string, ")");
}
// 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::Value> CheckForPartialUnknowns(
ExecutionFrame* frame, absl::Span<const cel::Value> args,
absl::Span<const AttributeTrail> attrs) {
std::vector<cel::Value> result;
result.reserve(args.size());
for (size_t i = 0; i < args.size(); i++) {
const AttributeTrail& trail = attrs.subspan(i, 1)[0];
if (frame->attribute_utility().CheckForUnknown(trail,
/*use_partial=*/true)) {
result.push_back(
frame->attribute_utility().CreateUnknownSet(trail.attribute()));
} else {
result.push_back(args.at(i));
}
}
return result;
}
bool IsUnknownFunctionResultError(const Value& result) {
if (!result->Is<cel::ErrorValue>()) {
return false;
}
const auto& status = result.GetError().NativeValue();
if (status.code() != absl::StatusCode::kUnavailable) {
return false;
}
auto payload = status.GetPayload(
cel::runtime_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,
bool receiver_style, int64_t expr_id)
: ExpressionStepBase(expr_id),
name_(name),
num_arguments_(num_arguments),
receiver_style_(receiver_style) {}
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<Value> DoEvaluate(ExecutionFrame* frame) const;
virtual absl::StatusOr<ResolveResult> ResolveFunction(
absl::Span<const cel::Value> args, const ExecutionFrame* frame) const = 0;
protected:
std::string name_;
size_t num_arguments_;
bool receiver_style_;
};
inline absl::StatusOr<Value> Invoke(
const cel::FunctionOverloadReference& overload, int64_t expr_id,
absl::Span<const cel::Value> args, ExecutionFrameBase& frame) {
cel::Function::InvokeContext context(frame.descriptor_pool(),
frame.message_factory(), frame.arena());
if (overload.descriptor.is_contextual()) {
context.set_embedder_context(frame.embedder_context());
}
CEL_ASSIGN_OR_RETURN(Value result,
overload.implementation.Invoke(args, context));
if (frame.unknown_function_results_enabled() &&
IsUnknownFunctionResultError(result)) {
return frame.attribute_utility().CreateUnknownSet(overload.descriptor,
expr_id, args);
}
return result;
}
Value NoOverloadResult(absl::string_view name,
absl::Span<const cel::Value> args, bool receiver_style,
ExecutionFrameBase& frame) {
// 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 (size_t i = 0; i < args.size(); i++) {
const auto& arg = args[i];
if (cel::InstanceOf<cel::ErrorValue>(arg)) {
return arg;
}
}
if (frame.unknown_processing_enabled()) {
// Already converted partial unknowns to unknown sets so just merge.
absl::optional<UnknownValue> unknown_set =
frame.attribute_utility().MergeUnknowns(args);
if (unknown_set.has_value()) {
return *unknown_set;
}
}
// If no errors or unknowns in input args, create new CelError for missing
// overload.
std::string signature;
if (receiver_style) {
if (args.empty()) {
// Should not be possible, but return a sensible error in case of logic
// error.
return ErrorValue(
CreateNoMatchingOverloadError(absl::StrCat("().", name, "()")));
}
return ErrorValue(CreateNoMatchingOverloadError(absl::StrCat(
"(",
ToLegacyKindName(cel::KindToString(ValueKindToKind(args[0].kind()))),
").", name, CallArgTypeString(args.subspan(1)))));
}
return cel::ErrorValue(CreateNoMatchingOverloadError(
absl::StrCat(name, CallArgTypeString(args))));
}
absl::StatusOr<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::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)) {
return Invoke(*matched_function, id(), input_args, *frame);
}
return NoOverloadResult(name_, input_args, receiver_style_, *frame);
}
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().PopAndPush(num_arguments_, std::move(result));
return absl::OkStatus();
}
absl::StatusOr<ResolveResult> ResolveStatic(
absl::Span<const cel::Value> input_args,
absl::Span<const cel::FunctionOverloadReference> overloads) {
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;
}
absl::StatusOr<ResolveResult> ResolveLazy(
absl::Span<const cel::Value> input_args, absl::string_view name,
bool receiver_style,
absl::Span<const cel::FunctionRegistry::LazyOverload> providers,
const ExecutionFrameBase& frame) {
ResolveResult result = absl::nullopt;
std::vector<cel::Kind> arg_types(input_args.size());
std::transform(
input_args.begin(), input_args.end(), arg_types.begin(),
[](const cel::Value& value) { return ValueKindToKind(value->kind()); });
cel::FunctionDescriptor matcher{name, receiver_style, arg_types};
const cel::ActivationInterface& activation = frame.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;
}
class EagerFunctionStep : public AbstractFunctionStep {
public:
EagerFunctionStep(std::vector<cel::FunctionOverloadReference> overloads,
const std::string& name, size_t num_args,
bool receiver_style, int64_t expr_id)
: AbstractFunctionStep(name, num_args, receiver_style, expr_id),
overloads_(std::move(overloads)) {}
absl::StatusOr<ResolveResult> ResolveFunction(
absl::Span<const cel::Value> input_args,
const ExecutionFrame* frame) const override {
return ResolveStatic(input_args, overloads_);
}
private:
std::vector<cel::FunctionOverloadReference> overloads_;
};
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<cel::FunctionRegistry::LazyOverload> providers,
int64_t expr_id)
: AbstractFunctionStep(name, num_args, receiver_style, expr_id),
providers_(std::move(providers)) {}
absl::StatusOr<ResolveResult> ResolveFunction(
absl::Span<const cel::Value> input_args,
const ExecutionFrame* frame) const override;
private:
std::vector<cel::FunctionRegistry::LazyOverload> providers_;
};
absl::StatusOr<ResolveResult> LazyFunctionStep::ResolveFunction(
absl::Span<const cel::Value> input_args,
const ExecutionFrame* frame) const {
return ResolveLazy(input_args, name_, receiver_style_, providers_, *frame);
}
class StaticResolver {
public:
explicit StaticResolver(std::vector<cel::FunctionOverloadReference> overloads)
: overloads_(std::move(overloads)) {}
absl::StatusOr<ResolveResult> Resolve(ExecutionFrameBase& frame,
absl::Span<const Value> input) const {
return ResolveStatic(input, overloads_);
}
private:
std::vector<cel::FunctionOverloadReference> overloads_;
};
class LazyResolver {
public:
explicit LazyResolver(
std::vector<cel::FunctionRegistry::LazyOverload> providers,
std::string name, bool receiver_style)
: providers_(std::move(providers)),
name_(std::move(name)),
receiver_style_(receiver_style) {}
absl::StatusOr<ResolveResult> Resolve(ExecutionFrameBase& frame,
absl::Span<const Value> input) const {
return ResolveLazy(input, name_, receiver_style_, providers_, frame);
}
private:
std::vector<cel::FunctionRegistry::LazyOverload> providers_;
std::string name_;
bool receiver_style_;
};
template <typename Resolver>
class DirectFunctionStepImpl : public DirectExpressionStep {
public:
DirectFunctionStepImpl(
int64_t expr_id, const std::string& name,
std::vector<std::unique_ptr<DirectExpressionStep>> arg_steps,
bool receiver_style, Resolver&& resolver)
: DirectExpressionStep(expr_id),
name_(name),
arg_steps_(std::move(arg_steps)),
receiver_style_(receiver_style),
resolver_(std::forward<Resolver>(resolver)) {}
absl::Status Evaluate(ExecutionFrameBase& frame, cel::Value& result,
AttributeTrail& trail) const override {
absl::InlinedVector<Value, 2> args;
absl::InlinedVector<AttributeTrail, 2> arg_trails;
args.resize(arg_steps_.size());
arg_trails.resize(arg_steps_.size());
for (size_t i = 0; i < arg_steps_.size(); i++) {
CEL_RETURN_IF_ERROR(
arg_steps_[i]->Evaluate(frame, args[i], arg_trails[i]));
}
if (frame.unknown_processing_enabled()) {
for (size_t i = 0; i < arg_trails.size(); i++) {
if (frame.attribute_utility().CheckForUnknown(arg_trails[i],
/*use_partial=*/true)) {
args[i] = frame.attribute_utility().CreateUnknownSet(
arg_trails[i].attribute());
}
}
}
CEL_ASSIGN_OR_RETURN(ResolveResult resolved_function,
resolver_.Resolve(frame, args));
if (resolved_function.has_value() &&
ShouldAcceptOverload(resolved_function->descriptor, args)) {
CEL_ASSIGN_OR_RETURN(result,
Invoke(*resolved_function, expr_id_, args, frame));
return absl::OkStatus();
}
result = NoOverloadResult(name_, args, receiver_style_, frame);
return absl::OkStatus();
}
absl::optional<std::vector<const DirectExpressionStep*>> GetDependencies()
const override {
std::vector<const DirectExpressionStep*> dependencies;
dependencies.reserve(arg_steps_.size());
for (const auto& arg_step : arg_steps_) {
dependencies.push_back(arg_step.get());
}
return dependencies;
}
absl::optional<std::vector<std::unique_ptr<DirectExpressionStep>>>
ExtractDependencies() override {
return std::move(arg_steps_);
}
private:
friend Resolver;
std::string name_;
std::vector<std::unique_ptr<DirectExpressionStep>> arg_steps_;
bool receiver_style_;
Resolver resolver_;
};
} // namespace
std::unique_ptr<DirectExpressionStep> CreateDirectFunctionStep(
int64_t expr_id, const cel::CallExpr& call,
std::vector<std::unique_ptr<DirectExpressionStep>> deps,
std::vector<cel::FunctionOverloadReference> overloads) {
return std::make_unique<DirectFunctionStepImpl<StaticResolver>>(
expr_id, call.function(), std::move(deps), call.has_target(),
StaticResolver(std::move(overloads)));
}
std::unique_ptr<DirectExpressionStep> CreateDirectLazyFunctionStep(
int64_t expr_id, const cel::CallExpr& call,
std::vector<std::unique_ptr<DirectExpressionStep>> deps,
std::vector<cel::FunctionRegistry::LazyOverload> providers) {
return std::make_unique<DirectFunctionStepImpl<LazyResolver>>(
expr_id, call.function(), std::move(deps), call.has_target(),
LazyResolver(std::move(providers), call.function(), call.has_target()));
}
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateFunctionStep(
const cel::CallExpr& call_expr, int64_t expr_id,
std::vector<cel::FunctionRegistry::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::CallExpr& 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, receiver_style, expr_id);
}
} // namespace google::api::expr::runtime