| 1 | /* Copyright (c) 2011, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | #include "my_config.h" |
| 26 | |
| 27 | #include <gmock/gmock.h> |
| 28 | #include <gtest/gtest.h> |
| 29 | |
| 30 | #include <algorithm> |
| 31 | #include <initializer_list> |
| 32 | #include <limits> |
| 33 | |
| 34 | #include "sql/item_regexp_func.h" |
| 35 | #include "sql/parse_tree_items.h" |
| 36 | #include "unittest/gunit/benchmark.h" |
| 37 | #include "unittest/gunit/item_utils.h" |
| 38 | #include "unittest/gunit/mock_parse_tree.h" |
| 39 | #include "unittest/gunit/test_utils.h" |
| 40 | |
| 41 | namespace item_func_regexp_unittest { |
| 42 | |
| 43 | using my_testing::Mock_pt_item_list; |
| 44 | using my_testing::Server_initializer; |
| 45 | using std::string; |
| 46 | |
| 47 | class ItemFuncRegexpTest : public ::testing::Test { |
| 48 | protected: |
| 49 | void SetUp() override { initializer.SetUp(); } |
| 50 | void TearDown() override { initializer.TearDown(); } |
| 51 | |
| 52 | THD *thd() { return initializer.thd(); } |
| 53 | |
| 54 | Server_initializer initializer; |
| 55 | |
| 56 | template <typename Item_type> |
| 57 | void test_print(const char *expected, |
| 58 | std::initializer_list<const char *> args) { |
| 59 | auto items = new (thd()->mem_root) Mock_pt_item_list(args); |
| 60 | Item *item = new Item_type(POS(), items); |
| 61 | Parse_context pc(thd(), thd()->lex->query_block); |
| 62 | item->itemize(&pc, &item); |
| 63 | item->fix_fields(thd(), nullptr); |
| 64 | String buf; |
| 65 | item->print(thd(), &buf, QT_ORDINARY); |
| 66 | EXPECT_STREQ(expected, buf.c_ptr_safe()); |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | TEST_F(ItemFuncRegexpTest, Print) { |
| 71 | test_print<Item_func_regexp_instr>("regexp_instr('abc','def')", |
| 72 | {"abc", "def"}); |
| 73 | test_print<Item_func_regexp_like>("regexp_like('abc','def')", {"abc", "def"}); |
| 74 | test_print<Item_func_regexp_replace>("regexp_replace('ab','c','d')", |
| 75 | {"ab", "c", "d"}); |
| 76 | test_print<Item_func_regexp_substr>("regexp_substr('x','y')", {"x", "y"}); |
| 77 | } |
| 78 | |
| 79 | static bool is_control_character(char c) { |
| 80 | // This set of control characters is by no means exhaustive. It's based solely |
| 81 | // on experimentation of which characters can't be used in a single-character |
| 82 | // pattern in this particular regexp library (ICU). |
| 83 | static const std::vector<char> control_chars = { |
| 84 | '.', '$', '|', '^', '(', ')', '*', '+', '?', '[', '\\', '{', '}'}; |
| 85 | return std::find(control_chars.begin(), control_chars.end(), c) != |
| 86 | control_chars.end(); |
| 87 | } |
| 88 | |
| 89 | Item *make_binary_literal(THD *thd, const string &s) { |
| 90 | POS pos; // We expect this object to be copied. |
| 91 | LEX_STRING lex_string = {const_cast<char *>(s.c_str()), s.length()}; |
| 92 | return new (thd->mem_root) PTI_text_literal_underscore_charset( |
| 93 | pos, false, &my_charset_bin, lex_string); |
| 94 | } |
| 95 | |
| 96 | TEST_F(ItemFuncRegexpTest, BinaryCharset) { |
| 97 | static const int max_char = std::numeric_limits<unsigned char>::max(); |
| 98 | for (int i = 0; i <= max_char; ++i) { |
| 99 | string subject; |
| 100 | subject = char(i); |
| 101 | for (int j = 0; j <= max_char; ++j) { |
| 102 | string pattern; |
| 103 | if (is_control_character(char(j))) pattern = '\\'; |
| 104 | pattern += char(j); |
| 105 | |
| 106 | Item *subject_item = make_binary_literal(thd(), subject); |
| 107 | Item *pattern_item = make_binary_literal(thd(), pattern); |
| 108 | auto rlike = make_resolved<Item_func_regexp_like>(thd(), subject_item, |
| 109 | pattern_item); |
| 110 | if (i == j) |
| 111 | EXPECT_EQ(1, rlike->val_int()) |
| 112 | << "They're equal(" << i |
| 113 | << "), they should match via regexp matching."; |
| 114 | else |
| 115 | EXPECT_EQ(0, rlike->val_int()) |
| 116 | << "They're not equal(" << i << " vs " << j |
| 117 | << "), they should not match via regexp matching."; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | Benchmark performance of LIKE vs REGEXP. |
| 124 | LIKE uses my_wildcmp which calls my_wildcmp_8bit_impl. |
| 125 | REGEXP uses the ICU regexp matcher, where most of the time is spent in |
| 126 | RegexMatcher::MatchChunkAt, ucase_toFullFolding and |
| 127 | CaseFoldingUCharIterator::next |
| 128 | It turns out ICU is orders of magnitute slower, esp. if there is no match. |
| 129 | */ |
| 130 | static const char *substrings[2] = { |
| 131 | "Folder=RootObject=" |
| 132 | "Operating System-WinWinServerLocalAdminis-trator1-ABCDEFGHI01-adminimm", |
| 133 | "tasktype=ReconcileTask"}; |
| 134 | |
| 135 | static std::string make_like_pattern() { |
| 136 | return std::string("%") + substrings[0] + "%" + substrings[1] + "%"; |
| 137 | } |
| 138 | |
| 139 | static std::string make_rlike_pattern() { |
| 140 | return std::string(".*") + substrings[0] + ".*" + substrings[1] + ".*"; |
| 141 | } |
| 142 | |
| 143 | static std::string make_matched_string() { |
| 144 | return std::string("hello") + substrings[0] + "42" + substrings[1] + |
| 145 | "goodbye"; |
| 146 | } |
| 147 | |
| 148 | CHARSET_INFO *init_collation(const char *name) { |
| 149 | MY_CHARSET_LOADER loader; |
| 150 | return my_collation_get_by_name(&loader, name, MYF(0)); |
| 151 | } |
| 152 | |
| 153 | Item_string *make_string_item(const std::string *str) { |
| 154 | CHARSET_INFO *cs = init_collation("ascii_general_ci"); |
| 155 | return new Item_string(str->data(), str->length(), cs); |
| 156 | } |
| 157 | |
| 158 | class LikeVSRlikeTest : public ::testing::Test { |
| 159 | protected: |
| 160 | void SetUp() override { |
| 161 | initializer.SetUp(); |
| 162 | dummy_subject_item = make_string_item(&dummy_string); |
| 163 | match_subject_item = make_string_item(&match_string); |
| 164 | like_pattern_item = make_string_item(&like_pattern); |
| 165 | rlike_pattern_item = make_string_item(&rlike_pattern); |
| 166 | } |
| 167 | void TearDown() override { initializer.TearDown(); } |
| 168 | |
| 169 | THD *thd() { return initializer.thd(); } |
| 170 | |
| 171 | const std::string dummy_string = "this is a dummy string"; |
| 172 | const std::string like_pattern = make_like_pattern(); |
| 173 | const std::string rlike_pattern = make_rlike_pattern(); |
| 174 | const std::string match_string = make_matched_string(); |
| 175 | |
| 176 | Server_initializer initializer; |
| 177 | Item_string *dummy_subject_item; |
| 178 | Item_string *match_subject_item; |
| 179 | Item_string *like_pattern_item; |
| 180 | Item_string *rlike_pattern_item; |
| 181 | }; |
| 182 | |
| 183 | // "dummy text" LIKE "pattern with three %" |
| 184 | // "matching text" LIKE "pattern with three %" |
| 185 | TEST_F(LikeVSRlikeTest, SimpleLike) { |
| 186 | Item_func_like *like_item = |
| 187 | new Item_func_like(dummy_subject_item, like_pattern_item); |
| 188 | EXPECT_EQ(0, like_item->fix_fields(thd(), nullptr)); |
| 189 | EXPECT_EQ(0, like_item->val_int()); |
| 190 | |
| 191 | like_item = new Item_func_like(match_subject_item, like_pattern_item); |
| 192 | EXPECT_EQ(0, like_item->fix_fields(thd(), nullptr)); |
| 193 | EXPECT_EQ(1, like_item->val_int()); |
| 194 | } |
| 195 | |
| 196 | // "dummy text" REGEXP "pattern with three .*" |
| 197 | // "matching text" REGEXP "pattern with three .*" |
| 198 | TEST_F(LikeVSRlikeTest, SimpleRLike) { |
| 199 | Item *like_item = make_resolved<Item_func_regexp_like>( |
| 200 | thd(), dummy_subject_item, rlike_pattern_item); |
| 201 | EXPECT_EQ(0, like_item->val_int()); |
| 202 | like_item = make_resolved<Item_func_regexp_like>(thd(), match_subject_item, |
| 203 | rlike_pattern_item); |
| 204 | EXPECT_EQ(1, like_item->val_int()); |
| 205 | } |
| 206 | |
| 207 | // Benchmark LIKE which does not match. |
| 208 | static void BM_LikeNoMatch(size_t num_iterations) { |
| 209 | StopBenchmarkTiming(); |
| 210 | |
| 211 | my_testing::Server_initializer initializer; |
| 212 | initializer.SetUp(); |
| 213 | |
| 214 | std::string dummy_string = "this is a dummy string"; |
| 215 | std::string like_pattern = make_like_pattern(); |
| 216 | Item_func_like *like_item = new Item_func_like( |
| 217 | make_string_item(&dummy_string), make_string_item(&like_pattern)); |
| 218 | like_item->fix_fields(initializer.thd(), nullptr); |
| 219 | |
| 220 | StartBenchmarkTiming(); |
| 221 | longlong num_matches = 0; |
| 222 | for (size_t i = 0; i < num_iterations; ++i) { |
| 223 | num_matches += like_item->val_int(); |
| 224 | } |
| 225 | StopBenchmarkTiming(); |
| 226 | EXPECT_EQ(0, num_matches); |
| 227 | |
| 228 | initializer.TearDown(); |
| 229 | } |
| 230 | BENCHMARK(BM_LikeNoMatch) |
| 231 | |
| 232 | // Benchmark LIKE with match. |
| 233 | static void BM_LikeWithMatch(size_t num_iterations) { |
| 234 | StopBenchmarkTiming(); |
| 235 | |
| 236 | my_testing::Server_initializer initializer; |
| 237 | initializer.SetUp(); |
| 238 | |
| 239 | std::string match_string = make_matched_string(); |
| 240 | std::string like_pattern = make_like_pattern(); |
| 241 | Item_func_like *like_item = new Item_func_like( |
| 242 | make_string_item(&match_string), make_string_item(&like_pattern)); |
| 243 | like_item->fix_fields(initializer.thd(), nullptr); |
| 244 | |
| 245 | StartBenchmarkTiming(); |
| 246 | longlong num_matches = 0; |
| 247 | for (size_t i = 0; i < num_iterations; ++i) { |
| 248 | num_matches += like_item->val_int(); |
| 249 | } |
| 250 | StopBenchmarkTiming(); |
| 251 | EXPECT_EQ(num_iterations, num_matches); |
| 252 | |
| 253 | initializer.TearDown(); |
| 254 | } |
| 255 | BENCHMARK(BM_LikeWithMatch) |
| 256 | |
| 257 | // Benchmark REGEXP which does not match. |
| 258 | static void BM_RlikeNoMatch(size_t num_iterations) { |
| 259 | StopBenchmarkTiming(); |
| 260 | |
| 261 | my_testing::Server_initializer initializer; |
| 262 | initializer.SetUp(); |
| 263 | |
| 264 | std::string dummy_string = "this is a dummy string"; |
| 265 | std::string rlike_pattern = make_rlike_pattern(); |
| 266 | Item *like_item = make_resolved<Item_func_regexp_like>( |
| 267 | initializer.thd(), make_string_item(&dummy_string), |
| 268 | make_string_item(&rlike_pattern)); |
| 269 | StartBenchmarkTiming(); |
| 270 | longlong num_matches = 0; |
| 271 | for (size_t i = 0; i < num_iterations; ++i) { |
| 272 | num_matches += like_item->val_int(); |
| 273 | } |
| 274 | StopBenchmarkTiming(); |
| 275 | EXPECT_EQ(0, num_matches); |
| 276 | |
| 277 | initializer.TearDown(); |
| 278 | } |
| 279 | BENCHMARK(BM_RlikeNoMatch) |
| 280 | |
| 281 | // Benchmark REGEXP with match. |
| 282 | static void BM_RlikeWithMatch(size_t num_iterations) { |
| 283 | StopBenchmarkTiming(); |
| 284 | |
| 285 | my_testing::Server_initializer initializer; |
| 286 | initializer.SetUp(); |
| 287 | |
| 288 | std::string match_string = make_matched_string(); |
| 289 | std::string rlike_pattern = make_rlike_pattern(); |
| 290 | Item *like_item = make_resolved<Item_func_regexp_like>( |
| 291 | initializer.thd(), make_string_item(&match_string), |
| 292 | make_string_item(&rlike_pattern)); |
| 293 | StartBenchmarkTiming(); |
| 294 | longlong num_matches = 0; |
| 295 | for (size_t i = 0; i < num_iterations; ++i) { |
| 296 | num_matches += like_item->val_int(); |
| 297 | } |
| 298 | StopBenchmarkTiming(); |
| 299 | EXPECT_EQ(num_iterations, num_matches); |
| 300 | |
| 301 | initializer.TearDown(); |
| 302 | } |
| 303 | BENCHMARK(BM_RlikeWithMatch) |
| 304 | |
| 305 | } // namespace item_func_regexp_unittest |