kszucs commented on code in PR #45001: URL: https://github.com/apache/arrow/pull/45001#discussion_r4004948265
########## cpp/src/arrow/compute/kernels/scalar_hash.cc: ########## @@ -0,0 +1,562 @@ +// 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 <limits> +#include <memory> +#include <string> +#include <utility> +#include <vector> + +#include "arrow/array/array_base.h" +#include "arrow/array/util.h" +#include "arrow/compute/cast.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" +#include "arrow/util/bit_util.h" +#include "arrow/util/bitmap_ops.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) + +// Free function (not dependent on ArrowType/Hasher) to avoid codegen per instantiation. +// Only called with a plain column; HashArray routes everything else (see +// NeedsRecursiveHash) elsewhere first. +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(); Review Comment: The null pointer is real but never dereferenced — no crash to fix. Tests added in 8ac2f3a04e. `ToColumnArray` does leave `var_length_buffer` null, but not from an all-null/empty array — those always carry a non-null (possibly zero-size) values buffer, and an `ArrayData` built with a genuinely null one is rejected by `ValidateFull` as `Value data buffer is null`. The shape that really produces it is the invalid `BinaryScalar`/`StringScalar` you named: `ArraySpan::FillFromScalar` sets `buffers[2].data` only `if (scalar.is_valid)`. No values buffer means all-equal offsets, so every row is zero-length and both implementations reach zero rows before reading a byte — `num_rows_safe` and `num_rows_to_process` each run to 0, and the scalar tail's `ProcessFullStripes` iterates `istripe < num_stripes - 1`, zero times for the `num_stripes == 1` that a zero-length row yields (the `memcpy` is guarded on `key_length > 0`, and `ProcessLastStripe` reads the stack-local `last_stripe_copy` under an all-zero mask, not `key`). The pointer only ever appears as `concatenated_keys + 0`, which is well-defined. `NullValuesBufferHashesWithoutCrashing` covers all four binary-like types and both kernels: null scalars, all-null/empty/empty-string arrays, and the same shapes as list and struct children. `ScalarInput` only covered `int32`, so this path had no coverage at all; my machine is arm64, so the new cases also buy AVX2 coverage on CI. -- 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]
