diff --git a/native-sql-engine/cpp/src/codegen/arrow_compute/ext/expression_codegen_visitor.cc b/native-sql-engine/cpp/src/codegen/arrow_compute/ext/expression_codegen_visitor.cc index b4a807289..767243c83 100644 --- a/native-sql-engine/cpp/src/codegen/arrow_compute/ext/expression_codegen_visitor.cc +++ b/native-sql-engine/cpp/src/codegen/arrow_compute/ext/expression_codegen_visitor.cc @@ -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 : (" diff --git a/native-sql-engine/cpp/src/precompile/gandiva.h b/native-sql-engine/cpp/src/precompile/gandiva.h index d722f799d..ed2a2ae93 100644 --- a/native-sql-engine/cpp/src/precompile/gandiva.h +++ b/native-sql-engine/cpp/src/precompile/gandiva.h @@ -26,6 +26,7 @@ #include #include #include +#include #include "third_party/gandiva/decimal_ops.h" #include "third_party/gandiva/types.h" @@ -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 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); } \ No newline at end of file