From 3a3b23c7b84b040d4d71702c2caaf5f272e893b9 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Fri, 13 Dec 2024 17:53:47 +0800 Subject: [PATCH 01/25] Repro --- cpp/src/arrow/acero/hash_join_node_test.cc | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/cpp/src/arrow/acero/hash_join_node_test.cc b/cpp/src/arrow/acero/hash_join_node_test.cc index 76ad9c7d650e..39ae1f0dbbcd 100644 --- a/cpp/src/arrow/acero/hash_join_node_test.cc +++ b/cpp/src/arrow/acero/hash_join_node_test.cc @@ -3448,3 +3448,76 @@ TEST(HashJoin, LARGE_MEMORY_TEST(BuildSideOver4GBVarLength)) { } // namespace acero } // namespace arrow + +// TEST(HashJoin, LargeBuildSide) { + +using namespace arrow; +using namespace arrow::acero; +int main() { + const int64_t num_small_rows = 18201475; + const int64_t num_large_rows = 360449051; + const int64_t num_batches = 8; + const int64_t seed = 42; + + auto small_schema = + schema({field("c0", int64()), field("key0", int64()), field("key1", int64()), + field("c1", int64()), field("c2", int64()), field("key2", int64()), + field("c3", int64()), field("c4", int64()), field("c5", int64())}); + auto large_schema = schema({field("key0", int64()), field("key1", int64()), + field("key2", int64()), field("c0", int64())}); + + const int64_t key_min = 0; + const int64_t key_max = std::numeric_limits::max(); + const double null_probability = 0; + + auto small_key0_arr = RandomArrayGenerator(seed).Int64( + num_small_rows / num_batches, key_min, key_max, null_probability); + auto small_key1_arr = RandomArrayGenerator(seed).Int64( + num_small_rows / num_batches, key_min, key_max, null_probability); + auto small_key2_arr = RandomArrayGenerator(seed).Int64( + num_small_rows / num_batches, key_min, key_max, null_probability); + ExecBatch small_batch( + {small_key0_arr, small_key0_arr, small_key1_arr, small_key0_arr, small_key0_arr, + small_key2_arr, small_key0_arr, small_key0_arr, small_key0_arr}, + num_small_rows / num_batches); + auto small_batches = + BatchesWithSchema{std::vector(num_batches, small_batch), small_schema}; + + auto large_key0_arr = RandomArrayGenerator(seed).Int64( + num_large_rows / num_batches, key_min, key_max, null_probability); + auto large_key1_arr = RandomArrayGenerator(seed).Int64( + num_large_rows / num_batches, key_min, key_max, null_probability); + auto large_key2_arr = RandomArrayGenerator(seed).Int64( + num_large_rows / num_batches, key_min, key_max, null_probability); + ExecBatch large_batch({large_key0_arr, large_key1_arr, large_key2_arr, large_key0_arr}, + num_large_rows / num_batches); + auto large_batches = + BatchesWithSchema{std::vector(num_batches, large_batch), large_schema}; + + Declaration small_source{"exec_batch_source", + ExecBatchSourceNodeOptions(std::move(small_batches.schema), + std::move(small_batches.batches))}; + Declaration large_source{"exec_batch_source", + ExecBatchSourceNodeOptions(std::move(large_batches.schema), + std::move(large_batches.batches))}; + + // HashJoinNodeOptions join_opts(JoinType::RIGHT_OUTER, + // /*left_keys=*/{"key0", "key1", "key2"}, + // /*right_keys=*/{"key0", "key1", "key2"}); + // Declaration join{ + // "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; + HashJoinNodeOptions join_opts(JoinType::LEFT_OUTER, + /*left_keys=*/{"key0", "key1", "key2"}, + /*right_keys=*/{"key0", "key1", "key2"}); + Declaration join{ + "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; + + Declaration agg{"aggregate", + {std::move(join)}, + AggregateNodeOptions{/*aggregates=*/{{"count_all", "count(*)"}}}}; + + // ASSERT_OK_AND_ASSIGN(std::ignore, DeclarationToTable(std::move(join))); + auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); + std::cout << result->ToString() << std::endl; + return 0; +} From d93380245c95d3cd53a732b28788fb514d7019ef Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Fri, 13 Dec 2024 18:03:39 +0800 Subject: [PATCH 02/25] Fix --- cpp/src/arrow/acero/hash_join_node_test.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpp/src/arrow/acero/hash_join_node_test.cc b/cpp/src/arrow/acero/hash_join_node_test.cc index 39ae1f0dbbcd..367869a56d14 100644 --- a/cpp/src/arrow/acero/hash_join_node_test.cc +++ b/cpp/src/arrow/acero/hash_join_node_test.cc @@ -3467,7 +3467,8 @@ int main() { field("key2", int64()), field("c0", int64())}); const int64_t key_min = 0; - const int64_t key_max = std::numeric_limits::max(); + // const int64_t key_max = std::numeric_limits::max() / 8; + const int64_t key_max = num_large_rows / 8; const double null_probability = 0; auto small_key0_arr = RandomArrayGenerator(seed).Int64( From a54025bf5a24b52af9362bcdcce0650ad6c14b53 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Fri, 20 Dec 2024 15:37:10 +0800 Subject: [PATCH 03/25] Update test --- cpp/src/arrow/acero/hash_join_node_test.cc | 28 ++++++++++++---------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/cpp/src/arrow/acero/hash_join_node_test.cc b/cpp/src/arrow/acero/hash_join_node_test.cc index 367869a56d14..69f98a0e4677 100644 --- a/cpp/src/arrow/acero/hash_join_node_test.cc +++ b/cpp/src/arrow/acero/hash_join_node_test.cc @@ -3449,11 +3449,11 @@ TEST(HashJoin, LARGE_MEMORY_TEST(BuildSideOver4GBVarLength)) { } // namespace acero } // namespace arrow -// TEST(HashJoin, LargeBuildSide) { - using namespace arrow; using namespace arrow::acero; -int main() { + +TEST(HashJoin, GH44513) { + // int main() { const int64_t num_small_rows = 18201475; const int64_t num_large_rows = 360449051; const int64_t num_batches = 8; @@ -3464,11 +3464,11 @@ int main() { field("c1", int64()), field("c2", int64()), field("key2", int64()), field("c3", int64()), field("c4", int64()), field("c5", int64())}); auto large_schema = schema({field("key0", int64()), field("key1", int64()), - field("key2", int64()), field("c0", int64())}); + field("key2", int64()), field("payload", int64())}); const int64_t key_min = 0; - // const int64_t key_max = std::numeric_limits::max() / 8; - const int64_t key_max = num_large_rows / 8; + const int64_t key_max = std::numeric_limits::max(); + // const int64_t key_max = num_large_rows / 8; const double null_probability = 0; auto small_key0_arr = RandomArrayGenerator(seed).Int64( @@ -3490,8 +3490,12 @@ int main() { num_large_rows / num_batches, key_min, key_max, null_probability); auto large_key2_arr = RandomArrayGenerator(seed).Int64( num_large_rows / num_batches, key_min, key_max, null_probability); - ExecBatch large_batch({large_key0_arr, large_key1_arr, large_key2_arr, large_key0_arr}, - num_large_rows / num_batches); + ASSERT_OK_AND_ASSIGN( + auto large_payload_arr, + Constant(MakeScalar(int64_t(2)))->Generate(num_large_rows / num_batches)); + ExecBatch large_batch( + {large_key0_arr, large_key1_arr, large_key2_arr, large_payload_arr}, + num_large_rows / num_batches); auto large_batches = BatchesWithSchema{std::vector(num_batches, large_batch), large_schema}; @@ -3513,12 +3517,12 @@ int main() { Declaration join{ "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; - Declaration agg{"aggregate", - {std::move(join)}, - AggregateNodeOptions{/*aggregates=*/{{"count_all", "count(*)"}}}}; + AggregateNodeOptions agg_opts{/*aggregates=*/{ + {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; + Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; // ASSERT_OK_AND_ASSIGN(std::ignore, DeclarationToTable(std::move(join))); auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); std::cout << result->ToString() << std::endl; - return 0; + // return 0; } From 395ad095dcf0ad09241e38f2f395a64ef55ae9b5 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Tue, 24 Dec 2024 20:33:58 +0800 Subject: [PATCH 04/25] Fix and test WIP --- cpp/src/arrow/acero/hash_join_node_test.cc | 245 ++++++++++++++++----- cpp/src/arrow/compute/key_map_internal.cc | 19 +- cpp/src/arrow/compute/key_map_internal.h | 19 +- 3 files changed, 222 insertions(+), 61 deletions(-) diff --git a/cpp/src/arrow/acero/hash_join_node_test.cc b/cpp/src/arrow/acero/hash_join_node_test.cc index 69f98a0e4677..a77152d98113 100644 --- a/cpp/src/arrow/acero/hash_join_node_test.cc +++ b/cpp/src/arrow/acero/hash_join_node_test.cc @@ -3452,7 +3452,7 @@ TEST(HashJoin, LARGE_MEMORY_TEST(BuildSideOver4GBVarLength)) { using namespace arrow; using namespace arrow::acero; -TEST(HashJoin, GH44513) { +TEST(HashJoin, GH44513_Old) { // int main() { const int64_t num_small_rows = 18201475; const int64_t num_large_rows = 360449051; @@ -3466,63 +3466,210 @@ TEST(HashJoin, GH44513) { auto large_schema = schema({field("key0", int64()), field("key1", int64()), field("key2", int64()), field("payload", int64())}); - const int64_t key_min = 0; - const int64_t key_max = std::numeric_limits::max(); - // const int64_t key_max = num_large_rows / 8; - const double null_probability = 0; - - auto small_key0_arr = RandomArrayGenerator(seed).Int64( - num_small_rows / num_batches, key_min, key_max, null_probability); - auto small_key1_arr = RandomArrayGenerator(seed).Int64( - num_small_rows / num_batches, key_min, key_max, null_probability); - auto small_key2_arr = RandomArrayGenerator(seed).Int64( - num_small_rows / num_batches, key_min, key_max, null_probability); + const int64_t small_key_min = 28487150299; + const int64_t small_key_max = 98486720299; + const double small_key_null_probability = 0; + auto small_key_arr = + RandomArrayGenerator(seed).Int64(num_small_rows / num_batches, small_key_min, + small_key_max, small_key_null_probability); + + // const int64_t num_matching_rows = 17060116; + // const double matching_rate = double(num_matching_rows) / double(num_small_rows) / + // (double(num_small_rows) / double(small_key_max)); + const int64_t large_key_min = 2018570299; + const int64_t large_key_max = 99756520299; + // const int64_t large_key_min = small_key_max * (1 - matching_rate); + // const int64_t large_key_max = 391014 + large_key_min; + const double large_key_null_probability = 0; + auto large_key_arr = + RandomArrayGenerator(seed).Int64(num_large_rows / num_batches, large_key_min, + large_key_max, large_key_null_probability); + + const int64_t payload_min = 0; + const int64_t payload_max = 96920; + const double payload_null_probability = 283328672.0 / num_large_rows; + + // auto small_key0_arr = RandomArrayGenerator(seed).Int64( + // num_small_rows / num_batches, key_min, key_max, key_null_probability); + // auto small_key1_arr = RandomArrayGenerator(seed).Int64( + // num_small_rows / num_batches, key_min, key_max, key_null_probability); + // auto small_key2_arr = RandomArrayGenerator(seed).Int64( + // num_small_rows / num_batches, key_min, key_max, key_null_probability); ExecBatch small_batch( - {small_key0_arr, small_key0_arr, small_key1_arr, small_key0_arr, small_key0_arr, - small_key2_arr, small_key0_arr, small_key0_arr, small_key0_arr}, + {small_key_arr, small_key_arr, small_key_arr, small_key_arr, small_key_arr, + small_key_arr, small_key_arr, small_key_arr, small_key_arr}, num_small_rows / num_batches); auto small_batches = BatchesWithSchema{std::vector(num_batches, small_batch), small_schema}; - auto large_key0_arr = RandomArrayGenerator(seed).Int64( - num_large_rows / num_batches, key_min, key_max, null_probability); - auto large_key1_arr = RandomArrayGenerator(seed).Int64( - num_large_rows / num_batches, key_min, key_max, null_probability); - auto large_key2_arr = RandomArrayGenerator(seed).Int64( - num_large_rows / num_batches, key_min, key_max, null_probability); - ASSERT_OK_AND_ASSIGN( - auto large_payload_arr, - Constant(MakeScalar(int64_t(2)))->Generate(num_large_rows / num_batches)); - ExecBatch large_batch( - {large_key0_arr, large_key1_arr, large_key2_arr, large_payload_arr}, - num_large_rows / num_batches); + // auto large_key0_arr = RandomArrayGenerator(seed).Int64( + // num_large_rows / num_batches, key_min, key_max, key_null_probability); + // auto large_key1_arr = RandomArrayGenerator(seed).Int64( + // num_large_rows / num_batches, key_min, key_max, key_null_probability); + // auto large_key2_arr = RandomArrayGenerator(seed).Int64( + // num_large_rows / num_batches, key_min, key_max, key_null_probability); + auto large_payload_arr = RandomArrayGenerator(seed).Int64( + num_large_rows / num_batches, payload_min, payload_max, payload_null_probability); + ExecBatch large_batch({large_key_arr, large_key_arr, large_key_arr, large_payload_arr}, + num_large_rows / num_batches); auto large_batches = BatchesWithSchema{std::vector(num_batches, large_batch), large_schema}; - Declaration small_source{"exec_batch_source", - ExecBatchSourceNodeOptions(std::move(small_batches.schema), - std::move(small_batches.batches))}; - Declaration large_source{"exec_batch_source", - ExecBatchSourceNodeOptions(std::move(large_batches.schema), - std::move(large_batches.batches))}; - - // HashJoinNodeOptions join_opts(JoinType::RIGHT_OUTER, - // /*left_keys=*/{"key0", "key1", "key2"}, - // /*right_keys=*/{"key0", "key1", "key2"}); - // Declaration join{ - // "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; - HashJoinNodeOptions join_opts(JoinType::LEFT_OUTER, - /*left_keys=*/{"key0", "key1", "key2"}, - /*right_keys=*/{"key0", "key1", "key2"}); - Declaration join{ - "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; + { + Declaration small_source{ + "exec_batch_source", + ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; + Declaration large_source{ + "exec_batch_source", + ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; + + HashJoinNodeOptions join_opts(JoinType::RIGHT_OUTER, + /*left_keys=*/{"key0", "key1", "key2"}, + /*right_keys=*/{"key0", "key1", "key2"}); + Declaration join{ + "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; + + AggregateNodeOptions agg_opts{/*aggregates=*/{ + {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; + Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; + + auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); + std::cout << result->ToString() << std::endl; + } + { + Declaration small_source{ + "exec_batch_source", + ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; + Declaration large_source{ + "exec_batch_source", + ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; - AggregateNodeOptions agg_opts{/*aggregates=*/{ - {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; - Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; + HashJoinNodeOptions join_opts(JoinType::LEFT_OUTER, + /*left_keys=*/{"key0", "key1", "key2"}, + /*right_keys=*/{"key0", "key1", "key2"}); + Declaration join{ + "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; + + AggregateNodeOptions agg_opts{/*aggregates=*/{ + {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; + Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; + + auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); + std::cout << result->ToString() << std::endl; + } - // ASSERT_OK_AND_ASSIGN(std::ignore, DeclarationToTable(std::move(join))); - auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); - std::cout << result->ToString() << std::endl; // return 0; } + +TEST(HashJoin, GH44513) { + const int64_t num_large_rows = 360449051; + const int64_t num_batches = 8; + const int64_t seed = 42; + + auto small_schema = + schema({field("key0", int64()), field("key1", int64()), field("key2", int64())}); + auto large_schema = schema({field("key0", int64()), field("key1", int64()), + field("key2", int64()), field("payload", int64())}); + + const int64_t key0_match = 88506230299; + const int64_t key1_match = 16556030299; + const int64_t key2_match = 11240299; + + const int64_t small_key_min = 28487150299; + const int64_t small_key_max = 98486720299; + const double small_key_null_probability = 0; + auto small_key_arr = + RandomArrayGenerator(seed).Int64(num_small_rows / num_batches, small_key_min, + small_key_max, small_key_null_probability); + + // const int64_t num_matching_rows = 17060116; + // const double matching_rate = double(num_matching_rows) / double(num_small_rows) / + // (double(num_small_rows) / double(small_key_max)); + const int64_t large_key_min = 2018570299; + const int64_t large_key_max = 99756520299; + // const int64_t large_key_min = small_key_max * (1 - matching_rate); + // const int64_t large_key_max = 391014 + large_key_min; + const double large_key_null_probability = 0; + auto large_key_arr = + RandomArrayGenerator(seed).Int64(num_large_rows / num_batches, large_key_min, + large_key_max, large_key_null_probability); + + const int64_t payload_min = 0; + const int64_t payload_max = 96920; + const double payload_null_probability = 283328672.0 / num_large_rows; + + // auto small_key0_arr = RandomArrayGenerator(seed).Int64( + // num_small_rows / num_batches, key_min, key_max, key_null_probability); + // auto small_key1_arr = RandomArrayGenerator(seed).Int64( + // num_small_rows / num_batches, key_min, key_max, key_null_probability); + // auto small_key2_arr = RandomArrayGenerator(seed).Int64( + // num_small_rows / num_batches, key_min, key_max, key_null_probability); + ExecBatch small_batch( + {small_key_arr, small_key_arr, small_key_arr, small_key_arr, small_key_arr, + small_key_arr, small_key_arr, small_key_arr, small_key_arr}, + num_small_rows / num_batches); + auto small_batches = + BatchesWithSchema{std::vector(num_batches, small_batch), + small_schema}; + + // auto large_key0_arr = RandomArrayGenerator(seed).Int64( + // num_large_rows / num_batches, key_min, key_max, key_null_probability); + // auto large_key1_arr = RandomArrayGenerator(seed).Int64( + // num_large_rows / num_batches, key_min, key_max, key_null_probability); + // auto large_key2_arr = RandomArrayGenerator(seed).Int64( + // num_large_rows / num_batches, key_min, key_max, key_null_probability); + auto large_payload_arr = RandomArrayGenerator(seed).Int64( + num_large_rows / num_batches, payload_min, payload_max, + payload_null_probability); + ExecBatch large_batch({large_key_arr, large_key_arr, large_key_arr, + large_payload_arr}, + num_large_rows / num_batches); + auto large_batches = + BatchesWithSchema{std::vector(num_batches, large_batch), + large_schema}; + + { + Declaration small_source{ + "exec_batch_source", + ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; + Declaration large_source{ + "exec_batch_source", + ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; + + HashJoinNodeOptions join_opts(JoinType::RIGHT_OUTER, + /*left_keys=*/{"key0", "key1", "key2"}, + /*right_keys=*/{"key0", "key1", "key2"}); + Declaration join{ + "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; + + AggregateNodeOptions agg_opts{/*aggregates=*/{ + {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; + Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; + + auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); + std::cout << result->ToString() << std::endl; + } + { + Declaration small_source{ + "exec_batch_source", + ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; + Declaration large_source{ + "exec_batch_source", + ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; + + HashJoinNodeOptions join_opts(JoinType::LEFT_OUTER, + /*left_keys=*/{"key0", "key1", "key2"}, + /*right_keys=*/{"key0", "key1", "key2"}); + Declaration join{ + "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; + + AggregateNodeOptions agg_opts{/*aggregates=*/{ + {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; + Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; + + auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); + std::cout << result->ToString() << std::endl; + } + + // return 0; + } diff --git a/cpp/src/arrow/compute/key_map_internal.cc b/cpp/src/arrow/compute/key_map_internal.cc index 81f1543ceff9..8a98ac4669cd 100644 --- a/cpp/src/arrow/compute/key_map_internal.cc +++ b/cpp/src/arrow/compute/key_map_internal.cc @@ -113,6 +113,7 @@ void SwissTable::extract_group_ids_imp(const int num_keys, const uint16_t* selec uint32_t hash = hashes[id]; int64_t pos = (hash >> (bits_hash_ - log_blocks_)) * element_multiplier + local_slots[id]; + ARROW_DCHECK(pos >= 0 && pos < 0xFFFFFFFF); uint32_t group_id = static_cast(elements[pos]); ARROW_DCHECK(group_id < num_inserted_ || num_inserted_ == 0); out_group_ids[id] = group_id; @@ -223,8 +224,9 @@ void SwissTable::init_slot_ids_for_new_keys(uint32_t num_ids, const uint16_t* id uint32_t iblock = hash >> (bits_hash_ - log_blocks_); uint64_t block; for (;;) { - block = *reinterpret_cast(blocks_->mutable_data() + - num_block_bytes * iblock); + int64_t pos = int64_t(num_block_bytes) * iblock; + DCHECK(pos >= 0 && pos <= std::numeric_limits::max()); + block = *reinterpret_cast(blocks_->mutable_data() + pos); block &= kHighBitOfEachByte; if (block) { break; @@ -254,9 +256,9 @@ void SwissTable::early_filter_imp(const int num_keys, const uint32_t* hashes, // Extract from hash: block index and stamp // uint32_t hash = hashes[i]; - uint32_t iblock = hash >> (bits_hash_ - bits_stamp_ - log_blocks_); + uint32_t iblock = block_id_and_stamp(hash); uint32_t stamp = iblock & stamp_mask; - iblock >>= bits_stamp_; + iblock = block_id(iblock); uint32_t num_block_bytes = num_groupid_bits + 8; const uint8_t* blockbase = @@ -393,8 +395,7 @@ bool SwissTable::find_next_stamp_match(const uint32_t hash, const uint32_t in_sl uint32_t* out_group_id) const { const uint64_t num_groupid_bits = num_groupid_bits_from_log_blocks(log_blocks_); constexpr uint64_t stamp_mask = 0x7f; - const int stamp = - static_cast((hash >> (bits_hash_ - log_blocks_ - bits_stamp_)) & stamp_mask); + const int stamp = static_cast(block_id_and_stamp(hash) & stamp_mask); uint64_t start_slot_id = wrap_global_slot_id(in_slot_id); int match_found; int local_slot; @@ -696,8 +697,7 @@ Status SwissTable::grow_double() { } int ihalf = block_id_new & 1; - uint8_t stamp_new = - hash >> ((bits_hash_ - log_blocks_after - bits_stamp_)) & stamp_mask; + uint8_t stamp_new = block_id_and_stamp(hash) & stamp_mask; uint64_t group_id_bit_offs = j * num_group_id_bits_before; uint64_t group_id = (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> @@ -739,8 +739,7 @@ Status SwissTable::grow_double() { (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> (group_id_bit_offs & 7)) & group_id_mask_before; - uint8_t stamp_new = - hash >> ((bits_hash_ - log_blocks_after - bits_stamp_)) & stamp_mask; + uint8_t stamp_new = block_id_and_stamp(hash) & stamp_mask; uint8_t* block_base_new = blocks_new->mutable_data() + block_id_new * block_size_after; diff --git a/cpp/src/arrow/compute/key_map_internal.h b/cpp/src/arrow/compute/key_map_internal.h index a5e784a9e446..b1832c0cc8ba 100644 --- a/cpp/src/arrow/compute/key_map_internal.h +++ b/cpp/src/arrow/compute/key_map_internal.h @@ -203,6 +203,22 @@ class ARROW_EXPORT SwissTable { // Resize large hash tables when 75% full. Status grow_double(); + uint32_t block_id_and_stamp(uint32_t hash) const { + if (ARROW_PREDICT_FALSE(log_blocks_ + bits_stamp_ >= bits_hash_)) { + return hash; + } + return hash >> (bits_hash_ - log_blocks_ - bits_stamp_); + } + + uint32_t block_id(uint32_t block_id_and_stamp) const { + if (ARROW_PREDICT_FALSE(log_blocks_ + bits_stamp_ >= bits_hash_)) { + return block_id_and_stamp >> (bits_hash_ - log_blocks_); + // return block_id_and_stamp >> (bits_stamp_ - (log_blocks_ + bits_stamp_ - + // bits_hash_)); + } + return block_id_and_stamp >> bits_stamp_; + } + // Number of hash bits stored in slots in a block. // The highest bits of hash determine block id. // The next set of highest bits is a "stamp" stored in a slot in a block. @@ -271,8 +287,7 @@ void SwissTable::insert_into_empty_slot(uint32_t slot_id, uint32_t hash, constexpr uint64_t stamp_mask = 0x7f; int start_slot = (slot_id & 7); - int stamp = - static_cast((hash >> (bits_hash_ - log_blocks_ - bits_stamp_)) & stamp_mask); + int stamp = static_cast(block_id_and_stamp(hash) & stamp_mask); uint64_t block_id = slot_id >> 3; uint8_t* blockbase = blocks_->mutable_data() + num_block_bytes * block_id; From c756f08e416b49b7c38227b1dd42de093f1808ca Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Tue, 24 Dec 2024 23:50:33 +0800 Subject: [PATCH 05/25] Fix --- cpp/src/arrow/acero/hash_join_node_test.cc | 224 +++++++++--------- cpp/src/arrow/compute/key_map_internal.cc | 20 +- cpp/src/arrow/compute/key_map_internal.h | 20 +- .../arrow/compute/key_map_internal_avx2.cc | 6 +- 4 files changed, 133 insertions(+), 137 deletions(-) diff --git a/cpp/src/arrow/acero/hash_join_node_test.cc b/cpp/src/arrow/acero/hash_join_node_test.cc index a77152d98113..481796b7a0bb 100644 --- a/cpp/src/arrow/acero/hash_join_node_test.cc +++ b/cpp/src/arrow/acero/hash_join_node_test.cc @@ -3561,115 +3561,115 @@ TEST(HashJoin, GH44513_Old) { // return 0; } -TEST(HashJoin, GH44513) { - const int64_t num_large_rows = 360449051; - const int64_t num_batches = 8; - const int64_t seed = 42; - - auto small_schema = - schema({field("key0", int64()), field("key1", int64()), field("key2", int64())}); - auto large_schema = schema({field("key0", int64()), field("key1", int64()), - field("key2", int64()), field("payload", int64())}); - - const int64_t key0_match = 88506230299; - const int64_t key1_match = 16556030299; - const int64_t key2_match = 11240299; - - const int64_t small_key_min = 28487150299; - const int64_t small_key_max = 98486720299; - const double small_key_null_probability = 0; - auto small_key_arr = - RandomArrayGenerator(seed).Int64(num_small_rows / num_batches, small_key_min, - small_key_max, small_key_null_probability); - - // const int64_t num_matching_rows = 17060116; - // const double matching_rate = double(num_matching_rows) / double(num_small_rows) / - // (double(num_small_rows) / double(small_key_max)); - const int64_t large_key_min = 2018570299; - const int64_t large_key_max = 99756520299; - // const int64_t large_key_min = small_key_max * (1 - matching_rate); - // const int64_t large_key_max = 391014 + large_key_min; - const double large_key_null_probability = 0; - auto large_key_arr = - RandomArrayGenerator(seed).Int64(num_large_rows / num_batches, large_key_min, - large_key_max, large_key_null_probability); - - const int64_t payload_min = 0; - const int64_t payload_max = 96920; - const double payload_null_probability = 283328672.0 / num_large_rows; - - // auto small_key0_arr = RandomArrayGenerator(seed).Int64( - // num_small_rows / num_batches, key_min, key_max, key_null_probability); - // auto small_key1_arr = RandomArrayGenerator(seed).Int64( - // num_small_rows / num_batches, key_min, key_max, key_null_probability); - // auto small_key2_arr = RandomArrayGenerator(seed).Int64( - // num_small_rows / num_batches, key_min, key_max, key_null_probability); - ExecBatch small_batch( - {small_key_arr, small_key_arr, small_key_arr, small_key_arr, small_key_arr, - small_key_arr, small_key_arr, small_key_arr, small_key_arr}, - num_small_rows / num_batches); - auto small_batches = - BatchesWithSchema{std::vector(num_batches, small_batch), - small_schema}; - - // auto large_key0_arr = RandomArrayGenerator(seed).Int64( - // num_large_rows / num_batches, key_min, key_max, key_null_probability); - // auto large_key1_arr = RandomArrayGenerator(seed).Int64( - // num_large_rows / num_batches, key_min, key_max, key_null_probability); - // auto large_key2_arr = RandomArrayGenerator(seed).Int64( - // num_large_rows / num_batches, key_min, key_max, key_null_probability); - auto large_payload_arr = RandomArrayGenerator(seed).Int64( - num_large_rows / num_batches, payload_min, payload_max, - payload_null_probability); - ExecBatch large_batch({large_key_arr, large_key_arr, large_key_arr, - large_payload_arr}, - num_large_rows / num_batches); - auto large_batches = - BatchesWithSchema{std::vector(num_batches, large_batch), - large_schema}; - - { - Declaration small_source{ - "exec_batch_source", - ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; - Declaration large_source{ - "exec_batch_source", - ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; - - HashJoinNodeOptions join_opts(JoinType::RIGHT_OUTER, - /*left_keys=*/{"key0", "key1", "key2"}, - /*right_keys=*/{"key0", "key1", "key2"}); - Declaration join{ - "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; - - AggregateNodeOptions agg_opts{/*aggregates=*/{ - {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; - Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; - - auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); - std::cout << result->ToString() << std::endl; - } - { - Declaration small_source{ - "exec_batch_source", - ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; - Declaration large_source{ - "exec_batch_source", - ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; - - HashJoinNodeOptions join_opts(JoinType::LEFT_OUTER, - /*left_keys=*/{"key0", "key1", "key2"}, - /*right_keys=*/{"key0", "key1", "key2"}); - Declaration join{ - "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; - - AggregateNodeOptions agg_opts{/*aggregates=*/{ - {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; - Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; - - auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); - std::cout << result->ToString() << std::endl; - } - - // return 0; - } +// TEST(HashJoin, GH44513) { +// const int64_t num_large_rows = 360449051; +// const int64_t num_batches = 8; +// const int64_t seed = 42; +// +// auto small_schema = +// schema({field("key0", int64()), field("key1", int64()), field("key2", int64())}); +// auto large_schema = schema({field("key0", int64()), field("key1", int64()), +// field("key2", int64()), field("payload", int64())}); +// +// const int64_t key0_match = 88506230299; +// const int64_t key1_match = 16556030299; +// const int64_t key2_match = 11240299; +// +// const int64_t small_key_min = 28487150299; +// const int64_t small_key_max = 98486720299; +// const double small_key_null_probability = 0; +// auto small_key_arr = +// RandomArrayGenerator(seed).Int64(num_small_rows / num_batches, small_key_min, +// small_key_max, small_key_null_probability); +// +const int64_t num_matching_rows = 17060116; +const double matching_rate = double(num_matching_rows) / double(num_small_rows) / + (double(num_small_rows) / double(small_key_max)); +// const int64_t large_key_min = 2018570299; +// const int64_t large_key_max = 99756520299; +const int64_t large_key_min = small_key_max * (1 - matching_rate); +const int64_t large_key_max = 391014 + large_key_min; +// const double large_key_null_probability = 0; +// auto large_key_arr = +// RandomArrayGenerator(seed).Int64(num_large_rows / num_batches, large_key_min, +// large_key_max, large_key_null_probability); +// +// const int64_t payload_min = 0; +// const int64_t payload_max = 96920; +// const double payload_null_probability = 283328672.0 / num_large_rows; +// +auto small_key0_arr = RandomArrayGenerator(seed).Int64( + num_small_rows / num_batches, key_min, key_max, key_null_probability); +auto small_key1_arr = RandomArrayGenerator(seed).Int64( + num_small_rows / num_batches, key_min, key_max, key_null_probability); +auto small_key2_arr = RandomArrayGenerator(seed).Int64( + num_small_rows / num_batches, key_min, key_max, key_null_probability); +// ExecBatch small_batch( +// {small_key_arr, small_key_arr, small_key_arr, small_key_arr, small_key_arr, +// small_key_arr, small_key_arr, small_key_arr, small_key_arr}, +// num_small_rows / num_batches); +// auto small_batches = +// BatchesWithSchema{std::vector(num_batches, small_batch), +// small_schema}; +// +auto large_key0_arr = RandomArrayGenerator(seed).Int64( + num_large_rows / num_batches, key_min, key_max, key_null_probability); +auto large_key1_arr = RandomArrayGenerator(seed).Int64( + num_large_rows / num_batches, key_min, key_max, key_null_probability); +auto large_key2_arr = RandomArrayGenerator(seed).Int64( + num_large_rows / num_batches, key_min, key_max, key_null_probability); +// auto large_payload_arr = RandomArrayGenerator(seed).Int64( +// num_large_rows / num_batches, payload_min, payload_max, +// payload_null_probability); +// ExecBatch large_batch({large_key_arr, large_key_arr, large_key_arr, +// large_payload_arr}, +// num_large_rows / num_batches); +// auto large_batches = +// BatchesWithSchema{std::vector(num_batches, large_batch), +// large_schema}; +// +// { +// Declaration small_source{ +// "exec_batch_source", +// ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; +// Declaration large_source{ +// "exec_batch_source", +// ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; +// +// HashJoinNodeOptions join_opts(JoinType::RIGHT_OUTER, +// /*left_keys=*/{"key0", "key1", "key2"}, +// /*right_keys=*/{"key0", "key1", "key2"}); +// Declaration join{ +// "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; +// +// AggregateNodeOptions agg_opts{/*aggregates=*/{ +// {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; +// Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; +// +// auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); +// std::cout << result->ToString() << std::endl; +// } +// { +// Declaration small_source{ +// "exec_batch_source", +// ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; +// Declaration large_source{ +// "exec_batch_source", +// ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; +// +// HashJoinNodeOptions join_opts(JoinType::LEFT_OUTER, +// /*left_keys=*/{"key0", "key1", "key2"}, +// /*right_keys=*/{"key0", "key1", "key2"}); +// Declaration join{ +// "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; +// +// AggregateNodeOptions agg_opts{/*aggregates=*/{ +// {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; +// Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; +// +// auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); +// std::cout << result->ToString() << std::endl; +// } +// +return 0; +// } diff --git a/cpp/src/arrow/compute/key_map_internal.cc b/cpp/src/arrow/compute/key_map_internal.cc index 8a98ac4669cd..f8811beeeed3 100644 --- a/cpp/src/arrow/compute/key_map_internal.cc +++ b/cpp/src/arrow/compute/key_map_internal.cc @@ -256,9 +256,9 @@ void SwissTable::early_filter_imp(const int num_keys, const uint32_t* hashes, // Extract from hash: block index and stamp // uint32_t hash = hashes[i]; - uint32_t iblock = block_id_and_stamp(hash); + uint32_t iblock = hash << bits_shift_for_block_and_stamp_; uint32_t stamp = iblock & stamp_mask; - iblock = block_id(iblock); + iblock <<= bits_shift_for_stamp_; uint32_t num_block_bytes = num_groupid_bits + 8; const uint8_t* blockbase = @@ -395,7 +395,8 @@ bool SwissTable::find_next_stamp_match(const uint32_t hash, const uint32_t in_sl uint32_t* out_group_id) const { const uint64_t num_groupid_bits = num_groupid_bits_from_log_blocks(log_blocks_); constexpr uint64_t stamp_mask = 0x7f; - const int stamp = static_cast(block_id_and_stamp(hash) & stamp_mask); + const int stamp = + static_cast((hash << bits_shift_for_block_and_stamp_) & stamp_mask); uint64_t start_slot_id = wrap_global_slot_id(in_slot_id); int match_found; int local_slot; @@ -697,7 +698,7 @@ Status SwissTable::grow_double() { } int ihalf = block_id_new & 1; - uint8_t stamp_new = block_id_and_stamp(hash) & stamp_mask; + uint8_t stamp_new = (hash << bits_shift_for_block_and_stamp_) & stamp_mask; uint64_t group_id_bit_offs = j * num_group_id_bits_before; uint64_t group_id = (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> @@ -739,7 +740,7 @@ Status SwissTable::grow_double() { (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> (group_id_bit_offs & 7)) & group_id_mask_before; - uint8_t stamp_new = block_id_and_stamp(hash) & stamp_mask; + uint8_t stamp_new = (hash << bits_shift_for_block_and_stamp_) & stamp_mask; uint8_t* block_base_new = blocks_new->mutable_data() + block_id_new * block_size_after; @@ -767,6 +768,13 @@ Status SwissTable::grow_double() { blocks_ = std::move(blocks_new); hashes_ = std::move(hashes_new_buffer); log_blocks_ = log_blocks_after; + if (log_blocks_ + bits_stamp_ > bits_hash_) { + bits_shift_for_block_and_stamp_ = 0; + bits_shift_for_stamp_ = bits_hash_ - log_blocks_; + } else { + bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; + bits_shift_for_stamp_ = bits_stamp_; + } return Status::OK(); } @@ -814,6 +822,8 @@ void SwissTable::cleanup() { hashes_ = nullptr; } log_blocks_ = 0; + bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; + bits_shift_for_stamp_ = bits_stamp_; num_inserted_ = 0; } diff --git a/cpp/src/arrow/compute/key_map_internal.h b/cpp/src/arrow/compute/key_map_internal.h index b1832c0cc8ba..2723c6246da5 100644 --- a/cpp/src/arrow/compute/key_map_internal.h +++ b/cpp/src/arrow/compute/key_map_internal.h @@ -203,22 +203,6 @@ class ARROW_EXPORT SwissTable { // Resize large hash tables when 75% full. Status grow_double(); - uint32_t block_id_and_stamp(uint32_t hash) const { - if (ARROW_PREDICT_FALSE(log_blocks_ + bits_stamp_ >= bits_hash_)) { - return hash; - } - return hash >> (bits_hash_ - log_blocks_ - bits_stamp_); - } - - uint32_t block_id(uint32_t block_id_and_stamp) const { - if (ARROW_PREDICT_FALSE(log_blocks_ + bits_stamp_ >= bits_hash_)) { - return block_id_and_stamp >> (bits_hash_ - log_blocks_); - // return block_id_and_stamp >> (bits_stamp_ - (log_blocks_ + bits_stamp_ - - // bits_hash_)); - } - return block_id_and_stamp >> bits_stamp_; - } - // Number of hash bits stored in slots in a block. // The highest bits of hash determine block id. // The next set of highest bits is a "stamp" stored in a slot in a block. @@ -230,6 +214,8 @@ class ARROW_EXPORT SwissTable { int log_minibatch_; // Base 2 log of the number of blocks int log_blocks_ = 0; + int bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; + int bits_shift_for_stamp_ = bits_stamp_; // Number of keys inserted into hash table uint32_t num_inserted_ = 0; @@ -287,7 +273,7 @@ void SwissTable::insert_into_empty_slot(uint32_t slot_id, uint32_t hash, constexpr uint64_t stamp_mask = 0x7f; int start_slot = (slot_id & 7); - int stamp = static_cast(block_id_and_stamp(hash) & stamp_mask); + int stamp = static_cast((hash << bits_shift_for_block_and_stamp_) & stamp_mask); uint64_t block_id = slot_id >> 3; uint8_t* blockbase = blocks_->mutable_data() + num_block_bytes * block_id; diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index 1a16603a0faa..b0e07543fa8c 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -45,10 +45,10 @@ int SwissTable::early_filter_imp_avx2_x8(const int num_hashes, const uint32_t* h // Calculate block index and hash stamp for a byte in a block // __m256i vhash = _mm256_loadu_si256(vhash_ptr + i); - __m256i vblock_id = _mm256_srlv_epi32( - vhash, _mm256_set1_epi32(bits_hash_ - bits_stamp_ - log_blocks_)); + __m256i vblock_id = + _mm256_srlv_epi32(vhash, _mm256_set1_epi32(bits_shift_for_block_and_stamp_)); __m256i vstamp = _mm256_and_si256(vblock_id, vstamp_mask); - vblock_id = _mm256_srli_epi32(vblock_id, bits_stamp_); + vblock_id = _mm256_srli_epi32(vblock_id, bits_shift_for_stamp_); // We now split inputs and process 4 at a time, // in order to process 64-bit blocks From d33b4884885b75ed1d595eb44756122c9b54c904 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Tue, 24 Dec 2024 23:53:47 +0800 Subject: [PATCH 06/25] Comment test --- cpp/src/arrow/acero/hash_join_node_test.cc | 222 ++++++++++----------- 1 file changed, 111 insertions(+), 111 deletions(-) diff --git a/cpp/src/arrow/acero/hash_join_node_test.cc b/cpp/src/arrow/acero/hash_join_node_test.cc index 481796b7a0bb..4e70901d8c61 100644 --- a/cpp/src/arrow/acero/hash_join_node_test.cc +++ b/cpp/src/arrow/acero/hash_join_node_test.cc @@ -3562,114 +3562,114 @@ TEST(HashJoin, GH44513_Old) { } // TEST(HashJoin, GH44513) { -// const int64_t num_large_rows = 360449051; -// const int64_t num_batches = 8; -// const int64_t seed = 42; -// -// auto small_schema = -// schema({field("key0", int64()), field("key1", int64()), field("key2", int64())}); -// auto large_schema = schema({field("key0", int64()), field("key1", int64()), -// field("key2", int64()), field("payload", int64())}); -// -// const int64_t key0_match = 88506230299; -// const int64_t key1_match = 16556030299; -// const int64_t key2_match = 11240299; -// -// const int64_t small_key_min = 28487150299; -// const int64_t small_key_max = 98486720299; -// const double small_key_null_probability = 0; -// auto small_key_arr = -// RandomArrayGenerator(seed).Int64(num_small_rows / num_batches, small_key_min, -// small_key_max, small_key_null_probability); -// -const int64_t num_matching_rows = 17060116; -const double matching_rate = double(num_matching_rows) / double(num_small_rows) / - (double(num_small_rows) / double(small_key_max)); -// const int64_t large_key_min = 2018570299; -// const int64_t large_key_max = 99756520299; -const int64_t large_key_min = small_key_max * (1 - matching_rate); -const int64_t large_key_max = 391014 + large_key_min; -// const double large_key_null_probability = 0; -// auto large_key_arr = -// RandomArrayGenerator(seed).Int64(num_large_rows / num_batches, large_key_min, -// large_key_max, large_key_null_probability); -// -// const int64_t payload_min = 0; -// const int64_t payload_max = 96920; -// const double payload_null_probability = 283328672.0 / num_large_rows; -// -auto small_key0_arr = RandomArrayGenerator(seed).Int64( - num_small_rows / num_batches, key_min, key_max, key_null_probability); -auto small_key1_arr = RandomArrayGenerator(seed).Int64( - num_small_rows / num_batches, key_min, key_max, key_null_probability); -auto small_key2_arr = RandomArrayGenerator(seed).Int64( - num_small_rows / num_batches, key_min, key_max, key_null_probability); -// ExecBatch small_batch( -// {small_key_arr, small_key_arr, small_key_arr, small_key_arr, small_key_arr, -// small_key_arr, small_key_arr, small_key_arr, small_key_arr}, -// num_small_rows / num_batches); -// auto small_batches = -// BatchesWithSchema{std::vector(num_batches, small_batch), -// small_schema}; -// -auto large_key0_arr = RandomArrayGenerator(seed).Int64( - num_large_rows / num_batches, key_min, key_max, key_null_probability); -auto large_key1_arr = RandomArrayGenerator(seed).Int64( - num_large_rows / num_batches, key_min, key_max, key_null_probability); -auto large_key2_arr = RandomArrayGenerator(seed).Int64( - num_large_rows / num_batches, key_min, key_max, key_null_probability); -// auto large_payload_arr = RandomArrayGenerator(seed).Int64( -// num_large_rows / num_batches, payload_min, payload_max, -// payload_null_probability); -// ExecBatch large_batch({large_key_arr, large_key_arr, large_key_arr, -// large_payload_arr}, -// num_large_rows / num_batches); -// auto large_batches = -// BatchesWithSchema{std::vector(num_batches, large_batch), -// large_schema}; -// -// { -// Declaration small_source{ -// "exec_batch_source", -// ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; -// Declaration large_source{ -// "exec_batch_source", -// ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; -// -// HashJoinNodeOptions join_opts(JoinType::RIGHT_OUTER, -// /*left_keys=*/{"key0", "key1", "key2"}, -// /*right_keys=*/{"key0", "key1", "key2"}); -// Declaration join{ -// "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; -// -// AggregateNodeOptions agg_opts{/*aggregates=*/{ -// {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; -// Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; -// -// auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); -// std::cout << result->ToString() << std::endl; -// } -// { -// Declaration small_source{ -// "exec_batch_source", -// ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; -// Declaration large_source{ -// "exec_batch_source", -// ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; -// -// HashJoinNodeOptions join_opts(JoinType::LEFT_OUTER, -// /*left_keys=*/{"key0", "key1", "key2"}, -// /*right_keys=*/{"key0", "key1", "key2"}); -// Declaration join{ -// "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; -// -// AggregateNodeOptions agg_opts{/*aggregates=*/{ -// {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; -// Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; -// -// auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); -// std::cout << result->ToString() << std::endl; -// } -// -return 0; -// } +// const int64_t num_large_rows = 360449051; +// const int64_t num_batches = 8; +// const int64_t seed = 42; + +// auto small_schema = +// schema({field("key0", int64()), field("key1", int64()), field("key2", int64())}); +// auto large_schema = schema({field("key0", int64()), field("key1", int64()), +// field("key2", int64()), field("payload", int64())}); + +// const int64_t key0_match = 88506230299; +// const int64_t key1_match = 16556030299; +// const int64_t key2_match = 11240299; + +// const int64_t small_key_min = 28487150299; +// const int64_t small_key_max = 98486720299; +// const double small_key_null_probability = 0; +// auto small_key_arr = +// RandomArrayGenerator(seed).Int64(num_small_rows / num_batches, small_key_min, +// small_key_max, small_key_null_probability); + +// // const int64_t num_matching_rows = 17060116; +// // const double matching_rate = double(num_matching_rows) / double(num_small_rows) / +// // (double(num_small_rows) / double(small_key_max)); +// const int64_t large_key_min = 2018570299; +// const int64_t large_key_max = 99756520299; +// // const int64_t large_key_min = small_key_max * (1 - matching_rate); +// // const int64_t large_key_max = 391014 + large_key_min; +// const double large_key_null_probability = 0; +// auto large_key_arr = +// RandomArrayGenerator(seed).Int64(num_large_rows / num_batches, large_key_min, +// large_key_max, large_key_null_probability); + +// const int64_t payload_min = 0; +// const int64_t payload_max = 96920; +// const double payload_null_probability = 283328672.0 / num_large_rows; + +// // auto small_key0_arr = RandomArrayGenerator(seed).Int64( +// // num_small_rows / num_batches, key_min, key_max, key_null_probability); +// // auto small_key1_arr = RandomArrayGenerator(seed).Int64( +// // num_small_rows / num_batches, key_min, key_max, key_null_probability); +// // auto small_key2_arr = RandomArrayGenerator(seed).Int64( +// // num_small_rows / num_batches, key_min, key_max, key_null_probability); +// ExecBatch small_batch( +// {small_key_arr, small_key_arr, small_key_arr, small_key_arr, small_key_arr, +// small_key_arr, small_key_arr, small_key_arr, small_key_arr}, +// num_small_rows / num_batches); +// auto small_batches = +// BatchesWithSchema{std::vector(num_batches, small_batch), +// small_schema}; + +// // auto large_key0_arr = RandomArrayGenerator(seed).Int64( +// // num_large_rows / num_batches, key_min, key_max, key_null_probability); +// // auto large_key1_arr = RandomArrayGenerator(seed).Int64( +// // num_large_rows / num_batches, key_min, key_max, key_null_probability); +// // auto large_key2_arr = RandomArrayGenerator(seed).Int64( +// // num_large_rows / num_batches, key_min, key_max, key_null_probability); +// auto large_payload_arr = RandomArrayGenerator(seed).Int64( +// num_large_rows / num_batches, payload_min, payload_max, +// payload_null_probability); +// ExecBatch large_batch({large_key_arr, large_key_arr, large_key_arr, +// large_payload_arr}, +// num_large_rows / num_batches); +// auto large_batches = +// BatchesWithSchema{std::vector(num_batches, large_batch), +// large_schema}; + +// { +// Declaration small_source{ +// "exec_batch_source", +// ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; +// Declaration large_source{ +// "exec_batch_source", +// ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; + +// HashJoinNodeOptions join_opts(JoinType::RIGHT_OUTER, +// /*left_keys=*/{"key0", "key1", "key2"}, +// /*right_keys=*/{"key0", "key1", "key2"}); +// Declaration join{ +// "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; + +// AggregateNodeOptions agg_opts{/*aggregates=*/{ +// {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; +// Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; + +// auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); +// std::cout << result->ToString() << std::endl; +// } +// { +// Declaration small_source{ +// "exec_batch_source", +// ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; +// Declaration large_source{ +// "exec_batch_source", +// ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; + +// HashJoinNodeOptions join_opts(JoinType::LEFT_OUTER, +// /*left_keys=*/{"key0", "key1", "key2"}, +// /*right_keys=*/{"key0", "key1", "key2"}); +// Declaration join{ +// "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; + +// AggregateNodeOptions agg_opts{/*aggregates=*/{ +// {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; +// Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; + +// auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); +// std::cout << result->ToString() << std::endl; +// } + +// // return 0; +// } From c824983a48ed11446f99065d7210e696a1715937 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Wed, 25 Dec 2024 00:24:41 +0800 Subject: [PATCH 07/25] Fix --- cpp/src/arrow/compute/key_map_internal.cc | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/cpp/src/arrow/compute/key_map_internal.cc b/cpp/src/arrow/compute/key_map_internal.cc index f8811beeeed3..717c21f5464b 100644 --- a/cpp/src/arrow/compute/key_map_internal.cc +++ b/cpp/src/arrow/compute/key_map_internal.cc @@ -656,6 +656,12 @@ Status SwissTable::grow_double() { int num_group_id_bits_after = num_groupid_bits_from_log_blocks(log_blocks_ + 1); uint64_t group_id_mask_before = ~0ULL >> (64 - num_group_id_bits_before); int log_blocks_after = log_blocks_ + 1; + int bits_shift_for_block_and_stamp_after = bits_hash_ - log_blocks_after - bits_stamp_; + int bits_shift_for_stamp_after = bits_stamp_; + if (bits_shift_for_block_and_stamp_after < 0) { + bits_shift_for_block_and_stamp_after = 0; + bits_shift_for_stamp_after = bits_hash_ - log_blocks_; + } uint64_t block_size_before = (8 + num_group_id_bits_before); uint64_t block_size_after = (8 + num_group_id_bits_after); uint64_t block_size_total_after = (block_size_after << log_blocks_after) + padding_; @@ -698,7 +704,7 @@ Status SwissTable::grow_double() { } int ihalf = block_id_new & 1; - uint8_t stamp_new = (hash << bits_shift_for_block_and_stamp_) & stamp_mask; + uint8_t stamp_new = (hash << bits_shift_for_block_and_stamp_after) & stamp_mask; uint64_t group_id_bit_offs = j * num_group_id_bits_before; uint64_t group_id = (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> @@ -740,7 +746,7 @@ Status SwissTable::grow_double() { (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> (group_id_bit_offs & 7)) & group_id_mask_before; - uint8_t stamp_new = (hash << bits_shift_for_block_and_stamp_) & stamp_mask; + uint8_t stamp_new = (hash << bits_shift_for_block_and_stamp_after) & stamp_mask; uint8_t* block_base_new = blocks_new->mutable_data() + block_id_new * block_size_after; @@ -768,13 +774,8 @@ Status SwissTable::grow_double() { blocks_ = std::move(blocks_new); hashes_ = std::move(hashes_new_buffer); log_blocks_ = log_blocks_after; - if (log_blocks_ + bits_stamp_ > bits_hash_) { - bits_shift_for_block_and_stamp_ = 0; - bits_shift_for_stamp_ = bits_hash_ - log_blocks_; - } else { - bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; - bits_shift_for_stamp_ = bits_stamp_; - } + bits_shift_for_block_and_stamp_ = bits_shift_for_block_and_stamp_after; + bits_shift_for_stamp_ = bits_shift_for_stamp_after; return Status::OK(); } From 53fe09fc460849fb7e650a0e8b29fae0dce3f08e Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Wed, 25 Dec 2024 00:28:45 +0800 Subject: [PATCH 08/25] Fix --- cpp/src/arrow/compute/key_map_internal.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/src/arrow/compute/key_map_internal.cc b/cpp/src/arrow/compute/key_map_internal.cc index 717c21f5464b..08434220c647 100644 --- a/cpp/src/arrow/compute/key_map_internal.cc +++ b/cpp/src/arrow/compute/key_map_internal.cc @@ -258,7 +258,7 @@ void SwissTable::early_filter_imp(const int num_keys, const uint32_t* hashes, uint32_t hash = hashes[i]; uint32_t iblock = hash << bits_shift_for_block_and_stamp_; uint32_t stamp = iblock & stamp_mask; - iblock <<= bits_shift_for_stamp_; + iblock >>= bits_shift_for_stamp_; uint32_t num_block_bytes = num_groupid_bits + 8; const uint8_t* blockbase = From 74ff31402f0fd050818853befbcba5b73f095fb3 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Wed, 25 Dec 2024 00:32:49 +0800 Subject: [PATCH 09/25] Fix --- cpp/src/arrow/compute/key_map_internal.cc | 8 ++++---- cpp/src/arrow/compute/key_map_internal.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cpp/src/arrow/compute/key_map_internal.cc b/cpp/src/arrow/compute/key_map_internal.cc index 08434220c647..6884ef42d41f 100644 --- a/cpp/src/arrow/compute/key_map_internal.cc +++ b/cpp/src/arrow/compute/key_map_internal.cc @@ -256,7 +256,7 @@ void SwissTable::early_filter_imp(const int num_keys, const uint32_t* hashes, // Extract from hash: block index and stamp // uint32_t hash = hashes[i]; - uint32_t iblock = hash << bits_shift_for_block_and_stamp_; + uint32_t iblock = hash >> bits_shift_for_block_and_stamp_; uint32_t stamp = iblock & stamp_mask; iblock >>= bits_shift_for_stamp_; @@ -396,7 +396,7 @@ bool SwissTable::find_next_stamp_match(const uint32_t hash, const uint32_t in_sl const uint64_t num_groupid_bits = num_groupid_bits_from_log_blocks(log_blocks_); constexpr uint64_t stamp_mask = 0x7f; const int stamp = - static_cast((hash << bits_shift_for_block_and_stamp_) & stamp_mask); + static_cast((hash >> bits_shift_for_block_and_stamp_) & stamp_mask); uint64_t start_slot_id = wrap_global_slot_id(in_slot_id); int match_found; int local_slot; @@ -704,7 +704,7 @@ Status SwissTable::grow_double() { } int ihalf = block_id_new & 1; - uint8_t stamp_new = (hash << bits_shift_for_block_and_stamp_after) & stamp_mask; + uint8_t stamp_new = (hash >> bits_shift_for_block_and_stamp_after) & stamp_mask; uint64_t group_id_bit_offs = j * num_group_id_bits_before; uint64_t group_id = (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> @@ -746,7 +746,7 @@ Status SwissTable::grow_double() { (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> (group_id_bit_offs & 7)) & group_id_mask_before; - uint8_t stamp_new = (hash << bits_shift_for_block_and_stamp_after) & stamp_mask; + uint8_t stamp_new = (hash >> bits_shift_for_block_and_stamp_after) & stamp_mask; uint8_t* block_base_new = blocks_new->mutable_data() + block_id_new * block_size_after; diff --git a/cpp/src/arrow/compute/key_map_internal.h b/cpp/src/arrow/compute/key_map_internal.h index 2723c6246da5..f152f00b4af0 100644 --- a/cpp/src/arrow/compute/key_map_internal.h +++ b/cpp/src/arrow/compute/key_map_internal.h @@ -273,7 +273,7 @@ void SwissTable::insert_into_empty_slot(uint32_t slot_id, uint32_t hash, constexpr uint64_t stamp_mask = 0x7f; int start_slot = (slot_id & 7); - int stamp = static_cast((hash << bits_shift_for_block_and_stamp_) & stamp_mask); + int stamp = static_cast((hash >> bits_shift_for_block_and_stamp_) & stamp_mask); uint64_t block_id = slot_id >> 3; uint8_t* blockbase = blocks_->mutable_data() + num_block_bytes * block_id; From e79cca307618bd225986ba7d11a693ff8b17f102 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Wed, 25 Dec 2024 02:13:01 +0800 Subject: [PATCH 10/25] Hack --- cpp/src/arrow/acero/hash_join_node_test.cc | 204 ++++++++---------- cpp/src/arrow/compute/key_map_internal.cc | 36 ++-- cpp/src/arrow/compute/key_map_internal.h | 23 +- .../arrow/compute/key_map_internal_avx2.cc | 8 +- 4 files changed, 137 insertions(+), 134 deletions(-) diff --git a/cpp/src/arrow/acero/hash_join_node_test.cc b/cpp/src/arrow/acero/hash_join_node_test.cc index 4e70901d8c61..e2ea08726929 100644 --- a/cpp/src/arrow/acero/hash_join_node_test.cc +++ b/cpp/src/arrow/acero/hash_join_node_test.cc @@ -3561,115 +3561,95 @@ TEST(HashJoin, GH44513_Old) { // return 0; } -// TEST(HashJoin, GH44513) { -// const int64_t num_large_rows = 360449051; -// const int64_t num_batches = 8; -// const int64_t seed = 42; - -// auto small_schema = -// schema({field("key0", int64()), field("key1", int64()), field("key2", int64())}); -// auto large_schema = schema({field("key0", int64()), field("key1", int64()), -// field("key2", int64()), field("payload", int64())}); - -// const int64_t key0_match = 88506230299; -// const int64_t key1_match = 16556030299; -// const int64_t key2_match = 11240299; - -// const int64_t small_key_min = 28487150299; -// const int64_t small_key_max = 98486720299; -// const double small_key_null_probability = 0; -// auto small_key_arr = -// RandomArrayGenerator(seed).Int64(num_small_rows / num_batches, small_key_min, -// small_key_max, small_key_null_probability); - -// // const int64_t num_matching_rows = 17060116; -// // const double matching_rate = double(num_matching_rows) / double(num_small_rows) / -// // (double(num_small_rows) / double(small_key_max)); -// const int64_t large_key_min = 2018570299; -// const int64_t large_key_max = 99756520299; -// // const int64_t large_key_min = small_key_max * (1 - matching_rate); -// // const int64_t large_key_max = 391014 + large_key_min; -// const double large_key_null_probability = 0; -// auto large_key_arr = -// RandomArrayGenerator(seed).Int64(num_large_rows / num_batches, large_key_min, -// large_key_max, large_key_null_probability); - -// const int64_t payload_min = 0; -// const int64_t payload_max = 96920; -// const double payload_null_probability = 283328672.0 / num_large_rows; - -// // auto small_key0_arr = RandomArrayGenerator(seed).Int64( -// // num_small_rows / num_batches, key_min, key_max, key_null_probability); -// // auto small_key1_arr = RandomArrayGenerator(seed).Int64( -// // num_small_rows / num_batches, key_min, key_max, key_null_probability); -// // auto small_key2_arr = RandomArrayGenerator(seed).Int64( -// // num_small_rows / num_batches, key_min, key_max, key_null_probability); -// ExecBatch small_batch( -// {small_key_arr, small_key_arr, small_key_arr, small_key_arr, small_key_arr, -// small_key_arr, small_key_arr, small_key_arr, small_key_arr}, -// num_small_rows / num_batches); -// auto small_batches = -// BatchesWithSchema{std::vector(num_batches, small_batch), -// small_schema}; - -// // auto large_key0_arr = RandomArrayGenerator(seed).Int64( -// // num_large_rows / num_batches, key_min, key_max, key_null_probability); -// // auto large_key1_arr = RandomArrayGenerator(seed).Int64( -// // num_large_rows / num_batches, key_min, key_max, key_null_probability); -// // auto large_key2_arr = RandomArrayGenerator(seed).Int64( -// // num_large_rows / num_batches, key_min, key_max, key_null_probability); -// auto large_payload_arr = RandomArrayGenerator(seed).Int64( -// num_large_rows / num_batches, payload_min, payload_max, -// payload_null_probability); -// ExecBatch large_batch({large_key_arr, large_key_arr, large_key_arr, -// large_payload_arr}, -// num_large_rows / num_batches); -// auto large_batches = -// BatchesWithSchema{std::vector(num_batches, large_batch), -// large_schema}; - -// { -// Declaration small_source{ -// "exec_batch_source", -// ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; -// Declaration large_source{ -// "exec_batch_source", -// ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; - -// HashJoinNodeOptions join_opts(JoinType::RIGHT_OUTER, -// /*left_keys=*/{"key0", "key1", "key2"}, -// /*right_keys=*/{"key0", "key1", "key2"}); -// Declaration join{ -// "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; - -// AggregateNodeOptions agg_opts{/*aggregates=*/{ -// {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; -// Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; - -// auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); -// std::cout << result->ToString() << std::endl; -// } -// { -// Declaration small_source{ -// "exec_batch_source", -// ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; -// Declaration large_source{ -// "exec_batch_source", -// ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; - -// HashJoinNodeOptions join_opts(JoinType::LEFT_OUTER, -// /*left_keys=*/{"key0", "key1", "key2"}, -// /*right_keys=*/{"key0", "key1", "key2"}); -// Declaration join{ -// "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; - -// AggregateNodeOptions agg_opts{/*aggregates=*/{ -// {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; -// Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; - -// auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); -// std::cout << result->ToString() << std::endl; -// } - -// // return 0; -// } +TEST(HashJoin, GH44513) { + const int64_t num_large_rows = 360449051; + // const int64_t num_large_rows = 18201475; + const int64_t num_batches = 8; + const int64_t seed = 42; + + auto small_schema = + schema({field("key0", int64()), field("key1", int64()), field("key2", int64())}); + auto large_schema = schema({field("key0", int64()), field("key1", int64()), + field("key2", int64()), field("payload", int64())}); + + const int64_t key0_match = 88506230299; + const int64_t key1_match = 16556030299; + const int64_t key2_match = 11240299; + const int64_t payload_match = 42; + + ASSERT_OK_AND_ASSIGN(auto small_key0_arr, + Constant(MakeScalar(key0_match))->Generate(1)); + ASSERT_OK_AND_ASSIGN(auto small_key1_arr, + Constant(MakeScalar(key1_match))->Generate(1)); + ASSERT_OK_AND_ASSIGN(auto small_key2_arr, + Constant(MakeScalar(key2_match))->Generate(1)); + ExecBatch small_batch({small_key0_arr, small_key1_arr, small_key2_arr}, 1); + + auto large_unmatch_key_arr = + RandomArrayGenerator(seed).Int64(num_large_rows / num_batches, 0, key0_match - 1); + ASSERT_OK_AND_ASSIGN(auto large_unmatch_payload_arr, + MakeArrayOfNull(int64(), num_large_rows / num_batches)); + ExecBatch large_unmatch_batch({large_unmatch_key_arr, large_unmatch_key_arr, + large_unmatch_key_arr, large_unmatch_payload_arr}, + num_large_rows / num_batches); + + auto large_match_key0_arr = small_key0_arr; + auto large_match_key1_arr = small_key1_arr; + auto large_match_key2_arr = small_key2_arr; + ASSERT_OK_AND_ASSIGN(auto large_match_payload_arr, + Constant(MakeScalar(payload_match))->Generate(1)); + ExecBatch large_match_batch({large_match_key0_arr, large_match_key1_arr, + large_match_key2_arr, large_match_payload_arr}, + 1); + + auto small_batches = + BatchesWithSchema{std::vector{small_batch}, small_schema}; + auto large_batches = BatchesWithSchema{ + std::vector(num_batches, large_unmatch_batch), large_schema}; + large_batches.batches.push_back(large_match_batch); + + { + Declaration small_source{ + "exec_batch_source", + ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; + Declaration large_source{ + "exec_batch_source", + ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; + + HashJoinNodeOptions join_opts(JoinType::INNER, + /*left_keys=*/{"key0", "key1", "key2"}, + /*right_keys=*/{"key0", "key1", "key2"}); + Declaration join{ + "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; + + AggregateNodeOptions agg_opts{/*aggregates=*/{ + {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; + Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; + + auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); + std::cout << result->ToString() << std::endl; + } + { + Declaration small_source{ + "exec_batch_source", + ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; + Declaration large_source{ + "exec_batch_source", + ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; + + HashJoinNodeOptions join_opts(JoinType::LEFT_OUTER, + /*left_keys=*/{"key0", "key1", "key2"}, + /*right_keys=*/{"key0", "key1", "key2"}); + Declaration join{ + "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; + + AggregateNodeOptions agg_opts{/*aggregates=*/{ + {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; + Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; + + auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); + std::cout << result->ToString() << std::endl; + } + + // return 0; +} diff --git a/cpp/src/arrow/compute/key_map_internal.cc b/cpp/src/arrow/compute/key_map_internal.cc index 6884ef42d41f..e027280c8763 100644 --- a/cpp/src/arrow/compute/key_map_internal.cc +++ b/cpp/src/arrow/compute/key_map_internal.cc @@ -256,9 +256,11 @@ void SwissTable::early_filter_imp(const int num_keys, const uint32_t* hashes, // Extract from hash: block index and stamp // uint32_t hash = hashes[i]; - uint32_t iblock = hash >> bits_shift_for_block_and_stamp_; + uint32_t iblock = block_id_and_stamp(hash); + // uint32_t iblock = hash >> bits_shift_for_block_and_stamp_; uint32_t stamp = iblock & stamp_mask; - iblock >>= bits_shift_for_stamp_; + iblock = block_id(iblock); + // iblock >>= bits_shift_for_stamp_; uint32_t num_block_bytes = num_groupid_bits + 8; const uint8_t* blockbase = @@ -395,8 +397,8 @@ bool SwissTable::find_next_stamp_match(const uint32_t hash, const uint32_t in_sl uint32_t* out_group_id) const { const uint64_t num_groupid_bits = num_groupid_bits_from_log_blocks(log_blocks_); constexpr uint64_t stamp_mask = 0x7f; - const int stamp = - static_cast((hash >> bits_shift_for_block_and_stamp_) & stamp_mask); + const int stamp = static_cast(block_id_and_stamp(hash) & stamp_mask); + // static_cast((hash >> bits_shift_for_block_and_stamp_) & stamp_mask); uint64_t start_slot_id = wrap_global_slot_id(in_slot_id); int match_found; int local_slot; @@ -656,12 +658,12 @@ Status SwissTable::grow_double() { int num_group_id_bits_after = num_groupid_bits_from_log_blocks(log_blocks_ + 1); uint64_t group_id_mask_before = ~0ULL >> (64 - num_group_id_bits_before); int log_blocks_after = log_blocks_ + 1; - int bits_shift_for_block_and_stamp_after = bits_hash_ - log_blocks_after - bits_stamp_; - int bits_shift_for_stamp_after = bits_stamp_; - if (bits_shift_for_block_and_stamp_after < 0) { - bits_shift_for_block_and_stamp_after = 0; - bits_shift_for_stamp_after = bits_hash_ - log_blocks_; - } + // int bits_shift_for_block_and_stamp_after = bits_hash_ - log_blocks_after - + // bits_stamp_; int bits_shift_for_stamp_after = bits_stamp_; if + // (bits_shift_for_block_and_stamp_after < 0) { + // bits_shift_for_block_and_stamp_after = 0; + // bits_shift_for_stamp_after = bits_hash_ - log_blocks_after; + // } uint64_t block_size_before = (8 + num_group_id_bits_before); uint64_t block_size_after = (8 + num_group_id_bits_after); uint64_t block_size_total_after = (block_size_after << log_blocks_after) + padding_; @@ -704,7 +706,8 @@ Status SwissTable::grow_double() { } int ihalf = block_id_new & 1; - uint8_t stamp_new = (hash >> bits_shift_for_block_and_stamp_after) & stamp_mask; + uint8_t stamp_new = block_id_and_stamp(hash) & stamp_mask; + // uint8_t stamp_new = (hash >> bits_shift_for_block_and_stamp_after) & stamp_mask; uint64_t group_id_bit_offs = j * num_group_id_bits_before; uint64_t group_id = (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> @@ -746,7 +749,8 @@ Status SwissTable::grow_double() { (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> (group_id_bit_offs & 7)) & group_id_mask_before; - uint8_t stamp_new = (hash >> bits_shift_for_block_and_stamp_after) & stamp_mask; + uint8_t stamp_new = block_id_and_stamp(hash) & stamp_mask; + // uint8_t stamp_new = (hash >> bits_shift_for_block_and_stamp_after) & stamp_mask; uint8_t* block_base_new = blocks_new->mutable_data() + block_id_new * block_size_after; @@ -774,8 +778,8 @@ Status SwissTable::grow_double() { blocks_ = std::move(blocks_new); hashes_ = std::move(hashes_new_buffer); log_blocks_ = log_blocks_after; - bits_shift_for_block_and_stamp_ = bits_shift_for_block_and_stamp_after; - bits_shift_for_stamp_ = bits_shift_for_stamp_after; + // bits_shift_for_block_and_stamp_ = bits_shift_for_block_and_stamp_after; + // bits_shift_for_stamp_ = bits_shift_for_stamp_after; return Status::OK(); } @@ -823,8 +827,8 @@ void SwissTable::cleanup() { hashes_ = nullptr; } log_blocks_ = 0; - bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; - bits_shift_for_stamp_ = bits_stamp_; + // bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; + // bits_shift_for_stamp_ = bits_stamp_; num_inserted_ = 0; } diff --git a/cpp/src/arrow/compute/key_map_internal.h b/cpp/src/arrow/compute/key_map_internal.h index f152f00b4af0..78a1920ade81 100644 --- a/cpp/src/arrow/compute/key_map_internal.h +++ b/cpp/src/arrow/compute/key_map_internal.h @@ -203,6 +203,22 @@ class ARROW_EXPORT SwissTable { // Resize large hash tables when 75% full. Status grow_double(); + uint32_t block_id_and_stamp(uint32_t hash) const { + if (ARROW_PREDICT_FALSE(log_blocks_ + bits_stamp_ >= bits_hash_)) { + return hash; + } + return hash >> (bits_hash_ - log_blocks_ - bits_stamp_); + } + + uint32_t block_id(uint32_t block_id_and_stamp) const { + if (ARROW_PREDICT_FALSE(log_blocks_ + bits_stamp_ >= bits_hash_)) { + return block_id_and_stamp >> (bits_hash_ - log_blocks_); + // return block_id_and_stamp >> (bits_stamp_ - (log_blocks_ + bits_stamp_ - + // bits_hash_)); + } + return block_id_and_stamp >> bits_stamp_; + } + // Number of hash bits stored in slots in a block. // The highest bits of hash determine block id. // The next set of highest bits is a "stamp" stored in a slot in a block. @@ -214,8 +230,8 @@ class ARROW_EXPORT SwissTable { int log_minibatch_; // Base 2 log of the number of blocks int log_blocks_ = 0; - int bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; - int bits_shift_for_stamp_ = bits_stamp_; + // int bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; + // int bits_shift_for_stamp_ = bits_stamp_; // Number of keys inserted into hash table uint32_t num_inserted_ = 0; @@ -273,7 +289,8 @@ void SwissTable::insert_into_empty_slot(uint32_t slot_id, uint32_t hash, constexpr uint64_t stamp_mask = 0x7f; int start_slot = (slot_id & 7); - int stamp = static_cast((hash >> bits_shift_for_block_and_stamp_) & stamp_mask); + int stamp = static_cast(block_id_and_stamp(hash) & stamp_mask); + // int stamp = static_cast((hash >> bits_shift_for_block_and_stamp_) & stamp_mask); uint64_t block_id = slot_id >> 3; uint8_t* blockbase = blocks_->mutable_data() + num_block_bytes * block_id; diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index b0e07543fa8c..9842afb569e4 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -45,10 +45,12 @@ int SwissTable::early_filter_imp_avx2_x8(const int num_hashes, const uint32_t* h // Calculate block index and hash stamp for a byte in a block // __m256i vhash = _mm256_loadu_si256(vhash_ptr + i); - __m256i vblock_id = - _mm256_srlv_epi32(vhash, _mm256_set1_epi32(bits_shift_for_block_and_stamp_)); + __m256i vblock_id = _mm256_srlv_epi32( + vhash, _mm256_set1_epi32(bits_hash_ - bits_stamp_ - log_blocks_)); + // _mm256_srlv_epi32(vhash, _mm256_set1_epi32(bits_shift_for_block_and_stamp_)); __m256i vstamp = _mm256_and_si256(vblock_id, vstamp_mask); - vblock_id = _mm256_srli_epi32(vblock_id, bits_shift_for_stamp_); + vblock_id = _mm256_srli_epi32(vblock_id, bits_stamp_); + // vblock_id = _mm256_srli_epi32(vblock_id, bits_shift_for_stamp_); // We now split inputs and process 4 at a time, // in order to process 64-bit blocks From 289494f3e749a797152a3a251c3c246d336295d2 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Wed, 25 Dec 2024 03:34:27 +0800 Subject: [PATCH 11/25] Fix done --- cpp/src/arrow/compute/key_map_internal.cc | 51 ++++++++++++++--------- cpp/src/arrow/compute/key_map_internal.h | 38 ++++++++--------- 2 files changed, 50 insertions(+), 39 deletions(-) diff --git a/cpp/src/arrow/compute/key_map_internal.cc b/cpp/src/arrow/compute/key_map_internal.cc index e027280c8763..09d2248e3acf 100644 --- a/cpp/src/arrow/compute/key_map_internal.cc +++ b/cpp/src/arrow/compute/key_map_internal.cc @@ -256,11 +256,11 @@ void SwissTable::early_filter_imp(const int num_keys, const uint32_t* hashes, // Extract from hash: block index and stamp // uint32_t hash = hashes[i]; - uint32_t iblock = block_id_and_stamp(hash); - // uint32_t iblock = hash >> bits_shift_for_block_and_stamp_; + // uint32_t iblock = block_id_and_stamp(hash, log_blocks_); + uint32_t iblock = hash >> bits_shift_for_block_and_stamp_; uint32_t stamp = iblock & stamp_mask; - iblock = block_id(iblock); - // iblock >>= bits_shift_for_stamp_; + // iblock = block_id(iblock, log_blocks_); + iblock >>= bits_shift_for_block_; uint32_t num_block_bytes = num_groupid_bits + 8; const uint8_t* blockbase = @@ -397,8 +397,10 @@ bool SwissTable::find_next_stamp_match(const uint32_t hash, const uint32_t in_sl uint32_t* out_group_id) const { const uint64_t num_groupid_bits = num_groupid_bits_from_log_blocks(log_blocks_); constexpr uint64_t stamp_mask = 0x7f; - const int stamp = static_cast(block_id_and_stamp(hash) & stamp_mask); - // static_cast((hash >> bits_shift_for_block_and_stamp_) & stamp_mask); + // const int stamp = static_cast(block_id_and_stamp(hash, log_blocks_) & + // stamp_mask); + const int stamp = + static_cast((hash >> bits_shift_for_block_and_stamp_) & stamp_mask); uint64_t start_slot_id = wrap_global_slot_id(in_slot_id); int match_found; int local_slot; @@ -658,12 +660,12 @@ Status SwissTable::grow_double() { int num_group_id_bits_after = num_groupid_bits_from_log_blocks(log_blocks_ + 1); uint64_t group_id_mask_before = ~0ULL >> (64 - num_group_id_bits_before); int log_blocks_after = log_blocks_ + 1; - // int bits_shift_for_block_and_stamp_after = bits_hash_ - log_blocks_after - - // bits_stamp_; int bits_shift_for_stamp_after = bits_stamp_; if - // (bits_shift_for_block_and_stamp_after < 0) { - // bits_shift_for_block_and_stamp_after = 0; - // bits_shift_for_stamp_after = bits_hash_ - log_blocks_after; - // } + int bits_shift_for_block_and_stamp_after = bits_hash_ - log_blocks_after - bits_stamp_; + int bits_shift_for_block_after = bits_stamp_; + if (bits_shift_for_block_and_stamp_after < 0) { + bits_shift_for_block_and_stamp_after = 0; + bits_shift_for_block_after = bits_hash_ - log_blocks_after; + } uint64_t block_size_before = (8 + num_group_id_bits_before); uint64_t block_size_after = (8 + num_group_id_bits_after); uint64_t block_size_total_after = (block_size_after << log_blocks_after) + padding_; @@ -706,8 +708,8 @@ Status SwissTable::grow_double() { } int ihalf = block_id_new & 1; - uint8_t stamp_new = block_id_and_stamp(hash) & stamp_mask; - // uint8_t stamp_new = (hash >> bits_shift_for_block_and_stamp_after) & stamp_mask; + // uint8_t stamp_new = block_id_and_stamp(hash, log_blocks_after) & stamp_mask; + uint8_t stamp_new = (hash >> bits_shift_for_block_and_stamp_after) & stamp_mask; uint64_t group_id_bit_offs = j * num_group_id_bits_before; uint64_t group_id = (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> @@ -749,8 +751,8 @@ Status SwissTable::grow_double() { (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> (group_id_bit_offs & 7)) & group_id_mask_before; - uint8_t stamp_new = block_id_and_stamp(hash) & stamp_mask; - // uint8_t stamp_new = (hash >> bits_shift_for_block_and_stamp_after) & stamp_mask; + // uint8_t stamp_new = block_id_and_stamp(hash, log_blocks_after) & stamp_mask; + uint8_t stamp_new = (hash >> bits_shift_for_block_and_stamp_after) & stamp_mask; uint8_t* block_base_new = blocks_new->mutable_data() + block_id_new * block_size_after; @@ -778,8 +780,8 @@ Status SwissTable::grow_double() { blocks_ = std::move(blocks_new); hashes_ = std::move(hashes_new_buffer); log_blocks_ = log_blocks_after; - // bits_shift_for_block_and_stamp_ = bits_shift_for_block_and_stamp_after; - // bits_shift_for_stamp_ = bits_shift_for_stamp_after; + bits_shift_for_block_and_stamp_ = bits_shift_for_block_and_stamp_after; + bits_shift_for_block_ = bits_shift_for_block_after; return Status::OK(); } @@ -791,6 +793,15 @@ Status SwissTable::init(int64_t hardware_flags, MemoryPool* pool, int log_blocks log_minibatch_ = util::MiniBatch::kLogMiniBatchLength; log_blocks_ = log_blocks; + if (log_blocks_ + bits_stamp_ > bits_hash_) { + bits_shift_for_block_and_stamp_ = 0; + bits_shift_for_block_ = bits_hash_ - log_blocks_; + // bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; + // bits_shift_for_block_ = bits_stamp_; + } else { + bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; + bits_shift_for_block_ = bits_stamp_; + } int num_groupid_bits = num_groupid_bits_from_log_blocks(log_blocks_); num_inserted_ = 0; @@ -827,8 +838,8 @@ void SwissTable::cleanup() { hashes_ = nullptr; } log_blocks_ = 0; - // bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; - // bits_shift_for_stamp_ = bits_stamp_; + bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; + bits_shift_for_block_ = bits_stamp_; num_inserted_ = 0; } diff --git a/cpp/src/arrow/compute/key_map_internal.h b/cpp/src/arrow/compute/key_map_internal.h index 78a1920ade81..1c677c62bcc4 100644 --- a/cpp/src/arrow/compute/key_map_internal.h +++ b/cpp/src/arrow/compute/key_map_internal.h @@ -203,21 +203,21 @@ class ARROW_EXPORT SwissTable { // Resize large hash tables when 75% full. Status grow_double(); - uint32_t block_id_and_stamp(uint32_t hash) const { - if (ARROW_PREDICT_FALSE(log_blocks_ + bits_stamp_ >= bits_hash_)) { - return hash; - } - return hash >> (bits_hash_ - log_blocks_ - bits_stamp_); - } - - uint32_t block_id(uint32_t block_id_and_stamp) const { - if (ARROW_PREDICT_FALSE(log_blocks_ + bits_stamp_ >= bits_hash_)) { - return block_id_and_stamp >> (bits_hash_ - log_blocks_); - // return block_id_and_stamp >> (bits_stamp_ - (log_blocks_ + bits_stamp_ - - // bits_hash_)); - } - return block_id_and_stamp >> bits_stamp_; - } + // static uint32_t block_id_and_stamp(uint32_t hash, int log_blocks) { + // if (ARROW_PREDICT_FALSE(log_blocks + bits_stamp_ >= bits_hash_)) { + // return hash; + // } + // return hash >> (bits_hash_ - log_blocks - bits_stamp_); + // } + + // static uint32_t block_id(uint32_t block_id_and_stamp, int log_blocks) { + // if (ARROW_PREDICT_FALSE(log_blocks + bits_stamp_ >= bits_hash_)) { + // return block_id_and_stamp >> (bits_hash_ - log_blocks); + // // return block_id_and_stamp >> (bits_stamp_ - (log_blocks_ + bits_stamp_ - + // // bits_hash_)); + // } + // return block_id_and_stamp >> bits_stamp_; + // } // Number of hash bits stored in slots in a block. // The highest bits of hash determine block id. @@ -230,8 +230,8 @@ class ARROW_EXPORT SwissTable { int log_minibatch_; // Base 2 log of the number of blocks int log_blocks_ = 0; - // int bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; - // int bits_shift_for_stamp_ = bits_stamp_; + int bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; + int bits_shift_for_block_ = bits_stamp_; // Number of keys inserted into hash table uint32_t num_inserted_ = 0; @@ -289,8 +289,8 @@ void SwissTable::insert_into_empty_slot(uint32_t slot_id, uint32_t hash, constexpr uint64_t stamp_mask = 0x7f; int start_slot = (slot_id & 7); - int stamp = static_cast(block_id_and_stamp(hash) & stamp_mask); - // int stamp = static_cast((hash >> bits_shift_for_block_and_stamp_) & stamp_mask); + // int stamp = static_cast(block_id_and_stamp(hash, log_blocks_) & stamp_mask); + int stamp = static_cast((hash >> bits_shift_for_block_and_stamp_) & stamp_mask); uint64_t block_id = slot_id >> 3; uint8_t* blockbase = blocks_->mutable_data() + num_block_bytes * block_id; From 936b72a823e9c0fdf9cc90f0ff327414f1365579 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Wed, 25 Dec 2024 12:03:45 +0800 Subject: [PATCH 12/25] Fix avx2 path index overflow --- .../arrow/compute/key_map_internal_avx2.cc | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index 9842afb569e4..cba219609ea2 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -394,16 +394,28 @@ int SwissTable::extract_group_ids_avx2(const int num_keys, const uint32_t* hashe } else { for (int i = 0; i < num_keys / unroll; ++i) { __m256i hash = _mm256_loadu_si256(reinterpret_cast(hashes) + i); + __m256i hash_lo = _mm256_cvtepi32_epi64(_mm256_castsi256_si128(hash)); + __m256i hash_hi = _mm256_cvtepi32_epi64(_mm256_extracti128_si256(hash, 1)); __m256i local_slot = _mm256_set1_epi64x(reinterpret_cast(local_slots)[i]); local_slot = _mm256_shuffle_epi8( local_slot, _mm256_setr_epi32(0x80808000, 0x80808001, 0x80808002, 0x80808003, 0x80808004, 0x80808005, 0x80808006, 0x80808007)); local_slot = _mm256_mullo_epi32(local_slot, _mm256_set1_epi32(byte_size)); - __m256i pos = _mm256_srlv_epi32(hash, _mm256_set1_epi32(bits_hash_ - log_blocks_)); - pos = _mm256_mullo_epi32(pos, _mm256_set1_epi32(byte_multiplier)); - pos = _mm256_add_epi32(pos, local_slot); - __m256i group_id = _mm256_i32gather_epi32(elements, pos, 1); + __m256i local_slot_lo = _mm256_cvtepi32_epi64(_mm256_castsi256_si128(local_slot)); + __m256i local_slot_hi = + _mm256_cvtepi32_epi64(_mm256_extracti128_si256(local_slot, 1)); + __m256i pos_lo = + _mm256_srlv_epi64(hash_lo, _mm256_set1_epi64x(bits_hash_ - log_blocks_)); + __m256i pos_hi = + _mm256_srlv_epi64(hash_hi, _mm256_set1_epi64x(bits_hash_ - log_blocks_)); + pos_lo = _mm256_mul_epi32(pos_lo, _mm256_set1_epi32(byte_multiplier)); + pos_hi = _mm256_mul_epi32(pos_hi, _mm256_set1_epi32(byte_multiplier)); + pos_lo = _mm256_add_epi64(pos_lo, local_slot_lo); + pos_hi = _mm256_add_epi64(pos_hi, local_slot_hi); + __m128i group_id_lo = _mm256_i64gather_epi32(elements, pos_lo, 1); + __m128i group_id_hi = _mm256_i64gather_epi32(elements, pos_hi, 1); + __m256i group_id = _mm256_set_m128i(group_id_lo, group_id_hi); group_id = _mm256_and_si256(group_id, _mm256_set1_epi32(mask)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(out_group_ids) + i, group_id); } From 71350f38f6aed5f800043e6010afb48bb9a6cc5b Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Wed, 25 Dec 2024 12:13:14 +0800 Subject: [PATCH 13/25] Fix avx2 path index overflow --- cpp/src/arrow/compute/key_map_internal_avx2.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index cba219609ea2..a26ba4858215 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -415,7 +415,7 @@ int SwissTable::extract_group_ids_avx2(const int num_keys, const uint32_t* hashe pos_hi = _mm256_add_epi64(pos_hi, local_slot_hi); __m128i group_id_lo = _mm256_i64gather_epi32(elements, pos_lo, 1); __m128i group_id_hi = _mm256_i64gather_epi32(elements, pos_hi, 1); - __m256i group_id = _mm256_set_m128i(group_id_lo, group_id_hi); + __m256i group_id = _mm256_set_m128i(group_id_hi, group_id_lo); group_id = _mm256_and_si256(group_id, _mm256_set1_epi32(mask)); _mm256_storeu_si256(reinterpret_cast<__m256i*>(out_group_ids) + i, group_id); } From 3ffedd55eb15df0667da995e0f974bea03c4b6bc Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Wed, 25 Dec 2024 12:31:47 +0800 Subject: [PATCH 14/25] Fix avx2 path index overflow --- cpp/src/arrow/compute/key_map_internal_avx2.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index a26ba4858215..8acb8e828d38 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -45,12 +45,13 @@ int SwissTable::early_filter_imp_avx2_x8(const int num_hashes, const uint32_t* h // Calculate block index and hash stamp for a byte in a block // __m256i vhash = _mm256_loadu_si256(vhash_ptr + i); - __m256i vblock_id = _mm256_srlv_epi32( - vhash, _mm256_set1_epi32(bits_hash_ - bits_stamp_ - log_blocks_)); - // _mm256_srlv_epi32(vhash, _mm256_set1_epi32(bits_shift_for_block_and_stamp_)); + // __m256i vblock_id = _mm256_srlv_epi32( + // vhash, _mm256_set1_epi32(bits_hash_ - bits_stamp_ - log_blocks_)); + __m256i vblock_id = + _mm256_srlv_epi32(vhash, _mm256_set1_epi32(bits_shift_for_block_and_stamp_)); __m256i vstamp = _mm256_and_si256(vblock_id, vstamp_mask); - vblock_id = _mm256_srli_epi32(vblock_id, bits_stamp_); - // vblock_id = _mm256_srli_epi32(vblock_id, bits_shift_for_stamp_); + // vblock_id = _mm256_srli_epi32(vblock_id, bits_stamp_); + vblock_id = _mm256_srli_epi32(vblock_id, bits_shift_for_block_); // We now split inputs and process 4 at a time, // in order to process 64-bit blocks From 13b69a760676770b66e73b309f298ef616040946 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Wed, 25 Dec 2024 12:57:13 +0800 Subject: [PATCH 15/25] Fix sign-extend for hash 32 to 64 --- cpp/src/arrow/compute/key_map_internal_avx2.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index 8acb8e828d38..43cde91f6827 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -395,8 +395,9 @@ int SwissTable::extract_group_ids_avx2(const int num_keys, const uint32_t* hashe } else { for (int i = 0; i < num_keys / unroll; ++i) { __m256i hash = _mm256_loadu_si256(reinterpret_cast(hashes) + i); - __m256i hash_lo = _mm256_cvtepi32_epi64(_mm256_castsi256_si128(hash)); - __m256i hash_hi = _mm256_cvtepi32_epi64(_mm256_extracti128_si256(hash, 1)); + // NB: Use zero-extend conversion for unsigned hash. + __m256i hash_lo = _mm256_cvtepu32_epi64(_mm256_castsi256_si128(hash)); + __m256i hash_hi = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(hash, 1)); __m256i local_slot = _mm256_set1_epi64x(reinterpret_cast(local_slots)[i]); local_slot = _mm256_shuffle_epi8( From af07e9bf18f812b8f751d39678ba80765cc0e273 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Wed, 25 Dec 2024 13:24:17 +0800 Subject: [PATCH 16/25] Update test --- cpp/src/arrow/acero/hash_join_node_test.cc | 48 +++++++++++----------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/cpp/src/arrow/acero/hash_join_node_test.cc b/cpp/src/arrow/acero/hash_join_node_test.cc index e2ea08726929..001602698f15 100644 --- a/cpp/src/arrow/acero/hash_join_node_test.cc +++ b/cpp/src/arrow/acero/hash_join_node_test.cc @@ -3562,7 +3562,7 @@ TEST(HashJoin, GH44513_Old) { } TEST(HashJoin, GH44513) { - const int64_t num_large_rows = 360449051; + const int64_t num_large_rows = 360449051 * 4; // const int64_t num_large_rows = 18201475; const int64_t num_batches = 8; const int64_t seed = 42; @@ -3585,8 +3585,8 @@ TEST(HashJoin, GH44513) { Constant(MakeScalar(key2_match))->Generate(1)); ExecBatch small_batch({small_key0_arr, small_key1_arr, small_key2_arr}, 1); - auto large_unmatch_key_arr = - RandomArrayGenerator(seed).Int64(num_large_rows / num_batches, 0, key0_match - 1); + auto large_unmatch_key_arr = RandomArrayGenerator(seed).Int64( + num_large_rows / num_batches, key0_match + 1, 99756520299); ASSERT_OK_AND_ASSIGN(auto large_unmatch_payload_arr, MakeArrayOfNull(int64(), num_large_rows / num_batches)); ExecBatch large_unmatch_batch({large_unmatch_key_arr, large_unmatch_key_arr, @@ -3608,6 +3608,27 @@ TEST(HashJoin, GH44513) { std::vector(num_batches, large_unmatch_batch), large_schema}; large_batches.batches.push_back(large_match_batch); + // { + // Declaration small_source{ + // "exec_batch_source", + // ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; + // Declaration large_source{ + // "exec_batch_source", + // ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; + + // HashJoinNodeOptions join_opts(JoinType::INNER, + // /*left_keys=*/{"key0", "key1", "key2"}, + // /*right_keys=*/{"key0", "key1", "key2"}); + // Declaration join{ + // "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; + + // AggregateNodeOptions agg_opts{/*aggregates=*/{ + // {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; + // Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; + + // auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); + // std::cout << result->ToString() << std::endl; + // } { Declaration small_source{ "exec_batch_source", @@ -3619,27 +3640,6 @@ TEST(HashJoin, GH44513) { HashJoinNodeOptions join_opts(JoinType::INNER, /*left_keys=*/{"key0", "key1", "key2"}, /*right_keys=*/{"key0", "key1", "key2"}); - Declaration join{ - "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; - - AggregateNodeOptions agg_opts{/*aggregates=*/{ - {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; - Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; - - auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); - std::cout << result->ToString() << std::endl; - } - { - Declaration small_source{ - "exec_batch_source", - ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; - Declaration large_source{ - "exec_batch_source", - ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; - - HashJoinNodeOptions join_opts(JoinType::LEFT_OUTER, - /*left_keys=*/{"key0", "key1", "key2"}, - /*right_keys=*/{"key0", "key1", "key2"}); Declaration join{ "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; From fe35443be7bdb1418a6a052c8c14c31b06879177 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Fri, 27 Dec 2024 18:55:51 +0800 Subject: [PATCH 17/25] Some cleanup --- cpp/src/arrow/acero/hash_join_node_test.cc | 205 ------------------ cpp/src/arrow/compute/key_map_internal.cc | 14 +- cpp/src/arrow/compute/key_map_internal.h | 17 -- .../arrow/compute/key_map_internal_avx2.cc | 3 - 4 files changed, 2 insertions(+), 237 deletions(-) diff --git a/cpp/src/arrow/acero/hash_join_node_test.cc b/cpp/src/arrow/acero/hash_join_node_test.cc index 001602698f15..76ad9c7d650e 100644 --- a/cpp/src/arrow/acero/hash_join_node_test.cc +++ b/cpp/src/arrow/acero/hash_join_node_test.cc @@ -3448,208 +3448,3 @@ TEST(HashJoin, LARGE_MEMORY_TEST(BuildSideOver4GBVarLength)) { } // namespace acero } // namespace arrow - -using namespace arrow; -using namespace arrow::acero; - -TEST(HashJoin, GH44513_Old) { - // int main() { - const int64_t num_small_rows = 18201475; - const int64_t num_large_rows = 360449051; - const int64_t num_batches = 8; - const int64_t seed = 42; - - auto small_schema = - schema({field("c0", int64()), field("key0", int64()), field("key1", int64()), - field("c1", int64()), field("c2", int64()), field("key2", int64()), - field("c3", int64()), field("c4", int64()), field("c5", int64())}); - auto large_schema = schema({field("key0", int64()), field("key1", int64()), - field("key2", int64()), field("payload", int64())}); - - const int64_t small_key_min = 28487150299; - const int64_t small_key_max = 98486720299; - const double small_key_null_probability = 0; - auto small_key_arr = - RandomArrayGenerator(seed).Int64(num_small_rows / num_batches, small_key_min, - small_key_max, small_key_null_probability); - - // const int64_t num_matching_rows = 17060116; - // const double matching_rate = double(num_matching_rows) / double(num_small_rows) / - // (double(num_small_rows) / double(small_key_max)); - const int64_t large_key_min = 2018570299; - const int64_t large_key_max = 99756520299; - // const int64_t large_key_min = small_key_max * (1 - matching_rate); - // const int64_t large_key_max = 391014 + large_key_min; - const double large_key_null_probability = 0; - auto large_key_arr = - RandomArrayGenerator(seed).Int64(num_large_rows / num_batches, large_key_min, - large_key_max, large_key_null_probability); - - const int64_t payload_min = 0; - const int64_t payload_max = 96920; - const double payload_null_probability = 283328672.0 / num_large_rows; - - // auto small_key0_arr = RandomArrayGenerator(seed).Int64( - // num_small_rows / num_batches, key_min, key_max, key_null_probability); - // auto small_key1_arr = RandomArrayGenerator(seed).Int64( - // num_small_rows / num_batches, key_min, key_max, key_null_probability); - // auto small_key2_arr = RandomArrayGenerator(seed).Int64( - // num_small_rows / num_batches, key_min, key_max, key_null_probability); - ExecBatch small_batch( - {small_key_arr, small_key_arr, small_key_arr, small_key_arr, small_key_arr, - small_key_arr, small_key_arr, small_key_arr, small_key_arr}, - num_small_rows / num_batches); - auto small_batches = - BatchesWithSchema{std::vector(num_batches, small_batch), small_schema}; - - // auto large_key0_arr = RandomArrayGenerator(seed).Int64( - // num_large_rows / num_batches, key_min, key_max, key_null_probability); - // auto large_key1_arr = RandomArrayGenerator(seed).Int64( - // num_large_rows / num_batches, key_min, key_max, key_null_probability); - // auto large_key2_arr = RandomArrayGenerator(seed).Int64( - // num_large_rows / num_batches, key_min, key_max, key_null_probability); - auto large_payload_arr = RandomArrayGenerator(seed).Int64( - num_large_rows / num_batches, payload_min, payload_max, payload_null_probability); - ExecBatch large_batch({large_key_arr, large_key_arr, large_key_arr, large_payload_arr}, - num_large_rows / num_batches); - auto large_batches = - BatchesWithSchema{std::vector(num_batches, large_batch), large_schema}; - - { - Declaration small_source{ - "exec_batch_source", - ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; - Declaration large_source{ - "exec_batch_source", - ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; - - HashJoinNodeOptions join_opts(JoinType::RIGHT_OUTER, - /*left_keys=*/{"key0", "key1", "key2"}, - /*right_keys=*/{"key0", "key1", "key2"}); - Declaration join{ - "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; - - AggregateNodeOptions agg_opts{/*aggregates=*/{ - {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; - Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; - - auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); - std::cout << result->ToString() << std::endl; - } - { - Declaration small_source{ - "exec_batch_source", - ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; - Declaration large_source{ - "exec_batch_source", - ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; - - HashJoinNodeOptions join_opts(JoinType::LEFT_OUTER, - /*left_keys=*/{"key0", "key1", "key2"}, - /*right_keys=*/{"key0", "key1", "key2"}); - Declaration join{ - "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; - - AggregateNodeOptions agg_opts{/*aggregates=*/{ - {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; - Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; - - auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); - std::cout << result->ToString() << std::endl; - } - - // return 0; -} - -TEST(HashJoin, GH44513) { - const int64_t num_large_rows = 360449051 * 4; - // const int64_t num_large_rows = 18201475; - const int64_t num_batches = 8; - const int64_t seed = 42; - - auto small_schema = - schema({field("key0", int64()), field("key1", int64()), field("key2", int64())}); - auto large_schema = schema({field("key0", int64()), field("key1", int64()), - field("key2", int64()), field("payload", int64())}); - - const int64_t key0_match = 88506230299; - const int64_t key1_match = 16556030299; - const int64_t key2_match = 11240299; - const int64_t payload_match = 42; - - ASSERT_OK_AND_ASSIGN(auto small_key0_arr, - Constant(MakeScalar(key0_match))->Generate(1)); - ASSERT_OK_AND_ASSIGN(auto small_key1_arr, - Constant(MakeScalar(key1_match))->Generate(1)); - ASSERT_OK_AND_ASSIGN(auto small_key2_arr, - Constant(MakeScalar(key2_match))->Generate(1)); - ExecBatch small_batch({small_key0_arr, small_key1_arr, small_key2_arr}, 1); - - auto large_unmatch_key_arr = RandomArrayGenerator(seed).Int64( - num_large_rows / num_batches, key0_match + 1, 99756520299); - ASSERT_OK_AND_ASSIGN(auto large_unmatch_payload_arr, - MakeArrayOfNull(int64(), num_large_rows / num_batches)); - ExecBatch large_unmatch_batch({large_unmatch_key_arr, large_unmatch_key_arr, - large_unmatch_key_arr, large_unmatch_payload_arr}, - num_large_rows / num_batches); - - auto large_match_key0_arr = small_key0_arr; - auto large_match_key1_arr = small_key1_arr; - auto large_match_key2_arr = small_key2_arr; - ASSERT_OK_AND_ASSIGN(auto large_match_payload_arr, - Constant(MakeScalar(payload_match))->Generate(1)); - ExecBatch large_match_batch({large_match_key0_arr, large_match_key1_arr, - large_match_key2_arr, large_match_payload_arr}, - 1); - - auto small_batches = - BatchesWithSchema{std::vector{small_batch}, small_schema}; - auto large_batches = BatchesWithSchema{ - std::vector(num_batches, large_unmatch_batch), large_schema}; - large_batches.batches.push_back(large_match_batch); - - // { - // Declaration small_source{ - // "exec_batch_source", - // ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; - // Declaration large_source{ - // "exec_batch_source", - // ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; - - // HashJoinNodeOptions join_opts(JoinType::INNER, - // /*left_keys=*/{"key0", "key1", "key2"}, - // /*right_keys=*/{"key0", "key1", "key2"}); - // Declaration join{ - // "hashjoin", {std::move(large_source), std::move(small_source)}, join_opts}; - - // AggregateNodeOptions agg_opts{/*aggregates=*/{ - // {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; - // Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; - - // auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); - // std::cout << result->ToString() << std::endl; - // } - { - Declaration small_source{ - "exec_batch_source", - ExecBatchSourceNodeOptions(small_batches.schema, small_batches.batches)}; - Declaration large_source{ - "exec_batch_source", - ExecBatchSourceNodeOptions(large_batches.schema, large_batches.batches)}; - - HashJoinNodeOptions join_opts(JoinType::INNER, - /*left_keys=*/{"key0", "key1", "key2"}, - /*right_keys=*/{"key0", "key1", "key2"}); - Declaration join{ - "hashjoin", {std::move(small_source), std::move(large_source)}, join_opts}; - - AggregateNodeOptions agg_opts{/*aggregates=*/{ - {"count_all", "count(*)"}, {"sum", nullptr, "payload", "sum(payload)"}}}; - Declaration agg{"aggregate", {std::move(join)}, std::move(agg_opts)}; - - auto result = DeclarationToTable(std::move(agg)).ValueOrDie(); - std::cout << result->ToString() << std::endl; - } - - // return 0; -} diff --git a/cpp/src/arrow/compute/key_map_internal.cc b/cpp/src/arrow/compute/key_map_internal.cc index 09d2248e3acf..9cfa907b0dbd 100644 --- a/cpp/src/arrow/compute/key_map_internal.cc +++ b/cpp/src/arrow/compute/key_map_internal.cc @@ -113,7 +113,6 @@ void SwissTable::extract_group_ids_imp(const int num_keys, const uint16_t* selec uint32_t hash = hashes[id]; int64_t pos = (hash >> (bits_hash_ - log_blocks_)) * element_multiplier + local_slots[id]; - ARROW_DCHECK(pos >= 0 && pos < 0xFFFFFFFF); uint32_t group_id = static_cast(elements[pos]); ARROW_DCHECK(group_id < num_inserted_ || num_inserted_ == 0); out_group_ids[id] = group_id; @@ -224,9 +223,8 @@ void SwissTable::init_slot_ids_for_new_keys(uint32_t num_ids, const uint16_t* id uint32_t iblock = hash >> (bits_hash_ - log_blocks_); uint64_t block; for (;;) { - int64_t pos = int64_t(num_block_bytes) * iblock; - DCHECK(pos >= 0 && pos <= std::numeric_limits::max()); - block = *reinterpret_cast(blocks_->mutable_data() + pos); + block = *reinterpret_cast(blocks_->mutable_data() + + num_block_bytes * iblock); block &= kHighBitOfEachByte; if (block) { break; @@ -256,10 +254,8 @@ void SwissTable::early_filter_imp(const int num_keys, const uint32_t* hashes, // Extract from hash: block index and stamp // uint32_t hash = hashes[i]; - // uint32_t iblock = block_id_and_stamp(hash, log_blocks_); uint32_t iblock = hash >> bits_shift_for_block_and_stamp_; uint32_t stamp = iblock & stamp_mask; - // iblock = block_id(iblock, log_blocks_); iblock >>= bits_shift_for_block_; uint32_t num_block_bytes = num_groupid_bits + 8; @@ -397,8 +393,6 @@ bool SwissTable::find_next_stamp_match(const uint32_t hash, const uint32_t in_sl uint32_t* out_group_id) const { const uint64_t num_groupid_bits = num_groupid_bits_from_log_blocks(log_blocks_); constexpr uint64_t stamp_mask = 0x7f; - // const int stamp = static_cast(block_id_and_stamp(hash, log_blocks_) & - // stamp_mask); const int stamp = static_cast((hash >> bits_shift_for_block_and_stamp_) & stamp_mask); uint64_t start_slot_id = wrap_global_slot_id(in_slot_id); @@ -708,7 +702,6 @@ Status SwissTable::grow_double() { } int ihalf = block_id_new & 1; - // uint8_t stamp_new = block_id_and_stamp(hash, log_blocks_after) & stamp_mask; uint8_t stamp_new = (hash >> bits_shift_for_block_and_stamp_after) & stamp_mask; uint64_t group_id_bit_offs = j * num_group_id_bits_before; uint64_t group_id = @@ -751,7 +744,6 @@ Status SwissTable::grow_double() { (util::SafeLoadAs(block_base + 8 + (group_id_bit_offs >> 3)) >> (group_id_bit_offs & 7)) & group_id_mask_before; - // uint8_t stamp_new = block_id_and_stamp(hash, log_blocks_after) & stamp_mask; uint8_t stamp_new = (hash >> bits_shift_for_block_and_stamp_after) & stamp_mask; uint8_t* block_base_new = @@ -796,8 +788,6 @@ Status SwissTable::init(int64_t hardware_flags, MemoryPool* pool, int log_blocks if (log_blocks_ + bits_stamp_ > bits_hash_) { bits_shift_for_block_and_stamp_ = 0; bits_shift_for_block_ = bits_hash_ - log_blocks_; - // bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; - // bits_shift_for_block_ = bits_stamp_; } else { bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; bits_shift_for_block_ = bits_stamp_; diff --git a/cpp/src/arrow/compute/key_map_internal.h b/cpp/src/arrow/compute/key_map_internal.h index 1c677c62bcc4..9d51b768695c 100644 --- a/cpp/src/arrow/compute/key_map_internal.h +++ b/cpp/src/arrow/compute/key_map_internal.h @@ -203,22 +203,6 @@ class ARROW_EXPORT SwissTable { // Resize large hash tables when 75% full. Status grow_double(); - // static uint32_t block_id_and_stamp(uint32_t hash, int log_blocks) { - // if (ARROW_PREDICT_FALSE(log_blocks + bits_stamp_ >= bits_hash_)) { - // return hash; - // } - // return hash >> (bits_hash_ - log_blocks - bits_stamp_); - // } - - // static uint32_t block_id(uint32_t block_id_and_stamp, int log_blocks) { - // if (ARROW_PREDICT_FALSE(log_blocks + bits_stamp_ >= bits_hash_)) { - // return block_id_and_stamp >> (bits_hash_ - log_blocks); - // // return block_id_and_stamp >> (bits_stamp_ - (log_blocks_ + bits_stamp_ - - // // bits_hash_)); - // } - // return block_id_and_stamp >> bits_stamp_; - // } - // Number of hash bits stored in slots in a block. // The highest bits of hash determine block id. // The next set of highest bits is a "stamp" stored in a slot in a block. @@ -289,7 +273,6 @@ void SwissTable::insert_into_empty_slot(uint32_t slot_id, uint32_t hash, constexpr uint64_t stamp_mask = 0x7f; int start_slot = (slot_id & 7); - // int stamp = static_cast(block_id_and_stamp(hash, log_blocks_) & stamp_mask); int stamp = static_cast((hash >> bits_shift_for_block_and_stamp_) & stamp_mask); uint64_t block_id = slot_id >> 3; uint8_t* blockbase = blocks_->mutable_data() + num_block_bytes * block_id; diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index 43cde91f6827..9649695a9a64 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -45,12 +45,9 @@ int SwissTable::early_filter_imp_avx2_x8(const int num_hashes, const uint32_t* h // Calculate block index and hash stamp for a byte in a block // __m256i vhash = _mm256_loadu_si256(vhash_ptr + i); - // __m256i vblock_id = _mm256_srlv_epi32( - // vhash, _mm256_set1_epi32(bits_hash_ - bits_stamp_ - log_blocks_)); __m256i vblock_id = _mm256_srlv_epi32(vhash, _mm256_set1_epi32(bits_shift_for_block_and_stamp_)); __m256i vstamp = _mm256_and_si256(vblock_id, vstamp_mask); - // vblock_id = _mm256_srli_epi32(vblock_id, bits_stamp_); vblock_id = _mm256_srli_epi32(vblock_id, bits_shift_for_block_); // We now split inputs and process 4 at a time, From 2913796a7df3afe9320849f98c935543541e3d65 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Fri, 27 Dec 2024 19:27:10 +0800 Subject: [PATCH 18/25] Comment about new variables --- cpp/src/arrow/compute/key_map_internal.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cpp/src/arrow/compute/key_map_internal.h b/cpp/src/arrow/compute/key_map_internal.h index 9d51b768695c..41ff8a1adc76 100644 --- a/cpp/src/arrow/compute/key_map_internal.h +++ b/cpp/src/arrow/compute/key_map_internal.h @@ -214,6 +214,12 @@ class ARROW_EXPORT SwissTable { int log_minibatch_; // Base 2 log of the number of blocks int log_blocks_ = 0; + // When log_blocks_ is greater than 25, there will be overlapping bits between block id + // and stamp within a 32-bit hash value. So we must check if this is the case when + // right-shifting a hash value to retrieve block id and stamp. The following two + // variables will be derived from log_blocks_ as log_blocks changes, and use them as the + // number of bits to right shift, rather than branching on whether log_blocks_ > 25 + // every time in tight loops. int bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; int bits_shift_for_block_ = bits_stamp_; // Number of keys inserted into hash table From 14a0a1decc968d504713ea9f2f6032966ff24a00 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Fri, 27 Dec 2024 22:07:19 +0800 Subject: [PATCH 19/25] More comments --- cpp/src/arrow/compute/key_map_internal_avx2.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index 9649695a9a64..0d1e1be3fc76 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -392,6 +392,9 @@ int SwissTable::extract_group_ids_avx2(const int num_keys, const uint32_t* hashe } else { for (int i = 0; i < num_keys / unroll; ++i) { __m256i hash = _mm256_loadu_si256(reinterpret_cast(hashes) + i); + // Extend hash and local_slot to 64-bit to compute 64-bit group id offsets to + // gather. + // This is to prevent index overflow issues in GH-44513. // NB: Use zero-extend conversion for unsigned hash. __m256i hash_lo = _mm256_cvtepu32_epi64(_mm256_castsi256_si128(hash)); __m256i hash_hi = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(hash, 1)); From c91468903161000dc6b81efbc89e318980cfd0cf Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Tue, 7 Jan 2025 08:51:24 +0800 Subject: [PATCH 20/25] Replace vector shifting with immediate number version --- .../arrow/compute/key_map_internal_avx2.cc | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index 0d1e1be3fc76..1decc107edfb 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -45,8 +45,7 @@ int SwissTable::early_filter_imp_avx2_x8(const int num_hashes, const uint32_t* h // Calculate block index and hash stamp for a byte in a block // __m256i vhash = _mm256_loadu_si256(vhash_ptr + i); - __m256i vblock_id = - _mm256_srlv_epi32(vhash, _mm256_set1_epi32(bits_shift_for_block_and_stamp_)); + __m256i vblock_id = _mm256_srli_epi32(vhash, bits_shift_for_block_and_stamp_); __m256i vstamp = _mm256_and_si256(vblock_id, vstamp_mask); vblock_id = _mm256_srli_epi32(vblock_id, bits_shift_for_block_); @@ -301,19 +300,15 @@ int SwissTable::early_filter_imp_avx2_x32(const int num_hashes, const uint32_t* _mm256_and_si256(vhash2, _mm256_set1_epi32(0xffff0000))); vhash1 = _mm256_or_si256(_mm256_srli_epi32(vhash1, 16), _mm256_and_si256(vhash3, _mm256_set1_epi32(0xffff0000))); - __m256i vstamp_A = _mm256_and_si256( - _mm256_srlv_epi32(vhash0, _mm256_set1_epi32(16 - log_blocks_ - 7)), - _mm256_set1_epi16(0x7f)); - __m256i vstamp_B = _mm256_and_si256( - _mm256_srlv_epi32(vhash1, _mm256_set1_epi32(16 - log_blocks_ - 7)), - _mm256_set1_epi16(0x7f)); + __m256i vstamp_A = _mm256_and_si256(_mm256_srli_epi32(vhash0, 16 - log_blocks_ - 7), + _mm256_set1_epi16(0x7f)); + __m256i vstamp_B = _mm256_and_si256(_mm256_srli_epi32(vhash1, 16 - log_blocks_ - 7), + _mm256_set1_epi16(0x7f)); __m256i vstamp = _mm256_or_si256(vstamp_A, _mm256_slli_epi16(vstamp_B, 8)); - __m256i vblock_id_A = - _mm256_and_si256(_mm256_srlv_epi32(vhash0, _mm256_set1_epi32(16 - log_blocks_)), - _mm256_set1_epi16(block_id_mask)); - __m256i vblock_id_B = - _mm256_and_si256(_mm256_srlv_epi32(vhash1, _mm256_set1_epi32(16 - log_blocks_)), - _mm256_set1_epi16(block_id_mask)); + __m256i vblock_id_A = _mm256_and_si256(_mm256_srli_epi32(vhash0, 16 - log_blocks_), + _mm256_set1_epi16(block_id_mask)); + __m256i vblock_id_B = _mm256_and_si256(_mm256_srli_epi32(vhash1, 16 - log_blocks_), + _mm256_set1_epi16(block_id_mask)); __m256i vblock_id = _mm256_or_si256(vblock_id_A, _mm256_slli_epi16(vblock_id_B, 8)); // Visit all block bytes in reverse order (overwriting data on multiple matches) @@ -407,10 +402,8 @@ int SwissTable::extract_group_ids_avx2(const int num_keys, const uint32_t* hashe __m256i local_slot_lo = _mm256_cvtepi32_epi64(_mm256_castsi256_si128(local_slot)); __m256i local_slot_hi = _mm256_cvtepi32_epi64(_mm256_extracti128_si256(local_slot, 1)); - __m256i pos_lo = - _mm256_srlv_epi64(hash_lo, _mm256_set1_epi64x(bits_hash_ - log_blocks_)); - __m256i pos_hi = - _mm256_srlv_epi64(hash_hi, _mm256_set1_epi64x(bits_hash_ - log_blocks_)); + __m256i pos_lo = _mm256_srli_epi64(hash_lo, bits_hash_ - log_blocks_); + __m256i pos_hi = _mm256_srli_epi64(hash_hi, bits_hash_ - log_blocks_); pos_lo = _mm256_mul_epi32(pos_lo, _mm256_set1_epi32(byte_multiplier)); pos_hi = _mm256_mul_epi32(pos_hi, _mm256_set1_epi32(byte_multiplier)); pos_lo = _mm256_add_epi64(pos_lo, local_slot_lo); From 86adc3dcc1970924f99fb8f296e599f9acdd00a7 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Tue, 7 Jan 2025 10:00:49 +0800 Subject: [PATCH 21/25] Directly shuffle 8 bytes to 8 64-bit lanes --- cpp/src/arrow/compute/key_map_internal_avx2.cc | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index 1decc107edfb..9933eaa7542a 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -395,13 +395,14 @@ int SwissTable::extract_group_ids_avx2(const int num_keys, const uint32_t* hashe __m256i hash_hi = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(hash, 1)); __m256i local_slot = _mm256_set1_epi64x(reinterpret_cast(local_slots)[i]); - local_slot = _mm256_shuffle_epi8( - local_slot, _mm256_setr_epi32(0x80808000, 0x80808001, 0x80808002, 0x80808003, - 0x80808004, 0x80808005, 0x80808006, 0x80808007)); - local_slot = _mm256_mullo_epi32(local_slot, _mm256_set1_epi32(byte_size)); - __m256i local_slot_lo = _mm256_cvtepi32_epi64(_mm256_castsi256_si128(local_slot)); - __m256i local_slot_hi = - _mm256_cvtepi32_epi64(_mm256_extracti128_si256(local_slot, 1)); + __m256i local_slot_lo = _mm256_shuffle_epi8( + local_slot, _mm256_setr_epi32(0x80808000, 0x80808080, 0x80808001, 0x80808080, + 0x80808002, 0x80808080, 0x80808003, 0x80808080)); + __m256i local_slot_hi = _mm256_shuffle_epi8( + local_slot, _mm256_setr_epi32(0x80808004, 0x80808080, 0x80808005, 0x80808080, + 0x80808006, 0x80808080, 0x80808007, 0x80808080)); + local_slot_lo = _mm256_mul_epi32(local_slot_lo, _mm256_set1_epi32(byte_size)); + local_slot_lo = _mm256_mul_epi32(local_slot_hi, _mm256_set1_epi32(byte_size)); __m256i pos_lo = _mm256_srli_epi64(hash_lo, bits_hash_ - log_blocks_); __m256i pos_hi = _mm256_srli_epi64(hash_hi, bits_hash_ - log_blocks_); pos_lo = _mm256_mul_epi32(pos_lo, _mm256_set1_epi32(byte_multiplier)); From 8762886d49aa688eceee2e95c6915992739935ff Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Tue, 7 Jan 2025 10:24:28 +0800 Subject: [PATCH 22/25] Refine comment --- cpp/src/arrow/compute/key_map_internal_avx2.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index 9933eaa7542a..e121a6394af9 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -388,8 +388,7 @@ int SwissTable::extract_group_ids_avx2(const int num_keys, const uint32_t* hashe for (int i = 0; i < num_keys / unroll; ++i) { __m256i hash = _mm256_loadu_si256(reinterpret_cast(hashes) + i); // Extend hash and local_slot to 64-bit to compute 64-bit group id offsets to - // gather. - // This is to prevent index overflow issues in GH-44513. + // gather from. This is to prevent index overflow issues in GH-44513. // NB: Use zero-extend conversion for unsigned hash. __m256i hash_lo = _mm256_cvtepu32_epi64(_mm256_castsi256_si128(hash)); __m256i hash_hi = _mm256_cvtepu32_epi64(_mm256_extracti128_si256(hash, 1)); From 37c39480035caba163a40866d8954ff8ebc5314e Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Tue, 7 Jan 2025 10:26:51 +0800 Subject: [PATCH 23/25] Factor out computation of bits to right shift into functions --- cpp/src/arrow/compute/key_map_internal.cc | 22 ++++++----------- cpp/src/arrow/compute/key_map_internal.h | 30 +++++++++++++++++------ 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/cpp/src/arrow/compute/key_map_internal.cc b/cpp/src/arrow/compute/key_map_internal.cc index 9cfa907b0dbd..cc7b99d0a952 100644 --- a/cpp/src/arrow/compute/key_map_internal.cc +++ b/cpp/src/arrow/compute/key_map_internal.cc @@ -654,12 +654,9 @@ Status SwissTable::grow_double() { int num_group_id_bits_after = num_groupid_bits_from_log_blocks(log_blocks_ + 1); uint64_t group_id_mask_before = ~0ULL >> (64 - num_group_id_bits_before); int log_blocks_after = log_blocks_ + 1; - int bits_shift_for_block_and_stamp_after = bits_hash_ - log_blocks_after - bits_stamp_; - int bits_shift_for_block_after = bits_stamp_; - if (bits_shift_for_block_and_stamp_after < 0) { - bits_shift_for_block_and_stamp_after = 0; - bits_shift_for_block_after = bits_hash_ - log_blocks_after; - } + int bits_shift_for_block_and_stamp_after = + ComputeBitsShiftForBlockAndStamp(log_blocks_after); + int bits_shift_for_block_after = ComputeBitsShiftForBlock(log_blocks_after); uint64_t block_size_before = (8 + num_group_id_bits_before); uint64_t block_size_after = (8 + num_group_id_bits_after); uint64_t block_size_total_after = (block_size_after << log_blocks_after) + padding_; @@ -785,13 +782,8 @@ Status SwissTable::init(int64_t hardware_flags, MemoryPool* pool, int log_blocks log_minibatch_ = util::MiniBatch::kLogMiniBatchLength; log_blocks_ = log_blocks; - if (log_blocks_ + bits_stamp_ > bits_hash_) { - bits_shift_for_block_and_stamp_ = 0; - bits_shift_for_block_ = bits_hash_ - log_blocks_; - } else { - bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; - bits_shift_for_block_ = bits_stamp_; - } + bits_shift_for_block_and_stamp_ = ComputeBitsShiftForBlockAndStamp(log_blocks_); + bits_shift_for_block_ = ComputeBitsShiftForBlock(log_blocks_); int num_groupid_bits = num_groupid_bits_from_log_blocks(log_blocks_); num_inserted_ = 0; @@ -828,8 +820,8 @@ void SwissTable::cleanup() { hashes_ = nullptr; } log_blocks_ = 0; - bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; - bits_shift_for_block_ = bits_stamp_; + bits_shift_for_block_and_stamp_ = ComputeBitsShiftForBlockAndStamp(log_blocks_); + bits_shift_for_block_ = ComputeBitsShiftForBlock(log_blocks_); num_inserted_ = 0; } diff --git a/cpp/src/arrow/compute/key_map_internal.h b/cpp/src/arrow/compute/key_map_internal.h index 41ff8a1adc76..66a9957006dd 100644 --- a/cpp/src/arrow/compute/key_map_internal.h +++ b/cpp/src/arrow/compute/key_map_internal.h @@ -203,6 +203,23 @@ class ARROW_EXPORT SwissTable { // Resize large hash tables when 75% full. Status grow_double(); + // When log_blocks is greater than 25, there will be overlapping bits between block id + // and stamp within a 32-bit hash value. So we must check if this is the case when + // right shifting a hash value to retrieve block id and stamp. The following two + // functions derive the number of bits to right shift from the given log_blocks. + static int ComputeBitsShiftForBlockAndStamp(int log_blocks) { + if (ARROW_PREDICT_FALSE(log_blocks + bits_stamp_ > bits_hash_)) { + return 0; + } + return bits_hash_ - log_blocks - bits_stamp_; + } + static int ComputeBitsShiftForBlock(int log_blocks) { + if (ARROW_PREDICT_FALSE(log_blocks + bits_stamp_ > bits_hash_)) { + return bits_hash_ - log_blocks; + } + return bits_stamp_; + } + // Number of hash bits stored in slots in a block. // The highest bits of hash determine block id. // The next set of highest bits is a "stamp" stored in a slot in a block. @@ -214,14 +231,11 @@ class ARROW_EXPORT SwissTable { int log_minibatch_; // Base 2 log of the number of blocks int log_blocks_ = 0; - // When log_blocks_ is greater than 25, there will be overlapping bits between block id - // and stamp within a 32-bit hash value. So we must check if this is the case when - // right-shifting a hash value to retrieve block id and stamp. The following two - // variables will be derived from log_blocks_ as log_blocks changes, and use them as the - // number of bits to right shift, rather than branching on whether log_blocks_ > 25 - // every time in tight loops. - int bits_shift_for_block_and_stamp_ = bits_hash_ - log_blocks_ - bits_stamp_; - int bits_shift_for_block_ = bits_stamp_; + // The following two variables are derived from log_blocks_ as log_blocks_ changes, and + // used in tight loops to avoid calling the ComputeXXX functions (introducing a + // branching on whether log_blocks_ + bits_stamp_ > bits_hash_). + int bits_shift_for_block_and_stamp_ = ComputeBitsShiftForBlockAndStamp(log_blocks_); + int bits_shift_for_block_ = ComputeBitsShiftForBlock(log_blocks_); // Number of keys inserted into hash table uint32_t num_inserted_ = 0; From 4462ceb7a464ab61319fa67c725561af67896bc9 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Tue, 7 Jan 2025 11:12:31 +0800 Subject: [PATCH 24/25] Change signed mulitply to unsigned --- cpp/src/arrow/compute/key_map_internal_avx2.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index e121a6394af9..9aee770c4109 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -400,12 +400,12 @@ int SwissTable::extract_group_ids_avx2(const int num_keys, const uint32_t* hashe __m256i local_slot_hi = _mm256_shuffle_epi8( local_slot, _mm256_setr_epi32(0x80808004, 0x80808080, 0x80808005, 0x80808080, 0x80808006, 0x80808080, 0x80808007, 0x80808080)); - local_slot_lo = _mm256_mul_epi32(local_slot_lo, _mm256_set1_epi32(byte_size)); - local_slot_lo = _mm256_mul_epi32(local_slot_hi, _mm256_set1_epi32(byte_size)); + local_slot_lo = _mm256_mul_epu32(local_slot_lo, _mm256_set1_epi32(byte_size)); + local_slot_lo = _mm256_mul_epu32(local_slot_hi, _mm256_set1_epi32(byte_size)); __m256i pos_lo = _mm256_srli_epi64(hash_lo, bits_hash_ - log_blocks_); __m256i pos_hi = _mm256_srli_epi64(hash_hi, bits_hash_ - log_blocks_); - pos_lo = _mm256_mul_epi32(pos_lo, _mm256_set1_epi32(byte_multiplier)); - pos_hi = _mm256_mul_epi32(pos_hi, _mm256_set1_epi32(byte_multiplier)); + pos_lo = _mm256_mul_epu32(pos_lo, _mm256_set1_epi32(byte_multiplier)); + pos_hi = _mm256_mul_epu32(pos_hi, _mm256_set1_epi32(byte_multiplier)); pos_lo = _mm256_add_epi64(pos_lo, local_slot_lo); pos_hi = _mm256_add_epi64(pos_hi, local_slot_hi); __m128i group_id_lo = _mm256_i64gather_epi32(elements, pos_lo, 1); From 74dbb6e249f7a2f1feabcacac0864750d88a0b41 Mon Sep 17 00:00:00 2001 From: Rossi Sun Date: Mon, 13 Jan 2025 21:00:22 +0800 Subject: [PATCH 25/25] Update cpp/src/arrow/compute/key_map_internal_avx2.cc Co-authored-by: Antoine Pitrou --- cpp/src/arrow/compute/key_map_internal_avx2.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/src/arrow/compute/key_map_internal_avx2.cc b/cpp/src/arrow/compute/key_map_internal_avx2.cc index 9aee770c4109..be54f7de6397 100644 --- a/cpp/src/arrow/compute/key_map_internal_avx2.cc +++ b/cpp/src/arrow/compute/key_map_internal_avx2.cc @@ -401,7 +401,7 @@ int SwissTable::extract_group_ids_avx2(const int num_keys, const uint32_t* hashe local_slot, _mm256_setr_epi32(0x80808004, 0x80808080, 0x80808005, 0x80808080, 0x80808006, 0x80808080, 0x80808007, 0x80808080)); local_slot_lo = _mm256_mul_epu32(local_slot_lo, _mm256_set1_epi32(byte_size)); - local_slot_lo = _mm256_mul_epu32(local_slot_hi, _mm256_set1_epi32(byte_size)); + local_slot_hi = _mm256_mul_epu32(local_slot_hi, _mm256_set1_epi32(byte_size)); __m256i pos_lo = _mm256_srli_epi64(hash_lo, bits_hash_ - log_blocks_); __m256i pos_hi = _mm256_srli_epi64(hash_hi, bits_hash_ - log_blocks_); pos_lo = _mm256_mul_epu32(pos_lo, _mm256_set1_epi32(byte_multiplier));