kszucs commented on code in PR #45001:
URL: https://github.com/apache/arrow/pull/45001#discussion_r3641063937


##########
cpp/src/arrow/compute/kernels/scalar_hash.cc:
##########
@@ -0,0 +1,288 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <algorithm>
+
+#include "arrow/array/array_base.h"
+#include "arrow/array/builder_primitive.h"
+#include "arrow/compute/kernels/common_internal.h"
+#include "arrow/compute/key_hash_internal.h"
+#include "arrow/compute/light_array_internal.h"
+#include "arrow/compute/registry_internal.h"
+#include "arrow/compute/util.h"
+#include "arrow/result.h"
+
+namespace arrow {
+namespace compute {
+namespace internal {
+
+// Define symbols visible within `arrow::compute::internal` in this file;
+// these symbols are not visible outside of this file.
+namespace {
+
+// ------------------------------
+// Kernel implementations
+// It is expected that HashArrowType is either UInt32Type or UInt64Type 
(default)
+
+// Not dependent on the ArrowType/Hasher template arguments below, so defined
+// as a free function to avoid unnecessary code generation per instantiation.
+// Never called with a nested (list-like or struct) array: HashArray handles 
those
+// itself, either via HashChild (which reduces them to a plain UInt32/UInt64 
array
+// before it ever reaches here) or the is_list_like branch directly.
+Result<KeyColumnArray> ToColumnArray(const ArraySpan& array) {
+  KeyColumnMetadata metadata;
+  const uint8_t* validity_buffer = nullptr;
+  const uint8_t* fixed_length_buffer = nullptr;
+  const uint8_t* var_length_buffer = nullptr;
+
+  if (array.GetBuffer(0) != nullptr) {
+    validity_buffer = array.GetBuffer(0)->data();
+  }
+  if (array.GetBuffer(1) != nullptr) {
+    fixed_length_buffer = array.GetBuffer(1)->data();
+  }
+
+  auto type = array.type;
+  auto type_id = type->id();
+  if (type_id == Type::NA) {
+    metadata = KeyColumnMetadata(true, 0, true);
+  } else if (type_id == Type::BOOL) {
+    metadata = KeyColumnMetadata(true, 0);
+  } else if (is_fixed_width(type_id)) {
+    metadata = KeyColumnMetadata(true, type->bit_width() / 8);
+  } else if (is_binary_like(type_id)) {
+    metadata = KeyColumnMetadata(false, sizeof(uint32_t));
+    if (array.GetBuffer(2) != nullptr) {
+      var_length_buffer = array.GetBuffer(2)->data();
+    }
+  } else if (is_large_binary_like(type_id)) {
+    metadata = KeyColumnMetadata(false, sizeof(uint64_t));
+    if (array.GetBuffer(2) != nullptr) {
+      var_length_buffer = array.GetBuffer(2)->data();
+    }
+  } else {
+    return Status::TypeError("Unsupported column data type ", type->name(),
+                             " used with hash32/hash64 compute kernel");
+  }
+
+  return KeyColumnArray(metadata, array.length, validity_buffer, 
fixed_length_buffer,
+                        var_length_buffer);
+}
+
+// Zeroes out[i] wherever array is null.
+template <typename c_type>
+void ZeroNulls(const ArraySpan& array, c_type* out) {
+  if (array.GetBuffer(0) == nullptr) {
+    return;
+  }
+  for (int64_t i = 0; i < array.length; i++) {
+    if (array.IsNull(i)) {
+      out[i] = 0;
+    }
+  }
+}
+
+template <typename ArrowType, typename Hasher>
+struct FastHashScalar {
+  using c_type = typename ArrowType::c_type;
+
+  // Folds the hashes of one list-like row's children into a single hash. 
Seeded
+  // with CombineHashes(0, 0) rather than 0 so an empty list doesn't collide 
with
+  // a null list (zeroed separately below).
+  static c_type CombineRange(const c_type* value_hashes, int64_t start, 
int64_t end) {
+    c_type combined = Hasher::CombineHashes(0, 0);
+    for (int64_t j = start; j < end; j++) {
+      combined = Hasher::CombineHashes(combined, value_hashes[j]);
+    }
+    return combined;
+  }
+
+  static Result<std::shared_ptr<ArrayData>> HashChild(const ArraySpan& child,
+                                                      LightContext* hash_ctx,
+                                                      MemoryPool* memory_pool) 
{
+    auto arrow_type = TypeTraits<ArrowType>::type_singleton();
+    auto buffer_size = child.length * sizeof(c_type);
+    ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateBuffer(buffer_size, 
memory_pool));
+    ARROW_RETURN_NOT_OK(
+        HashArray(child, hash_ctx, memory_pool, 
buffer->mutable_data_as<c_type>()));
+    return ArrayData::Make(arrow_type, child.length,
+                           {child.GetBuffer(0), std::move(buffer)}, 
child.null_count);
+  }
+
+  static Status HashArray(const ArraySpan& array, LightContext* hash_ctx,
+                          MemoryPool* memory_pool, c_type* out) {
+    // KeyColumnArray objects are being passed to the hashing utility
+    KeyColumnArray column;
+    std::vector<KeyColumnArray> columns(1);
+
+    auto type_id = array.type->id();
+    if (type_id == Type::EXTENSION) {
+      auto extension_type = checked_cast<const ExtensionType*>(array.type);
+      auto storage_array = array;
+      storage_array.type = extension_type->storage_type().get();
+      return HashArray(storage_array, hash_ctx, memory_pool, out);
+    }
+
+    if (type_id == Type::STRUCT) {
+      std::vector<std::shared_ptr<ArrayData>> 
child_hashes(array.child_data.size());
+      columns.resize(array.child_data.size());
+      for (size_t i = 0; i < array.child_data.size(); i++) {
+        auto child = array.child_data[i];
+        if (is_nested(child.type->id())) {
+          ARROW_ASSIGN_OR_RAISE(child_hashes[i], HashChild(child, hash_ctx, 
memory_pool));
+          ARROW_ASSIGN_OR_RAISE(column, ToColumnArray(*child_hashes[i]));
+        } else {
+          ARROW_ASSIGN_OR_RAISE(column, ToColumnArray(child));
+        }
+        columns[i] = column.Slice(array.offset, array.length);
+      }
+      Hasher::HashMultiColumn(columns, hash_ctx, out);
+      // A struct's own null bit is independent of its children's; a null 
struct row
+      // commonly still has valid-looking child data, so it must be forced to 0
+      // explicitly rather than relying on the per-field columns above.
+      ZeroNulls(array, out);
+    } else if (is_list_like(type_id)) {
+      // Each child element is already hashed by HashChild; fold each row's 
slice of
+      // child hashes down to one value directly, rather than routing through
+      // Hasher::HashMultiColumn.
+      auto values = array.child_data[0];
+      ARROW_ASSIGN_OR_RAISE(auto value_hashes, HashChild(values, hash_ctx, 
memory_pool));
+      const c_type* value_hash_data = 
value_hashes->buffers[1]->data_as<c_type>();

Review Comment:
   This is correct as a performance observation, not a correctness bug — the 
offset arithmetic already accounts for `values.offset` so results are right, 
just potentially wasteful when the top-level list/map array is heavily sliced 
(since `ArrayData::Slice()` doesn't slice `child_data`). Fixing it properly 
means slicing the child `ArraySpan` down to the actually-referenced offset 
range before hashing and rebasing the index math, which is more surgery than I 
want to take on in this PR without a benchmark showing it matters in practice. 
Filed as a follow-up optimization rather than fixing here — happy to revisit if 
you feel strongly it should block this PR.



-- 
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