Skip to content

Commit 8120187

Browse files
timofey-stepanovcopybara-github
authored andcommitted
Koda-specific wrapper around MakeBoundOperator.
Also support functions that return `absl::Status` in `arolla::MakeBoundOperator`. The goal is to have a single entry point for all Koda QExpr operators defined via `OparatorFamily`. The current motivation is to avoid direct usages of `OperatorEvalError` in their bodies (see the child CL). PiperOrigin-RevId: 716550906 Change-Id: I907e9a09cb6af83ddc7bacbe1a0e81994aafbb62
1 parent 1695c00 commit 8120187

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

koladata/internal/op_utils/BUILD

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,3 +917,28 @@ cc_test(
917917
"@com_google_googletest//:gtest_main",
918918
],
919919
)
920+
921+
cc_library(
922+
name = "qexpr",
923+
hdrs = ["qexpr.h"],
924+
deps = [
925+
":error",
926+
"@com_google_absl//absl/status",
927+
"@com_google_arolla//arolla/memory",
928+
"@com_google_arolla//arolla/qexpr",
929+
],
930+
)
931+
932+
cc_test(
933+
name = "qexpr_test",
934+
srcs = ["qexpr_test.cc"],
935+
deps = [
936+
":qexpr",
937+
"//koladata/internal:error_cc_proto",
938+
"//koladata/internal:error_utils",
939+
"@com_google_absl//absl/status",
940+
"@com_google_arolla//arolla/memory",
941+
"@com_google_arolla//arolla/qexpr",
942+
"@com_google_googletest//:gtest_main",
943+
],
944+
)

koladata/internal/op_utils/qexpr.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
#ifndef KOLADATA_INTERNAL_OP_UTILS_QEXPR_H_
16+
#define KOLADATA_INTERNAL_OP_UTILS_QEXPR_H_
17+
18+
#include <memory>
19+
#include <string>
20+
#include <utility>
21+
22+
#include "absl/status/status.h"
23+
#include "koladata/internal/op_utils/error.h"
24+
#include "arolla/memory/frame.h"
25+
#include "arolla/qexpr/bound_operators.h"
26+
#include "arolla/qexpr/eval_context.h"
27+
#include "arolla/qexpr/operators.h"
28+
29+
namespace koladata {
30+
31+
// Creates a bound operator implemented by the provided functor. All the errors
32+
// returned by the functor are wrapped into OperatorEvalError with the given
33+
// name.
34+
template <typename Functor>
35+
std::unique_ptr<arolla::BoundOperator> MakeBoundOperator(std::string name,
36+
Functor functor) {
37+
static_assert(std::is_same_v<decltype(functor(
38+
std::declval<arolla::EvaluationContext*>(),
39+
std::declval<arolla::FramePtr>())),
40+
absl::Status>,
41+
"functor(ctx, frame) must return absl::Status");
42+
return arolla::MakeBoundOperator(
43+
internal::ReturnsOperatorEvalError(std::move(name), std::move(functor)));
44+
}
45+
46+
} // namespace koladata
47+
48+
#endif // KOLADATA_INTERNAL_OP_UTILS_QEXPR_H_
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
#include "koladata/internal/op_utils/qexpr.h"
16+
17+
#include <optional>
18+
19+
#include "gmock/gmock.h"
20+
#include "gtest/gtest.h"
21+
#include "absl/status/status.h"
22+
#include "koladata/internal/error.pb.h"
23+
#include "koladata/internal/error_utils.h"
24+
#include "arolla/memory/frame.h"
25+
#include "arolla/memory/memory_allocation.h"
26+
#include "arolla/qexpr/eval_context.h"
27+
28+
namespace koladata {
29+
namespace {
30+
31+
using ::testing::Eq;
32+
using ::testing::status::StatusIs;
33+
34+
TEST(QExpr, MakeBoundOperator) {
35+
auto bound_op = MakeBoundOperator(
36+
"op_name", [](arolla::EvaluationContext* ctx, arolla::FramePtr frame) {
37+
return absl::InvalidArgumentError("test error");
38+
});
39+
arolla::EvaluationContext ctx;
40+
arolla::FrameLayout memory_layout = arolla::FrameLayout::Builder().Build();
41+
arolla::MemoryAllocation alloc(&memory_layout);
42+
43+
bound_op->Run(&ctx, alloc.frame());
44+
45+
EXPECT_THAT(ctx.status(),
46+
StatusIs(absl::StatusCode::kInvalidArgument, "test error"));
47+
std::optional<internal::Error> payload =
48+
internal::GetErrorPayload(ctx.status());
49+
ASSERT_TRUE(payload.has_value());
50+
EXPECT_THAT(payload->error_message(), Eq("op_name: test error"));
51+
}
52+
53+
} // namespace
54+
} // namespace koladata

0 commit comments

Comments
 (0)