drin commented on code in PR #13487: URL: https://github.com/apache/arrow/pull/13487#discussion_r926056846
########## cpp/src/arrow/compute/kernels/scalar_hash.cc: ########## @@ -0,0 +1,194 @@ +// 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/exec/key_hash.h" +#include "arrow/compute/exec/util.h" +#include "arrow/compute/kernels/common.h" +#include "arrow/compute/light_array.h" +#include "arrow/result.h" + +// NOTES: +// * `KeyColumnArray` comes from light_array.h +// * Should be replaceable with `ExecSpan` + +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 fast_hash_32_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 32-bit and emitted for each valid row.\n" + "Null (or invalid) rows emit a null in the output."), + {"hash_input"}}; + +const FunctionDoc fast_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 +struct FastHash32Scalar { + static Status Exec(KernelContext* ctx, const ExecSpan& input_arg, ExecResult* out) { + if (input_arg.num_values() != 1 or not input_arg[0].is_array()) { + return Status::Invalid("FastHash32 currently supports a single array input"); + } + + // Initialize stack-based memory allocator with an allocator and memory size + util::TempVectorStack stack_memallocator; + ARROW_RETURN_NOT_OK(stack_memallocator.Init(ctx->exec_context()->memory_pool(), + max_bitwidth * max_batchsize)); + + // Prepare input data structure for propagation to hash function + ArraySpan hash_input = input_arg[0].array; + ARROW_ASSIGN_OR_RAISE(KeyColumnArray input_keycol, + ColumnArrayFromArrayData(hash_input.ToArrayData(), + default_rstart, hash_input.length)); + + // Call hashing function + std::vector<uint32_t> hash_results(hash_input.length); + LightContext hash_ctx; + + hash_ctx.hardware_flags = ctx->exec_context()->cpu_info()->hardware_flags(); + hash_ctx.stack = &stack_memallocator; + Hashing32::HashMultiColumn({input_keycol}, &hash_ctx, hash_results.data()); + + // Prepare results of hash function for kernel output argument + arrow::UInt32Builder builder; + ARROW_RETURN_NOT_OK(builder.Reserve(hash_results.size())); + ARROW_RETURN_NOT_OK(builder.AppendValues(hash_results)); + ARROW_ASSIGN_OR_RAISE(auto result_array, builder.Finish()); + + out->value = ArraySpan{*(result_array->data())}; + return Status::OK(); + } + + // This 96 represents the most bits *per row* that the Hashing32 and Hashing64 + // algorithms allocate from the provided `TempVectorStack` + static constexpr uint32_t max_bitwidth = 96; Review Comment: sounds good. it was just because the TempVectorStack takes a size in bits, not in bytes. but I will take your changes. -- 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]
