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


##########
cpp/src/arrow/compute/api_scalar.h:
##########
@@ -1786,5 +1786,42 @@ ARROW_EXPORT Result<Datum> NanosecondsBetween(const 
Datum& left, const Datum& ri
 /// \note API not yet finalized
 ARROW_EXPORT Result<Datum> MapLookup(const Datum& map, MapLookupOptions 
options,
                                      ExecContext* ctx = NULLPTR);
+
+/// \brief Construct a hash value for each row of the input.
+///
+/// The result is an Array of length equal to the length of the input; 
however, the output
+/// shall be a UInt32Array, with each element being a hash constructed from 
each row of
+/// the input. If the input Array is a NestedArray, this means that each 
"attribute" or
+/// "field" of the input NestedArray corresponding to the same "row" will 
collectively
+/// produce a single uint64_t hash. At the moment, this function does not take 
options,
+/// though these may be added in the future.
+///
+/// \param[in] input_array input data to hash
+/// \param[in] ctx function execution context, optional
+/// \return elementwise hash values
+///
+/// \since 19.0.0

Review Comment:
   ```suggestion
   /// \since 21.0.0
   ```



##########
cpp/src/arrow/compute/kernels/scalar_hash.cc:
##########
@@ -0,0 +1,240 @@
+// 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/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)
+
+template <typename ArrowType, typename Hasher>
+struct FastHashScalar {
+  using c_type = typename ArrowType::c_type;
+
+  static Result<KeyColumnArray> ToColumnArray(
+      const ArraySpan& array, LightContext* ctx,
+      const uint8_t* list_values_buffer = nullptr) {
+    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));
+      var_length_buffer = array.GetBuffer(2)->data();
+    } else if (is_large_binary_like(type_id)) {
+      metadata = KeyColumnMetadata(false, sizeof(uint64_t));
+      var_length_buffer = array.GetBuffer(2)->data();
+    } else if (type_id == Type::MAP) {
+      metadata = KeyColumnMetadata(false, sizeof(uint32_t));
+      var_length_buffer = list_values_buffer;
+    } else if (type_id == Type::LIST) {
+      metadata = KeyColumnMetadata(false, sizeof(uint32_t));
+      var_length_buffer = list_values_buffer;
+    } else if (type_id == Type::LARGE_LIST) {
+      metadata = KeyColumnMetadata(false, sizeof(uint64_t));
+      var_length_buffer = list_values_buffer;
+    } else if (type_id == Type::FIXED_SIZE_LIST) {
+      auto list_type = checked_cast<const FixedSizeListType*>(type);
+      metadata = KeyColumnMetadata(true, list_type->list_size());
+      fixed_length_buffer = list_values_buffer;
+    } 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);
+  }
+
+  static Result<std::shared_ptr<ArrayData>> HashChild(const ArraySpan& array,
+                                                      const ArraySpan& child,
+                                                      LightContext* hash_ctx,
+                                                      MemoryPool* memory_pool) 
{
+    auto arrow_type = std::make_shared<ArrowType>();

Review Comment:
   Can we use `TypeTraits<ArrowType>::type_singleton()` here?



##########
cpp/src/arrow/compute/kernels/scalar_hash_test.cc:
##########
@@ -0,0 +1,539 @@
+// 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 <gtest/gtest.h>
+#include <unordered_set>
+
+#include "arrow/chunked_array.h"
+#include "arrow/compute/api.h"
+#include "arrow/compute/kernels/test_util_internal.h"
+#include "arrow/compute/key_hash_internal.h"
+#include "arrow/compute/util.h"
+#include "arrow/result.h"
+#include "arrow/status.h"
+#include "arrow/testing/extension_type.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/matchers.h"
+#include "arrow/testing/random.h"
+#include "arrow/testing/util.h"
+#include "arrow/util/cpu_info.h"
+#include "arrow/util/key_value_metadata.h"
+
+namespace arrow {
+namespace compute {
+
+constexpr auto kSeed = 0x94378165;
+constexpr auto kArrayLengths = {0, 50, 100};
+constexpr auto kNullProbabilities = {0.0, 0.5, 1.0};
+
+class TestScalarHash : public ::testing::Test {
+ public:
+  template <typename c_type>
+  void AssertHashesEqual(const std::shared_ptr<Array>& arr, Datum res,
+                         std::vector<c_type> exp) {
+    auto res_array = res.array();
+    for (int64_t val_ndx = 0; val_ndx < arr->length(); ++val_ndx) {
+      c_type actual_hash = res_array->GetValues<c_type>(1)[val_ndx];
+      if (arr->IsNull(val_ndx)) {
+        ASSERT_EQ(0, actual_hash);
+      } else {
+        ASSERT_EQ(exp[val_ndx], actual_hash);
+      }
+    }
+  }
+
+  template <typename c_type>
+  std::vector<c_type> HashPrimitive(const std::shared_ptr<Array>& arr) {
+    std::vector<c_type> hashes(arr->length());
+    // Choose the Hasher type conditionally based on c_type
+
+    if constexpr (std::is_same<c_type, uint64_t>::value) {

Review Comment:
   ```suggestion
       if constexpr (std::is_same_v<c_type, uint64_t>) {
   ```



##########
cpp/src/arrow/compute/CMakeLists.txt:
##########
@@ -138,6 +138,15 @@ add_arrow_compute_test(row_test
 
 add_arrow_benchmark(function_benchmark PREFIX "arrow-compute")
 
+if(ARROW_BUILD_BENCHMARKS AND ARROW_COMPUTE)
+  add_arrow_benchmark(key_hash_benchmark PREFIX "arrow-compute")
+  if(ARROW_BUILD_STATIC)
+    target_link_libraries(arrow-compute-key-hash-benchmark PUBLIC 
arrow_compute_static)
+  else()
+    target_link_libraries(arrow-compute-key-hash-benchmark PUBLIC 
arrow_compute_shared)
+  endif()
+endif()

Review Comment:
   Could you use `EXTRA_LINK_LIBS` to remove `if(ARROW_BUILD_BENCHMARKS)`?
   
   ```suggestion
   if(ARROW_TEST_LINKAGE STREQUAL "static")
     set(ARROW_COMPUTE_BENCHMARK_LINK_LIBS arrow_compute_static)
   else()
     set(ARROW_COMPUTE_BENCHMARK_LINK_LIBS arrow_compute_shared)
   endif()
   if(ARROW_COMPUTE)
     add_arrow_benchmark(key_hash_benchmark PREFIX "arrow-compute" 
EXTRA_LINK_LIBS ${ARROW_COMPUTE_BENCHMARK_LINK_LIBS})
   endif()
   ```



##########
cpp/src/arrow/compute/api_scalar.h:
##########
@@ -1786,5 +1786,42 @@ ARROW_EXPORT Result<Datum> NanosecondsBetween(const 
Datum& left, const Datum& ri
 /// \note API not yet finalized
 ARROW_EXPORT Result<Datum> MapLookup(const Datum& map, MapLookupOptions 
options,
                                      ExecContext* ctx = NULLPTR);
+
+/// \brief Construct a hash value for each row of the input.
+///
+/// The result is an Array of length equal to the length of the input; 
however, the output
+/// shall be a UInt32Array, with each element being a hash constructed from 
each row of
+/// the input. If the input Array is a NestedArray, this means that each 
"attribute" or
+/// "field" of the input NestedArray corresponding to the same "row" will 
collectively
+/// produce a single uint64_t hash. At the moment, this function does not take 
options,

Review Comment:
   `uint32_t`?
   
   ```suggestion
   /// produce a single uint32_t hash. At the moment, this function does not 
take options,
   ```



##########
cpp/src/arrow/compute/api_scalar.h:
##########
@@ -1786,5 +1786,42 @@ ARROW_EXPORT Result<Datum> NanosecondsBetween(const 
Datum& left, const Datum& ri
 /// \note API not yet finalized
 ARROW_EXPORT Result<Datum> MapLookup(const Datum& map, MapLookupOptions 
options,
                                      ExecContext* ctx = NULLPTR);
+
+/// \brief Construct a hash value for each row of the input.
+///
+/// The result is an Array of length equal to the length of the input; 
however, the output
+/// shall be a UInt32Array, with each element being a hash constructed from 
each row of
+/// the input. If the input Array is a NestedArray, this means that each 
"attribute" or
+/// "field" of the input NestedArray corresponding to the same "row" will 
collectively
+/// produce a single uint64_t hash. At the moment, this function does not take 
options,
+/// though these may be added in the future.
+///
+/// \param[in] input_array input data to hash
+/// \param[in] ctx function execution context, optional
+/// \return elementwise hash values
+///
+/// \since 19.0.0
+/// \note API not yet finalized
+ARROW_EXPORT
+Result<Datum> Hash32(const Datum& input_array, ExecContext* ctx = NULLPTR);
+
+/// \brief Construct a hash value for each row of the input.
+///
+/// The result is an Array of length equal to the length of the input; 
however, the output
+/// shall be a UInt64Array, with each element being a hash constructed from 
each row of
+/// the input. If the input Array is a NestedArray, this means that each 
"attribute" or
+/// "field" of the input NestedArray corresponding to the same "row" will 
collectively
+/// produce a single uint64_t hash. At the moment, this function does not take 
options,
+/// though these may be added in the future.
+///
+/// \param[in] input_array input data to hash
+/// \param[in] ctx function execution context, optional
+/// \return elementwise hash values
+///
+/// \since 19.0.0

Review Comment:
   ```suggestion
   /// \since 21.0.0
   ```



##########
cpp/src/arrow/compute/kernels/scalar_hash_benchmark.cc:
##########
@@ -0,0 +1,188 @@
+// 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 <cstdint>
+#include <limits>
+#include <random>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/random.h"
+#include "arrow/util/hashing.h"
+
+#include "arrow/array/array_nested.h"
+#include "arrow/compute/exec.h"
+
+namespace arrow {
+namespace internal {
+
+// ------------------------------
+// Anonymous namespace with global params
+
+namespace {
+// copied from scalar_string_benchmark
+constexpr auto kSeed = 0x94378165;
+constexpr double null_prob = 0.2;
+
+static random::RandomArrayGenerator hashing_rng(kSeed);
+}  // namespace
+
+// ------------------------------
+// Convenience functions
+
+static Result<std::shared_ptr<StructArray>> MakeStructArray(int64_t n_values,
+                                                            int32_t min_strlen,
+                                                            int32_t 
max_strlen) {
+  auto vals_first = hashing_rng.Int64(n_values, 0, 
std::numeric_limits<int64_t>::max());
+  auto vals_second = hashing_rng.String(n_values, min_strlen, max_strlen, 
null_prob);
+  auto vals_third = hashing_rng.Int64(n_values, 0, 
std::numeric_limits<int64_t>::max());
+
+  return arrow::StructArray::Make(
+      arrow::ArrayVector{vals_first, vals_second, vals_third},
+      arrow::FieldVector{arrow::field("first", arrow::int64()),
+                         arrow::field("second", arrow::utf8()),
+                         arrow::field("third", arrow::int64())});
+}
+
+// ------------------------------
+// Benchmark implementations
+
+static void Hash64Int64(benchmark::State& state) {  // NOLINT non-const 
reference
+  auto test_vals = hashing_rng.Int64(10000, 0, 
std::numeric_limits<int64_t>::max());
+
+  while (state.KeepRunning()) {
+    ASSERT_OK_AND_ASSIGN(Datum hash_result, compute::CallFunction("hash64", 
{test_vals}));
+    benchmark::DoNotOptimize(hash_result);
+  }
+
+  state.SetBytesProcessed(state.iterations() * test_vals->length() * 
sizeof(int64_t));
+  state.SetItemsProcessed(state.iterations() * test_vals->length());
+}
+
+static void Hash64StructSmallStrings(
+    benchmark::State& state) {  // NOLINT non-const reference
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<StructArray> values_array,
+                       MakeStructArray(10000, 2, 20));
+
+  // 2nd column (index 1) is a string column, which has offset type of int32_t
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<Array> values_second,
+                       values_array->GetFlattenedField(1));
+  std::shared_ptr<StringArray> str_vals =

Review Comment:
   ```suggestion
     auto str_vals =
   ```



##########
cpp/src/arrow/compute/kernels/scalar_hash_benchmark.cc:
##########
@@ -0,0 +1,188 @@
+// 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 <cstdint>
+#include <limits>
+#include <random>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/random.h"
+#include "arrow/util/hashing.h"
+
+#include "arrow/array/array_nested.h"
+#include "arrow/compute/exec.h"
+
+namespace arrow {
+namespace internal {
+
+// ------------------------------
+// Anonymous namespace with global params
+
+namespace {
+// copied from scalar_string_benchmark
+constexpr auto kSeed = 0x94378165;
+constexpr double null_prob = 0.2;
+
+static random::RandomArrayGenerator hashing_rng(kSeed);
+}  // namespace
+
+// ------------------------------
+// Convenience functions
+
+static Result<std::shared_ptr<StructArray>> MakeStructArray(int64_t n_values,
+                                                            int32_t min_strlen,
+                                                            int32_t 
max_strlen) {
+  auto vals_first = hashing_rng.Int64(n_values, 0, 
std::numeric_limits<int64_t>::max());
+  auto vals_second = hashing_rng.String(n_values, min_strlen, max_strlen, 
null_prob);
+  auto vals_third = hashing_rng.Int64(n_values, 0, 
std::numeric_limits<int64_t>::max());
+
+  return arrow::StructArray::Make(
+      arrow::ArrayVector{vals_first, vals_second, vals_third},
+      arrow::FieldVector{arrow::field("first", arrow::int64()),
+                         arrow::field("second", arrow::utf8()),
+                         arrow::field("third", arrow::int64())});
+}
+
+// ------------------------------
+// Benchmark implementations
+
+static void Hash64Int64(benchmark::State& state) {  // NOLINT non-const 
reference
+  auto test_vals = hashing_rng.Int64(10000, 0, 
std::numeric_limits<int64_t>::max());
+
+  while (state.KeepRunning()) {
+    ASSERT_OK_AND_ASSIGN(Datum hash_result, compute::CallFunction("hash64", 
{test_vals}));
+    benchmark::DoNotOptimize(hash_result);
+  }
+
+  state.SetBytesProcessed(state.iterations() * test_vals->length() * 
sizeof(int64_t));
+  state.SetItemsProcessed(state.iterations() * test_vals->length());
+}
+
+static void Hash64StructSmallStrings(
+    benchmark::State& state) {  // NOLINT non-const reference
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<StructArray> values_array,
+                       MakeStructArray(10000, 2, 20));
+
+  // 2nd column (index 1) is a string column, which has offset type of int32_t
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<Array> values_second,
+                       values_array->GetFlattenedField(1));
+  std::shared_ptr<StringArray> str_vals =
+      std::static_pointer_cast<StringArray>(values_second);
+  int32_t total_string_size = str_vals->total_values_length();
+
+  while (state.KeepRunning()) {
+    ASSERT_OK_AND_ASSIGN(Datum hash_result,
+                         compute::CallFunction("hash64", {values_array}));
+    benchmark::DoNotOptimize(hash_result);
+  }
+
+  state.SetBytesProcessed(state.iterations() *
+                          ((values_array->length() * sizeof(int64_t)) +
+                           (total_string_size) +
+                           (values_array->length() * sizeof(int64_t))));
+  state.SetItemsProcessed(state.iterations() * 3 * values_array->length());
+}
+
+static void Hash64StructMediumStrings(
+    benchmark::State& state) {  // NOLINT non-const reference
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<StructArray> values_array,
+                       MakeStructArray(10000, 20, 120));
+
+  // 2nd column (index 1) is a string column, which has offset type of int32_t
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<Array> values_second,
+                       values_array->GetFlattenedField(1));
+  std::shared_ptr<StringArray> str_vals =
+      std::static_pointer_cast<StringArray>(values_second);
+  int32_t total_string_size = str_vals->total_values_length();
+
+  while (state.KeepRunning()) {
+    ASSERT_OK_AND_ASSIGN(Datum hash_result,
+                         compute::CallFunction("hash64", {values_array}));
+    benchmark::DoNotOptimize(hash_result);
+  }
+
+  state.SetBytesProcessed(state.iterations() *
+                          ((values_array->length() * sizeof(int64_t)) +
+                           (total_string_size) +
+                           (values_array->length() * sizeof(int64_t))));
+  state.SetItemsProcessed(state.iterations() * 3 * values_array->length());
+}
+
+static void Hash64StructLargeStrings(
+    benchmark::State& state) {  // NOLINT non-const reference
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<StructArray> values_array,
+                       MakeStructArray(10000, 120, 2000));
+
+  // 2nd column (index 1) is a string column, which has offset type of int32_t
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<Array> values_second,
+                       values_array->GetFlattenedField(1));
+  std::shared_ptr<StringArray> str_vals =

Review Comment:
   ```suggestion
     auto str_vals =
   ```



##########
cpp/src/arrow/compute/kernels/scalar_hash_benchmark.cc:
##########
@@ -0,0 +1,188 @@
+// 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 <cstdint>
+#include <limits>
+#include <random>
+#include <string>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/random.h"
+#include "arrow/util/hashing.h"
+
+#include "arrow/array/array_nested.h"
+#include "arrow/compute/exec.h"
+
+namespace arrow {
+namespace internal {
+
+// ------------------------------
+// Anonymous namespace with global params
+
+namespace {
+// copied from scalar_string_benchmark
+constexpr auto kSeed = 0x94378165;
+constexpr double null_prob = 0.2;
+
+static random::RandomArrayGenerator hashing_rng(kSeed);
+}  // namespace
+
+// ------------------------------
+// Convenience functions
+
+static Result<std::shared_ptr<StructArray>> MakeStructArray(int64_t n_values,
+                                                            int32_t min_strlen,
+                                                            int32_t 
max_strlen) {
+  auto vals_first = hashing_rng.Int64(n_values, 0, 
std::numeric_limits<int64_t>::max());
+  auto vals_second = hashing_rng.String(n_values, min_strlen, max_strlen, 
null_prob);
+  auto vals_third = hashing_rng.Int64(n_values, 0, 
std::numeric_limits<int64_t>::max());
+
+  return arrow::StructArray::Make(
+      arrow::ArrayVector{vals_first, vals_second, vals_third},
+      arrow::FieldVector{arrow::field("first", arrow::int64()),
+                         arrow::field("second", arrow::utf8()),
+                         arrow::field("third", arrow::int64())});
+}
+
+// ------------------------------
+// Benchmark implementations
+
+static void Hash64Int64(benchmark::State& state) {  // NOLINT non-const 
reference
+  auto test_vals = hashing_rng.Int64(10000, 0, 
std::numeric_limits<int64_t>::max());
+
+  while (state.KeepRunning()) {
+    ASSERT_OK_AND_ASSIGN(Datum hash_result, compute::CallFunction("hash64", 
{test_vals}));
+    benchmark::DoNotOptimize(hash_result);
+  }
+
+  state.SetBytesProcessed(state.iterations() * test_vals->length() * 
sizeof(int64_t));
+  state.SetItemsProcessed(state.iterations() * test_vals->length());
+}
+
+static void Hash64StructSmallStrings(
+    benchmark::State& state) {  // NOLINT non-const reference
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<StructArray> values_array,
+                       MakeStructArray(10000, 2, 20));
+
+  // 2nd column (index 1) is a string column, which has offset type of int32_t
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<Array> values_second,
+                       values_array->GetFlattenedField(1));
+  std::shared_ptr<StringArray> str_vals =
+      std::static_pointer_cast<StringArray>(values_second);
+  int32_t total_string_size = str_vals->total_values_length();
+
+  while (state.KeepRunning()) {
+    ASSERT_OK_AND_ASSIGN(Datum hash_result,
+                         compute::CallFunction("hash64", {values_array}));
+    benchmark::DoNotOptimize(hash_result);
+  }
+
+  state.SetBytesProcessed(state.iterations() *
+                          ((values_array->length() * sizeof(int64_t)) +
+                           (total_string_size) +
+                           (values_array->length() * sizeof(int64_t))));
+  state.SetItemsProcessed(state.iterations() * 3 * values_array->length());
+}
+
+static void Hash64StructMediumStrings(
+    benchmark::State& state) {  // NOLINT non-const reference
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<StructArray> values_array,
+                       MakeStructArray(10000, 20, 120));
+
+  // 2nd column (index 1) is a string column, which has offset type of int32_t
+  ASSERT_OK_AND_ASSIGN(std::shared_ptr<Array> values_second,
+                       values_array->GetFlattenedField(1));
+  std::shared_ptr<StringArray> str_vals =

Review Comment:
   ```suggestion
     auto str_vals =
   ```



##########
cpp/src/arrow/compute/kernels/scalar_hash_test.cc:
##########
@@ -0,0 +1,539 @@
+// 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 <gtest/gtest.h>
+#include <unordered_set>
+
+#include "arrow/chunked_array.h"
+#include "arrow/compute/api.h"
+#include "arrow/compute/kernels/test_util_internal.h"
+#include "arrow/compute/key_hash_internal.h"
+#include "arrow/compute/util.h"
+#include "arrow/result.h"
+#include "arrow/status.h"
+#include "arrow/testing/extension_type.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/testing/matchers.h"
+#include "arrow/testing/random.h"
+#include "arrow/testing/util.h"
+#include "arrow/util/cpu_info.h"
+#include "arrow/util/key_value_metadata.h"
+
+namespace arrow {
+namespace compute {
+
+constexpr auto kSeed = 0x94378165;
+constexpr auto kArrayLengths = {0, 50, 100};
+constexpr auto kNullProbabilities = {0.0, 0.5, 1.0};
+
+class TestScalarHash : public ::testing::Test {
+ public:
+  template <typename c_type>
+  void AssertHashesEqual(const std::shared_ptr<Array>& arr, Datum res,
+                         std::vector<c_type> exp) {
+    auto res_array = res.array();
+    for (int64_t val_ndx = 0; val_ndx < arr->length(); ++val_ndx) {
+      c_type actual_hash = res_array->GetValues<c_type>(1)[val_ndx];
+      if (arr->IsNull(val_ndx)) {
+        ASSERT_EQ(0, actual_hash);
+      } else {
+        ASSERT_EQ(exp[val_ndx], actual_hash);
+      }
+    }
+  }
+
+  template <typename c_type>
+  std::vector<c_type> HashPrimitive(const std::shared_ptr<Array>& arr) {
+    std::vector<c_type> hashes(arr->length());
+    // Choose the Hasher type conditionally based on c_type
+
+    if constexpr (std::is_same<c_type, uint64_t>::value) {
+      Hashing64::HashFixed(false, static_cast<uint32_t>(arr->length()),
+                           arr->type()->bit_width() / 8,
+                           arr->data()->GetValues<uint8_t>(1), hashes.data());
+    } else {
+      
Hashing32::HashFixed(::arrow::internal::CpuInfo::GetInstance()->hardware_flags(),
+                           false, static_cast<uint32_t>(arr->length()),
+                           arr->type()->bit_width() / 8,
+                           arr->data()->GetValues<uint8_t>(1), hashes.data(), 
nullptr);
+    }
+
+    return hashes;
+  }
+
+  template <typename c_type>
+  std::vector<c_type> HashBinaryLike(const std::shared_ptr<Array>& arr) {
+    std::vector<c_type> hashes(arr->length());
+    auto length = static_cast<uint32_t>(arr->length());
+    auto values = arr->data()->GetValues<uint8_t>(2);
+    if constexpr (std::is_same<c_type, uint64_t>::value) {

Review Comment:
   ```suggestion
       if constexpr (std::is_same_v<c_type, uint64_t>) {
   ```



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