Skip to content

Commit

Permalink
Split useful helper code out of MergeOperationView into a separate li…
Browse files Browse the repository at this point in the history
…b. (#443)

Closes #443

COPYBARA_INTEGRATE_REVIEW=#443 from google-research:literal_operator_view@winterrowd c76e017
PiperOrigin-RevId: 433783822
  • Loading branch information
markww authored and arcs-c3po committed Mar 10, 2022
1 parent b2069ff commit b59bff4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 39 deletions.
12 changes: 12 additions & 0 deletions src/frontends/sql/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ cc_library(
hdrs = ["merge_operation_view.h"],
visibility = ["//src/frontends/sql:__subpackages__"],
deps = [
":operation_view_utils",
"//src/common/logging",
"//src/frontends/sql:decoder_context",
"//src/ir:module",
Expand All @@ -45,3 +46,14 @@ cc_library(
"//src/ir:value",
],
)

cc_library(
name = "operation_view_utils",
testonly = True,
srcs = ["operation_view_utils.cc"],
hdrs = ["operation_view_utils.h"],
deps = [
"//src/ir:value",
"@absl//absl/strings",
],
)
40 changes: 1 addition & 39 deletions src/frontends/sql/testing/merge_operation_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
#ifndef SRC_FRONTENDS_SQL_TESTING_MERGE_OPERATION_VIEW_H_
#define SRC_FRONTENDS_SQL_TESTING_MERGE_OPERATION_VIEW_H_

#include "absl/strings/match.h"
#include "absl/strings/numbers.h"
#include "src/common/logging/logging.h"
#include "src/frontends/sql/decoder_context.h"
#include "src/ir/module.h"
#include "src/ir/operator.h"
#include "src/frontends/sql/testing/operation_view_utils.h"

namespace raksha::frontends::sql::testing {

Expand All @@ -47,43 +46,6 @@ class MergeOperationView {
}

private:
static std::optional<uint64_t> ExtractIdxAfterPrefix(
absl::string_view str, absl::string_view prefix) {
if (!absl::StartsWith(str, prefix)) return std::nullopt;
uint64_t result = 0;
std::string index_str(str.substr(prefix.size()));
if (!absl::SimpleAtoi(index_str, &result)) return std::nullopt;
return result;
}

static std::vector<ir::Value> GetVecWithPrefix(ir::NamedValueMap map,
absl::string_view prefix) {
uint64_t inferred_vec_length = 0;

// The numbering of inputs should be dense. Thus, we can find the largest
// index with the given prefix, add one to it, and consider that the
// length of the vector.
for (auto &[key, value] : map) {
if (std::optional<uint64_t> opt_idx =
ExtractIdxAfterPrefix(key, prefix)) {
inferred_vec_length = std::max(inferred_vec_length, *opt_idx + 1);
}
}

// For each item with the prefix up to the inferred length, get the value
// and put it in the vector. If the numbering is not dense as we expect,
// fail.
std::vector<ir::Value> result;
result.reserve(inferred_vec_length);
for (uint64_t i = 0; i < inferred_vec_length; ++i) {
auto find_result = map.find(absl::StrCat(prefix, i));
CHECK(find_result != map.end())
<< "Found a hole in the key numbering: " << find_result->first;
result.push_back(find_result->second);
}
return result;
}

const ir::Operation *merge_operation_;
};

Expand Down
45 changes: 45 additions & 0 deletions src/frontends/sql/testing/operation_view_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "operation_view_utils.h"

#include "absl/strings/match.h"
#include "absl/strings/numbers.h"

namespace raksha::frontends::sql::testing {

std::optional<uint64_t> ExtractIdxAfterPrefix(
absl::string_view str, absl::string_view prefix) {
if (!absl::StartsWith(str, prefix)) return std::nullopt;
uint64_t result = 0;
std::string index_str(str.substr(prefix.size()));
if (!absl::SimpleAtoi(index_str, &result)) return std::nullopt;
return result;
}

std::vector<ir::Value> GetVecWithPrefix(const ir::NamedValueMap &map,
absl::string_view prefix) {
uint64_t inferred_vec_length = 0;

// The numbering of inputs should be dense. Thus, we can find the largest
// index with the given prefix, add one to it, and consider that the
// length of the vector.
for (auto &[key, value] : map) {
if (std::optional<uint64_t> opt_idx =
ExtractIdxAfterPrefix(key, prefix)) {
inferred_vec_length = std::max(inferred_vec_length, *opt_idx + 1);
}
}

// For each item with the prefix up to the inferred length, get the value
// and put it in the vector. If the numbering is not dense as we expect,
// fail.
std::vector<ir::Value> result;
result.reserve(inferred_vec_length);
for (uint64_t i = 0; i < inferred_vec_length; ++i) {
auto find_result = map.find(absl::StrCat(prefix, i));
CHECK(find_result != map.end())
<< "Found a hole in the key numbering: " << find_result->first;
result.push_back(find_result->second);
}
return result;
}

} // namespace raksha::frontends::sql::testing
31 changes: 31 additions & 0 deletions src/frontends/sql/testing/operation_view_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef SRC_FRONTENDS_SQL_TESTING_OPERATION_VIEW_UTILS_H_
#define SRC_FRONTENDS_SQL_TESTING_OPERATION_VIEW_UTILS_H_

#include <cstdint>
#include <optional>
#include <vector>

#include "absl/strings/string_view.h"
#include "src/ir/value.h"

namespace raksha::frontends::sql::testing {

// This helper expects that the provided `str` is of the form `prefix_[0-9]+`.
// This function skips past the prefix, interprets the number as an unsigned
// int, and returns it. If any of those assumptions are not met, it returns
// std::nullopt instead.
std::optional<uint64_t> ExtractIdxAfterPrefix(
absl::string_view str, absl::string_view prefix);

// This method assumes that, within the given `NamedValueMap`, there are a
// subset of elements that start with some prefix `prefix` and end with a
// numeric index. Further, it assumes that those indicies are densely
// clustered and starting at 0, such that they can be described as an array.
// This method produces a `vector` from the values with that prefix, with the
// trailing numeric suffixes being translated into positions in the resulting
// vector. If the density assumption is not met, this method asserts.
std::vector<ir::Value> GetVecWithPrefix(const ir::NamedValueMap &map,
absl::string_view prefix);
} // namespace raksha::frontends::sql::testing

#endif // SRC_FRONTENDS_SQL_TESTING_OPERATION_VIEW_UTILS_H_

0 comments on commit b59bff4

Please sign in to comment.