Skip to content

Commit 275fd27

Browse files
committed
fix(file-index): address BSI and Bloom writer review issues
1 parent 3277cdf commit 275fd27

7 files changed

Lines changed: 47 additions & 10 deletions

‎src/paimon/common/file_index/bloomfilter/bloom_filter_file_index.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Status BloomFilterFileIndexWriter::AddBatch(::ArrowArray* batch) {
119119
}
120120
PAIMON_ASSIGN_OR_RAISE(
121121
std::vector<Literal> values,
122-
LiteralConverter::ConvertLiteralsFromArray(*struct_array->field(0), /*own_data=*/true));
122+
LiteralConverter::ConvertLiteralsFromArray(*struct_array->field(0), /*own_data=*/false));
123123
for (const Literal& value : values) {
124124
if (!value.IsNull()) {
125125
filter_.AddHash(hash_function_(value));
@@ -130,7 +130,7 @@ Status BloomFilterFileIndexWriter::AddBatch(::ArrowArray* batch) {
130130

131131
Result<PAIMON_UNIQUE_PTR<Bytes>> BloomFilterFileIndexWriter::SerializedBytes() const {
132132
constexpr int32_t kHeaderLength = sizeof(int32_t);
133-
const int32_t bit_set_length = filter_.GetBitSet().BitSize() / BloomFilter64::BYTE_SIZE;
133+
const int32_t bit_set_length = filter_.GetBitSet().ByteLength();
134134
PAIMON_UNIQUE_PTR<Bytes> bytes =
135135
Bytes::AllocateBytes(kHeaderLength + bit_set_length, pool_.get());
136136
const auto num_hash_functions = static_cast<uint32_t>(filter_.GetNumHashFunctions());

‎src/paimon/common/file_index/bsi/bit_slice_index_bitmap_file_index.cpp‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ BitSliceIndexBitmapFileIndexWriter::BitSliceIndexBitmapFileIndexWriter(
8181
const std::shared_ptr<arrow::Field>& field,
8282
const BitSliceIndexBitmapFileIndex::ValueMapperType& value_mapper,
8383
const std::shared_ptr<MemoryPool>& pool)
84-
: struct_type_(arrow::struct_({field})), value_mapper_(value_mapper), pool_(pool) {}
84+
: struct_type_(arrow::struct_({field})),
85+
field_name_(field->name()),
86+
value_mapper_(value_mapper),
87+
pool_(pool) {}
8588

8689
Status BitSliceIndexBitmapFileIndexWriter::AddBatch(::ArrowArray* batch) {
8790
PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr<arrow::Array> array,
@@ -102,14 +105,18 @@ Status BitSliceIndexBitmapFileIndexWriter::AddBatch(::ArrowArray* batch) {
102105
}
103106
PAIMON_ASSIGN_OR_RAISE(
104107
std::vector<Literal> literals,
105-
LiteralConverter::ConvertLiteralsFromArray(*struct_array->field(0), /*own_data=*/true));
108+
LiteralConverter::ConvertLiteralsFromArray(*struct_array->field(0), /*own_data=*/false));
106109
values_.reserve(values_.size() + literals.size());
107110
for (const Literal& literal : literals) {
108111
if (literal.IsNull()) {
109112
values_.emplace_back(std::nullopt);
110113
continue;
111114
}
112115
PAIMON_ASSIGN_OR_RAISE(int64_t value, value_mapper_(literal));
116+
if (value == std::numeric_limits<int64_t>::min()) {
117+
return Status::Invalid(
118+
fmt::format("bsi index does not support INT64_MIN for field '{}'", field_name_));
119+
}
113120
values_.emplace_back(value);
114121
if (value < 0) {
115122
const int64_t absolute_value = SafeAbs(value);

‎src/paimon/common/file_index/bsi/bit_slice_index_bitmap_file_index.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class BitSliceIndexBitmapFileIndexWriter : public FileIndexWriter {
8787

8888
private:
8989
std::shared_ptr<arrow::DataType> struct_type_;
90+
std::string field_name_;
9091
BitSliceIndexBitmapFileIndex::ValueMapperType value_mapper_;
9192
std::vector<std::optional<int64_t>> values_;
9293
int64_t positive_min_ = 0;

‎src/paimon/common/file_index/bsi/bit_slice_index_bitmap_file_index_test.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ TEST_F(BitSliceIndexBitmapIndexReaderTest, TestMix) {
161161

162162
TEST_F(BitSliceIndexBitmapIndexReaderTest, TestWriterRejectsInt64MinAndUnsupportedType) {
163163
ASSERT_NOK_WITH_MSG(WriteIndex(arrow::int64(), R"([[-9223372036854775808]])"),
164-
"values should be non-negative");
164+
"bsi index does not support INT64_MIN for field 'f0'");
165165

166166
BitSliceIndexBitmapFileIndex file_index({});
167167
ASSERT_NOK_WITH_MSG(file_index.CreateWriter(CreateArrowSchema(arrow::boolean()).get(), pool_),

‎src/paimon/common/utils/bloom_filter64.cpp‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ bool BloomFilter64::BitSet::Get(int32_t index) const {
4949
}
5050

5151
int32_t BloomFilter64::BitSet::BitSize() const {
52-
return (bytes_->size() - offset_) * BloomFilter64::BYTE_SIZE;
52+
return ByteLength() * BloomFilter64::BYTE_SIZE;
53+
}
54+
55+
int32_t BloomFilter64::BitSet::ByteLength() const {
56+
return static_cast<int32_t>(bytes_->size() - offset_);
5357
}
5458

5559
void BloomFilter64::BitSet::ToByteArray(int32_t offset, int32_t length, char* bytes) const {
@@ -80,14 +84,21 @@ Result<BloomFilter64> BloomFilter64::Create(int64_t items, double fpp,
8084
1, static_cast<int32_t>(std::round(static_cast<double>(num_bits) / items * log_two)));
8185
auto bytes = std::make_shared<Bytes>(num_bits / BYTE_SIZE, pool.get());
8286
auto bit_set = std::make_unique<BitSet>(bytes, /*offset=*/0);
83-
return BloomFilter64(num_hash_functions, std::move(bit_set));
87+
return BloomFilter64(num_hash_functions, std::move(bit_set), pool);
8488
}
8589

8690
BloomFilter64::BloomFilter64(int32_t num_hash_functions, std::unique_ptr<BitSet>&& bit_set)
8791
: num_bits_(bit_set->BitSize()),
8892
num_hash_functions_(num_hash_functions),
8993
bit_set_(std::move(bit_set)) {}
9094

95+
BloomFilter64::BloomFilter64(int32_t num_hash_functions, std::unique_ptr<BitSet>&& bit_set,
96+
const std::shared_ptr<MemoryPool>& pool)
97+
: num_bits_(bit_set->BitSize()),
98+
num_hash_functions_(num_hash_functions),
99+
pool_(pool),
100+
bit_set_(std::move(bit_set)) {}
101+
91102
void BloomFilter64::AddHash(int64_t hash64) {
92103
auto hash1 = static_cast<int32_t>(hash64);
93104
auto hash2 = static_cast<int32_t>(static_cast<uint64_t>(hash64) >> 32);

‎src/paimon/common/utils/bloom_filter64.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class PAIMON_EXPORT BloomFilter64 {
5757
void Set(int32_t index);
5858
bool Get(int32_t index) const;
5959
int32_t BitSize() const;
60+
int32_t ByteLength() const;
6061
void ToByteArray(int32_t offset, int32_t length, char* bytes) const;
6162

6263
private:
@@ -67,11 +68,15 @@ class PAIMON_EXPORT BloomFilter64 {
6768
std::shared_ptr<Bytes> bytes_;
6869
};
6970

71+
private:
72+
BloomFilter64(int32_t num_hash_functions, std::unique_ptr<BitSet>&& bit_set,
73+
const std::shared_ptr<MemoryPool>& pool);
74+
7075
static constexpr int32_t BYTE_SIZE = 8;
7176

72-
private:
7377
int32_t num_bits_ = -1;
7478
int32_t num_hash_functions_ = -1;
79+
std::shared_ptr<MemoryPool> pool_;
7580
std::unique_ptr<BitSet> bit_set_;
7681
};
7782
} // namespace paimon

‎src/paimon/common/utils/bloom_filter64_test.cpp‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,19 @@ TEST(BloomFilter64Test, TestInvalidItemsAndFpp) {
8282
"bloom filter size exceeds the supported range");
8383
}
8484

85+
TEST(BloomFilter64Test, TestKeepMemoryPoolAlive) {
86+
std::weak_ptr<MemoryPool> weak_pool;
87+
{
88+
std::shared_ptr<MemoryPool> pool(GetMemoryPool());
89+
weak_pool = pool;
90+
ASSERT_OK_AND_ASSIGN(BloomFilter64 bloom_filter,
91+
BloomFilter64::Create(/*items=*/100, /*fpp=*/0.1, pool));
92+
pool.reset();
93+
ASSERT_FALSE(weak_pool.expired());
94+
}
95+
ASSERT_TRUE(weak_pool.expired());
96+
}
97+
8598
TEST(BloomFilter64Test, TestCompatibleWithJava) {
8699
// data: -10, -5, 0, 13, 100, 200, 500
87100
std::vector<uint8_t> se_bytes = {241, 255, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0};
@@ -97,8 +110,8 @@ TEST(BloomFilter64Test, TestCompatibleWithJava) {
97110

98111
ASSERT_OK_AND_ASSIGN(BloomFilter64 bloom_filter2, BloomFilter64::Create(10, 0.01, pool));
99112
ASSERT_EQ(7, bloom_filter2.GetNumHashFunctions());
100-
ASSERT_EQ(se_bytes.size() * BloomFilter64::BYTE_SIZE, bloom_filter2.num_bits_);
101-
ASSERT_EQ(se_bytes.size(), bloom_filter2.GetBitSet().bytes_->size());
113+
ASSERT_EQ(se_bytes.size() * 8, bloom_filter2.GetBitSet().BitSize());
114+
ASSERT_EQ(se_bytes.size(), bloom_filter2.GetBitSet().ByteLength());
102115
}
103116

104117
} // namespace paimon::test

0 commit comments

Comments
 (0)