Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion be/src/exprs/function/function_string_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -839,11 +839,18 @@ class FunctionCountSubString : public IFunction {
size_t find_pos(size_t pos, const StringRef str_ref, const StringRef pattern_ref) const {
size_t old_size = pos;
size_t str_size = str_ref.size;
while (pos < str_size &&
if (pattern_ref.size > str_size || pos > str_size - pattern_ref.size) {
return str_size - old_size;
}
const size_t last_match_pos = str_size - pattern_ref.size;
while (pos <= last_match_pos &&
memcmp_small_allow_overflow15((const uint8_t*)str_ref.data + pos,
(const uint8_t*)pattern_ref.data, pattern_ref.size)) {
pos++;
}
if (pos > last_match_pos) {
return str_size - old_size;
}
return pos - old_size;
}

Expand Down
6 changes: 6 additions & 0 deletions be/test/exprs/function/function_string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3648,6 +3648,9 @@ TEST(function_string_test, function_count_substring_test) {
{{std::string("hello world"), std::string("")}, std::int32_t(0)},
{{std::string(""), std::string("l")}, std::int32_t(0)},
{{std::string(""), std::string("")}, std::int32_t(0)},
{{std::string("ccc"), std::string("cc")}, std::int32_t(1)},
{{std::string("aaaa"), std::string("aa")}, std::int32_t(2)},
{{std::string("ab"), std::string("abc")}, std::int32_t(0)},
// utf-8 characters
{{std::string("你好123世界"), std::string("世")}, std::int32_t(1)},
{{std::string("你好123世界"), std::string("你")}, std::int32_t(1)},
Expand All @@ -3673,6 +3676,9 @@ TEST(function_string_test, function_count_substring_test) {
{{std::string("hello world"), std::string(""), std::int32_t(0)}, std::int32_t(0)},
{{std::string(""), std::string("l"), std::int32_t(1)}, std::int32_t(0)},
{{std::string(""), std::string(""), std::int32_t(1)}, std::int32_t(0)},
{{std::string("ccc"), std::string("cc"), std::int32_t(1)}, std::int32_t(1)},
{{std::string("ccc"), std::string("cc"), std::int32_t(3)}, std::int32_t(0)},
{{std::string("ab"), std::string("abc"), std::int32_t(1)}, std::int32_t(0)},
// utf-8 characters
{{std::string("你好123世界"), std::string("世"), std::int32_t(3)}, std::int32_t(1)},
{{std::string("你好123世界"), std::string("你"), std::int32_t(1)}, std::int32_t(1)},
Expand Down
Loading