Copilot commented on code in PR #45001: URL: https://github.com/apache/arrow/pull/45001#discussion_r3644591084
########## cpp/src/arrow/compute/kernels/scalar_hash.cc: ########## @@ -0,0 +1,373 @@ +// 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 <utility> + +#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; + } + } +} + +// Folds one row's child hashes into a single hash. Seeded with CombineHashes(0, 0), +// not 0, so an empty list doesn't collide with a null list (zeroed separately below). +// Free function since it only depends on c_type/Hasher, not ArrowType. +template <typename c_type, typename Hasher> +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; +} + +// Combines rows for LIST/LARGE_LIST/MAP, whose offsets buffers differ only in width. +// `bias` is values.offset + rel_start, so offsets[i] - bias locates row i. +template <typename c_type, typename Hasher, typename OffsetT> +void CombineOffsetRows(int64_t length, const OffsetT* offsets, int64_t bias, + const c_type* value_hash_data, c_type* out) { + for (int64_t i = 0; i < length; i++) { + out[i] = CombineRange<c_type, Hasher>(value_hash_data, offsets[i] - bias, + offsets[i + 1] - bias); + } +} + +template <typename ArrowType, typename Hasher> +struct FastHashScalar { + using c_type = typename ArrowType::c_type; + + // Hashes the [offset, offset + length) slice of `child`. + static Result<std::shared_ptr<ArrayData>> HashChild(const ArraySpan& child, + int64_t offset, int64_t length, + LightContext* hash_ctx, + MemoryPool* memory_pool) { + auto sliced = child; + sliced.SetSlice(offset, length); + auto arrow_type = TypeTraits<ArrowType>::type_singleton(); + auto buffer_size = sliced.length * sizeof(c_type); + ARROW_ASSIGN_OR_RAISE(auto buffer, AllocateBuffer(buffer_size, memory_pool)); + ARROW_RETURN_NOT_OK( + HashArray(sliced, hash_ctx, memory_pool, buffer->mutable_data_as<c_type>())); + return ArrayData::Make(arrow_type, sliced.length, + {sliced.GetBuffer(0), std::move(buffer)}, sliced.null_count); + } Review Comment: HashChild builds an ArrayData with offset=0 but reuses the child’s validity bitmap without adjusting for the slice offset. If `offset != 0` and the hashed child is later treated as a KeyColumnArray (e.g. nested struct fields), the validity bits will be read starting at bit 0 instead of `offset`, which can misclassify null/non-null rows and produce incorrect combined hashes. -- 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]
