20 #include <rapidjson/document.h>
21 #include <boost/algorithm/string/predicate.hpp>
23 namespace StringOps_Namespace {
25 boost::regex StringOp::generateRegex(
const std::string& op_name,
26 const std::string& regex_pattern,
27 const std::string& regex_params,
28 const bool supports_sub_matches) {
29 bool is_case_sensitive =
false;
30 bool is_case_insensitive =
false;
32 for (
const auto& c : regex_params) {
35 is_case_sensitive =
true;
38 is_case_insensitive =
true;
41 if (!supports_sub_matches) {
42 throw std::runtime_error(op_name +
43 " does not support 'e' (sub-matches) option.");
50 if (supports_sub_matches) {
51 throw std::runtime_error(
"Unrecognized regex parameter for " + op_name +
52 ", expected either 'c' 'i', or 'e'.");
54 throw std::runtime_error(
"Unrecognized regex parameter for " + op_name +
55 ", expected either 'c' or 'i'.");
59 if (!is_case_sensitive && !is_case_insensitive) {
60 throw std::runtime_error(op_name +
61 " params must either specify case-sensitivity ('c') or "
62 "case-insensitivity ('i').");
64 if (is_case_sensitive && is_case_insensitive) {
65 throw std::runtime_error(op_name +
66 " params cannot specify both case-sensitivity ('c') and "
67 "case-insensitivity ('i').");
69 if (is_case_insensitive) {
70 return boost::regex(regex_pattern,
71 boost::regex_constants::extended |
72 boost::regex_constants::optimize |
73 boost::regex_constants::icase);
77 boost::regex_constants::extended | boost::regex_constants::optimize);
81 NullableStrType TryStringCast::operator()(
const std::string& str)
const {
82 UNREACHABLE() <<
"Invalid string output for TryStringCast";
83 return NullableStrType();
86 Datum TryStringCast::numericEval(
const std::string_view str)
const {
94 }
catch (std::runtime_error& e) {
99 NullableStrType Position::operator()(
const std::string& str)
const {
100 UNREACHABLE() <<
"Invalid string output for Position";
104 Datum Position::numericEval(
const std::string_view str)
const {
108 const int64_t str_len = str.size();
109 const int64_t wrapped_start = start_ >= 0 ? start_ : str_len + start_;
111 const auto search_index = str.find(search_str_, wrapped_start);
112 if (search_index == std::string::npos) {
115 return_datum.
bigintval =
static_cast<int64_t
>(search_index) + 1;
128 int s1_len = s1.size();
129 int s2_len = s2.size();
131 if (s1_len == 0 || s2_len == 0) {
135 int match_distance = std::max(s1_len, s2_len) / 2 - 1;
136 std::vector<bool> s1_match(s1_len,
false);
137 std::vector<bool> s2_match(s2_len,
false);
140 int transpositions = 0;
142 for (
int i = 0; i < s1_len; ++i) {
143 int start = std::max(0, i - match_distance);
144 int end = std::min(i + match_distance + 1, s2_len);
146 for (
int j = start; j < end; ++j) {
150 if (s1[i] != s2[j]) {
165 for (
int i = 0; i < s1_len; ++i) {
169 while (!s2_match[k]) {
172 if (s1[i] != s2[k]) {
178 double score = ((matches / (double)s1_len) + (matches / (double)s2_len) +
179 ((matches - transpositions / 2.0) / matches)) /
189 int n = std::min({
static_cast<int>(s1.size()),
190 static_cast<int>(s2.size()),
194 if (s1[l] != s2[l]) {
200 double jaro_winkler_score = jaro_score + winkler_adjustment;
202 return jaro_winkler_score * 100;
205 NullableStrType JarowinklerSimilarity::operator()(
const std::string& str)
const {
206 UNREACHABLE() <<
"Invalid string output for Jarowinkler Similarity";
210 Datum JarowinklerSimilarity::numericEval(
const std::string_view str)
const {
216 return_datum.
bigintval =
static_cast<int64_t
>(std::round(jaro_winkler_score));
220 Datum JarowinklerSimilarity::numericEval(
const std::string_view str1,
221 const std::string_view str2)
const {
222 if (str1.empty() || str2.empty()) {
227 return_datum.
bigintval =
static_cast<int64_t
>(std::round(jaro_winkler_score));
254 template <
typename T>
256 const size_t len1 = s1.size(), len2 = s2.size();
257 std::vector<std::vector<T>> d(len1 + 1, std::vector<T>(len2 + 1));
260 for (
size_t i = 1; i <= len1; ++i) {
263 for (
size_t i = 1; i <= len2; ++i) {
267 for (
size_t i = 1; i <= len1; ++i) {
268 for (
size_t j = 1; j <= len2; ++j) {
269 d[i][j] = std::min({d[i - 1][j] + 1,
271 d[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1)});
275 return d[len1][len2];
279 const size_t max_len = std::max(s1.size(), s2.size());
282 return compute_levenshtein_distance_template<uint8_t>(s1, s2);
283 }
else if (max_len < 65536) {
284 return compute_levenshtein_distance_template<uint16_t>(s1, s2);
285 }
else if (max_len < std::numeric_limits<uint32_t>::max()) {
286 return compute_levenshtein_distance_template<uint32_t>(s1, s2);
288 return compute_levenshtein_distance_template<uint64_t>(s1, s2);
292 NullableStrType LevenshteinDistance::operator()(
const std::string& str)
const {
293 UNREACHABLE() <<
"Invalid string output for Levenshtein Distance";
297 Datum LevenshteinDistance::numericEval(
const std::string_view str)
const {
303 return_datum.
bigintval =
static_cast<int64_t
>(std::round(levenshtein_distance));
307 Datum LevenshteinDistance::numericEval(
const std::string_view str1,
308 const std::string_view str2)
const {
309 if (str1.empty() || str2.empty()) {
314 return_datum.
bigintval =
static_cast<int64_t
>(std::round(levenshtein_distance));
318 NullableStrType Lower::operator()(
const std::string& str)
const {
319 std::string output_str(str);
321 output_str.begin(), output_str.end(), output_str.begin(), [](
unsigned char c) {
322 return std::tolower(c);
327 NullableStrType Upper::operator()(
const std::string& str)
const {
328 std::string output_str(str);
330 output_str.begin(), output_str.end(), output_str.begin(), [](
unsigned char c) {
331 return std::toupper(c);
336 NullableStrType InitCap::operator()(
const std::string& str)
const {
337 std::string output_str(str);
338 bool last_char_whitespace =
true;
339 for (
auto& c : output_str) {
340 if (isspace(c) || delimiter_bitmap_[reinterpret_cast<const uint8_t&>(c)]) {
341 last_char_whitespace =
true;
344 if (last_char_whitespace) {
346 last_char_whitespace =
false;
354 NullableStrType Reverse::operator()(
const std::string& str)
const {
355 const std::string reversed_str = std::string(str.rbegin(), str.rend());
359 NullableStrType Repeat::operator()(
const std::string& str)
const {
360 std::string repeated_str;
361 repeated_str.reserve(str.size() * n_);
362 for (
size_t r = 0; r < n_; ++r) {
368 NullableStrType Concat::operator()(
const std::string& str)
const {
369 return reverse_order_ ? str_literal_ + str : str + str_literal_;
372 NullableStrType Concat::operator()(
const std::string& str1,
373 const std::string& str2)
const {
377 NullableStrType Pad::operator()(
const std::string& str)
const {
378 return pad_mode_ == Pad::PadMode::LEFT ? lpad(str) : rpad(str);
381 std::string Pad::lpad(
const std::string& str)
const {
382 const auto str_len = str.size();
383 const size_t chars_to_fill = str_len < padded_length_ ? padded_length_ - str_len : 0UL;
384 if (chars_to_fill == 0UL) {
385 return str.substr(0, padded_length_);
389 if (padding_string_length_ == 1UL) {
390 return std::string(chars_to_fill, padding_char_) + str;
393 std::string fitted_padding_str;
394 fitted_padding_str.reserve(chars_to_fill);
395 for (
size_t i = 0; i < chars_to_fill; ++i) {
396 fitted_padding_str.push_back(padding_string_[i % padding_string_length_]);
398 return fitted_padding_str + str;
401 std::string Pad::rpad(
const std::string& str)
const {
402 const auto str_len = str.size();
403 const size_t chars_to_fill = str_len < padded_length_ ? padded_length_ - str_len : 0UL;
404 if (chars_to_fill == 0UL) {
405 return str.substr(str_len - padded_length_, std::string::npos);
409 if (padding_string_length_ == 1UL) {
410 return str + std::string(chars_to_fill, padding_char_);
413 std::string fitted_padding_str;
414 fitted_padding_str.reserve(chars_to_fill);
415 for (
size_t i = 0; i < chars_to_fill; ++i) {
416 fitted_padding_str.push_back(padding_string_[i % padding_string_length_]);
418 return str + fitted_padding_str;
424 return PadMode::LEFT;
426 return PadMode::RIGHT;
430 return PadMode::LEFT;
434 NullableStrType Trim::operator()(
const std::string& str)
const {
435 const auto str_len = str.size();
436 size_t trim_begin = 0;
437 if (trim_mode_ == TrimMode::LEFT || trim_mode_ == TrimMode::BOTH) {
438 while (trim_begin < str_len &&
439 trim_char_bitmap_[reinterpret_cast<const uint8_t&>(str[trim_begin])]) {
443 size_t trim_end = str_len - 1;
444 if (trim_mode_ == TrimMode::RIGHT || trim_mode_ == TrimMode::BOTH) {
445 while (trim_end > trim_begin &&
446 trim_char_bitmap_[reinterpret_cast<const uint8_t&>(str[trim_end])]) {
450 if (trim_begin == 0 && trim_end == str_len - 1) {
453 return str.substr(trim_begin, trim_end + 1 - trim_begin);
456 Trim::TrimMode Trim::op_kind_to_trim_mode(
const SqlStringOpKind op_kind) {
459 return Trim::TrimMode::BOTH;
461 return Trim::TrimMode::LEFT;
463 return Trim::TrimMode::RIGHT;
467 return Trim::TrimMode::BOTH;
471 NullableStrType Substring::operator()(
const std::string& str)
const {
474 const int64_t str_len = str.size();
475 const int64_t wrapped_start = start_ >= 0 ? start_ : str_len + start_;
476 const size_t capped_start =
477 wrapped_start > str_len ? str_len : (wrapped_start < 0 ? 0 : wrapped_start);
478 return str.substr(capped_start, length_);
481 NullableStrType Overlay::operator()(
const std::string& base_str)
const {
484 const int64_t str_len = base_str.size();
485 const int64_t wrapped_start = start_ >= 0 ? start_ : str_len + start_;
486 const size_t capped_start =
487 wrapped_start > str_len ? str_len : (wrapped_start < 0 ? 0 : wrapped_start);
488 std::string replaced_str = base_str.substr(0, capped_start);
489 replaced_str += insert_str_;
490 const size_t remainder_start =
491 std::min(wrapped_start + replacement_length_,
size_t(str_len));
492 const size_t remainder_length =
static_cast<size_t>(str_len) - remainder_start;
493 replaced_str += base_str.substr(remainder_start, remainder_length);
497 NullableStrType Replace::operator()(
const std::string& str)
const {
498 std::string replaced_str(str);
500 size_t search_start_index = 0;
502 search_start_index = replaced_str.find(pattern_str_, search_start_index);
503 if (search_start_index == std::string::npos) {
506 replaced_str.replace(search_start_index, pattern_str_len_, replacement_str_);
507 search_start_index += replacement_str_len_;
512 NullableStrType SplitPart::operator()(
const std::string& str)
const {
516 if (delimiter_ ==
"") {
520 const size_t str_len = str.size();
521 size_t delimiter_pos = reverse_ ? str_len : 0UL;
522 size_t last_delimiter_pos;
523 size_t delimiter_idx = 0UL;
526 last_delimiter_pos = delimiter_pos;
527 delimiter_pos = reverse_ ? str.rfind(delimiter_, delimiter_pos - 1UL)
528 : str.find(delimiter_, delimiter_pos + delimiter_length_);
529 }
while (delimiter_pos != std::string::npos && ++delimiter_idx < split_part_);
531 if (delimiter_idx == 0UL && split_part_ == 1UL) {
537 if (delimiter_pos == std::string::npos &&
538 (delimiter_idx < split_part_ - 1UL || delimiter_idx < 1UL)) {
540 return NullableStrType();
544 const size_t substr_start =
545 delimiter_pos == std::string::npos ? 0UL : delimiter_pos + delimiter_length_;
546 return str.substr(substr_start, last_delimiter_pos - substr_start);
548 const size_t substr_start =
549 split_part_ == 1UL ? 0UL : last_delimiter_pos + delimiter_length_;
550 return str.substr(substr_start, delimiter_pos - substr_start);
554 NullableStrType RegexpReplace::operator()(
const std::string& str)
const {
555 const int64_t str_len = str.size();
556 const int64_t pos = start_pos_ < 0 ? str_len + start_pos_ : start_pos_;
557 const size_t wrapped_start = std::clamp(pos, int64_t(0), str_len);
558 if (occurrence_ == 0L) {
560 std::string::const_iterator replace_start(str.cbegin() + wrapped_start);
561 boost::regex_replace(std::back_inserter(result),
566 return str.substr(0UL, wrapped_start) +
result;
568 const auto occurrence_match_pos = RegexpReplace::get_nth_regex_match(
572 occurrence_ > 0 ? occurrence_ - 1 : occurrence_);
573 if (occurrence_match_pos.first == std::string::npos) {
578 std::string::const_iterator replace_start(str.cbegin() + occurrence_match_pos.first);
579 std::string::const_iterator replace_end(str.cbegin() + occurrence_match_pos.second);
580 std::string replaced_match;
581 boost::regex_replace(std::back_inserter(replaced_match),
586 return str.substr(0UL, occurrence_match_pos.first) + replaced_match +
587 str.substr(occurrence_match_pos.second, std::string::npos);
591 std::pair<size_t, size_t> RegexpReplace::get_nth_regex_match(
592 const std::string& str,
593 const size_t start_pos,
594 const boost::regex& regex_pattern,
595 const int64_t occurrence) {
596 std::vector<std::pair<size_t, size_t>> regex_match_positions;
597 std::string::const_iterator search_start(str.cbegin() + start_pos);
599 int64_t match_idx = 0;
600 size_t string_pos = start_pos;
601 while (boost::regex_search(search_start, str.cend(), match, regex_pattern)) {
602 string_pos += match.position(
size_t(0)) + match.length(0);
603 regex_match_positions.emplace_back(
604 std::make_pair(string_pos - match.length(0), string_pos));
605 if (match_idx++ == occurrence) {
606 return regex_match_positions.back();
609 match.suffix().first;
615 const int64_t wrapped_match = occurrence >= 0 ? occurrence : match_idx + occurrence;
616 if (wrapped_match < 0 || wrapped_match >= match_idx) {
618 return std::make_pair(std::string::npos, std::string::npos);
620 return regex_match_positions[wrapped_match];
623 NullableStrType RegexpSubstr::operator()(
const std::string& str)
const {
624 const int64_t str_len = str.size();
625 const int64_t pos = start_pos_ < 0 ? str_len + start_pos_ : start_pos_;
626 const size_t wrapped_start = std::clamp(pos, int64_t(0), str_len);
627 int64_t match_idx = 0;
629 std::vector<std::string> regex_matches;
630 std::string::const_iterator search_start(str.cbegin() + wrapped_start);
632 while (boost::regex_search(search_start, str.cend(), match, regex_pattern_)) {
633 if (match_idx++ == occurrence_) {
634 if (sub_match_info_.first) {
635 return RegexpSubstr::get_sub_match(match, sub_match_info_);
637 return NullableStrType(match[0]);
639 regex_matches.emplace_back(match[0]);
641 match.suffix().first;
643 const int64_t wrapped_match = occurrence_ >= 0 ? occurrence_ : match_idx + occurrence_;
644 if (wrapped_match < 0 || wrapped_match >= match_idx) {
645 return NullableStrType();
647 if (sub_match_info_.first) {
648 return RegexpSubstr::get_sub_match(match, sub_match_info_);
650 return regex_matches[wrapped_match];
653 std::string RegexpSubstr::get_sub_match(
const boost::smatch& match,
654 const std::pair<bool, int64_t> sub_match_info) {
655 const int64_t num_sub_matches = match.size() - 1;
656 const int64_t wrapped_sub_match = sub_match_info.second >= 0
657 ? sub_match_info.second
658 : num_sub_matches + sub_match_info.second;
659 if (wrapped_sub_match < 0 || wrapped_sub_match >= num_sub_matches) {
662 return match[wrapped_sub_match + 1];
665 std::pair<bool, int64_t> RegexpSubstr::set_sub_match_info(
666 const std::string& regex_pattern,
667 const int64_t sub_match_group_idx) {
668 if (regex_pattern.find(
"e", 0UL) == std::string::npos) {
669 return std::make_pair(
false, 0UL);
671 return std::make_pair(
672 true, sub_match_group_idx > 0L ? sub_match_group_idx - 1 : sub_match_group_idx);
676 JsonValue::JsonParseMode JsonValue::parse_json_parse_mode(std::string_view json_path) {
677 size_t const string_pos = json_path.find(
'$');
678 if (string_pos == 0) {
680 return JsonValue::JsonParseMode::PARSE_MODE_LAX;
681 }
else if (string_pos == std::string::npos) {
682 throw std::runtime_error(
"JSON search path must include a '$' literal.");
684 std::string_view
const prefix = json_path.substr(0, string_pos);
685 if (boost::iequals(prefix, std::string_view(
"lax "))) {
686 return JsonValue::JsonParseMode::PARSE_MODE_LAX;
687 }
else if (boost::iequals(prefix, std::string_view(
"strict "))) {
688 if constexpr (JsonValue::allow_strict_json_parsing) {
689 return JsonValue::JsonParseMode::PARSE_MODE_STRICT;
691 throw std::runtime_error(
"Strict parsing not currently supported for JSON_VALUE.");
694 throw std::runtime_error(
"Issue parsing JSON_VALUE Parse Mode.");
698 std::vector<JsonValue::JsonKey> JsonValue::parse_json_path(
const std::string& json_path) {
700 size_t string_pos = json_path.find(
"$");
701 if (string_pos == std::string::npos) {
702 throw std::runtime_error(
"JSON search path must begin with '$' literal.");
707 static const auto& key_regex = *
new boost::regex(
708 R
"~(^(\.(([[:alpha:]][[:alnum:]_-]*)|"([[:alpha:]][ [:alnum:]_-]*)"))|\[([[:digit:]]+)\])~",
709 boost::regex_constants::extended | boost::regex_constants::optimize);
710 static_assert(std::is_trivially_destructible_v<decltype(key_regex)>);
712 std::string::const_iterator search_start(json_path.cbegin() + string_pos);
714 std::vector<JsonKey> json_keys;
715 while (boost::regex_search(search_start, json_path.cend(), match, key_regex)) {
717 if (match.position(
size_t(0)) != 0L) {
719 throw std::runtime_error(
"JSON search path parsing error: '" + json_path +
"'");
721 size_t matching_expr = 0;
722 if (match[3].matched) {
725 }
else if (match[4].matched) {
728 }
else if (match[5].matched) {
733 string_pos += match.length(0);
735 const std::string key_match(match[matching_expr].first, match[matching_expr].second);
737 if (isalpha(key_match[0])) {
739 json_keys.emplace_back(JsonKey(key_match));
742 json_keys.emplace_back(JsonKey(std::stoi(key_match)));
745 match.suffix().first;
747 if (json_keys.empty()) {
748 throw std::runtime_error(
"No keys found in JSON search path.");
750 if (string_pos < json_path.size()) {
751 throw std::runtime_error(
"JSON path parsing error.");
756 NullableStrType JsonValue::operator()(
const std::string& str)
const {
757 rapidjson::Document document;
758 if (document.Parse(str.c_str()).HasParseError()) {
759 if constexpr (JsonValue::allow_strict_json_parsing) {
760 return handle_parse_error(str);
762 return NullableStrType();
765 rapidjson::Value& json_val = document;
766 for (
const auto& json_key : json_keys_) {
767 switch (json_key.key_kind) {
768 case JsonKeyKind::JSON_OBJECT: {
769 if (!json_val.IsObject() || !json_val.HasMember(json_key.object_key)) {
770 if constexpr (JsonValue::allow_strict_json_parsing) {
771 return handle_key_error(str);
773 return NullableStrType();
776 json_val = json_val[json_key.object_key];
779 case JsonKeyKind::JSON_ARRAY: {
780 if (!json_val.IsArray() || json_val.Size() <= json_key.array_key) {
781 if constexpr (JsonValue::allow_strict_json_parsing) {
782 return handle_key_error(str);
784 return NullableStrType();
787 json_val = json_val[json_key.array_key];
793 if (json_val.IsString()) {
794 return NullableStrType(std::string(json_val.GetString()));
795 }
else if (json_val.IsNumber()) {
796 if (json_val.IsDouble()) {
798 }
else if (json_val.IsInt64()) {
800 }
else if (json_val.IsUint64()) {
806 if constexpr (JsonValue::allow_strict_json_parsing) {
807 return handle_key_error(str);
809 return NullableStrType();
812 }
else if (json_val.IsBool()) {
813 return NullableStrType(std::string(json_val.IsTrue() ?
"true" :
"false"));
814 }
else if (json_val.IsNull()) {
815 return NullableStrType();
819 if constexpr (JsonValue::allow_strict_json_parsing) {
820 return handle_key_error(str);
822 return NullableStrType();
827 NullableStrType Base64Encode::operator()(
const std::string& str)
const {
831 NullableStrType Base64Decode::operator()(
const std::string& str)
const {
835 std::string StringOps::operator()(
const std::string& str)
const {
836 NullableStrType modified_str(str);
837 if (modified_str.is_null) {
840 for (
const auto& string_op : string_ops_) {
841 modified_str = string_op->operator()(modified_str.str);
842 if (modified_str.is_null) {
846 return modified_str.str;
849 std::string StringOps::multi_input_eval(
const std::string_view str1,
850 const std::string_view str2)
const {
851 NullableStrType modified_str1(str1);
852 NullableStrType modified_str2(str2);
853 if (modified_str1.is_null || modified_str2.is_null) {
856 for (
const auto& string_op : string_ops_) {
857 modified_str1 = string_op->operator()(modified_str1.str, modified_str2.str);
858 if (modified_str1.is_null) {
862 return modified_str1.str;
865 std::string_view StringOps::operator()(
const std::string_view sv,
866 std::string& sv_storage)
const {
868 NullableStrType nullable_str(sv);
869 for (
const auto& string_op : string_ops_) {
870 nullable_str = string_op->operator()(nullable_str.str);
871 if (nullable_str.is_null) {
875 sv_storage = nullable_str.str;
879 Datum StringOps::numericEval(
const std::string_view str)
const {
880 const auto num_string_producing_ops = string_ops_.size() - 1;
881 if (num_string_producing_ops == 0UL) {
884 return string_ops_.back()->numericEval(str);
886 NullableStrType modified_str(str);
887 for (
size_t string_op_idx = 0; string_op_idx < num_string_producing_ops;
889 const auto& string_op = string_ops_[string_op_idx];
890 modified_str = string_op->operator()(modified_str.str);
891 if (modified_str.is_null) {
895 return string_ops_.back()->numericEval(modified_str.str);
898 Datum StringOps::numericEval(
const std::string_view str1,
899 const std::string_view str2)
const {
900 const auto num_string_producing_ops = string_ops_.size() - 1;
904 CHECK_EQ(num_string_producing_ops, 0UL);
905 return string_ops_.back()->numericEval(str1, str2);
908 std::vector<std::unique_ptr<const StringOp>> StringOps::genStringOpsFromOpInfos(
909 const std::vector<StringOpInfo>& string_op_infos)
const {
912 std::vector<std::unique_ptr<const StringOp>> string_ops;
913 string_ops.reserve(string_op_infos.size());
914 for (
const auto& string_op_info : string_op_infos) {
923 std::optional<std::string> var_string_optional_literal;
924 const auto op_kind = string_op_info.
getOpKind();
928 return std::make_unique<const NullOp>(var_string_optional_literal, op_kind);
939 CHECK_EQ(num_non_variable_literals, 0UL);
940 return std::make_unique<const Lower>(var_string_optional_literal);
943 CHECK_EQ(num_non_variable_literals, 0UL);
944 return std::make_unique<const Upper>(var_string_optional_literal);
947 CHECK_EQ(num_non_variable_literals, 0UL);
948 return std::make_unique<const InitCap>(var_string_optional_literal);
951 CHECK_EQ(num_non_variable_literals, 0UL);
952 return std::make_unique<const Reverse>(var_string_optional_literal);
955 CHECK_EQ(num_non_variable_literals, 1UL);
956 const auto num_repeats_literal = string_op_info.
getIntLiteral(1);
957 return std::make_unique<const Repeat>(var_string_optional_literal,
958 num_repeats_literal);
962 CHECK_GE(num_non_variable_literals, 0UL);
963 CHECK_LE(num_non_variable_literals, 1UL);
964 if (num_non_variable_literals == 1UL) {
967 return std::make_unique<const Concat>(var_string_optional_literal,
971 return std::make_unique<const Concat>(var_string_optional_literal);
976 CHECK_EQ(num_non_variable_literals, 2UL);
977 const auto padded_length_literal = string_op_info.
getIntLiteral(1);
979 return std::make_unique<Pad>(var_string_optional_literal,
981 padded_length_literal,
982 padding_string_literal);
987 CHECK_EQ(num_non_variable_literals, 1UL);
989 return std::make_unique<Trim>(
990 var_string_optional_literal, op_kind, trim_chars_literal);
993 CHECK_GE(num_non_variable_literals, 1UL);
994 CHECK_LE(num_non_variable_literals, 2UL);
995 const auto start_pos_literal = string_op_info.
getIntLiteral(1);
997 if (has_length_literal) {
999 return std::make_unique<const Substring>(
1000 var_string_optional_literal, start_pos_literal, length_literal);
1002 return std::make_unique<const Substring>(var_string_optional_literal,
1007 CHECK_GE(num_non_variable_literals, 2UL);
1008 CHECK_LE(num_non_variable_literals, 3UL);
1010 const auto start_pos_literal = string_op_info.
getIntLiteral(2);
1012 if (has_length_literal) {
1013 const auto length_literal = string_op_info.
getIntLiteral(3);
1014 return std::make_unique<const Overlay>(var_string_optional_literal,
1015 replace_string_literal,
1019 return std::make_unique<const Overlay>(
1020 var_string_optional_literal, replace_string_literal, start_pos_literal);
1024 CHECK_GE(num_non_variable_literals, 2UL);
1025 CHECK_LE(num_non_variable_literals, 2UL);
1027 const auto replacement_string_literal = string_op_info.
getStringLiteral(2);
1028 return std::make_unique<const Replace>(var_string_optional_literal,
1029 pattern_string_literal,
1030 replacement_string_literal);
1033 CHECK_GE(num_non_variable_literals, 2UL);
1034 CHECK_LE(num_non_variable_literals, 2UL);
1036 const auto split_part_literal = string_op_info.
getIntLiteral(2);
1037 return std::make_unique<const SplitPart>(
1038 var_string_optional_literal, delimiter_literal, split_part_literal);
1041 CHECK_GE(num_non_variable_literals, 5UL);
1042 CHECK_LE(num_non_variable_literals, 5UL);
1045 const auto start_pos_literal = string_op_info.
getIntLiteral(3);
1046 const auto occurrence_literal = string_op_info.
getIntLiteral(4);
1048 return std::make_unique<const RegexpReplace>(var_string_optional_literal,
1050 replacement_literal,
1053 regex_params_literal);
1056 CHECK_GE(num_non_variable_literals, 5UL);
1057 CHECK_LE(num_non_variable_literals, 5UL);
1059 const auto start_pos_literal = string_op_info.
getIntLiteral(2);
1060 const auto occurrence_literal = string_op_info.
getIntLiteral(3);
1062 const auto sub_match_idx_literal = string_op_info.
getIntLiteral(5);
1063 return std::make_unique<const RegexpSubstr>(var_string_optional_literal,
1067 regex_params_literal,
1068 sub_match_idx_literal);
1071 CHECK_EQ(num_non_variable_literals, 1UL);
1073 return std::make_unique<const JsonValue>(var_string_optional_literal,
1077 CHECK_EQ(num_non_variable_literals, 0UL);
1078 return std::make_unique<const Base64Encode>(var_string_optional_literal);
1081 CHECK_EQ(num_non_variable_literals, 0UL);
1082 return std::make_unique<const Base64Decode>(var_string_optional_literal);
1085 CHECK_EQ(num_non_variable_literals, 0UL);
1086 return std::make_unique<const TryStringCast>(return_ti,
1087 var_string_optional_literal);
1090 CHECK_GE(num_non_variable_literals, 1UL);
1091 CHECK_LE(num_non_variable_literals, 2UL);
1094 if (has_start_pos_literal) {
1095 const auto start_pos_literal = string_op_info.
getIntLiteral(2);
1096 return std::make_unique<const Position>(
1097 var_string_optional_literal, search_literal, start_pos_literal);
1099 return std::make_unique<const Position>(var_string_optional_literal,
1104 CHECK_GE(num_non_variable_literals, 0UL);
1105 CHECK_LE(num_non_variable_literals, 1UL);
1106 if (num_non_variable_literals == 1UL) {
1108 return std::make_unique<const JarowinklerSimilarity>(var_string_optional_literal,
1111 return std::make_unique<const JarowinklerSimilarity>(var_string_optional_literal);
1115 CHECK_GE(num_non_variable_literals, 0UL);
1116 CHECK_LE(num_non_variable_literals, 1UL);
1117 if (num_non_variable_literals == 1UL) {
1119 return std::make_unique<const LevenshteinDistance>(var_string_optional_literal,
1122 return std::make_unique<const LevenshteinDistance>(var_string_optional_literal);
1127 return std::make_unique<NullOp>(var_string_optional_literal, op_kind);
1131 return std::make_unique<NullOp>(var_string_optional_literal, op_kind);
1138 const std::string null_str{
""};
1139 return std::make_pair(null_str,
true);
1142 return string_op->operator()().toPair();
1148 return string_op->numericEval();
const SQLTypeInfo & getReturnType() const
Datum apply_numeric_op_to_literals(const StringOpInfo &string_op_info)
T compute_levenshtein_distance_template(std::string_view s1, std::string_view s2)
size_t numLiterals() const
bool intLiteralArgAtIdxExists(const size_t index) const
double compute_jaro_winkler_score(std::string_view s1, std::string_view s2)
int64_t getIntLiteral(const size_t index) const
size_t numNonVariableLiterals() const
bool hasNullLiteralArg() const
bool hasVarStringLiteral() const
std::pair< std::string, bool > apply_string_op_to_literals(const StringOpInfo &string_op_info)
Datum StringToDatum(const std::string_view s, SQLTypeInfo &ti)
OUTPUT transform(INPUT const &input, FUNC const &func)
Datum NullDatum(const SQLTypeInfo &ti)
constexpr int winkler_k_prefix_length
double compute_jaro_score(std::string_view s1, std::string_view s2)
int64_t compute_levenshtein_distance(std::string_view s1, std::string_view s2)
std::string getStringLiteral(const size_t index) const
std::string decode_base64(const std::string &val, bool trim_nulls)
static std::string encode_base64(const std::string &val)
const SqlStringOpKind & getOpKind() const
constexpr double winkler_k_scaling_factor
std::unique_ptr< const StringOp > gen_string_op(const StringOpInfo &string_op_info)