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


##########
cpp/src/arrow/compute/kernels/scalar_hash.cc:
##########
@@ -0,0 +1,199 @@
+// 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.
+
+/**
+ * @file  scalar_hash.cc
+ * @brief Element-wise (scalar) kernels for hashing values.
+ */
+
+#include <algorithm>
+//#include <iostream>
+
+#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/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 {
+
+// Function documentation
+const FunctionDoc hash_64_doc{
+    "Construct a hash for every element of the input argument",
+    ("An element-wise function that uses an xxHash-like algorithm.\n"
+     "This function is not suitable for cryptographic purposes.\n"
+     "Hash results are 64-bit and emitted for each valid row.\n"
+     "Null (or invalid) rows emit a null in the output."),
+    {"hash_input"}};
+
+// ------------------------------
+// Kernel implementations
+// It is expected that HashArrowType is either UInt32Type or UInt64Type 
(default)
+// template <typename HashArrowType = UInt64Type>
+struct FastHashScalar {
+  static Result<KeyColumnArray> ToColumnArray(const ArraySpan& array, 
LightContext* ctx) {
+    auto type_id = array.type->id();
+    KeyColumnMetadata metadata;
+    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, array.type->bit_width() / 8);
+    } else if (is_binary_like(type_id)) {
+      metadata = KeyColumnMetadata(false, sizeof(uint32_t));
+    } else if (is_large_binary_like(type_id)) {
+      metadata = KeyColumnMetadata(false, sizeof(uint64_t));
+    } else {
+      return Status::TypeError("Unsupported column data type ", 
array.type->name(),
+                               " used with KeyColumnMetadata");
+    }
+    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();
+    }
+    if (array.GetBuffer(2) != nullptr) {
+      var_length_buffer = array.GetBuffer(2)->data();
+    }
+    return KeyColumnArray(metadata, array.length, validity_buffer, 
fixed_length_buffer,
+                          var_length_buffer);
+  }
+
+  static Result<std::shared_ptr<ArrayData>> HashChild(const ArraySpan& array,
+                                                      const ArraySpan& child,
+                                                      LightContext* hash_ctx,
+                                                      MemoryPool* memory_pool) 
{
+    auto buffer_size = child.length * sizeof(uint64_t);
+    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<uint64_t>()));
+    return ArrayData::Make(uint64(), child.length,
+                           {array.GetBuffer(0), std::move(buffer)}, 
array.null_count);
+  }
+
+  static Status HashArray(const ArraySpan& array, LightContext* hash_ctx,
+                          MemoryPool* memory_pool, uint64_t* out) {
+    std::vector<KeyColumnArray> columns(1);
+
+    auto type_id = array.type->id();
+    if (is_list_like(type_id)) {

Review Comment:
   The list handling logic is incorrect, fixing it.



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