forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect_optimization.cc
More file actions
958 lines (832 loc) · 33.8 KB
/
Copy pathselect_optimization.cc
File metadata and controls
958 lines (832 loc) · 33.8 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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "extensions/select_optimization.h"
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/algorithm/container.h"
#include "absl/base/nullability.h"
#include "absl/container/flat_hash_map.h"
#include "absl/functional/overload.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
#include "absl/types/variant.h"
#include "base/attribute.h"
#include "base/builtins.h"
#include "common/ast.h"
#include "common/ast_rewrite.h"
#include "common/casting.h"
#include "common/constant.h"
#include "common/expr.h"
#include "common/function_descriptor.h"
#include "common/kind.h"
#include "common/native_type.h"
#include "common/type.h"
#include "common/value.h"
#include "eval/compiler/flat_expr_builder.h"
#include "eval/compiler/flat_expr_builder_extensions.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 "internal/casts.h"
#include "internal/number.h"
#include "internal/status_macros.h"
#include "runtime/internal/errors.h"
#include "runtime/internal/runtime_friend_access.h"
#include "runtime/internal/runtime_impl.h"
#include "runtime/runtime_builder.h"
#include "google/protobuf/arena.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/message.h"
namespace cel::extensions {
namespace {
using ::cel::Ast;
using ::cel::AstRewriterBase;
using ::cel::CallExpr;
using ::cel::ConstantKind;
using ::cel::Expr;
using ::cel::ExprKind;
using ::cel::SelectExpr;
using ::google::api::expr::runtime::AttributeTrail;
using ::google::api::expr::runtime::DirectExpressionStep;
using ::google::api::expr::runtime::ExecutionFrame;
using ::google::api::expr::runtime::ExecutionFrameBase;
using ::google::api::expr::runtime::ExpressionStepBase;
using ::google::api::expr::runtime::PlannerContext;
using ::google::api::expr::runtime::ProgramOptimizer;
// Represents a single select operation (field access or indexing).
// For struct-typed field accesses, includes the field name and the field
// number.
struct SelectInstruction {
int64_t number;
std::string name;
};
// Represents a single qualifier in a traversal path.
// TODO(uncreated-issue/51): support variable indexes.
using QualifierInstruction =
absl::variant<SelectInstruction, std::string, int64_t, uint64_t, bool>;
struct SelectPath {
Expr* operand;
std::vector<QualifierInstruction> select_instructions;
bool test_only;
// TODO(uncreated-issue/54): support for optionals.
};
// Generates the AST representation of the qualification path for the optimized
// select branch. I.e., the list-typed second argument of the cel.@attribute
// call.
Expr MakeSelectPathExpr(
const std::vector<QualifierInstruction>& select_instructions) {
Expr result;
auto& ast_list = result.mutable_list_expr().mutable_elements();
ast_list.reserve(select_instructions.size());
auto visitor = absl::Overload(
[&](const SelectInstruction& instruction) {
Expr ast_instruction;
Expr field_number;
field_number.mutable_const_expr().set_int64_value(instruction.number);
Expr field_name;
field_name.mutable_const_expr().set_string_value(instruction.name);
auto& field_specifier =
ast_instruction.mutable_list_expr().mutable_elements();
field_specifier.emplace_back().set_expr(std::move(field_number));
field_specifier.emplace_back().set_expr(std::move(field_name));
ast_list.emplace_back().set_expr(std::move(ast_instruction));
},
[&](absl::string_view instruction) {
Expr const_expr;
const_expr.mutable_const_expr().set_string_value(instruction);
ast_list.emplace_back().set_expr(std::move(const_expr));
},
[&](int64_t instruction) {
Expr const_expr;
const_expr.mutable_const_expr().set_int64_value(instruction);
ast_list.emplace_back().set_expr(std::move(const_expr));
},
[&](uint64_t instruction) {
Expr const_expr;
const_expr.mutable_const_expr().set_uint64_value(instruction);
ast_list.emplace_back().set_expr(std::move(const_expr));
},
[&](bool instruction) {
Expr const_expr;
const_expr.mutable_const_expr().set_bool_value(instruction);
ast_list.emplace_back().set_expr(std::move(const_expr));
});
for (const auto& instruction : select_instructions) {
absl::visit(visitor, instruction);
}
return result;
}
// Returns a single select operation based on the inferred type of the operand
// and the field name. If the operand type doesn't define the field, returns
// nullopt.
absl::optional<SelectInstruction> GetSelectInstruction(
const StructType& runtime_type, PlannerContext& planner_context,
absl::string_view field_name) {
auto field_or = planner_context.type_reflector()
.FindStructTypeFieldByName(runtime_type, field_name)
.value_or(absl::nullopt);
if (field_or.has_value()) {
return SelectInstruction{field_or->number(), std::string(field_or->name())};
}
return absl::nullopt;
}
absl::StatusOr<SelectQualifier> SelectQualifierFromList(const ListExpr& list) {
if (list.elements().size() != 2) {
return absl::InvalidArgumentError("Invalid cel.attribute select list");
}
const Expr& field_number = list.elements()[0].expr();
const Expr& field_name = list.elements()[1].expr();
if (!field_number.has_const_expr() ||
!field_number.const_expr().has_int64_value()) {
return absl::InvalidArgumentError(
"Invalid cel.attribute field select number");
}
if (!field_name.has_const_expr() ||
!field_name.const_expr().has_string_value()) {
return absl::InvalidArgumentError(
"Invalid cel.attribute field select name");
}
return FieldSpecifier{field_number.const_expr().int64_value(),
field_name.const_expr().string_value()};
}
// Returns a qualifier instruction derived from a unoptimized ast.
absl::StatusOr<QualifierInstruction> SelectInstructionFromConstant(
const Constant& constant) {
if (constant.has_int_value()) {
return QualifierInstruction(constant.int_value());
} else if (constant.has_uint_value()) {
return QualifierInstruction(constant.uint_value());
} else if (constant.has_bool_value()) {
return QualifierInstruction(constant.bool_value());
} else if (constant.has_string_value()) {
return QualifierInstruction(constant.string_value());
} else if (constant.has_double_value()) {
cel::internal::Number number(constant.double_value());
if (number.LosslessConvertibleToInt()) {
return QualifierInstruction(number.AsInt());
} else if (number.LosslessConvertibleToUint()) {
return QualifierInstruction(number.AsUint());
}
}
return absl::InvalidArgumentError("invalid index constant for cel.attribute");
}
absl::StatusOr<SelectQualifier> SelectQualifierFromConstant(
const Constant& constant) {
if (constant.has_int_value()) {
return AttributeQualifier::OfInt(constant.int_value());
} else if (constant.has_uint_value()) {
return AttributeQualifier::OfUint(constant.uint_value());
} else if (constant.has_bool_value()) {
return AttributeQualifier::OfBool(constant.bool_value());
} else if (constant.has_string_value()) {
return AttributeQualifier::OfString(constant.string_value());
}
// TODO(uncreated-issue/51): double keys could possibly be valid selectors, but
// the other stacks don't implement the optimization yet and we normalize the
// key to a uint or int if we do the late AST rewrite during planning.
return absl::InvalidArgumentError("invalid cel.attribute constant");
}
absl::StatusOr<size_t> ListIndexFromQualifier(const AttributeQualifier& qual) {
int64_t value = -1;
switch (qual.kind()) {
case Kind::kInt:
value = *qual.GetInt64Key();
break;
default:
// TODO(uncreated-issue/51): type-checker will reject an unsigned literal, but
// should be supported as a dyn / variable.
return runtime_internal::CreateNoMatchingOverloadError(
cel::builtin::kIndex);
}
if (value < 0) {
return absl::InvalidArgumentError("list index less than 0");
}
return static_cast<size_t>(value);
}
absl::StatusOr<Value> MapKeyFromQualifier(const AttributeQualifier& qual,
google::protobuf::Arena* absl_nonnull arena) {
switch (qual.kind()) {
case Kind::kInt:
return cel::IntValue(*qual.GetInt64Key());
case Kind::kUint:
return cel::UintValue(*qual.GetUint64Key());
case Kind::kBool:
return cel::BoolValue(*qual.GetBoolKey());
case Kind::kString:
return StringValue::From(*qual.GetStringKey(), arena);
default:
return runtime_internal::CreateNoMatchingOverloadError(
cel::builtin::kIndex);
}
}
absl::StatusOr<Value> ApplyQualifier(
const Value& operand, const SelectQualifier& qualifier,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena) {
return absl::visit(
absl::Overload(
[&](const FieldSpecifier& field_specifier) -> absl::StatusOr<Value> {
if (!operand.Is<StructValue>()) {
return cel::ErrorValue(
cel::runtime_internal::CreateNoMatchingOverloadError(
"<select>"));
}
return operand.GetStruct().GetFieldByName(
field_specifier.name, descriptor_pool, message_factory, arena);
},
[&](const AttributeQualifier& qualifier) -> absl::StatusOr<Value> {
if (operand.Is<ListValue>()) {
auto index_or = ListIndexFromQualifier(qualifier);
if (!index_or.ok()) {
return cel::ErrorValue(index_or.status());
}
return operand.GetList().Get(*index_or, descriptor_pool,
message_factory, arena);
} else if (operand.Is<MapValue>()) {
auto key_or = MapKeyFromQualifier(qualifier, arena);
if (!key_or.ok()) {
return cel::ErrorValue(key_or.status());
}
return operand.GetMap().Get(*key_or, descriptor_pool,
message_factory, arena);
}
return cel::ErrorValue(
cel::runtime_internal::CreateNoMatchingOverloadError(
cel::builtin::kIndex));
}),
qualifier);
}
absl::StatusOr<Value> FallbackSelect(
const Value& root, absl::Span<const SelectQualifier> select_path,
bool presence_test,
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
google::protobuf::MessageFactory* absl_nonnull message_factory,
google::protobuf::Arena* absl_nonnull arena) {
const Value* elem = &root;
Value result;
for (const auto& instruction :
select_path.subspan(0, select_path.size() - 1)) {
CEL_ASSIGN_OR_RETURN(result,
ApplyQualifier(*elem, instruction, descriptor_pool,
message_factory, arena));
if (result->Is<ErrorValue>()) {
return result;
}
elem = &result;
}
const auto& last_instruction = select_path.back();
if (presence_test) {
return absl::visit(
absl::Overload(
[&](const FieldSpecifier& field_specifier)
-> absl::StatusOr<Value> {
if (!elem->Is<StructValue>()) {
return cel::ErrorValue(
cel::runtime_internal::CreateNoMatchingOverloadError(
"<select>"));
}
CEL_ASSIGN_OR_RETURN(
bool present,
elem->GetStruct().HasFieldByName(field_specifier.name));
return cel::BoolValue(present);
},
[&](const AttributeQualifier& qualifier) -> absl::StatusOr<Value> {
if (!elem->Is<MapValue>() || qualifier.kind() != Kind::kString) {
return cel::ErrorValue(
cel::runtime_internal::CreateNoMatchingOverloadError(
"has"));
}
return elem->GetMap().Has(
StringValue(arena, *qualifier.GetStringKey()),
descriptor_pool, message_factory, arena);
}),
last_instruction);
}
return ApplyQualifier(*elem, last_instruction, descriptor_pool,
message_factory, arena);
}
absl::StatusOr<std::vector<SelectQualifier>> SelectInstructionsFromCall(
const CallExpr& call) {
if (call.args().size() < 2 || !call.args()[1].has_list_expr()) {
return absl::InvalidArgumentError("Invalid cel.attribute call");
}
std::vector<SelectQualifier> instructions;
const auto& ast_path = call.args()[1].list_expr().elements();
instructions.reserve(ast_path.size());
for (const ListExprElement& element : ast_path) {
// Optimized field select.
if (element.has_expr()) {
const auto& element_expr = element.expr();
if (element_expr.has_list_expr()) {
CEL_ASSIGN_OR_RETURN(instructions.emplace_back(),
SelectQualifierFromList(element_expr.list_expr()));
} else if (element_expr.has_const_expr()) {
CEL_ASSIGN_OR_RETURN(
instructions.emplace_back(),
SelectQualifierFromConstant(element_expr.const_expr()));
} else {
return absl::InvalidArgumentError("Invalid cel.attribute call");
}
} else {
return absl::InvalidArgumentError("Invalid cel.attribute call");
}
}
// TODO(uncreated-issue/54): support for optionals.
return instructions;
}
class RewriterImpl : public AstRewriterBase {
public:
RewriterImpl(const Ast& ast, PlannerContext& planner_context)
: ast_(ast), planner_context_(planner_context) {}
void PreVisitExpr(const Expr& expr) override { path_.push_back(&expr); }
void PreVisitSelect(const Expr& expr, const SelectExpr& select) override {
const Expr& operand = select.operand();
const std::string& field_name = select.field();
// Select optimization can generalize to lists and maps, but for now only
// support message traversal.
const TypeSpec checker_type = ast_.GetTypeOrDyn(operand.id());
absl::optional<Type> rt_type =
(checker_type.has_message_type())
? GetRuntimeType(checker_type.message_type().type())
: absl::nullopt;
if (rt_type.has_value() && (*rt_type).Is<StructType>()) {
const StructType& runtime_type = rt_type->GetStruct();
absl::optional<SelectInstruction> field_or =
GetSelectInstruction(runtime_type, planner_context_, field_name);
if (field_or.has_value()) {
candidates_[&expr] = std::move(field_or).value();
}
} else if (checker_type.has_map_type()) {
candidates_[&expr] = QualifierInstruction(field_name);
}
// else
// TODO(uncreated-issue/54): add support for either dyn or any. Excluded to
// simplify program plan.
}
void PreVisitCall(const Expr& expr, const CallExpr& call) override {
if (call.args().size() != 2 || call.function() != ::cel::builtin::kIndex) {
return;
}
const auto& qualifier_expr = call.args()[1];
if (qualifier_expr.has_const_expr()) {
auto qualifier_or =
SelectInstructionFromConstant(qualifier_expr.const_expr());
if (!qualifier_or.ok()) {
// TODO(uncreated-issue/54): should warn, but by default warnings fail overall
// program planning.
return;
}
candidates_[&expr] = std::move(qualifier_or).value();
}
// TODO(uncreated-issue/54): support variable indexes
}
bool PostVisitRewrite(Expr& expr) override {
if (!progress_status_.ok()) {
return false;
}
path_.pop_back();
auto candidate_iter = candidates_.find(&expr);
if (candidate_iter == candidates_.end()) {
return false;
}
// On post visit, filter candidates that aren't rooted on a message or a
// select chain.
const QualifierInstruction& candidate = candidate_iter->second;
if (!HasOptimizeableRoot(&expr, candidate)) {
candidates_.erase(candidate_iter);
return false;
}
if (!path_.empty() && candidates_.find(path_.back()) != candidates_.end()) {
// parent is optimizeable, defer rewriting until we consider the parent.
return false;
}
SelectPath path = GetSelectPath(&expr);
// generate the new cel.attribute call.
absl::string_view fn = path.test_only ? kCelHasField : kCelAttribute;
Expr operand(std::move(*path.operand));
Expr call;
call.set_id(expr.id());
call.mutable_call_expr().set_function(std::string(fn));
call.mutable_call_expr().mutable_args().reserve(2);
call.mutable_call_expr().mutable_args().push_back(std::move(operand));
call.mutable_call_expr().mutable_args().push_back(
MakeSelectPathExpr(path.select_instructions));
// TODO(uncreated-issue/54): support for optionals.
expr = std::move(call);
return true;
}
absl::Status GetProgressStatus() const { return progress_status_; }
private:
SelectPath GetSelectPath(Expr* expr) {
SelectPath result;
result.test_only = false;
Expr* operand = expr;
auto candidate_iter = candidates_.find(operand);
while (candidate_iter != candidates_.end()) {
result.select_instructions.push_back(candidate_iter->second);
if (operand->has_select_expr()) {
if (operand->select_expr().test_only()) {
result.test_only = true;
}
operand = &(operand->mutable_select_expr().mutable_operand());
} else {
ABSL_DCHECK(operand->has_call_expr());
operand = &(operand->mutable_call_expr().mutable_args()[0]);
}
candidate_iter = candidates_.find(operand);
}
absl::c_reverse(result.select_instructions);
result.operand = operand;
return result;
}
// Check whether the candidate has a message type as a root (the operand for
// the batched select operation).
// Called on post visit.
bool HasOptimizeableRoot(const Expr* expr,
const QualifierInstruction& candidate) {
if (absl::holds_alternative<SelectInstruction>(candidate)) {
return true;
}
const Expr* operand = nullptr;
if (expr->has_call_expr() && expr->call_expr().args().size() == 2 &&
expr->call_expr().function() == ::cel::builtin::kIndex) {
operand = &expr->call_expr().args()[0];
} else if (expr->has_select_expr()) {
operand = &expr->select_expr().operand();
}
if (operand == nullptr) {
return false;
}
return candidates_.find(operand) != candidates_.end();
}
absl::optional<Type> GetRuntimeType(absl::string_view type_name) {
return planner_context_.type_reflector().FindType(type_name).value_or(
absl::nullopt);
}
void SetProgressStatus(const absl::Status& status) {
if (progress_status_.ok() && !status.ok()) {
progress_status_ = status;
}
}
const Ast& ast_;
PlannerContext& planner_context_;
// ids of potentially optimizeable expr nodes.
absl::flat_hash_map<const Expr*, QualifierInstruction> candidates_;
std::vector<const Expr*> path_;
absl::Status progress_status_;
};
class OptimizedSelectImpl {
public:
OptimizedSelectImpl(std::vector<SelectQualifier> select_path,
std::vector<AttributeQualifier> qualifiers,
bool presence_test, SelectOptimizationOptions options)
: select_path_(std::move(select_path)),
qualifiers_(std::move(qualifiers)),
presence_test_(presence_test),
options_(options)
{
ABSL_DCHECK(!select_path_.empty());
}
// Move constructible.
OptimizedSelectImpl(const OptimizedSelectImpl&) = delete;
OptimizedSelectImpl& operator=(const OptimizedSelectImpl&) = delete;
OptimizedSelectImpl(OptimizedSelectImpl&&) = default;
OptimizedSelectImpl& operator=(OptimizedSelectImpl&&) = delete;
absl::StatusOr<Value> ApplySelect(ExecutionFrameBase& frame,
const StructValue& struct_value) const;
AttributeTrail GetAttributeTrail(const AttributeTrail& operand_trail) const;
absl::optional<Attribute> attribute() const { return attribute_; }
const std::vector<AttributeQualifier>& qualifiers() const {
return qualifiers_;
}
private:
absl::optional<Attribute> attribute_;
std::vector<SelectQualifier> select_path_;
std::vector<AttributeQualifier> qualifiers_;
bool presence_test_;
SelectOptimizationOptions options_;
};
// Check for unknowns or missing attributes.
absl::StatusOr<absl::optional<Value>> CheckForMarkedAttributes(
ExecutionFrameBase& frame, const AttributeTrail& attribute_trail) {
if (attribute_trail.empty()) {
return absl::nullopt;
}
if (frame.unknown_processing_enabled() &&
frame.attribute_utility().CheckForUnknownExact(attribute_trail)) {
// Check if the inferred attribute is marked. Only matches if this attribute
// or a parent is marked unknown (use_partial = false).
// Partial matches (i.e. descendant of this attribute is marked) aren't
// considered yet in case another operation would select an unmarked
// descended attribute.
//
// TODO(uncreated-issue/51): this may return a more specific attribute than the
// declared pattern. Follow up will truncate the returned attribute to match
// the pattern.
return frame.attribute_utility().CreateUnknownSet(
attribute_trail.attribute());
}
if (frame.missing_attribute_errors_enabled() &&
frame.attribute_utility().CheckForMissingAttribute(attribute_trail)) {
return frame.attribute_utility().CreateMissingAttributeError(
attribute_trail.attribute());
}
return absl::nullopt;
}
absl::StatusOr<Value> OptimizedSelectImpl::ApplySelect(
ExecutionFrameBase& frame, const StructValue& struct_value) const {
auto value_or =
(options_.force_fallback_implementation)
? absl::UnimplementedError("Forced fallback impl")
: struct_value.Qualify(select_path_, presence_test_,
frame.descriptor_pool(),
frame.message_factory(), frame.arena());
if (!value_or.ok()) {
if (value_or.status().code() == absl::StatusCode::kUnimplemented) {
return FallbackSelect(struct_value, select_path_, presence_test_,
frame.descriptor_pool(), frame.message_factory(),
frame.arena());
}
return value_or.status();
}
if (value_or->second < 0 || value_or->second >= select_path_.size()) {
return std::move(value_or->first);
}
return FallbackSelect(
value_or->first,
absl::MakeConstSpan(select_path_).subspan(value_or->second),
presence_test_, frame.descriptor_pool(), frame.message_factory(),
frame.arena());
}
AttributeTrail OptimizedSelectImpl::GetAttributeTrail(
const AttributeTrail& operand_trail) const {
if (operand_trail.empty()) {
return AttributeTrail();
}
std::vector<AttributeQualifier> qualifiers = std::vector<AttributeQualifier>(
operand_trail.attribute().qualifier_path().begin(),
operand_trail.attribute().qualifier_path().end());
qualifiers.reserve(qualifiers_.size() + qualifiers.size());
absl::c_copy(qualifiers_, std::back_inserter(qualifiers));
return AttributeTrail(
Attribute(std::string(operand_trail.attribute().variable_name()),
std::move(qualifiers)));
}
class StackMachineImpl : public ExpressionStepBase {
public:
StackMachineImpl(int expr_id, OptimizedSelectImpl impl)
: ExpressionStepBase(expr_id), impl_(std::move(impl)) {}
absl::Status Evaluate(ExecutionFrame* frame) const override;
private:
// Get the effective attribute for the optimized select expression.
// Assumes the operand is the top of stack if the attribute wasn't known at
// plan time.
AttributeTrail GetAttributeTrail(ExecutionFrame* frame) const;
OptimizedSelectImpl impl_;
};
AttributeTrail StackMachineImpl::GetAttributeTrail(
ExecutionFrame* frame) const {
const auto& attr = frame->value_stack().PeekAttribute();
return impl_.GetAttributeTrail(attr);
}
absl::Status StackMachineImpl::Evaluate(ExecutionFrame* frame) const {
// Default empty.
AttributeTrail attribute_trail;
// TODO(uncreated-issue/51): add support for variable qualifiers and string literal
// variable names.
constexpr size_t kStackInputs = 1;
// For now, we expect the operand to be top of stack.
const Value& operand = frame->value_stack().Peek();
if (operand->Is<ErrorValue>() || operand->Is<UnknownValue>()) {
// Just forward the error which is already top of stack.
return absl::OkStatus();
}
if (frame->enable_attribute_tracking()) {
// Compute the attribute trail then check for any marked values.
// When possible, this is computed at plan time based on the optimized
// select arguments.
// TODO(uncreated-issue/51): add support variable qualifiers
attribute_trail = GetAttributeTrail(frame);
CEL_ASSIGN_OR_RETURN(absl::optional<Value> value,
CheckForMarkedAttributes(*frame, attribute_trail));
if (value.has_value()) {
frame->value_stack().Pop(kStackInputs);
frame->value_stack().Push(std::move(value).value(),
std::move(attribute_trail));
return absl::OkStatus();
}
}
if (!operand->Is<StructValue>()) {
return absl::InvalidArgumentError(
"Expected struct type for select optimization.");
}
CEL_ASSIGN_OR_RETURN(Value result,
impl_.ApplySelect(*frame, operand.GetStruct()));
frame->value_stack().Pop(kStackInputs);
frame->value_stack().Push(std::move(result), std::move(attribute_trail));
return absl::OkStatus();
}
class RecursiveImpl : public DirectExpressionStep {
public:
RecursiveImpl(int64_t expr_id, std::unique_ptr<DirectExpressionStep> operand,
OptimizedSelectImpl impl)
: DirectExpressionStep(expr_id),
operand_(std::move(operand)),
impl_(std::move(impl)) {}
absl::Status Evaluate(ExecutionFrameBase& frame, Value& result,
AttributeTrail& attribute) const override;
private:
// Get the effective attribute for the optimized select expression.
// Assumes the operand is the top of stack if the attribute wasn't known at
// plan time.
AttributeTrail GetAttributeTrail(const AttributeTrail& operand_trail) const;
std::unique_ptr<DirectExpressionStep> operand_;
OptimizedSelectImpl impl_;
};
AttributeTrail RecursiveImpl::GetAttributeTrail(
const AttributeTrail& operand_trail) const {
return impl_.GetAttributeTrail(operand_trail);
}
absl::Status RecursiveImpl::Evaluate(ExecutionFrameBase& frame, Value& result,
AttributeTrail& attribute) const {
CEL_RETURN_IF_ERROR(operand_->Evaluate(frame, result, attribute));
if (InstanceOf<ErrorValue>(result) || InstanceOf<UnknownValue>(result)) {
// Just forward.
return absl::OkStatus();
}
if (frame.attribute_tracking_enabled()) {
attribute = impl_.GetAttributeTrail(attribute);
CEL_ASSIGN_OR_RETURN(auto value,
CheckForMarkedAttributes(frame, attribute));
if (value.has_value()) {
result = std::move(value).value();
return absl::OkStatus();
}
}
if (!InstanceOf<StructValue>(result)) {
return absl::InvalidArgumentError(
"Expected struct type for select optimization");
}
CEL_ASSIGN_OR_RETURN(result,
impl_.ApplySelect(frame, Cast<StructValue>(result)));
return absl::OkStatus();
}
class SelectOptimizer : public ProgramOptimizer {
public:
explicit SelectOptimizer(const SelectOptimizationOptions& options)
: options_(options) {}
absl::Status OnPreVisit(PlannerContext& context, const Expr& node) override {
return absl::OkStatus();
}
absl::Status OnPostVisit(PlannerContext& context, const Expr& node) override;
private:
SelectOptimizationOptions options_;
};
absl::Status SelectOptimizer::OnPostVisit(PlannerContext& context,
const Expr& node) {
if (!node.has_call_expr()) {
return absl::OkStatus();
}
absl::string_view fn = node.call_expr().function();
if (fn != kCelHasField && fn != kCelAttribute) {
return absl::OkStatus();
}
if (node.call_expr().args().size() < 2 ||
node.call_expr().args().size() > 3) {
return absl::InvalidArgumentError("Invalid cel.attribute call");
}
if (node.call_expr().args().size() == 3) {
return absl::UnimplementedError("Optionals not yet supported");
}
CEL_ASSIGN_OR_RETURN(std::vector<SelectQualifier> instructions,
SelectInstructionsFromCall(node.call_expr()));
if (instructions.empty()) {
return absl::InvalidArgumentError("Invalid cel.attribute no select steps.");
}
bool presence_test = false;
if (fn == kCelHasField) {
presence_test = true;
}
const Expr& operand = node.call_expr().args()[0];
absl::string_view identifier;
if (operand.has_ident_expr()) {
identifier = operand.ident_expr().name();
}
if (absl::StrContains(identifier, ".")) {
return absl::UnimplementedError("qualified identifiers not supported.");
}
std::vector<AttributeQualifier> qualifiers;
qualifiers.reserve(instructions.size());
for (const auto& instruction : instructions) {
qualifiers.push_back(
absl::visit(absl::Overload(
[](const FieldSpecifier& field) {
return AttributeQualifier::OfString(field.name);
},
[](const AttributeQualifier& q) { return q; }),
instruction));
}
// TODO(uncreated-issue/51): If the first argument is a string literal, the custom
// step needs to handle variable lookup.
auto* subexpression = context.program_builder().GetSubexpression(&node);
if (subexpression == nullptr || subexpression->IsFlattened()) {
// No information on the subprogram, can't optimize.
return absl::OkStatus();
}
OptimizedSelectImpl impl(std::move(instructions), std::move(qualifiers),
presence_test, options_);
if (subexpression->IsRecursive()) {
auto program = subexpression->ExtractRecursiveProgram();
auto deps = program.step->ExtractDependencies();
if (!deps.has_value() || deps->empty()) {
return absl::InvalidArgumentError("Unexpected cel.@attribute call");
}
subexpression->set_recursive_program(
std::make_unique<RecursiveImpl>(node.id(), std::move(deps->at(0)),
std::move(impl)),
program.depth);
return absl::OkStatus();
}
google::api::expr::runtime::ExecutionPath path;
// else, we need to preserve the original plan for the first argument.
if (context.GetSubplan(operand).empty()) {
// Indicates another extension modified the step. Nothing to do here.
return absl::OkStatus();
}
CEL_ASSIGN_OR_RETURN(auto operand_subplan, context.ExtractSubplan(operand));
absl::c_move(operand_subplan, std::back_inserter(path));
path.push_back(
std::make_unique<StackMachineImpl>(node.id(), std::move(impl)));
return context.ReplaceSubplan(node, std::move(path));
}
google::api::expr::runtime::FlatExprBuilder* GetFlatExprBuilder(
RuntimeBuilder& builder) {
auto& runtime =
runtime_internal::RuntimeFriendAccess::GetMutableRuntime(builder);
if (runtime_internal::RuntimeFriendAccess::RuntimeTypeId(runtime) ==
NativeTypeId::For<runtime_internal::RuntimeImpl>()) {
auto& runtime_impl =
cel::internal::down_cast<runtime_internal::RuntimeImpl&>(runtime);
return &runtime_impl.expr_builder();
}
return nullptr;
}
} // namespace
absl::Status SelectOptimizationAstUpdater::UpdateAst(PlannerContext& context,
Ast& ast) const {
RewriterImpl rewriter(ast, context);
AstRewrite(ast.mutable_root_expr(), rewriter);
return rewriter.GetProgressStatus();
}
google::api::expr::runtime::ProgramOptimizerFactory
CreateSelectOptimizationProgramOptimizer(
const SelectOptimizationOptions& options) {
return [=](PlannerContext& context, const Ast& ast) {
return std::make_unique<SelectOptimizer>(options);
};
}
absl::Status EnableSelectOptimization(
cel::RuntimeBuilder& builder, const SelectOptimizationOptions& options) {
auto* flat_expr_builder = GetFlatExprBuilder(builder);
if (flat_expr_builder == nullptr) {
return absl::InvalidArgumentError(
"SelectOptimization requires default runtime implementation");
}
flat_expr_builder->AddAstTransform(
std::make_unique<SelectOptimizationAstUpdater>());
// Add overloads for select optimization signature.
// These are never bound, only used to prevent the builder from failing on
// the overloads check.
CEL_RETURN_IF_ERROR(builder.function_registry().RegisterLazyFunction(
FunctionDescriptor(kCelAttribute, false, {Kind::kAny, Kind::kList})));
CEL_RETURN_IF_ERROR(builder.function_registry().RegisterLazyFunction(
FunctionDescriptor(kCelHasField, false, {Kind::kAny, Kind::kList})));
// Add runtime implementation.
flat_expr_builder->AddProgramOptimizer(
CreateSelectOptimizationProgramOptimizer(options));
return absl::OkStatus();
}
} // namespace cel::extensions