| 1 | /* Copyright (c) 2019, 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 | #include <cstddef> |
| 25 | #include <cstdint> |
| 26 | #include <memory> |
| 27 | #include <optional> |
| 28 | #include <random> |
| 29 | #include <string> |
| 30 | #include <utility> |
| 31 | #include <vector> |
| 32 | |
| 33 | #include <gmock/gmock.h> // IWYU pragma: keep |
| 34 | #include <gtest/gtest.h> // IWYU pragma: keep |
| 35 | |
| 36 | #include "extra/xxhash/my_xxhash.h" |
| 37 | #include "my_alloc.h" |
| 38 | #include "my_bitmap.h" |
| 39 | #include "my_config.h" |
| 40 | #include "my_inttypes.h" |
| 41 | #include "my_murmur3.h" |
| 42 | #include "mysql/components/services/bits/psi_bits.h" |
| 43 | #include "prealloced_array.h" |
| 44 | #include "sql/field.h" |
| 45 | #include "sql/item.h" |
| 46 | #include "sql/item_cmpfunc.h" |
| 47 | #include "sql/iterators/hash_join_iterator.h" |
| 48 | #include "sql/iterators/row_iterator.h" |
| 49 | #include "sql/join_type.h" |
| 50 | #include "sql/mem_root_array.h" |
| 51 | #include "sql/pack_rows.h" |
| 52 | #include "sql/sql_class.h" |
| 53 | #include "sql/sql_executor.h" |
| 54 | #include "sql/sql_opt_exec_shared.h" |
| 55 | #include "sql/sql_optimizer.h" |
| 56 | #include "sql/table.h" |
| 57 | #include "sql_string.h" |
| 58 | #include "template_utils.h" |
| 59 | #include "unittest/gunit/benchmark.h" |
| 60 | #include "unittest/gunit/fake_integer_iterator.h" |
| 61 | #include "unittest/gunit/fake_string_iterator.h" |
| 62 | #include "unittest/gunit/fake_table.h" |
| 63 | #include "unittest/gunit/mock_field_long.h" |
| 64 | #include "unittest/gunit/parsertest.h" |
| 65 | #include "unittest/gunit/temptable/mock_field_varstring.h" |
| 66 | #include "unittest/gunit/test_utils.h" |
| 67 | |
| 68 | class Query_block; |
| 69 | |
| 70 | using pack_rows::TableCollection; |
| 71 | using std::nullopt; |
| 72 | using std::optional; |
| 73 | using std::pair; |
| 74 | using std::string; |
| 75 | using std::vector; |
| 76 | using testing::ElementsAre; |
| 77 | using testing::UnorderedElementsAre; |
| 78 | |
| 79 | namespace hash_join_unittest { |
| 80 | |
| 81 | static TableCollection CreateTenTableJoin( |
| 82 | const my_testing::Server_initializer &initializer, MEM_ROOT *mem_root, |
| 83 | bool store_data) { |
| 84 | constexpr int kNumColumns = 10; |
| 85 | constexpr bool kColumnsNullable = true; |
| 86 | constexpr int kNumTablesInJoin = 10; |
| 87 | Prealloced_array<TABLE *, 4> tables(PSI_NOT_INSTRUMENTED); |
| 88 | |
| 89 | // Set up a ten-table join. For simplicity, allocate everything on a MEM_ROOT |
| 90 | // that will take care of releasing allocated memory. |
| 91 | Query_block *query_block = parse(&initializer, "SELECT * FROM dummy", 0); |
| 92 | JOIN join(initializer.thd(), query_block); |
| 93 | join.qep_tab = mem_root->ArrayAlloc<QEP_TAB>(kNumTablesInJoin); |
| 94 | join.tables = kNumTablesInJoin; |
| 95 | for (int i = 0; i < kNumTablesInJoin; ++i) { |
| 96 | Fake_TABLE *fake_table = |
| 97 | new (mem_root) Fake_TABLE(kNumColumns, kColumnsNullable); |
| 98 | fake_table->pos_in_table_list->set_tableno(i); |
| 99 | QEP_TAB *qep_tab = &join.qep_tab[i]; |
| 100 | qep_tab->set_qs(new (mem_root) QEP_shared); |
| 101 | qep_tab->set_table(fake_table); |
| 102 | qep_tab->table_ref = fake_table->pos_in_table_list; |
| 103 | |
| 104 | if (store_data) { |
| 105 | bitmap_set_all(fake_table->write_set); |
| 106 | |
| 107 | for (unsigned j = 0; j < fake_table->s->fields; ++j) { |
| 108 | fake_table->field[j]->store(1000, /*is_unsigned=*/false); |
| 109 | } |
| 110 | } |
| 111 | tables.push_back(fake_table); |
| 112 | } |
| 113 | |
| 114 | return {tables, |
| 115 | /*store_rowids=*/false, |
| 116 | /*tables_to_get_rowid_for=*/0, |
| 117 | /*tables_to_store_contents_of_null_rows_for=*/0}; |
| 118 | } |
| 119 | |
| 120 | static void DestroyFakeTables(const TableCollection &table_collection) { |
| 121 | for (const pack_rows::Table &table : table_collection.tables()) |
| 122 | destroy(pointer_cast<Fake_TABLE *>(table.table)); |
| 123 | } |
| 124 | |
| 125 | static void BM_StoreFromTableBuffersNoData(size_t num_iterations) { |
| 126 | StopBenchmarkTiming(); |
| 127 | |
| 128 | my_testing::Server_initializer initializer; |
| 129 | initializer.SetUp(); |
| 130 | MEM_ROOT mem_root; |
| 131 | TableCollection table_collection = |
| 132 | CreateTenTableJoin(initializer, &mem_root, false); |
| 133 | |
| 134 | String buffer; |
| 135 | buffer.reserve(1024); |
| 136 | |
| 137 | StartBenchmarkTiming(); |
| 138 | for (size_t i = 0; i < num_iterations; ++i) { |
| 139 | ASSERT_FALSE(StoreFromTableBuffers(table_collection, &buffer)); |
| 140 | ASSERT_GT(buffer.length(), 0); |
| 141 | } |
| 142 | StopBenchmarkTiming(); |
| 143 | |
| 144 | DestroyFakeTables(table_collection); |
| 145 | } |
| 146 | BENCHMARK(BM_StoreFromTableBuffersNoData) |
| 147 | |
| 148 | static void BM_StoreFromTableBuffersWithData(size_t num_iterations) { |
| 149 | StopBenchmarkTiming(); |
| 150 | |
| 151 | my_testing::Server_initializer initializer; |
| 152 | initializer.SetUp(); |
| 153 | |
| 154 | MEM_ROOT mem_root; |
| 155 | TableCollection table_collection = |
| 156 | CreateTenTableJoin(initializer, &mem_root, true); |
| 157 | |
| 158 | String buffer; |
| 159 | buffer.reserve(1024); |
| 160 | |
| 161 | StartBenchmarkTiming(); |
| 162 | for (size_t i = 0; i < num_iterations; ++i) { |
| 163 | ASSERT_FALSE(StoreFromTableBuffers(table_collection, &buffer)); |
| 164 | ASSERT_GT(buffer.length(), 0); |
| 165 | } |
| 166 | StopBenchmarkTiming(); |
| 167 | |
| 168 | DestroyFakeTables(table_collection); |
| 169 | } |
| 170 | BENCHMARK(BM_StoreFromTableBuffersWithData) |
| 171 | |
| 172 | // Return eight bytes of data. |
| 173 | static vector<uchar> GetShortData() { return {1, 2, 3, 4, 5, 6, 7, 8}; } |
| 174 | |
| 175 | // Return 1024 bytes of data. |
| 176 | static vector<uchar> GetLongData() { |
| 177 | constexpr int kDataSize = 1024; |
| 178 | vector<uchar> data(kDataSize); |
| 179 | for (int i = 0; i < kDataSize; ++i) { |
| 180 | data.push_back(i); |
| 181 | } |
| 182 | return data; |
| 183 | } |
| 184 | |
| 185 | static void BM_Murmur3ShortData(size_t num_iterations) { |
| 186 | StopBenchmarkTiming(); |
| 187 | |
| 188 | vector<uchar> data = GetShortData(); |
| 189 | StartBenchmarkTiming(); |
| 190 | |
| 191 | size_t sum = 0; |
| 192 | for (size_t i = 0; i < num_iterations; ++i) { |
| 193 | sum += murmur3_32(&data[0], data.size(), 0); |
| 194 | } |
| 195 | StopBenchmarkTiming(); |
| 196 | |
| 197 | // The sum variable is just to assure that the compiler doesn't optimize away |
| 198 | // the entire for loop. |
| 199 | EXPECT_NE(0, sum); |
| 200 | SetBytesProcessed(num_iterations * data.size()); |
| 201 | } |
| 202 | BENCHMARK(BM_Murmur3ShortData) |
| 203 | |
| 204 | static void BM_MurmurLongData(size_t num_iterations) { |
| 205 | StopBenchmarkTiming(); |
| 206 | |
| 207 | vector<uchar> data = GetLongData(); |
| 208 | StartBenchmarkTiming(); |
| 209 | |
| 210 | size_t sum = 0; |
| 211 | for (size_t i = 0; i < num_iterations; ++i) { |
| 212 | sum += murmur3_32(&data[0], data.size(), 0); |
| 213 | } |
| 214 | StopBenchmarkTiming(); |
| 215 | |
| 216 | // The sum variable is just to assure that the compiler doesn't optimize away |
| 217 | // the entire for loop. |
| 218 | EXPECT_NE(0, sum); |
| 219 | SetBytesProcessed(num_iterations * data.size()); |
| 220 | } |
| 221 | BENCHMARK(BM_MurmurLongData) |
| 222 | |
| 223 | static void BM_XXHash64ShortData(size_t num_iterations) { |
| 224 | StopBenchmarkTiming(); |
| 225 | |
| 226 | vector<uchar> data = GetShortData(); |
| 227 | StartBenchmarkTiming(); |
| 228 | |
| 229 | size_t sum = 0; |
| 230 | for (size_t i = 0; i < num_iterations; ++i) { |
| 231 | sum += MY_XXH64(&data[0], data.size(), 0); |
| 232 | } |
| 233 | StopBenchmarkTiming(); |
| 234 | |
| 235 | // The sum variable is just to assure that the compiler doesn't optimize away |
| 236 | // the entire for loop. |
| 237 | EXPECT_NE(0, sum); |
| 238 | SetBytesProcessed(num_iterations * data.size()); |
| 239 | } |
| 240 | BENCHMARK(BM_XXHash64ShortData) |
| 241 | |
| 242 | static void BM_XXHash64LongData(size_t num_iterations) { |
| 243 | StopBenchmarkTiming(); |
| 244 | |
| 245 | vector<uchar> data = GetLongData(); |
| 246 | StartBenchmarkTiming(); |
| 247 | |
| 248 | size_t sum = 0; |
| 249 | for (size_t i = 0; i < num_iterations; ++i) { |
| 250 | sum += MY_XXH64(&data[0], data.size(), 0); |
| 251 | } |
| 252 | StopBenchmarkTiming(); |
| 253 | |
| 254 | // The sum variable is just to assure that the compiler doesn't optimize away |
| 255 | // the entire for loop. |
| 256 | EXPECT_NE(0, sum); |
| 257 | SetBytesProcessed(num_iterations * data.size()); |
| 258 | } |
| 259 | BENCHMARK(BM_XXHash64LongData) |
| 260 | |
| 261 | // A class that takes care of setting up an environment for testing a hash join |
| 262 | // iterator. The constructors will set up two tables (left and right), as well |
| 263 | // as two (fake) iterators that reads data from these two tables. Both tables |
| 264 | // has only one column, and the join condition between the two tables is a |
| 265 | // simple equality between these two columns. There are two different |
| 266 | // constructors; one for integer data sets and one for string data sets. |
| 267 | // |
| 268 | // The user must provide the data contents for both tables in the constructor. |
| 269 | class HashJoinTestHelper { |
| 270 | public: |
| 271 | unique_ptr_destroy_only<RowIterator> left_iterator; |
| 272 | unique_ptr_destroy_only<RowIterator> right_iterator; |
| 273 | QEP_TAB *left_qep_tab; |
| 274 | QEP_TAB *right_qep_tab; |
| 275 | HashJoinCondition *join_condition = nullptr; |
| 276 | Mem_root_array<Item *> extra_conditions; |
| 277 | |
| 278 | HashJoinTestHelper(const Server_initializer &initializer, |
| 279 | vector<optional<int>> left_dataset, |
| 280 | vector<optional<int>> right_dataset, |
| 281 | bool is_nullable = false, bool null_safe_equal = false) { |
| 282 | m_left_table_field.reset(new (&m_mem_root) Mock_field_long( |
| 283 | "column1", is_nullable, /*is_unsigned=*/false)); |
| 284 | m_left_table.reset(new (&m_mem_root) Fake_TABLE(m_left_table_field.get())); |
| 285 | |
| 286 | m_right_table_field.reset(new (&m_mem_root) Mock_field_long( |
| 287 | "column1", is_nullable, /*is_unsigned=*/false)); |
| 288 | m_right_table.reset(new (&m_mem_root) |
| 289 | Fake_TABLE(m_right_table_field.get())); |
| 290 | SetupFakeTables(initializer, null_safe_equal); |
| 291 | |
| 292 | left_iterator.reset(new (&m_mem_root) FakeIntegerIterator( |
| 293 | initializer.thd(), m_left_table.get(), |
| 294 | down_cast<Field_long *>(m_left_table->field[0]), |
| 295 | std::move(left_dataset))); |
| 296 | right_iterator.reset(new (&m_mem_root) FakeIntegerIterator( |
| 297 | initializer.thd(), m_right_table.get(), |
| 298 | down_cast<Field_long *>(m_right_table->field[0]), |
| 299 | std::move(right_dataset))); |
| 300 | } |
| 301 | |
| 302 | HashJoinTestHelper(const Server_initializer &initializer, |
| 303 | vector<optional<string>> left_dataset, |
| 304 | vector<optional<string>> right_dataset, |
| 305 | bool is_nullable = false, bool null_safe_equal = false) |
| 306 | : extra_conditions(initializer.thd()->mem_root) { |
| 307 | m_left_table_field.reset(new (&m_mem_root) Mock_field_varstring( |
| 308 | nullptr, "column1", /*char_len=*/255, is_nullable)); |
| 309 | m_left_table.reset(new (&m_mem_root) Fake_TABLE(m_left_table_field.get())); |
| 310 | |
| 311 | m_right_table_field.reset(new (&m_mem_root) Mock_field_varstring( |
| 312 | nullptr, "column1", /*char_len=*/255, is_nullable)); |
| 313 | m_right_table.reset(new (&m_mem_root) |
| 314 | Fake_TABLE(m_right_table_field.get())); |
| 315 | SetupFakeTables(initializer, null_safe_equal); |
| 316 | |
| 317 | left_iterator.reset(new (&m_mem_root) FakeStringIterator( |
| 318 | initializer.thd(), m_left_table.get(), |
| 319 | down_cast<Field_varstring *>(m_left_table->field[0]), |
| 320 | std::move(left_dataset))); |
| 321 | right_iterator.reset(new (&m_mem_root) FakeStringIterator( |
| 322 | initializer.thd(), m_right_table.get(), |
| 323 | down_cast<Field_varstring *>(m_right_table->field[0]), |
| 324 | std::move(right_dataset))); |
| 325 | } |
| 326 | |
| 327 | Prealloced_array<TABLE *, 4> left_tables() const { |
| 328 | return Prealloced_array<TABLE *, 4>{left_qep_tab->table()}; |
| 329 | } |
| 330 | Prealloced_array<TABLE *, 4> right_tables() const { |
| 331 | return Prealloced_array<TABLE *, 4>{right_qep_tab->table()}; |
| 332 | } |
| 333 | |
| 334 | private: |
| 335 | void SetupFakeTables(const Server_initializer &initializer, |
| 336 | bool null_safe_equal) { |
| 337 | bitmap_set_all(m_left_table->write_set); |
| 338 | bitmap_set_all(m_left_table->read_set); |
| 339 | bitmap_set_all(m_right_table->write_set); |
| 340 | bitmap_set_all(m_right_table->read_set); |
| 341 | |
| 342 | Query_block *query_block = |
| 343 | parse(&initializer, |
| 344 | "SELECT * FROM t1 JOIN t2 ON (t1.column1 = t2.column1);", 0); |
| 345 | JOIN *join = new (&m_mem_root) JOIN(initializer.thd(), query_block); |
| 346 | join->tables = 2; |
| 347 | join->qep_tab = m_mem_root.ArrayAlloc<QEP_TAB>(join->tables); |
| 348 | |
| 349 | left_qep_tab = &join->qep_tab[0]; |
| 350 | left_qep_tab->set_qs(new (&m_mem_root) QEP_shared); |
| 351 | left_qep_tab->set_idx(0); |
| 352 | left_qep_tab->set_table(m_left_table.get()); |
| 353 | left_qep_tab->table_ref = m_left_table->pos_in_table_list; |
| 354 | left_qep_tab->set_join(join); |
| 355 | |
| 356 | right_qep_tab = &join->qep_tab[1]; |
| 357 | right_qep_tab->set_qs(new (&m_mem_root) QEP_shared); |
| 358 | right_qep_tab->set_idx(1); |
| 359 | right_qep_tab->set_table(m_right_table.get()); |
| 360 | right_qep_tab->table_ref = m_right_table->pos_in_table_list; |
| 361 | right_qep_tab->set_join(join); |
| 362 | |
| 363 | Item_field *const left_arg = new Item_field(m_left_table->field[0]); |
| 364 | Item_field *const right_arg = new Item_field(m_right_table->field[0]); |
| 365 | Item_eq_base *eq; |
| 366 | if (null_safe_equal) { |
| 367 | eq = new Item_func_equal(left_arg, right_arg); |
| 368 | } else { |
| 369 | eq = new Item_func_eq(left_arg, right_arg); |
| 370 | } |
| 371 | eq->set_cmp_func(); |
| 372 | join_condition = new (&m_mem_root) HashJoinCondition(eq, &m_mem_root); |
| 373 | } |
| 374 | |
| 375 | // For simplicity, we allocate everything on a MEM_ROOT that takes care of |
| 376 | // releasing any memory. However, we must ensure that the destructor is called |
| 377 | // for Mock_field_varstring. Wrapping the fields in a unique_ptr_destroy_only |
| 378 | // will ensure this. |
| 379 | MEM_ROOT m_mem_root; |
| 380 | unique_ptr_destroy_only<Fake_TABLE> m_left_table; |
| 381 | unique_ptr_destroy_only<Fake_TABLE> m_right_table; |
| 382 | unique_ptr_destroy_only<Field> m_left_table_field; |
| 383 | unique_ptr_destroy_only<Field> m_right_table_field; |
| 384 | }; |
| 385 | |
| 386 | static vector<optional<int>> CollectIntResults(HashJoinIterator *iterator, |
| 387 | Field *field) { |
| 388 | vector<optional<int>> results; |
| 389 | int error; |
| 390 | while ((error = iterator->Read()) == 0) { |
| 391 | if (field->is_null()) { |
| 392 | results.emplace_back(nullopt); |
| 393 | } else { |
| 394 | results.emplace_back(field->val_int()); |
| 395 | } |
| 396 | } |
| 397 | EXPECT_EQ(-1, error); // EOF |
| 398 | return results; |
| 399 | } |
| 400 | |
| 401 | static vector<optional<string>> CollectStringResults(HashJoinIterator *iterator, |
| 402 | Field *field) { |
| 403 | vector<optional<string>> results; |
| 404 | String buffer; |
| 405 | int error; |
| 406 | while ((error = iterator->Read()) == 0) { |
| 407 | if (field->is_null()) { |
| 408 | results.emplace_back(nullopt); |
| 409 | } else { |
| 410 | const String *res = field->val_str(&buffer); |
| 411 | results.emplace_back(to_string(*res)); |
| 412 | } |
| 413 | } |
| 414 | EXPECT_EQ(-1, error); // EOF |
| 415 | return results; |
| 416 | } |
| 417 | |
| 418 | TEST(HashJoinTest, InnerJoinIntOneToOneMatch) { |
| 419 | my_testing::Server_initializer initializer; |
| 420 | initializer.SetUp(); |
| 421 | |
| 422 | HashJoinTestHelper test_helper(initializer, vector<optional<int>>{3}, {3}); |
| 423 | |
| 424 | HashJoinIterator hash_join_iterator( |
| 425 | initializer.thd(), std::move(test_helper.left_iterator), |
| 426 | test_helper.left_tables(), /*estimated_build_rows=*/1000, |
| 427 | std::move(test_helper.right_iterator), test_helper.right_tables(), |
| 428 | /*store_rowids=*/false, |
| 429 | /*tables_to_get_rowid_for=*/0, 10 * 1024 * 1024 /* 10 MB */, |
| 430 | {*test_helper.join_condition}, true, JoinType::INNER, |
| 431 | test_helper.extra_conditions, |
| 432 | /*probe_input_batch_mode=*/false, nullptr); |
| 433 | |
| 434 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 435 | EXPECT_THAT(CollectIntResults(&hash_join_iterator, |
| 436 | test_helper.left_qep_tab->table()->field[0]), |
| 437 | ElementsAre(3)); |
| 438 | } |
| 439 | |
| 440 | TEST(HashJoinTest, InnerJoinIntNoMatch) { |
| 441 | my_testing::Server_initializer initializer; |
| 442 | initializer.SetUp(); |
| 443 | |
| 444 | HashJoinTestHelper test_helper(initializer, {2, 4}, {3, 5}); |
| 445 | |
| 446 | HashJoinIterator hash_join_iterator( |
| 447 | initializer.thd(), std::move(test_helper.left_iterator), |
| 448 | test_helper.left_tables(), /*estimated_build_rows=*/1000, |
| 449 | std::move(test_helper.right_iterator), test_helper.right_tables(), |
| 450 | /*store_rowids=*/false, |
| 451 | /*tables_to_get_rowid_for=*/0, 10 * 1024 * 1024 /* 10 MB */, |
| 452 | {*test_helper.join_condition}, true, JoinType::INNER, |
| 453 | test_helper.extra_conditions, |
| 454 | /*probe_input_batch_mode=*/false, nullptr); |
| 455 | |
| 456 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 457 | EXPECT_EQ(-1, hash_join_iterator.Read()); |
| 458 | } |
| 459 | |
| 460 | TEST(HashJoinTest, InnerJoinIntOneToManyMatch) { |
| 461 | my_testing::Server_initializer initializer; |
| 462 | initializer.SetUp(); |
| 463 | |
| 464 | HashJoinTestHelper test_helper(initializer, {2}, {2, 2}); |
| 465 | |
| 466 | HashJoinIterator hash_join_iterator( |
| 467 | initializer.thd(), std::move(test_helper.left_iterator), |
| 468 | test_helper.left_tables(), |
| 469 | /*estimated_build_rows=*/1000, std::move(test_helper.right_iterator), |
| 470 | test_helper.right_tables(), /*store_rowids=*/false, |
| 471 | /*tables_to_get_rowid_for=*/0, 10 * 1024 * 1024 /* 10 MB */, |
| 472 | {*test_helper.join_condition}, true, JoinType::INNER, |
| 473 | test_helper.extra_conditions, |
| 474 | /*probe_input_batch_mode=*/false, nullptr); |
| 475 | |
| 476 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 477 | |
| 478 | // We expect two result rows before the iterator should return -1 (EOF). |
| 479 | EXPECT_THAT(CollectIntResults(&hash_join_iterator, |
| 480 | test_helper.left_qep_tab->table()->field[0]), |
| 481 | ElementsAre(2, 2)); |
| 482 | } |
| 483 | |
| 484 | TEST(HashJoinTest, InnerJoinStringOneToOneMatch) { |
| 485 | my_testing::Server_initializer initializer; |
| 486 | initializer.SetUp(); |
| 487 | |
| 488 | HashJoinTestHelper test_helper(initializer, {"abc"}, {"abc"}); |
| 489 | |
| 490 | HashJoinIterator hash_join_iterator( |
| 491 | initializer.thd(), std::move(test_helper.left_iterator), |
| 492 | test_helper.left_tables(), /*estimated_build_rows=*/1000, |
| 493 | std::move(test_helper.right_iterator), test_helper.right_tables(), |
| 494 | /*store_rowids=*/false, |
| 495 | /*tables_to_get_rowid_for=*/0, 10 * 1024 * 1024 /* 10 MB */, |
| 496 | {*test_helper.join_condition}, true, JoinType::INNER, |
| 497 | test_helper.extra_conditions, |
| 498 | /*probe_input_batch_mode=*/false, nullptr); |
| 499 | |
| 500 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 501 | EXPECT_THAT(CollectStringResults(&hash_join_iterator, |
| 502 | test_helper.left_qep_tab->table()->field[0]), |
| 503 | ElementsAre("abc")); |
| 504 | } |
| 505 | |
| 506 | TEST(HashJoinTest, HashTableCaching) { |
| 507 | my_testing::Server_initializer initializer; |
| 508 | initializer.SetUp(); |
| 509 | |
| 510 | HashJoinTestHelper test_helper(initializer, {2, 3}, {1, 2, 3}); |
| 511 | FakeIntegerIterator *build_iterator = |
| 512 | down_cast<FakeIntegerIterator *>(test_helper.left_iterator.get()); |
| 513 | |
| 514 | uint64_t hash_table_generation = 0; |
| 515 | HashJoinIterator hash_join_iterator( |
| 516 | initializer.thd(), std::move(test_helper.left_iterator), |
| 517 | test_helper.left_tables(), /*estimated_build_rows=*/1000, |
| 518 | std::move(test_helper.right_iterator), test_helper.right_tables(), |
| 519 | /*store_rowids=*/false, |
| 520 | /*tables_to_get_rowid_for=*/0, 10 * 1024 * 1024 /* 10 MB */, |
| 521 | {*test_helper.join_condition}, true, JoinType::INNER, |
| 522 | test_helper.extra_conditions, |
| 523 | /*probe_input_batch_mode=*/false, &hash_table_generation); |
| 524 | |
| 525 | Field *const probe_field = test_helper.left_qep_tab->table()->field[0]; |
| 526 | |
| 527 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 528 | EXPECT_EQ(3, build_iterator->num_read_calls()); |
| 529 | |
| 530 | EXPECT_THAT(CollectIntResults(&hash_join_iterator, probe_field), |
| 531 | ElementsAre(2, 3)); |
| 532 | |
| 533 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 534 | EXPECT_EQ(3, build_iterator->num_read_calls()); // Unchanged due to caching. |
| 535 | |
| 536 | EXPECT_THAT(CollectIntResults(&hash_join_iterator, probe_field), |
| 537 | ElementsAre(2, 3)); |
| 538 | |
| 539 | hash_table_generation = 1; |
| 540 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 541 | EXPECT_EQ(6, build_iterator->num_read_calls()); |
| 542 | |
| 543 | EXPECT_THAT(CollectIntResults(&hash_join_iterator, probe_field), |
| 544 | ElementsAre(2, 3)); |
| 545 | } |
| 546 | |
| 547 | // Do a benchmark of HashJoinIterator::Init(). This function is responsible for |
| 548 | // building the hash table, and this step is also known as the "build phase". |
| 549 | // |
| 550 | // The table that the hash table is built from is a single-column table with |
| 551 | // 10000 uniformly distributed values between [0, 10000). We give the hash table |
| 552 | // enough memory so that it doesn't spill out to disk. |
| 553 | static void BM_HashTableIteratorBuild(size_t num_iterations) { |
| 554 | StopBenchmarkTiming(); |
| 555 | |
| 556 | my_testing::Server_initializer initializer; |
| 557 | initializer.SetUp(); |
| 558 | |
| 559 | const int min_value = 0; |
| 560 | const int max_value = 10000; |
| 561 | const int seed = 8834245; |
| 562 | std::mt19937 generator(seed); |
| 563 | std::uniform_int_distribution<> distribution(min_value, max_value); |
| 564 | |
| 565 | vector<optional<int>> left_dataset; |
| 566 | vector<optional<int>> right_dataset; |
| 567 | for (int i = 0; i < (max_value - min_value); ++i) { |
| 568 | left_dataset.emplace_back(distribution(generator)); |
| 569 | right_dataset.emplace_back(distribution(generator)); |
| 570 | } |
| 571 | |
| 572 | HashJoinTestHelper test_helper(initializer, std::move(left_dataset), |
| 573 | std::move(right_dataset)); |
| 574 | |
| 575 | HashJoinIterator hash_join_iterator( |
| 576 | initializer.thd(), std::move(test_helper.left_iterator), |
| 577 | test_helper.left_tables(), /*estimated_build_rows=*/1000, |
| 578 | std::move(test_helper.right_iterator), test_helper.right_tables(), |
| 579 | /*store_rowids=*/false, |
| 580 | /*tables_to_get_rowid_for=*/0, 10 * 1024 * 1024 /* 10 MB */, |
| 581 | {*test_helper.join_condition}, true, JoinType::INNER, |
| 582 | test_helper.extra_conditions, |
| 583 | /*probe_input_batch_mode=*/false, nullptr); |
| 584 | |
| 585 | StartBenchmarkTiming(); |
| 586 | for (size_t i = 0; i < num_iterations; ++i) { |
| 587 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 588 | } |
| 589 | StopBenchmarkTiming(); |
| 590 | } |
| 591 | BENCHMARK(BM_HashTableIteratorBuild) |
| 592 | |
| 593 | // Do a benchmark of HashJoinIterator::Read(). This function will read a row |
| 594 | // from the right table, and look for a matching row in the hash table. This is |
| 595 | // also known as the "probe phase". |
| 596 | // |
| 597 | // The table that the hash table is built from is a single-column table with |
| 598 | // 10000 uniformly distributed values between [0, 10000). We give the hash table |
| 599 | // enough memory so that is doesn't spill out to disk. |
| 600 | static void BM_HashTableIteratorProbe(size_t num_iterations) { |
| 601 | StopBenchmarkTiming(); |
| 602 | |
| 603 | my_testing::Server_initializer initializer; |
| 604 | initializer.SetUp(); |
| 605 | |
| 606 | const int min_value = 0; |
| 607 | const int max_value = 10000; |
| 608 | const int seed = 8834245; |
| 609 | std::mt19937 generator(seed); |
| 610 | std::uniform_int_distribution<> distribution(min_value, max_value); |
| 611 | |
| 612 | vector<optional<int>> left_dataset; |
| 613 | vector<optional<int>> right_dataset; |
| 614 | for (int i = 0; i < (max_value - min_value); ++i) { |
| 615 | left_dataset.emplace_back(distribution(generator)); |
| 616 | right_dataset.emplace_back(distribution(generator)); |
| 617 | } |
| 618 | HashJoinTestHelper test_helper(initializer, std::move(left_dataset), |
| 619 | std::move(right_dataset)); |
| 620 | |
| 621 | HashJoinIterator hash_join_iterator( |
| 622 | initializer.thd(), std::move(test_helper.left_iterator), |
| 623 | test_helper.left_tables(), /*estimated_build_rows=*/1000, |
| 624 | std::move(test_helper.right_iterator), test_helper.right_tables(), |
| 625 | /*store_rowids=*/false, |
| 626 | /*tables_to_get_rowid_for=*/0, 10 * 1024 * 1024 /* 10 MB */, |
| 627 | {*test_helper.join_condition}, true, JoinType::INNER, |
| 628 | test_helper.extra_conditions, |
| 629 | /*probe_input_batch_mode=*/false, nullptr); |
| 630 | |
| 631 | for (size_t i = 0; i < num_iterations; ++i) { |
| 632 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 633 | StartBenchmarkTiming(); |
| 634 | int result; |
| 635 | do { |
| 636 | result = hash_join_iterator.Read(); |
| 637 | } while (result == 0); |
| 638 | StopBenchmarkTiming(); |
| 639 | } |
| 640 | } |
| 641 | BENCHMARK(BM_HashTableIteratorProbe) |
| 642 | |
| 643 | // Do a benchmark of lookup in the hash table for semijoin. This is to see if |
| 644 | // there is any difference between equal_range() and find(), |
| 645 | // |
| 646 | // The table that the hash table is built from is a single-column table with |
| 647 | // 10000 uniformly distributed values between [0, 5000). We give the hash table |
| 648 | // enough memory so that is doesn't spill out to disk. |
| 649 | static void BM_HashTableIteratorProbeSemiJoin(size_t num_iterations) { |
| 650 | StopBenchmarkTiming(); |
| 651 | |
| 652 | my_testing::Server_initializer initializer; |
| 653 | initializer.SetUp(); |
| 654 | |
| 655 | const int num_value = 10000; |
| 656 | const int min_value = 0; |
| 657 | const int max_value = 5000; |
| 658 | const int seed = 8834245; |
| 659 | std::mt19937 generator(seed); |
| 660 | std::uniform_int_distribution<> distribution(min_value, max_value); |
| 661 | |
| 662 | vector<optional<int>> left_dataset; |
| 663 | vector<optional<int>> right_dataset; |
| 664 | for (int i = 0; i < num_value; ++i) { |
| 665 | left_dataset.emplace_back(distribution(generator)); |
| 666 | right_dataset.emplace_back(distribution(generator)); |
| 667 | } |
| 668 | HashJoinTestHelper test_helper(initializer, std::move(left_dataset), |
| 669 | std::move(right_dataset)); |
| 670 | |
| 671 | HashJoinIterator hash_join_iterator( |
| 672 | initializer.thd(), std::move(test_helper.left_iterator), |
| 673 | test_helper.left_tables(), /*estimated_build_rows=*/1000, |
| 674 | std::move(test_helper.right_iterator), test_helper.right_tables(), |
| 675 | /*store_rowids=*/false, |
| 676 | /*tables_to_get_rowid_for=*/0, 10 * 1024 * 1024 /* 10 MB */, |
| 677 | {*test_helper.join_condition}, true, JoinType::SEMI, |
| 678 | test_helper.extra_conditions, |
| 679 | /*probe_input_batch_mode=*/false, nullptr); |
| 680 | |
| 681 | for (size_t i = 0; i < num_iterations; ++i) { |
| 682 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 683 | StartBenchmarkTiming(); |
| 684 | int result; |
| 685 | do { |
| 686 | result = hash_join_iterator.Read(); |
| 687 | } while (result == 0); |
| 688 | StopBenchmarkTiming(); |
| 689 | } |
| 690 | } |
| 691 | BENCHMARK(BM_HashTableIteratorProbeSemiJoin) |
| 692 | |
| 693 | TEST(HashJoinTest, SemiJoinInt) { |
| 694 | my_testing::Server_initializer initializer; |
| 695 | initializer.SetUp(); |
| 696 | |
| 697 | // The iterator will execute something that is equivalent to the query |
| 698 | // "SELECT * FROM probe_data WHERE a IN (SELECT b FROM build_data);" |
| 699 | HashJoinTestHelper test_helper(initializer, {3, 3, 4, 5}, {3, 5, 6}); |
| 700 | |
| 701 | HashJoinIterator hash_join_iterator( |
| 702 | initializer.thd(), std::move(test_helper.left_iterator), |
| 703 | test_helper.left_tables(), /*estimated_build_rows=*/1000, |
| 704 | std::move(test_helper.right_iterator), test_helper.right_tables(), |
| 705 | /*store_rowids=*/false, |
| 706 | /*tables_to_get_rowid_for=*/0, 10 * 1024 * 1024 /* 10 MB */, |
| 707 | {*test_helper.join_condition}, true, JoinType::SEMI, |
| 708 | test_helper.extra_conditions, |
| 709 | /*probe_input_batch_mode=*/false, nullptr); |
| 710 | |
| 711 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 712 | |
| 713 | EXPECT_THAT(CollectIntResults(&hash_join_iterator, |
| 714 | test_helper.right_qep_tab->table()->field[0]), |
| 715 | ElementsAre(3, 5)); |
| 716 | } |
| 717 | |
| 718 | TEST(HashJoinTest, AntiJoinInt) { |
| 719 | my_testing::Server_initializer initializer; |
| 720 | initializer.SetUp(); |
| 721 | |
| 722 | // The iterator will execute something that is equivalent to the query |
| 723 | // "SELECT * FROM probe_data WHERE a NOT IN (SELECT b FROM build_data);" |
| 724 | HashJoinTestHelper test_helper(initializer, {3, 3, 4, 5}, {3, 5, 6}); |
| 725 | |
| 726 | HashJoinIterator hash_join_iterator( |
| 727 | initializer.thd(), std::move(test_helper.left_iterator), |
| 728 | test_helper.left_tables(), /*estimated_build_rows=*/1000, |
| 729 | std::move(test_helper.right_iterator), test_helper.right_tables(), |
| 730 | /*store_rowids=*/false, |
| 731 | /*tables_to_get_rowid_for=*/0, 10 * 1024 * 1024 /* 10 MB */, |
| 732 | {*test_helper.join_condition}, true, JoinType::ANTI, |
| 733 | test_helper.extra_conditions, |
| 734 | /*probe_input_batch_mode=*/false, nullptr); |
| 735 | |
| 736 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 737 | |
| 738 | EXPECT_EQ(0, hash_join_iterator.Read()); |
| 739 | EXPECT_EQ(6, test_helper.right_qep_tab->table()->field[0]->val_int()); |
| 740 | EXPECT_EQ(-1, hash_join_iterator.Read()); |
| 741 | } |
| 742 | |
| 743 | // Test that antijoin works correctly when the hash table spills to disk. |
| 744 | TEST(HashJoinTest, AntiJoinIntSpillToDisk) { |
| 745 | my_testing::Server_initializer initializer; |
| 746 | initializer.SetUp(); |
| 747 | |
| 748 | vector<optional<int>> probe_data = { |
| 749 | 1, 2, 3, 4, 1998, 1999, 2000, 2001, -1, -2, nullopt, nullopt, 2, 3, 4}; |
| 750 | |
| 751 | vector<optional<int>> build_data; |
| 752 | build_data.emplace_back(nullopt); |
| 753 | for (int i = 0; i < 1000; ++i) { |
| 754 | build_data.emplace_back(i * 2); |
| 755 | } |
| 756 | |
| 757 | HashJoinTestHelper test_helper{initializer, build_data, probe_data, |
| 758 | /*is_nullable=*/true}; |
| 759 | |
| 760 | // The iterator will execute something that is equivalent to the query |
| 761 | // "SELECT * FROM probe_data WHERE a NOT IN (SELECT b FROM build_data);" |
| 762 | // We set max_memory_available so low that build_data doesn't fit in the join |
| 763 | // buffer and spills to disk. |
| 764 | HashJoinIterator hash_join_iterator{initializer.thd(), |
| 765 | std::move(test_helper.left_iterator), |
| 766 | test_helper.left_tables(), |
| 767 | static_cast<double>(build_data.size()), |
| 768 | std::move(test_helper.right_iterator), |
| 769 | test_helper.right_tables(), |
| 770 | /*store_rowids=*/false, |
| 771 | /*tables_to_get_rowid_for=*/0, |
| 772 | /*max_memory_available=*/128, |
| 773 | {*test_helper.join_condition}, |
| 774 | /*allow_spill_to_disk=*/true, |
| 775 | JoinType::ANTI, |
| 776 | test_helper.extra_conditions, |
| 777 | /*probe_input_batch_mode=*/false, |
| 778 | /*hash_table_generation=*/nullptr}; |
| 779 | |
| 780 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 781 | |
| 782 | EXPECT_GT(hash_join_iterator.ChunkCount(), 0) |
| 783 | << "The hash table didn't spill to disk."; |
| 784 | |
| 785 | EXPECT_THAT(CollectIntResults(&hash_join_iterator, |
| 786 | test_helper.right_qep_tab->table()->field[0]), |
| 787 | UnorderedElementsAre(nullopt, nullopt, -1, -2, 1, 3, 3, 1999, |
| 788 | 2000, 2001)); |
| 789 | } |
| 790 | |
| 791 | TEST(HashJoinTest, LeftHashJoinInt) { |
| 792 | my_testing::Server_initializer initializer; |
| 793 | initializer.SetUp(); |
| 794 | |
| 795 | // The iterator will execute something that is equivalent to the query |
| 796 | // "SELECT * FROM left_data p LEFT JOIN right_data b ON p.col = b.col;" |
| 797 | HashJoinTestHelper test_helper(initializer, vector<optional<int>>{3}, {}); |
| 798 | |
| 799 | HashJoinIterator hash_join_iterator( |
| 800 | initializer.thd(), std::move(test_helper.right_iterator), |
| 801 | test_helper.right_tables(), /*estimated_build_rows=*/1000, |
| 802 | std::move(test_helper.left_iterator), test_helper.left_tables(), |
| 803 | /*store_rowids=*/false, /*tables_to_get_rowid_for=*/0, |
| 804 | 10 * 1024 * 1024 /* 10 MB */, {*test_helper.join_condition}, true, |
| 805 | JoinType::OUTER, test_helper.extra_conditions, |
| 806 | /*probe_input_batch_mode=*/false, nullptr); |
| 807 | |
| 808 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 809 | |
| 810 | EXPECT_EQ(0, hash_join_iterator.Read()); |
| 811 | EXPECT_EQ(3, test_helper.left_qep_tab->table()->field[0]->val_int()); |
| 812 | EXPECT_FALSE(test_helper.left_qep_tab->table()->field[0]->is_null()); |
| 813 | |
| 814 | test_helper.right_qep_tab->table()->field[0]->val_int(); |
| 815 | EXPECT_TRUE(test_helper.right_qep_tab->table()->field[0]->is_null()); |
| 816 | |
| 817 | EXPECT_EQ(-1, hash_join_iterator.Read()); |
| 818 | } |
| 819 | |
| 820 | TEST(HashJoinTest, HashJoinResetNullFlagBeforeBuild) { |
| 821 | my_testing::Server_initializer initializer; |
| 822 | initializer.SetUp(); |
| 823 | |
| 824 | // The iterator will execute something that is equivalent to the query |
| 825 | // "SELECT * FROM left_data p LEFT JOIN right_data b ON p.col = b.col;" |
| 826 | HashJoinTestHelper test_helper(initializer, vector<optional<int>>{3}, {3}); |
| 827 | |
| 828 | // Explicitly set the NULL row flag for the right/build input. The hash join |
| 829 | // iterator should reset this flag before building the hash table. |
| 830 | test_helper.right_iterator->SetNullRowFlag(/*is_null_row=*/true); |
| 831 | |
| 832 | HashJoinIterator hash_join_iterator( |
| 833 | initializer.thd(), std::move(test_helper.right_iterator), |
| 834 | test_helper.right_tables(), /*estimated_build_rows=*/1000, |
| 835 | std::move(test_helper.left_iterator), test_helper.left_tables(), |
| 836 | /*store_rowids=*/false, /*tables_to_get_rowid_for=*/0, |
| 837 | 10 * 1024 * 1024 /* 10 MB */, {*test_helper.join_condition}, true, |
| 838 | JoinType::OUTER, test_helper.extra_conditions, |
| 839 | /*probe_input_batch_mode=*/false, nullptr); |
| 840 | |
| 841 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 842 | |
| 843 | // Verify that we do not get any NULL value back, even though we explicitly |
| 844 | // set the NULL row flag before Init was called; Init() should reset the NULL |
| 845 | // row flag before reading from the build table. |
| 846 | EXPECT_EQ(0, hash_join_iterator.Read()); |
| 847 | EXPECT_EQ(3, test_helper.left_qep_tab->table()->field[0]->val_int()); |
| 848 | EXPECT_FALSE(test_helper.left_qep_tab->table()->field[0]->is_null()); |
| 849 | |
| 850 | EXPECT_EQ(3, test_helper.right_qep_tab->table()->field[0]->val_int()); |
| 851 | EXPECT_FALSE(test_helper.right_qep_tab->table()->field[0]->is_null()); |
| 852 | |
| 853 | EXPECT_EQ(-1, hash_join_iterator.Read()); |
| 854 | } |
| 855 | |
| 856 | TEST(HashJoinTest, HashJoinChunkFiles) { |
| 857 | my_testing::Server_initializer initializer; |
| 858 | initializer.SetUp(); |
| 859 | |
| 860 | vector<optional<int>> left_dataset; |
| 861 | int dataset_sz = 1000; |
| 862 | if (SIZEOF_VOIDP == 4) { |
| 863 | // 32-bit arch, modify #rows to get same numbers for chunk calculations |
| 864 | dataset_sz *= 2; |
| 865 | } |
| 866 | for (int i = 0; i < dataset_sz; ++i) { |
| 867 | left_dataset.emplace_back(i); |
| 868 | } |
| 869 | |
| 870 | HashJoinTestHelper test_helper(initializer, left_dataset, left_dataset); |
| 871 | |
| 872 | HashJoinIterator hash_join_iterator( |
| 873 | initializer.thd(), std::move(test_helper.left_iterator), |
| 874 | test_helper.left_tables(), /*estimated_build_rows=*/dataset_sz, |
| 875 | std::move(test_helper.right_iterator), test_helper.right_tables(), |
| 876 | /*store_rowids=*/false, |
| 877 | /*tables_to_get_rowid_for=*/0, 1024 /* 1 KB */, |
| 878 | {*test_helper.join_condition}, true, JoinType::INNER, |
| 879 | test_helper.extra_conditions, |
| 880 | /*probe_input_batch_mode=*/false, nullptr); |
| 881 | |
| 882 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 883 | |
| 884 | // We hash 1000 rows (64-bit arch) or 2000 rows (32-bit arch). The hash |
| 885 | // table can normally hold about 410 rows on 64-bit machines and 820 rows on |
| 886 | // 32-bit machines (verified experimentally). To get the required number of |
| 887 | // chunks, the number of remaining rows should be divided by the number of |
| 888 | // hash table rows. But as a safeguard, this calculation is adjusted to yield |
| 889 | // a few extra chunks rather than risk having too few chunks. So the number |
| 890 | // of remaining rows is instead divided by a reduced count of hash table rows |
| 891 | // The reduced count is obtained by multiplying the hash table row count by |
| 892 | // a 'reduction factor' of 0.9. E.g. for 64-bit rows: |
| 893 | // reduced_rows_in_hash_table = 410 * 0.9 = 369 |
| 894 | // remaining_rows = 1000 - 410 = 590 |
| 895 | // required number of chunks = remaining_rows / reduced_rows_in_hash_table |
| 896 | // = 590 / 369 = 1.59, rounded up to 2 |
| 897 | // So a count of 2 chunks is expected. |
| 898 | EXPECT_EQ(2, hash_join_iterator.ChunkCount()); |
| 899 | } |
| 900 | |
| 901 | TEST(HashJoinTest, InnerJoinIntNullable) { |
| 902 | my_testing::Server_initializer initializer; |
| 903 | initializer.SetUp(); |
| 904 | |
| 905 | HashJoinTestHelper test_helper(initializer, |
| 906 | {-2, 0, nullopt, 1, 2, 3, nullopt}, |
| 907 | {-2, -1, 0, nullopt, 2, 3, 4, nullopt}, |
| 908 | /*is_nullable=*/true); |
| 909 | |
| 910 | HashJoinIterator hash_join_iterator( |
| 911 | initializer.thd(), std::move(test_helper.left_iterator), |
| 912 | test_helper.left_tables(), |
| 913 | /*estimated_build_rows=*/1000, std::move(test_helper.right_iterator), |
| 914 | test_helper.right_tables(), /*store_rowids=*/false, |
| 915 | /*tables_to_get_rowid_for=*/0, |
| 916 | /*max_memory_available=*/size_t{10} * 1024 * 1024, |
| 917 | {*test_helper.join_condition}, |
| 918 | /*allow_spill_to_disk=*/true, JoinType::INNER, |
| 919 | test_helper.extra_conditions, |
| 920 | /*probe_input_batch_mode=*/false, /*hash_table_generation=*/nullptr); |
| 921 | |
| 922 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 923 | |
| 924 | EXPECT_THAT(CollectIntResults(&hash_join_iterator, |
| 925 | test_helper.left_qep_tab->table()->field[0]), |
| 926 | ElementsAre(-2, 0, 2, 3)); |
| 927 | } |
| 928 | |
| 929 | TEST(HashJoinTest, InnerJoinStringNullable) { |
| 930 | my_testing::Server_initializer initializer; |
| 931 | initializer.SetUp(); |
| 932 | |
| 933 | HashJoinTestHelper test_helper(initializer, |
| 934 | {"abc", nullopt, "xyz", "", "x", nullopt}, |
| 935 | {"abc", nullopt, "xyz", "", "y", nullopt}, |
| 936 | /*is_nullable=*/true); |
| 937 | |
| 938 | HashJoinIterator hash_join_iterator( |
| 939 | initializer.thd(), std::move(test_helper.left_iterator), |
| 940 | test_helper.left_tables(), |
| 941 | /*estimated_build_rows=*/1000, std::move(test_helper.right_iterator), |
| 942 | test_helper.right_tables(), /*store_rowids=*/false, |
| 943 | /*tables_to_get_rowid_for=*/0, |
| 944 | /*max_memory_available=*/size_t{10} * 1024 * 1024, |
| 945 | {*test_helper.join_condition}, |
| 946 | /*allow_spill_to_disk=*/true, JoinType::INNER, |
| 947 | test_helper.extra_conditions, |
| 948 | /*probe_input_batch_mode=*/false, /*hash_table_generation=*/nullptr); |
| 949 | |
| 950 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 951 | |
| 952 | EXPECT_THAT(CollectStringResults(&hash_join_iterator, |
| 953 | test_helper.left_qep_tab->table()->field[0]), |
| 954 | ElementsAre("abc", "xyz", "")); |
| 955 | } |
| 956 | |
| 957 | TEST(HashJoinTest, InnerJoinIntNullSafeEqual) { |
| 958 | my_testing::Server_initializer initializer; |
| 959 | initializer.SetUp(); |
| 960 | |
| 961 | HashJoinTestHelper test_helper(initializer, {nullopt, 0, 1, 2, nullopt, 3}, |
| 962 | {nullopt, 0, 1, nullopt, 1, 2, 4}, |
| 963 | /*is_nullable=*/true, |
| 964 | /*null_safe_equal=*/true); |
| 965 | |
| 966 | HashJoinIterator hash_join_iterator( |
| 967 | initializer.thd(), std::move(test_helper.left_iterator), |
| 968 | test_helper.left_tables(), |
| 969 | /*estimated_build_rows=*/1000, std::move(test_helper.right_iterator), |
| 970 | test_helper.right_tables(), /*store_rowids=*/false, |
| 971 | /*tables_to_get_rowid_for=*/0, |
| 972 | /*max_memory_available=*/size_t{10} * 1024 * 1024, |
| 973 | {*test_helper.join_condition}, |
| 974 | /*allow_spill_to_disk=*/true, JoinType::INNER, |
| 975 | test_helper.extra_conditions, |
| 976 | /*probe_input_batch_mode=*/false, /*hash_table_generation=*/nullptr); |
| 977 | |
| 978 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 979 | |
| 980 | EXPECT_THAT(CollectIntResults(&hash_join_iterator, |
| 981 | test_helper.left_qep_tab->table()->field[0]), |
| 982 | ElementsAre(nullopt, nullopt, 0, 1, nullopt, nullopt, 1, 2)); |
| 983 | } |
| 984 | |
| 985 | TEST(HashJoinTest, LeftJoinIntNullSafeEqual) { |
| 986 | my_testing::Server_initializer initializer; |
| 987 | initializer.SetUp(); |
| 988 | |
| 989 | HashJoinTestHelper test_helper(initializer, {nullopt, 0, 1, 2, nullopt, 3}, |
| 990 | {nullopt, 0, 1, nullopt, 1, 2, 4}, |
| 991 | /*is_nullable=*/true, |
| 992 | /*null_safe_equal=*/true); |
| 993 | |
| 994 | HashJoinIterator hash_join_iterator( |
| 995 | initializer.thd(), std::move(test_helper.left_iterator), |
| 996 | test_helper.left_tables(), |
| 997 | /*estimated_build_rows=*/1000, std::move(test_helper.right_iterator), |
| 998 | test_helper.right_tables(), /*store_rowids=*/false, |
| 999 | /*tables_to_get_rowid_for=*/0, |
| 1000 | /*max_memory_available=*/size_t{10} * 1024 * 1024, |
| 1001 | {*test_helper.join_condition}, |
| 1002 | /*allow_spill_to_disk=*/true, JoinType::OUTER, |
| 1003 | test_helper.extra_conditions, |
| 1004 | /*probe_input_batch_mode=*/false, /*hash_table_generation=*/nullptr); |
| 1005 | |
| 1006 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 1007 | |
| 1008 | const Field *const build_field = test_helper.left_qep_tab->table()->field[0]; |
| 1009 | const Field *const probe_field = test_helper.right_qep_tab->table()->field[0]; |
| 1010 | String buffer; |
| 1011 | vector<pair<optional<int>, optional<int>>> results; |
| 1012 | int error; |
| 1013 | while ((error = hash_join_iterator.Read()) == 0) { |
| 1014 | pair<optional<int>, optional<int>> &values = results.emplace_back(); |
| 1015 | if (!build_field->is_null()) { |
| 1016 | values.first = build_field->val_int(); |
| 1017 | } |
| 1018 | if (!probe_field->is_null()) { |
| 1019 | values.second = probe_field->val_int(); |
| 1020 | } |
| 1021 | } |
| 1022 | EXPECT_EQ(-1, error); |
| 1023 | EXPECT_THAT( |
| 1024 | results, |
| 1025 | ElementsAre(pair(nullopt, nullopt), pair(nullopt, nullopt), pair(0, 0), |
| 1026 | pair(1, 1), pair(nullopt, nullopt), pair(nullopt, nullopt), |
| 1027 | pair(1, 1), pair(2, 2), pair(nullopt, 4))); |
| 1028 | } |
| 1029 | |
| 1030 | TEST(HashJoinTest, InnerJoinStringNullSafeEqual) { |
| 1031 | my_testing::Server_initializer initializer; |
| 1032 | initializer.SetUp(); |
| 1033 | |
| 1034 | HashJoinTestHelper test_helper(initializer, |
| 1035 | {nullopt, "", "abc", "def", nullopt, "abc"}, |
| 1036 | {nullopt, "", "abc", nullopt, "xyz", "def"}, |
| 1037 | /*is_nullable=*/true, |
| 1038 | /*null_safe_equal=*/true); |
| 1039 | |
| 1040 | HashJoinIterator hash_join_iterator( |
| 1041 | initializer.thd(), std::move(test_helper.left_iterator), |
| 1042 | test_helper.left_tables(), |
| 1043 | /*estimated_build_rows=*/1000, std::move(test_helper.right_iterator), |
| 1044 | test_helper.right_tables(), /*store_rowids=*/false, |
| 1045 | /*tables_to_get_rowid_for=*/0, |
| 1046 | /*max_memory_available=*/size_t{10} * 1024 * 1024, |
| 1047 | {*test_helper.join_condition}, |
| 1048 | /*allow_spill_to_disk=*/true, JoinType::INNER, |
| 1049 | test_helper.extra_conditions, |
| 1050 | /*probe_input_batch_mode=*/false, /*hash_table_generation=*/nullptr); |
| 1051 | |
| 1052 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 1053 | |
| 1054 | EXPECT_THAT( |
| 1055 | CollectStringResults(&hash_join_iterator, |
| 1056 | test_helper.left_qep_tab->table()->field[0]), |
| 1057 | ElementsAre(nullopt, nullopt, "", "abc", "abc", nullopt, nullopt, "def")); |
| 1058 | } |
| 1059 | |
| 1060 | TEST(HashJoinTest, LeftJoinStringNullSafeEqual) { |
| 1061 | my_testing::Server_initializer initializer; |
| 1062 | initializer.SetUp(); |
| 1063 | |
| 1064 | HashJoinTestHelper test_helper(initializer, |
| 1065 | {nullopt, "", "abc", "def", nullopt, "abc"}, |
| 1066 | {nullopt, "", "abc", nullopt, "xyz", "def"}, |
| 1067 | /*is_nullable=*/true, |
| 1068 | /*null_safe_equal=*/true); |
| 1069 | |
| 1070 | HashJoinIterator hash_join_iterator( |
| 1071 | initializer.thd(), std::move(test_helper.left_iterator), |
| 1072 | test_helper.left_tables(), |
| 1073 | /*estimated_build_rows=*/1000, std::move(test_helper.right_iterator), |
| 1074 | test_helper.right_tables(), /*store_rowids=*/false, |
| 1075 | /*tables_to_get_rowid_for=*/0, |
| 1076 | /*max_memory_available=*/size_t{10} * 1024 * 1024, |
| 1077 | {*test_helper.join_condition}, |
| 1078 | /*allow_spill_to_disk=*/true, JoinType::OUTER, |
| 1079 | test_helper.extra_conditions, |
| 1080 | /*probe_input_batch_mode=*/false, /*hash_table_generation=*/nullptr); |
| 1081 | |
| 1082 | ASSERT_FALSE(hash_join_iterator.Init()); |
| 1083 | |
| 1084 | const Field *const build_field = test_helper.left_qep_tab->table()->field[0]; |
| 1085 | const Field *const probe_field = test_helper.right_qep_tab->table()->field[0]; |
| 1086 | String buffer; |
| 1087 | vector<pair<optional<string>, optional<string>>> results; |
| 1088 | int error; |
| 1089 | while ((error = hash_join_iterator.Read()) == 0) { |
| 1090 | pair<optional<string>, optional<string>> &row = results.emplace_back(); |
| 1091 | if (!build_field->is_null()) { |
| 1092 | row.first = to_string(*build_field->val_str(&buffer)); |
| 1093 | } |
| 1094 | if (!probe_field->is_null()) { |
| 1095 | row.second = to_string(*probe_field->val_str(&buffer)); |
| 1096 | } |
| 1097 | } |
| 1098 | EXPECT_EQ(-1, error); |
| 1099 | EXPECT_THAT(results, |
| 1100 | ElementsAre(pair(nullopt, nullopt), pair(nullopt, nullopt), |
| 1101 | pair("", ""), pair("abc", "abc"), pair("abc", "abc"), |
| 1102 | pair(nullopt, nullopt), pair(nullopt, nullopt), |
| 1103 | pair(nullopt, "xyz"), pair("def", "def"))); |
| 1104 | } |
| 1105 | |
| 1106 | } // namespace hash_join_unittest |