-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split useful helper code out of MergeOperationView into a separate li…
…b. (#443) Closes #443 COPYBARA_INTEGRATE_REVIEW=#443 from google-research:literal_operator_view@winterrowd c76e017 PiperOrigin-RevId: 433783822
- Loading branch information
Showing
4 changed files
with
89 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |