This commit is contained in:
Han Fei 2023-03-14 17:44:02 +01:00
parent de8d0040a4
commit 01be209e43
2 changed files with 9 additions and 5 deletions

View File

@ -1,4 +1,5 @@
#include <set> #include <set>
#include <string>
#include <Common/Exception.h> #include <Common/Exception.h>
#include <Common/PODArray.h> #include <Common/PODArray.h>
@ -359,7 +360,7 @@ finish:
} }
} }
if (max_length >= MIN_LENGTH_FOR_STRSTR || !first_call) if (max_length >= MIN_LENGTH_FOR_STRSTR || (!first_call && max_length > 0))
{ {
required_substring.literal = candidate_it->first; required_substring.literal = candidate_it->first;
required_substring.prefix = candidate_it->second == 0; required_substring.prefix = candidate_it->second == 0;
@ -430,6 +431,7 @@ OptimizedRegularExpressionImpl<thread_safe>::OptimizedRegularExpressionImpl(cons
std::vector<std::string> alternatives; /// this vector collects patterns in (xx|xx|xx). for now it's not used. std::vector<std::string> alternatives; /// this vector collects patterns in (xx|xx|xx). for now it's not used.
analyze(regexp_, required_substring, is_trivial, required_substring_is_prefix, alternatives); analyze(regexp_, required_substring, is_trivial, required_substring_is_prefix, alternatives);
/// Just three following options are supported /// Just three following options are supported
if (options & (~(RE_CASELESS | RE_NO_CAPTURE | RE_DOT_NL))) if (options & (~(RE_CASELESS | RE_NO_CAPTURE | RE_DOT_NL)))
throw DB::Exception(DB::ErrorCodes::CANNOT_COMPILE_REGEXP, "OptimizedRegularExpression: Unsupported option."); throw DB::Exception(DB::ErrorCodes::CANNOT_COMPILE_REGEXP, "OptimizedRegularExpression: Unsupported option.");

View File

@ -4,7 +4,7 @@
TEST(OptimizeRE, analyze) TEST(OptimizeRE, analyze)
{ {
auto test_f = [](const std::string & regexp, const std::string & answer, std::vector<std::string> expect_alternatives = {}) auto test_f = [](const std::string & regexp, const std::string & answer, std::vector<std::string> expect_alternatives = {}, bool trival_expected = false)
{ {
std::string required; std::string required;
bool is_trivial; bool is_trivial;
@ -14,8 +14,10 @@ TEST(OptimizeRE, analyze)
std::cerr << regexp << std::endl; std::cerr << regexp << std::endl;
EXPECT_EQ(required, answer); EXPECT_EQ(required, answer);
EXPECT_EQ(alternatives, expect_alternatives); EXPECT_EQ(alternatives, expect_alternatives);
EXPECT_EQ(is_trivial, trival_expected);
}; };
test_f("abc", "abc"); test_f("abc", "abc", {}, true);
test_f("c([^k]*)de", "");
test_f("abc(de)fg", "abcdefg"); test_f("abc(de)fg", "abcdefg");
test_f("abc(de|xyz)fg", "abc", {"abcdefg", "abcxyzfg"}); test_f("abc(de|xyz)fg", "abc", {"abcdefg", "abcxyzfg"});
test_f("abc(de?f|xyz)fg", "abc", {"abcd", "abcxyzfg"}); test_f("abc(de?f|xyz)fg", "abc", {"abcd", "abcxyzfg"});
@ -35,8 +37,8 @@ TEST(OptimizeRE, analyze)
test_f(R"(abc(de|xyz|(\{xx\}))fg)", "abc", {"abcdefg", "abcxyzfg", "abc{xx}fg"}); test_f(R"(abc(de|xyz|(\{xx\}))fg)", "abc", {"abcdefg", "abcxyzfg", "abc{xx}fg"});
test_f("abc(abc|fg)?xyzz", "xyzz"); test_f("abc(abc|fg)?xyzz", "xyzz");
test_f("abc(abc|fg){0,1}xyzz", "xyzz"); test_f("abc(abc|fg){0,1}xyzz", "xyzz");
test_f("abc(abc|fg)xyzz|bcdd?k|bc(f|g|h?)z", "", {"abcabcxyzz", "abcfgxyzz", "bcd", "bcfz", "bcgz", "bcz"}); test_f("abc(abc|fg)xyzz|bcdd?k|bc(f|g|h?)z", "", {"abcabcxyzz", "abcfgxyzz", "bcd", "bcfz", "bcgz", ""});
test_f("abc(abc|fg)xyzz|bc(dd?x|kk?y|(f))k|bc(f|g|h?)z", "", {"abcabcxyzz", "abcfgxyzz", "bcd", "bck", "bcfk", "bcfz", "bcgz", "bcz"}); test_f("abc(abc|fg)xyzz|bc(dd?x|kk?y|(f))k|bc(f|g|h?)z", "", {"abcabcxyzz", "abcfgxyzz", "bcd", "bck", "bcfk", "bcfz", "bcgz", ""});
test_f("((?:abc|efg|xyz)/[a-zA-Z0-9]{1-50})(/?[^ ]*|)", "", {"abc/", "efg/", "xyz/"}); test_f("((?:abc|efg|xyz)/[a-zA-Z0-9]{1-50})(/?[^ ]*|)", "", {"abc/", "efg/", "xyz/"});
test_f(R"([Bb]ai[Dd]u[Ss]pider(?:-[A-Za-z]{1,30})(?:-[A-Za-z]{1,30}|)|bingbot|\bYeti(?:-[a-z]{1,30}|)|Catchpoint(?: bot|)|[Cc]harlotte|Daumoa(?:-feedfetcher|)|(?:[a-zA-Z]{1,30}-|)Googlebot(?:-[a-zA-Z]{1,30}|))", "", {"pider-", "bingbot", "Yeti-", "Yeti", "Catchpoint bot", "Catchpoint", "harlotte", "Daumoa-feedfetcher", "Daumoa", "Googlebot-", "Googlebot"}); test_f(R"([Bb]ai[Dd]u[Ss]pider(?:-[A-Za-z]{1,30})(?:-[A-Za-z]{1,30}|)|bingbot|\bYeti(?:-[a-z]{1,30}|)|Catchpoint(?: bot|)|[Cc]harlotte|Daumoa(?:-feedfetcher|)|(?:[a-zA-Z]{1,30}-|)Googlebot(?:-[a-zA-Z]{1,30}|))", "", {"pider-", "bingbot", "Yeti-", "Yeti", "Catchpoint bot", "Catchpoint", "harlotte", "Daumoa-feedfetcher", "Daumoa", "Googlebot-", "Googlebot"});
} }