wgtmac commented on code in PR #37400: URL: https://github.com/apache/arrow/pull/37400#discussion_r2593041434
########## cpp/src/parquet/bloom_filter_writer.cc: ########## @@ -0,0 +1,305 @@ +// 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 "parquet/bloom_filter_writer.h" + +#include <map> +#include <utility> + +#include "arrow/array.h" +#include "arrow/io/interfaces.h" +#include "arrow/type_traits.h" +#include "arrow/util/bit_run_reader.h" +#include "arrow/util/checked_cast.h" +#include "arrow/util/unreachable.h" + +#include "parquet/exception.h" +#include "parquet/metadata.h" +#include "parquet/properties.h" +#include "parquet/schema.h" +#include "parquet/types.h" + +namespace parquet { + +constexpr int64_t kHashBatchSize = 256; + +template <typename ParquetType> +BloomFilterWriter<ParquetType>::BloomFilterWriter(const ColumnDescriptor* descr, + BloomFilter* bloom_filter) + : descr_(descr), bloom_filter_(bloom_filter) {} + +template <typename ParquetType> +bool BloomFilterWriter<ParquetType>::bloom_filter_enabled() const { + return bloom_filter_ != nullptr; +} + +template <typename ParquetType> +void BloomFilterWriter<ParquetType>::Update(const T* values, int64_t num_values) { + if (!bloom_filter_enabled()) { + return; + } + + if constexpr (std::is_same_v<ParquetType, BooleanType>) { + throw ParquetException("Bloom filter is not supported for boolean type"); + } + + std::array<uint64_t, kHashBatchSize> hashes; + for (int64_t i = 0; i < num_values; i += kHashBatchSize) { + auto batch_size = static_cast<int>(std::min(kHashBatchSize, num_values - i)); + if constexpr (std::is_same_v<ParquetType, FLBAType>) { + bloom_filter_->Hashes(values, descr_->type_length(), batch_size, hashes.data()); + } else { + bloom_filter_->Hashes(values, batch_size, hashes.data()); + } + bloom_filter_->InsertHashes(hashes.data(), batch_size); + } +} + +template <> +void BloomFilterWriter<BooleanType>::Update(const bool*, int64_t) { + if (!bloom_filter_enabled()) { + return; + } + throw ParquetException("Bloom filter is not supported for boolean type"); +} + +template <typename ParquetType> +void BloomFilterWriter<ParquetType>::UpdateSpaced(const T* values, int64_t num_values, + const uint8_t* valid_bits, + int64_t valid_bits_offset) { + if (!bloom_filter_enabled()) { + return; + } + + std::array<uint64_t, kHashBatchSize> hashes; + ::arrow::internal::VisitSetBitRunsVoid( + valid_bits, valid_bits_offset, num_values, [&](int64_t position, int64_t length) { + for (int64_t i = 0; i < length; i += kHashBatchSize) { + auto batch_size = static_cast<int>(std::min(kHashBatchSize, length - i)); + if constexpr (std::is_same_v<ParquetType, FLBAType>) { + bloom_filter_->Hashes(values + i + position, descr_->type_length(), + batch_size, hashes.data()); + } else { + bloom_filter_->Hashes(values + i + position, batch_size, hashes.data()); + } + bloom_filter_->InsertHashes(hashes.data(), batch_size); + } + }); +} + +template <> +void BloomFilterWriter<BooleanType>::UpdateSpaced(const bool*, int64_t, const uint8_t*, + int64_t) { + if (!bloom_filter_enabled()) { + return; + } + throw ParquetException("Bloom filter is not supported for boolean type"); +} + +template <typename ParquetType> +void BloomFilterWriter<ParquetType>::Update(const ::arrow::Array& values) { + ::arrow::Unreachable("Update for non-ByteArray type should be unreachable"); +} + +namespace { + +template <typename ArrayType> +void UpdateBinaryBloomFilter(BloomFilter& bloom_filter, const ArrayType& array) { + // Using a small batch size because an extra `byte_arrays` is used. + constexpr int64_t kBinaryHashBatchSize = 64; + std::array<ByteArray, kBinaryHashBatchSize> byte_arrays; + std::array<uint64_t, kBinaryHashBatchSize> hashes; + + auto batch_insert_hashes = [&](int count) { + if (count > 0) { + bloom_filter.Hashes(byte_arrays.data(), count, hashes.data()); + bloom_filter.InsertHashes(hashes.data(), count); + } + }; + + int batch_idx = 0; + ::arrow::internal::VisitSetBitRunsVoid( + array.null_bitmap_data(), array.offset(), array.length(), + [&](int64_t position, int64_t run_length) { + for (int64_t i = 0; i < run_length; ++i) { Review Comment: Ah, I didn't notice that difference since I took over this PR half way. Let me change them to be consistent. -- 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]
