felipecrv commented on code in PR #40484:
URL: https://github.com/apache/arrow/pull/40484#discussion_r1543461934


##########
cpp/src/arrow/compute/key_hash.cc:
##########
@@ -378,20 +378,40 @@ void Hashing32::HashFixed(int64_t hardware_flags, bool 
combine_hashes, uint32_t
   }
 }
 
-void Hashing32::HashMultiColumn(const std::vector<KeyColumnArray>& cols,
-                                LightContext* ctx, uint32_t* hashes) {
-  uint32_t num_rows = static_cast<uint32_t>(cols[0].length());
-
-  constexpr uint32_t max_batch_size = util::MiniBatch::kMiniBatchLength;
+Status Hashing32::HashMultiColumn(const std::vector<KeyColumnArray>& cols,
+                                  LightContext* ctx, uint32_t* hashes) {
+  auto num_rows = static_cast<uint32_t>(cols[0].length());
+  // The max_batch_size represents the number of rows to be processed in each 
iteration,
+  // and it is used to allocate enough space for the allocated TempVectorStack.
+  const auto max_batch_size =
+      std::min(num_rows, 
static_cast<uint32_t>(util::MiniBatch::kMiniBatchLength));
+
+  // pre calculate alloc size in TempVectorStack for hash_temp_buf, 
null_hash_temp_buf
+  // and null_indices_buf
+  const auto alloc_hash_temp_buf =
+      util::TempVectorStack::EstimatedAllocationSize(max_batch_size * 
sizeof(uint32_t));
+  const auto alloc_for_null_indices_buf =
+      util::TempVectorStack::EstimatedAllocationSize(max_batch_size * 
sizeof(uint16_t));
+  const auto alloc_size = alloc_hash_temp_buf * 2 + alloc_for_null_indices_buf;
+
+  std::unique_ptr<util::TempVectorStack> temp_stack(nullptr);
+  auto stack = ctx->stack;
+  if (!stack) {
+    temp_stack = std::make_unique<util::TempVectorStack>();
+    RETURN_NOT_OK(temp_stack->Init(default_memory_pool(), alloc_size));
+    stack = temp_stack.get();
+  } else {
+    RETURN_NOT_OK(stack->CheckAllocationOverflow(alloc_size));
+  }

Review Comment:
   If there is a possibility that `ctx->stack` is `nullptr`, then it's better 
to declare a `TempVectorStack *` parameter explicitly so the caller can 
allocate a stack with the right memory pool instead of this function internally 
relying on the global `default_memory_pool()`. Most calls would be passing 
`ctx, ctx->stack` except for the ones that for some reason don't have a stack 
in the context.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to