Skip to content

Commit

Permalink
Port the function to wscg
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILO-HE committed Jan 5, 2022
1 parent aed5c60 commit 63cfec3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,27 @@ arrow::Status ExpressionCodegenVisitor::Visit(const gandiva::FunctionNode& node)
prepare_ss << "}" << std::endl;
prepare_str_ += prepare_ss.str();
check_str_ = validity;
} else if (func_name.compare("translate")) {
codes_str_ = func_name + "_" + std::to_string(cur_func_id);
auto validity = codes_str_ + "_validity";
real_codes_str_ = codes_str_;
real_validity_str_ = validity;
std::stringstream prepare_ss;
prepare_ss << GetCTypeString(node.return_type()) << " " << codes_str_ << ";"
<< std::endl;
prepare_ss << "bool " << validity << " = " << child_visitor_list[0]->GetPreCheck()
<< ";" << std::endl;
prepare_ss << "if (" << validity << ") {" << std::endl;
prepare_ss << codes_str_ << " = translate"
<< "(" << child_visitor_list[0]->GetResult() << ", "
<< child_visitor_list[1]->GetResult() << ", "
<< child_visitor_list[2]->GetResult() << ");" << std::endl;
prepare_ss << "}" << std::endl;
for (int i = 0; i < 1; i++) {
prepare_str_ += child_visitor_list[i]->GetPrepare();
}
prepare_str_ += prepare_ss.str();
check_str_ = validity;
} else if (func_name.compare("substr") == 0) {
ss << child_visitor_list[0]->GetResult() << ".substr("
<< "((" << child_visitor_list[1]->GetResult() << " - 1) < 0 ? 0 : ("
Expand Down
26 changes: 26 additions & 0 deletions native-sql-engine/cpp/src/precompile/gandiva.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <cstdint>
#include <set>
#include <type_traits>
#include <unordered_map>

#include "third_party/gandiva/decimal_ops.h"
#include "third_party/gandiva/types.h"
Expand Down Expand Up @@ -314,4 +315,29 @@ bool like(const std::string& data, const std::string& pattern) {
std::string pcre_pattern = SqlLikePatternToPcre(pattern, 0);
RE2 regex(pcre_pattern);
return RE2::FullMatch(data, regex);
}

const std::string translate(const std::string text, const std::string matching_str, const std::string replace_str) {
char res[text.length()];
std::unordered_map<char, char> replace_map;
for (int i = 0; i < matching_str.length(); i++) {
if (i >= replace_str.length()) {
replace_map[matching_str[i]] = '\0';
} else {
replace_map[matching_str[i]] = replace_str[i];
}
}
int j = 0;
for (int i = 0; i < text.length(); i++) {
if (replace_map.find(text[i]) == replace_map.end()) {
res[j++] = text[i];
continue;
}
char replace_char = replace_map[text[i]];
if (replace_char != '\0') {
res[j++] = replace_char;
}
}
int out_len = j;
return std::string((char*)res, out_len);
}

0 comments on commit 63cfec3

Please sign in to comment.