You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@arrow.apache.org by ze...@apache.org on 2025/10/06 14:49:02 UTC
(arrow-go) branch main updated: fix(parquet): fix adaptive bloom filter duplicate hash counting, comparison logic, and GC safety (#527)
This is an automated email from the ASF dual-hosted git repository.
zeroshade pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-go.git
The following commit(s) were added to refs/heads/main by this push:
new 5417ef06 fix(parquet): fix adaptive bloom filter duplicate hash counting, comparison logic, and GC safety (#527)
5417ef06 is described below
commit 5417ef066679d75d5afeaf0cd368a41253f49da8
Author: Mandukhai Alimaa <11...@users.noreply.github.com>
AuthorDate: Mon Oct 6 09:48:57 2025 -0500
fix(parquet): fix adaptive bloom filter duplicate hash counting, comparison logic, and GC safety (#527)
### Rationale for this change
This PR addresses Issue #525 . The adaptive Bloom filter implementation
had several bugs that caused incorrect behavior.
### What changes are included in this PR?
Fixed bugs in the adaptive Bloom filter logic
Added targeted unit tests that tests the specific bugs mentioned in the
Issue #525
Added an end-to-end test for the adaptive Bloom filter to ensure
correctness
### Are these changes tested?
Newly added tests for the adaptive Bloom filter pass.
All existing test suites continue to pass.
### Are there any user-facing changes?
This PR does not introduce any user-facing API changes.
Closes #525
---
parquet/metadata/adaptive_bloom_filter.go | 20 ++--
parquet/metadata/bloom_filter_test.go | 172 ++++++++++++++++++++++++++++++
2 files changed, 183 insertions(+), 9 deletions(-)
diff --git a/parquet/metadata/adaptive_bloom_filter.go b/parquet/metadata/adaptive_bloom_filter.go
index c040645a..348fada3 100644
--- a/parquet/metadata/adaptive_bloom_filter.go
+++ b/parquet/metadata/adaptive_bloom_filter.go
@@ -18,7 +18,6 @@ package metadata
import (
"io"
- "runtime"
"slices"
"github.com/apache/arrow-go/v18/arrow"
@@ -30,7 +29,7 @@ import (
)
type bloomFilterCandidate struct {
- bloomFilter blockSplitBloomFilter
+ bloomFilter *blockSplitBloomFilter
expectedNDV uint32
}
@@ -58,10 +57,8 @@ func newBloomFilterCandidate(expectedNDV, numBytes, minBytes, maxBytes uint32, h
hashStrategy: defaultHashStrategy,
compression: defaultCompression,
}
- runtime.SetFinalizer(&bf, func(f *blockSplitBloomFilter) {
- f.data.Release()
- })
- return &bloomFilterCandidate{bloomFilter: bf, expectedNDV: expectedNDV}
+ addCleanup(&bf, nil)
+ return &bloomFilterCandidate{bloomFilter: &bf, expectedNDV: expectedNDV}
}
type adaptiveBlockSplitBloomFilter struct {
@@ -112,7 +109,7 @@ func (b *adaptiveBlockSplitBloomFilter) getCompression() *format.BloomFilterComp
func (b *adaptiveBlockSplitBloomFilter) optimalCandidate() *bloomFilterCandidate {
return slices.MinFunc(b.candidates, func(a, b *bloomFilterCandidate) int {
- return int(b.bloomFilter.Size() - a.bloomFilter.Size())
+ return int(a.bloomFilter.Size() - b.bloomFilter.Size())
})
}
@@ -141,12 +138,17 @@ func (b *adaptiveBlockSplitBloomFilter) InsertBulk(hashes []uint64) {
panic("adaptive bloom filter has been marked finalized, no more data allowed")
}
+ // Use a set to track unique hashes that are not already in the largest candidate
+ uniqueNewHashes := make(map[uint64]struct{})
for _, h := range hashes {
if !b.largestCandidate.bloomFilter.CheckHash(h) {
- b.numDistinct++
+ uniqueNewHashes[h] = struct{}{}
}
}
+ // Only increment numDistinct by the number of unique new hashes
+ b.numDistinct += int64(len(uniqueNewHashes))
+
b.candidates = slices.DeleteFunc(b.candidates, func(c *bloomFilterCandidate) bool {
return c.expectedNDV < uint32(b.numDistinct) && c != b.largestCandidate
})
@@ -191,7 +193,7 @@ func (b *adaptiveBlockSplitBloomFilter) initCandidates(maxBytes uint32, numCandi
}
b.largestCandidate = slices.MaxFunc(b.candidates, func(a, b *bloomFilterCandidate) int {
- return int(b.bloomFilter.Size() - a.bloomFilter.Size())
+ return int(a.bloomFilter.Size() - b.bloomFilter.Size())
})
}
diff --git a/parquet/metadata/bloom_filter_test.go b/parquet/metadata/bloom_filter_test.go
index 1f8a6011..ea671438 100644
--- a/parquet/metadata/bloom_filter_test.go
+++ b/parquet/metadata/bloom_filter_test.go
@@ -25,6 +25,7 @@ import (
"github.com/apache/arrow-go/v18/arrow/bitutil"
"github.com/apache/arrow-go/v18/arrow/memory"
"github.com/apache/arrow-go/v18/parquet"
+ "github.com/apache/arrow-go/v18/parquet/schema"
"github.com/stretchr/testify/assert"
)
@@ -188,3 +189,174 @@ func BenchmarkFilterInsertBulk(b *testing.B) {
}
b.SetBytes(bytesPerFilterBlock * int64(len(x)))
}
+
+func TestAdaptiveBloomFilterEdgeCases(t *testing.T) {
+ mem := memory.DefaultAllocator
+
+ // Create a simple column for testing
+ col := schema.NewColumn(schema.NewByteArrayNode("test", parquet.Repetitions.Required, -1), 1, 1)
+
+ t.Run("InsertBulk handles duplicate hashes correctly", func(t *testing.T) {
+ bf := NewAdaptiveBlockSplitBloomFilter(1024, 3, 0.01, col, mem).(*adaptiveBlockSplitBloomFilter)
+
+ // Insert the same hash multiple times
+ duplicateHash := uint64(12345)
+ hashes := []uint64{duplicateHash, duplicateHash, duplicateHash}
+
+ initialDistinct := bf.numDistinct
+ bf.InsertBulk(hashes)
+
+ expectedDistinct := initialDistinct + 1
+ assert.Equal(t, expectedDistinct, bf.numDistinct)
+ })
+
+ t.Run("candidate selection uses correct comparison logic", func(t *testing.T) {
+ // Use larger maxBytes and more candidates to ensure multiple candidates are created
+ bf := NewAdaptiveBlockSplitBloomFilter(8192, 5, 0.01, col, mem).(*adaptiveBlockSplitBloomFilter)
+
+ assert.GreaterOrEqual(t, len(bf.candidates), 2, "Expected at least 2 candidates with numCandidates=5")
+
+ // Test MinFunc - should return the candidate with smallest size
+ optimal := bf.optimalCandidate()
+ minSize := optimal.bloomFilter.Size()
+
+ for _, c := range bf.candidates {
+ assert.GreaterOrEqual(t, c.bloomFilter.Size(), minSize)
+ }
+
+ // Test MaxFunc - largestCandidate should have the largest size
+ maxSize := bf.largestCandidate.bloomFilter.Size()
+ for _, c := range bf.candidates {
+ assert.LessOrEqual(t, c.bloomFilter.Size(), maxSize)
+ }
+ })
+
+ t.Run("bloom filter data survives garbage collection", func(t *testing.T) {
+ bf := NewAdaptiveBlockSplitBloomFilter(1024, 1, 0.01, col, mem).(*adaptiveBlockSplitBloomFilter)
+
+ // Insert some data
+ hashes := []uint64{1, 2, 3, 4, 5}
+ bf.InsertBulk(hashes)
+
+ // Force garbage collection to trigger finalizers
+ runtime.GC()
+ runtime.GC()
+
+ // The bloom filter should still work after GC
+ for _, h := range hashes {
+ assert.Truef(t, bf.CheckHash(h), "hash %d not found after GC - potential GC safety issue", h)
+ }
+ })
+}
+
+func TestAdaptiveBloomFilterEndToEnd(t *testing.T) {
+ // This test simulates the full workflow: write parquet with adaptive bloom filters,
+ // then read it back and verify bloom filter functionality
+
+ mem := memory.DefaultAllocator
+
+ // Create test data
+ testValues := []parquet.ByteArray{
+ []byte("apple"),
+ []byte("banana"),
+ []byte("cherry"),
+ []byte("date"),
+ []byte("elderberry"),
+ []byte("fig"),
+ []byte("grape"),
+ []byte("honeydew"),
+ }
+
+ // Values that should NOT be in the bloom filter
+ absentValues := []parquet.ByteArray{
+ []byte("absent1"),
+ []byte("absent2"),
+ []byte("absent3"),
+ }
+
+ col := schema.NewColumn(schema.NewByteArrayNode("test_column", parquet.Repetitions.Required, -1), 1, 1)
+
+ t.Run("create adaptive bloom filter with test data", func(t *testing.T) {
+ // Create adaptive bloom filter as would be done during parquet writing
+ bf := NewAdaptiveBlockSplitBloomFilter(1024, 3, 0.01, col, mem).(*adaptiveBlockSplitBloomFilter)
+ hasher := bf.Hasher()
+
+ // Insert test values (this simulates what happens during parquet writing)
+ var hashes []uint64
+ for _, val := range testValues {
+ hashes = append(hashes, GetHash(hasher, val))
+ }
+
+ // Test bulk insertion with some duplicates
+ duplicatedHashes := append(hashes, hashes[0], hashes[1], hashes[0])
+ bf.InsertBulk(duplicatedHashes)
+
+ // Verify all original values are found
+ for _, val := range testValues {
+ assert.True(t, bf.CheckHash(GetHash(hasher, val)))
+ }
+
+ // Verify absent values are (most likely) not found
+ falsePositives := 0
+ for _, val := range absentValues {
+ hash := GetHash(hasher, val)
+ if bf.CheckHash(hash) {
+ falsePositives++
+ }
+ }
+
+ // With a 1% false positive rate and 3 absent values, we expect 0-1 false positives most of the time
+ if falsePositives > 1 {
+ t.Logf("Note: Got %d false positives out of %d absent values (this can happen with bloom filters)",
+ falsePositives, len(absentValues))
+ }
+
+ t.Logf("Bloom filter stats: size=%d bytes, numDistinct=%d, candidates=%d",
+ bf.Size(), bf.numDistinct, len(bf.candidates))
+ })
+
+ t.Run("verify duplicate handling in bulk operations", func(t *testing.T) {
+ bf := NewAdaptiveBlockSplitBloomFilter(1024, 3, 0.01, col, mem).(*adaptiveBlockSplitBloomFilter)
+ hasher := bf.Hasher()
+
+ // Create a slice with many duplicates
+ testHash := GetHash(hasher, testValues[0])
+ duplicateHashes := make([]uint64, 100)
+ for i := range duplicateHashes {
+ duplicateHashes[i] = testHash
+ }
+
+ initialDistinct := bf.numDistinct
+ bf.InsertBulk(duplicateHashes)
+
+ // Should only increment numDistinct by 1, not 100
+ expectedDistinct := initialDistinct + 1
+ assert.Equal(t, expectedDistinct, bf.numDistinct)
+
+ // The value should still be findable
+ assert.True(t, bf.CheckHash(testHash))
+ })
+
+ t.Run("verify optimal candidate selection", func(t *testing.T) {
+ // Create bloom filter with multiple candidates
+ bf := NewAdaptiveBlockSplitBloomFilter(4096, 4, 0.01, col, mem).(*adaptiveBlockSplitBloomFilter)
+
+ assert.GreaterOrEqual(t, len(bf.candidates), 2, "Expected at least 2 candidates with numCandidates=4")
+
+ // Insert some data to trigger candidate elimination
+ hasher := bf.Hasher()
+ for _, val := range testValues {
+ hash := GetHash(hasher, val)
+ bf.InsertHash(hash)
+
+ // Check optimal candidate selection is working
+ optimal := bf.optimalCandidate()
+ largest := bf.largestCandidate
+
+ assert.LessOrEqual(t, optimal.bloomFilter.Size(), largest.bloomFilter.Size())
+ }
+
+ t.Logf("Final candidates: %d, optimal size: %d bytes",
+ len(bf.candidates), bf.optimalCandidate().bloomFilter.Size())
+ })
+}