kszucs commented on code in PR #45001: URL: https://github.com/apache/arrow/pull/45001#discussion_r3641062862
########## 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 = TypeTraits<ArrowType>::type_singleton(); + auto buffer_size = child.length * sizeof(c_type); + ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateBuffer(buffer_size, memory_pool)); Review Comment: Confirmed this follows the same pattern as `scalar_if_else.cc`, which also allocates via `ctx->memory_pool()` inside a `MemAllocation::PREALLOCATE` kernel (e.g. for its intermediate selection buffers). So this isn't a violation of the contract — `PREALLOCATE` governs the *output* buffer allocation strategy for the kernel executor, not internal scratch allocations a kernel makes on its own via the memory pool. ########## cpp/src/arrow/compute/kernels/scalar_hash.cc: ########## @@ -0,0 +1,284 @@ +// 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)); + 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 { + 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); Review Comment: I checked this against `KeyColumnArray::Slice()` (light_array_internal.cc:81-115): the pointer arithmetic there is derived entirely from the `offset`/`length` arguments passed to `Slice()` itself — it never reads or bounds-checks against the pre-slice `length_` field. So constructing the unsliced `KeyColumnArray` with `array.length` instead of `array.offset + array.length` doesn't actually produce incorrect pointers; the stored length is simply overwritten by `sliced.length_ = length` right after. This is also exercised by the existing `CheckDeterministic` test helper, which calls `hash32`/`hash64` on sliced arrays (`arr->Slice(1)`, `arr->Slice(2, 2)`) and asserts the result matches the corresponding slice of the unsliced hash — that passes today. I'll leave this as-is rather than touching `ToColumnArray`'s length argument, since the established pattern isn't actually load-bearing for correctness here. -- 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]
