Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Split create map and create struct libraries. Apply IWYU and build_cleaner findings. No functional changes. #663

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion eval/compiler/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ cc_library(
"//eval/eval:const_value_step",
"//eval/eval:container_access_step",
"//eval/eval:create_list_step",
"//eval/eval:create_map_step",
"//eval/eval:create_struct_step",
"//eval/eval:evaluator_core",
"//eval/eval:function_step",
Expand Down Expand Up @@ -300,7 +301,7 @@ cc_test(
"//common:value",
"//eval/eval:const_value_step",
"//eval/eval:create_list_step",
"//eval/eval:create_struct_step",
"//eval/eval:create_map_step",
"//eval/eval:evaluator_core",
"//extensions/protobuf:ast_converters",
"//extensions/protobuf:memory_manager",
Expand Down
2 changes: 1 addition & 1 deletion eval/compiler/constant_folding_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "eval/compiler/resolver.h"
#include "eval/eval/const_value_step.h"
#include "eval/eval/create_list_step.h"
#include "eval/eval/create_struct_step.h"
#include "eval/eval/create_map_step.h"
#include "eval/eval/evaluator_core.h"
#include "extensions/protobuf/ast_converters.h"
#include "extensions/protobuf/memory_manager.h"
Expand Down
1 change: 1 addition & 0 deletions eval/compiler/flat_expr_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "eval/eval/const_value_step.h"
#include "eval/eval/container_access_step.h"
#include "eval/eval/create_list_step.h"
#include "eval/eval/create_map_step.h"
#include "eval/eval/create_struct_step.h"
#include "eval/eval/evaluator_core.h"
#include "eval/eval/function_step.h"
Expand Down
60 changes: 57 additions & 3 deletions eval/eval/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,31 @@ cc_library(
],
)

cc_library(
name = "create_map_step",
srcs = [
"create_map_step.cc",
],
hdrs = [
"create_map_step.h",
],
deps = [
":evaluator_core",
":expression_step_base",
"//base/ast_internal:expr",
"//common:casting",
"//common:memory",
"//common:type",
"//common:value",
"//internal:status_macros",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:optional",
],
)

cc_library(
name = "jump_step",
srcs = [
Expand Down Expand Up @@ -678,24 +703,53 @@ cc_test(
":evaluator_core",
":ident_step",
"//base:data",
"//common:type",
"//base/ast_internal:expr",
"//common:value",
"//eval/public:activation",
"//eval/public:cel_type_registry",
"//eval/public:cel_value",
"//eval/public:unknown_set",
"//eval/public/containers:container_backed_list_impl",
"//eval/public/containers:container_backed_map_impl",
"//eval/public/structs:cel_proto_wrapper",
"//eval/public/structs:proto_message_type_adapter",
"//eval/public/structs:protobuf_descriptor_type_provider",
"//eval/testutil:test_message_cc_proto",
"//extensions/protobuf:memory_manager",
"//internal:proto_matchers",
"//internal:status_macros",
"//internal:testing",
"//runtime:runtime_options",
"//testutil:util",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_cc_proto",
"@com_google_protobuf//:protobuf",
],
)

cc_test(
name = "create_map_step_test",
size = "small",
srcs = [
"create_map_step_test.cc",
],
deps = [
":cel_expression_flat_impl",
":create_map_step",
":evaluator_core",
":ident_step",
"//base:data",
"//base/ast_internal:expr",
"//eval/public:activation",
"//eval/public:cel_value",
"//eval/public:unknown_set",
"//eval/testutil:test_message_cc_proto",
"//internal:status_macros",
"//internal:testing",
"//runtime:runtime_options",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_googleapis//google/api/expr/v1alpha1:syntax_cc_proto",
"@com_google_protobuf//:protobuf",
],
Expand Down
148 changes: 148 additions & 0 deletions eval/eval/create_map_step.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright 2024 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 "eval/eval/create_map_step.h"

#include <cstddef>
#include <cstdint>
#include <memory>
#include <utility>
#include <vector>

#include "absl/container/flat_hash_set.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "base/ast_internal/expr.h"
#include "common/casting.h"
#include "common/memory.h"
#include "common/type.h"
#include "common/value.h"
#include "common/value_manager.h"
#include "eval/eval/evaluator_core.h"
#include "eval/eval/expression_step_base.h"
#include "internal/status_macros.h"

namespace google::api::expr::runtime {

namespace {

using ::cel::StructValueBuilderInterface;
using ::cel::UnknownValue;
using ::cel::Value;

absl::flat_hash_set<int32_t> MakeOptionalIndicesSet(
const cel::ast_internal::CreateStruct& create_struct_expr) {
absl::flat_hash_set<int32_t> optional_indices;
for (size_t i = 0; i < create_struct_expr.entries().size(); ++i) {
if (create_struct_expr.entries()[i].optional_entry()) {
optional_indices.insert(static_cast<int32_t>(i));
}
}
return optional_indices;
}

// `CreateStruct` implementation for map.
class CreateStructStepForMap final : public ExpressionStepBase {
public:
CreateStructStepForMap(int64_t expr_id, size_t entry_count,
absl::flat_hash_set<int32_t> optional_indices)
: ExpressionStepBase(expr_id),
entry_count_(entry_count),
optional_indices_(std::move(optional_indices)) {}

absl::Status Evaluate(ExecutionFrame* frame) const override;

private:
absl::StatusOr<Value> DoEvaluate(ExecutionFrame* frame) const;

size_t entry_count_;
absl::flat_hash_set<int32_t> optional_indices_;
};


absl::StatusOr<Value> CreateStructStepForMap::DoEvaluate(
ExecutionFrame* frame) const {
auto args = frame->value_stack().GetSpan(2 * entry_count_);

if (frame->enable_unknowns()) {
absl::optional<UnknownValue> unknown_set =
frame->attribute_utility().IdentifyAndMergeUnknowns(
args, frame->value_stack().GetAttributeSpan(args.size()), true);
if (unknown_set.has_value()) {
return *unknown_set;
}
}

CEL_ASSIGN_OR_RETURN(auto builder, frame->value_manager().NewMapValueBuilder(
cel::MapTypeView{}));
builder->Reserve(entry_count_);

for (size_t i = 0; i < entry_count_; i += 1) {
auto& map_key = args[2 * i];
CEL_RETURN_IF_ERROR(cel::CheckMapKey(map_key));
auto& map_value = args[(2 * i) + 1];
if (optional_indices_.contains(static_cast<int32_t>(i))) {
if (auto optional_map_value = cel::As<cel::OptionalValue>(map_value);
optional_map_value) {
if (!optional_map_value->HasValue()) {
continue;
}
auto key_status =
builder->Put(std::move(map_key), optional_map_value->Value());
if (!key_status.ok()) {
return frame->value_factory().CreateErrorValue(key_status);
}
} else {
return cel::TypeConversionError(map_value.DebugString(),
"optional_type")
.NativeValue();
}
} else {
auto key_status = builder->Put(std::move(map_key), std::move(map_value));
if (!key_status.ok()) {
return frame->value_factory().CreateErrorValue(key_status);
}
}
}

return std::move(*builder).Build();
}

absl::Status CreateStructStepForMap::Evaluate(ExecutionFrame* frame) const {
if (frame->value_stack().size() < 2 * entry_count_) {
return absl::InternalError("CreateStructStepForMap: stack underflow");
}

CEL_ASSIGN_OR_RETURN(auto result, DoEvaluate(frame));

frame->value_stack().PopAndPush(2 * entry_count_, std::move(result));

return absl::OkStatus();
}

} // namespace


absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateCreateStructStepForMap(
const cel::ast_internal::CreateStruct& create_struct_expr,
int64_t expr_id) {
// Make map-creating step.
return std::make_unique<CreateStructStepForMap>(
expr_id, create_struct_expr.entries().size(),
MakeOptionalIndicesSet(create_struct_expr));
}

} // namespace google::api::expr::runtime
33 changes: 33 additions & 0 deletions eval/eval/create_map_step.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2024 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.

#ifndef THIRD_PARTY_CEL_CPP_EVAL_EVAL_CREATE_MAP_STEP_H_
#define THIRD_PARTY_CEL_CPP_EVAL_EVAL_CREATE_MAP_STEP_H_

#include <cstdint>
#include <memory>

#include "absl/status/statusor.h"
#include "base/ast_internal/expr.h"
#include "eval/eval/evaluator_core.h"

namespace google::api::expr::runtime {

// Creates an `ExpressionStep` which performs `CreateStruct` for a map.
absl::StatusOr<std::unique_ptr<ExpressionStep>> CreateCreateStructStepForMap(
const cel::ast_internal::CreateStruct& create_struct_expr, int64_t expr_id);

} // namespace google::api::expr::runtime

#endif // THIRD_PARTY_CEL_CPP_EVAL_EVAL_CREATE_MAP_STEP_H_
Loading