Skip to content

Commit 2dc18f9

Browse files
jnthntatumcopybara-github
authored andcommitted
Inline legacy message field access logic into select/optimized_select implementations.
Refactor before adding switch over flag. PiperOrigin-RevId: 967990557
1 parent 87f68d8 commit 2dc18f9

4 files changed

Lines changed: 98 additions & 11 deletions

File tree

eval/eval/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ cc_library(
317317
"//common:type",
318318
"//common:value",
319319
"//common:value_kind",
320+
"//eval/public/structs:proto_message_type_adapter",
320321
"//internal:status_macros",
321322
"//runtime:runtime_options",
322323
"@com_google_absl//absl/log:absl_check",

eval/eval/select_step.cc

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "eval/eval/direct_expression_step.h"
2020
#include "eval/eval/evaluator_core.h"
2121
#include "eval/eval/expression_step_base.h"
22+
#include "eval/public/structs/proto_message_type_adapter.h"
2223
#include "internal/status_macros.h"
2324
#include "runtime/runtime_options.h"
2425
#include "google/protobuf/arena.h"
@@ -74,6 +75,29 @@ absl::optional<Value> CheckForMarkedAttributes(const AttributeTrail& trail,
7475
return std::nullopt;
7576
}
7677

78+
// Helper for StructValue::GetFieldByName. Used for opting out of old reflection
79+
// implementation.
80+
absl::Status WrappedStructGet(
81+
const Value& target, absl::string_view field,
82+
ProtoWrapperTypeOptions unboxing_option,
83+
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
84+
google::protobuf::MessageFactory* absl_nonnull message_factory,
85+
google::protobuf::Arena* absl_nonnull arena, Value* absl_nonnull result) {
86+
if (const google::protobuf::Message* message =
87+
cel::interop_internal::GetLegacyMessage(target);
88+
message != nullptr) {
89+
CelValue::MessageWrapper message_wrapper(
90+
message, &GetGenericProtoTypeInfoInstance());
91+
CEL_ASSIGN_OR_RETURN(CelValue cel_value,
92+
internal::GetGenericProtoAccessApisInstance().GetField(
93+
field, message_wrapper, unboxing_option,
94+
cel::MemoryManagerRef::Pooling(arena)));
95+
return cel::ModernValue(arena, cel_value, *result);
96+
}
97+
return target.GetStruct().GetFieldByName(
98+
field, unboxing_option, descriptor_pool, message_factory, arena, result);
99+
}
100+
77101
absl::Status PerformHas(const Value& target, absl::string_view field,
78102
const StringValue& field_value,
79103
const google::protobuf::DescriptorPool* descriptor_pool,
@@ -115,9 +139,9 @@ absl::Status PerformGet(const Value& target, absl::string_view field,
115139
return absl::OkStatus();
116140
}
117141
case ValueKind::kStruct: {
118-
auto status = target.GetStruct().GetFieldByName(
119-
field, unboxing_option, descriptor_pool, message_factory, arena,
120-
&result);
142+
auto status =
143+
WrappedStructGet(target, field, unboxing_option, descriptor_pool,
144+
message_factory, arena, &result);
121145
if (!status.ok()) {
122146
result = ErrorValue(std::move(status));
123147
}
@@ -154,9 +178,9 @@ absl::Status PerformOptionalGet(const Value& target, absl::string_view field,
154178
result = OptionalValue::None();
155179
return absl::OkStatus();
156180
}
157-
CEL_RETURN_IF_ERROR(target.GetStruct().GetFieldByName(
158-
field, unboxing_option, descriptor_pool, message_factory, arena,
159-
&result));
181+
CEL_RETURN_IF_ERROR(WrappedStructGet(target, field, unboxing_option,
182+
descriptor_pool, message_factory,
183+
arena, &result));
160184

161185
ABSL_DCHECK(!result.IsUnknown());
162186
result = OptionalValue::Of(std::move(result), arena);

extensions/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ cc_library(
331331
"//common:expr",
332332
"//common:function_descriptor",
333333
"//common:kind",
334+
"//common:memory",
334335
"//common:native_type",
335336
"//common:type",
336337
"//common:value",
@@ -340,10 +341,13 @@ cc_library(
340341
"//eval/eval:direct_expression_step",
341342
"//eval/eval:evaluator_core",
342343
"//eval/eval:expression_step_base",
344+
"//eval/public:cel_value",
345+
"//eval/public/structs:proto_message_type_adapter",
343346
"//internal:casts",
344347
"//internal:number",
345348
"//internal:status_macros",
346349
"//runtime:runtime_builder",
350+
"//runtime:runtime_options",
347351
"//runtime/internal:errors",
348352
"//runtime/internal:runtime_friend_access",
349353
"//runtime/internal:runtime_impl",
@@ -355,7 +359,6 @@ cc_library(
355359
"@com_google_absl//absl/status",
356360
"@com_google_absl//absl/status:statusor",
357361
"@com_google_absl//absl/strings",
358-
"@com_google_absl//absl/types:optional",
359362
"@com_google_absl//absl/types:span",
360363
"@com_google_absl//absl/types:variant",
361364
"@com_google_protobuf//:protobuf",

extensions/select_optimization.cc

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <cstdint>
1919
#include <iterator>
2020
#include <memory>
21+
#include <optional>
2122
#include <string>
2223
#include <utility>
2324
#include <vector>
@@ -31,7 +32,6 @@
3132
#include "absl/status/statusor.h"
3233
#include "absl/strings/match.h"
3334
#include "absl/strings/string_view.h"
34-
#include "absl/types/optional.h"
3535
#include "absl/types/span.h"
3636
#include "absl/types/variant.h"
3737
#include "base/attribute.h"
@@ -43,6 +43,8 @@
4343
#include "common/expr.h"
4444
#include "common/function_descriptor.h"
4545
#include "common/kind.h"
46+
#include "common/legacy_value.h"
47+
#include "common/memory.h"
4648
#include "common/native_type.h"
4749
#include "common/type.h"
4850
#include "common/value.h"
@@ -52,13 +54,16 @@
5254
#include "eval/eval/direct_expression_step.h"
5355
#include "eval/eval/evaluator_core.h"
5456
#include "eval/eval/expression_step_base.h"
57+
#include "eval/public/cel_value.h"
58+
#include "eval/public/structs/proto_message_type_adapter.h"
5559
#include "internal/casts.h"
5660
#include "internal/number.h"
5761
#include "internal/status_macros.h"
5862
#include "runtime/internal/errors.h"
5963
#include "runtime/internal/runtime_friend_access.h"
6064
#include "runtime/internal/runtime_impl.h"
6165
#include "runtime/runtime_builder.h"
66+
#include "runtime/runtime_options.h"
6267
#include "google/protobuf/arena.h"
6368
#include "google/protobuf/descriptor.h"
6469
#include "google/protobuf/message.h"
@@ -74,12 +79,15 @@ using ::cel::Expr;
7479
using ::cel::ExprKind;
7580
using ::cel::SelectExpr;
7681
using ::google::api::expr::runtime::AttributeTrail;
82+
using ::google::api::expr::runtime::CelValue;
7783
using ::google::api::expr::runtime::DirectExpressionStep;
7884
using ::google::api::expr::runtime::ExecutionFrame;
7985
using ::google::api::expr::runtime::ExecutionFrameBase;
8086
using ::google::api::expr::runtime::ExpressionStepBase;
87+
using ::google::api::expr::runtime::GetGenericProtoTypeInfoInstance;
8188
using ::google::api::expr::runtime::PlannerContext;
8289
using ::google::api::expr::runtime::ProgramOptimizer;
90+
using ::google::api::expr::runtime::internal::GetGenericProtoAccessApisInstance;
8391

8492
// Represents a single select operation (field access or indexing).
8593
// For struct-typed field accesses, includes the field name and the field
@@ -267,6 +275,57 @@ absl::StatusOr<Value> MapKeyFromQualifier(const AttributeQualifier& qual,
267275
}
268276
}
269277

278+
// Helper for StructValue::GetFieldByName. Used for opting out of old reflection
279+
// implementation.
280+
absl::StatusOr<Value> WrappedStructGet(
281+
const Value& target, absl::string_view field,
282+
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
283+
google::protobuf::MessageFactory* absl_nonnull message_factory,
284+
google::protobuf::Arena* absl_nonnull arena) {
285+
if (const google::protobuf::Message* message =
286+
cel::interop_internal::GetLegacyMessage(target);
287+
message != nullptr) {
288+
CelValue::MessageWrapper message_wrapper(
289+
message, &GetGenericProtoTypeInfoInstance());
290+
CEL_ASSIGN_OR_RETURN(
291+
CelValue cel_value,
292+
GetGenericProtoAccessApisInstance().GetField(
293+
field, message_wrapper, ProtoWrapperTypeOptions::kUnsetProtoDefault,
294+
MemoryManagerRef::Pooling(arena)));
295+
Value result;
296+
CEL_RETURN_IF_ERROR(cel::ModernValue(arena, cel_value, result));
297+
return result;
298+
}
299+
return target.GetStruct().GetFieldByName(field, descriptor_pool,
300+
message_factory, arena);
301+
}
302+
303+
// Helper for StructValue::Qualify. Used for opting out of old reflection
304+
// implementation.
305+
absl::StatusOr<std::pair<Value, ssize_t>> WrappedStructQualify(
306+
const StructValue& struct_value,
307+
absl::Span<const SelectQualifier> qualifiers, bool presence_test,
308+
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
309+
google::protobuf::MessageFactory* absl_nonnull message_factory,
310+
google::protobuf::Arena* absl_nonnull arena) {
311+
if (const google::protobuf::Message* message =
312+
cel::interop_internal::GetLegacyMessage(struct_value);
313+
message != nullptr) {
314+
CelValue::MessageWrapper message_wrapper(
315+
message, &GetGenericProtoTypeInfoInstance());
316+
CEL_ASSIGN_OR_RETURN(auto legacy_result,
317+
GetGenericProtoAccessApisInstance().Qualify(
318+
qualifiers, message_wrapper, presence_test,
319+
MemoryManagerRef::Pooling(arena)));
320+
Value result;
321+
CEL_RETURN_IF_ERROR(cel::ModernValue(arena, legacy_result.value, result));
322+
return std::pair<Value, ssize_t>{std::move(result),
323+
legacy_result.qualifier_count};
324+
}
325+
return struct_value.Qualify(qualifiers, presence_test, descriptor_pool,
326+
message_factory, arena);
327+
}
328+
270329
absl::StatusOr<Value> ApplyQualifier(
271330
const Value& operand, const SelectQualifier& qualifier,
272331
const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
@@ -280,8 +339,8 @@ absl::StatusOr<Value> ApplyQualifier(
280339
cel::runtime_internal::CreateNoMatchingOverloadError(
281340
"<select>"));
282341
}
283-
return operand.GetStruct().GetFieldByName(
284-
field_specifier.name, descriptor_pool, message_factory, arena);
342+
return WrappedStructGet(operand, field_specifier.name,
343+
descriptor_pool, message_factory, arena);
285344
},
286345
[&](const AttributeQualifier& qualifier) -> absl::StatusOr<Value> {
287346
if (operand.Is<ListValue>()) {
@@ -632,7 +691,7 @@ absl::StatusOr<Value> OptimizedSelectImpl::ApplySelect(
632691
auto value_or =
633692
(options_.force_fallback_implementation)
634693
? absl::UnimplementedError("Forced fallback impl")
635-
: struct_value.Qualify(select_path_, presence_test_,
694+
: WrappedStructQualify(struct_value, select_path_, presence_test_,
636695
frame.descriptor_pool(),
637696
frame.message_factory(), frame.arena());
638697

0 commit comments

Comments
 (0)