diff --git a/cpp/src/parquet/bloom_filter.cc b/cpp/src/parquet/bloom_filter.cc index 83e91cdcb3a..05bc18278ab 100644 --- a/cpp/src/parquet/bloom_filter.cc +++ b/cpp/src/parquet/bloom_filter.cc @@ -128,7 +128,7 @@ void BlockSplitBloomFilter::Init(const uint8_t* bitset, uint32_t num_bytes) { DCHECK(bitset != nullptr); if (num_bytes < kMinimumBloomFilterBytes || num_bytes > kMaximumBloomFilterBytes || - (num_bytes & (num_bytes - 1)) != 0) { + num_bytes % kBytesPerFilterBlock != 0) { throw ParquetException("Given length of bitset is illegal"); } @@ -399,8 +399,8 @@ uint32_t BlockSplitBloomFilter::NumFoldsForTargetFpp(double target_fpp, if (num_blocks < 2) { return 0; } - // Number of blocks is a power of two - DCHECK_EQ(num_blocks & (num_blocks - 1), 0); + // Each fold needs an even number of blocks so that every block has a partner. + const auto max_folds = static_cast(std::countr_zero(num_blocks)); // Estimate the fill rate after folding from the current average fill rate. // Folding ORs block groups together, so each fold changes the estimated fill rate @@ -409,8 +409,6 @@ uint32_t BlockSplitBloomFilter::NumFoldsForTargetFpp(double target_fpp, // // See also: Sailhan and Stehr, "Folding and Unfolding Bloom Filters", 2012: // https://hal.science/hal-01126174v1 - const auto max_folds = static_cast(std::countr_zero(num_blocks)); - uint32_t num_folds = 0; double unset_probability_after_folds = 1.0 - avg_fill; for (uint32_t i = 0; i < max_folds; ++i) { @@ -430,11 +428,10 @@ void BlockSplitBloomFilter::Fold(uint32_t num_folds) { const uint32_t num_blocks = NumBlocks(); // A fold group is a consecutive run of blocks ORed into one output block. - // Keeping the group size as (1 << num_folds) preserves a power-of-two bitset - // size. Folding by this power-of-two group size keeps the old-to-new bucket - // remapping aligned with bucket lookup and avoids false negatives. + // Equal-sized groups preserve the mapping from each old bucket to its new bucket. const uint32_t group_size = UINT32_C(1) << num_folds; DCHECK_LE(group_size, num_blocks); + DCHECK_EQ(num_blocks % group_size, 0); const uint32_t new_num_blocks = num_blocks / group_size; auto* bitset32 = reinterpret_cast(data_->mutable_data()); diff --git a/cpp/src/parquet/bloom_filter.h b/cpp/src/parquet/bloom_filter.h index d70a4d1822a..ca6d379ed83 100644 --- a/cpp/src/parquet/bloom_filter.h +++ b/cpp/src/parquet/bloom_filter.h @@ -217,7 +217,8 @@ class PARQUET_EXPORT BlockSplitBloomFilter : public BloomFilter { /// a Bloom filter from a parquet file. /// /// @param bitset The given bitset to initialize the Bloom filter. - /// @param num_bytes The number of bytes of given bitset. + /// @param num_bytes The size of the given bitset in bytes, a multiple of 32 within + /// [kMinimumBloomFilterBytes, kMaximumBloomFilterBytes]. void Init(const uint8_t* bitset, uint32_t num_bytes); /// Minimum Bloom filter size, it sets to 32 bytes to fit a tiny Bloom filter. diff --git a/cpp/src/parquet/bloom_filter_test.cc b/cpp/src/parquet/bloom_filter_test.cc index 029d955ea04..dd73d1733ff 100644 --- a/cpp/src/parquet/bloom_filter_test.cc +++ b/cpp/src/parquet/bloom_filter_test.cc @@ -59,7 +59,7 @@ TEST(ConstructorTest, TestBloomFilter) { std::unique_ptr bitset1(new uint8_t[1024]()); EXPECT_THROW(bloom_filter.Init(bitset1.get(), 0), ParquetException); - // It throws because the number of bytes of Bloom filter bitset must be a power of 2. + // A serialized bitset must consist of complete 32-byte blocks. std::unique_ptr bitset2(new uint8_t[1024]()); EXPECT_THROW(bloom_filter.Init(bitset2.get(), 1023), ParquetException); } @@ -255,6 +255,46 @@ TEST(CompatibilityTest, TestBloomFilter) { EXPECT_TRUE((*buffer1).Equals(*buffer2)); } +TEST(CompatibilityTest, ParquetGoThreeBlockBloomFilter) { + // parquet-go v0.25.1 wrote this header and bitset with SplitBlockFilter(10, "Value") + // for a single row group containing the INT32 values 0 through 63. + static constexpr uint8_t kSerialized[] = { + 0x15, 0xc0, 0x01, 0x1c, 0x1c, 0x00, 0x00, 0x1c, 0x1c, 0x00, 0x00, 0x1c, 0x1c, 0x00, + 0x00, 0x00, 0x2e, 0x4d, 0xce, 0x32, 0x75, 0x39, 0x2a, 0x5b, 0xda, 0x88, 0xf4, 0xb3, + 0xe7, 0x06, 0x9d, 0x34, 0x10, 0x29, 0x8d, 0x8c, 0xb4, 0x6b, 0x93, 0xce, 0x00, 0x7d, + 0x5c, 0x57, 0xd8, 0x2a, 0xeb, 0x97, 0xd8, 0xa1, 0x60, 0xa1, 0x52, 0xcc, 0xa2, 0xf1, + 0x20, 0xbd, 0x86, 0x53, 0x28, 0x1a, 0xc8, 0xa3, 0x6c, 0x1c, 0x11, 0x4f, 0x90, 0x78, + 0xe0, 0x70, 0x0b, 0x87, 0x30, 0x36, 0x9e, 0x0a, 0x51, 0xc4, 0x3f, 0xc1, 0xfc, 0x32, + 0x9d, 0x18, 0xaa, 0xcf, 0xe2, 0xc1, 0xe7, 0xa9, 0x1e, 0x5f, 0xe0, 0x5a, 0xbe, 0x5a, + 0x24, 0xe7, 0xbf, 0xb3, 0x68, 0x50, 0x6c, 0x36, 0xc6, 0xa6, 0x80, 0x9b, 0xa9, 0xed}; + ::arrow::io::BufferReader source( + ::arrow::Buffer::Wrap(kSerialized, sizeof(kSerialized))); + auto filter = BlockSplitBloomFilter::Deserialize(ReaderProperties{}, &source); + ASSERT_EQ(96, filter.GetBitsetSize()); + for (int32_t value = 0; value < 64; ++value) { + EXPECT_TRUE(filter.FindHash(filter.Hash(value))) << value; + } + EXPECT_FALSE(filter.FindHash(filter.Hash(-1))); + + filter.FoldToTargetFpp(0.05); + EXPECT_EQ(96, filter.GetBitsetSize()); +} + +TEST(BasicTest, FoldSixBlockBloomFilter) { + const uint8_t bitset[6 * 32] = {}; + BlockSplitBloomFilter filter; + filter.Init(bitset, sizeof(bitset)); + for (int32_t value = 0; value < 32; ++value) { + filter.InsertHash(filter.Hash(value)); + } + + filter.FoldToTargetFpp(0.05); + ASSERT_EQ(3 * 32, filter.GetBitsetSize()); + for (int32_t value = 0; value < 32; ++value) { + EXPECT_TRUE(filter.FindHash(filter.Hash(value))) << value; + } +} + // OptimalValueTest is used to test whether OptimalNumOfBits returns expected // numbers according to formula: // num_of_bits = -8.0 * ndv / log(1 - pow(fpp, 1.0 / 8.0))