-
Notifications
You must be signed in to change notification settings - Fork 34
feat: support write for deletion vector #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
580aa90
feat: support write for deletion vector
lucasfang 13a2420
fix
lucasfang abcd03c
fix
lucasfang f808c97
fix
lucasfang 6f0ca5c
fix
lucasfang 164bcd4
fix
lucasfang d08ed9f
fix
lucasfang e3e5ea2
Merge branch 'main' into compaction_dev
lucasfang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/paimon/core/deletionvectors/bitmap_deletion_vector.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * Copyright 2026-present Alibaba Inc. | ||
| * | ||
| * Licensed 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 "paimon/core/deletionvectors/bitmap_deletion_vector.h" | ||
|
|
||
| #include "arrow/util/crc32.h" | ||
| #include "fmt/format.h" | ||
| #include "paimon/common/io/memory_segment_output_stream.h" | ||
| #include "paimon/io/byte_array_input_stream.h" | ||
| #include "paimon/io/data_input_stream.h" | ||
|
|
||
| namespace paimon { | ||
|
|
||
| Result<int32_t> BitmapDeletionVector::SerializeTo(const std::shared_ptr<MemoryPool>& pool, | ||
| DataOutputStream* out) { | ||
| PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<Bytes> data, SerializeToBytes(pool)); | ||
| int64_t size = data->size(); | ||
| if (size < 0 || size > std::numeric_limits<int32_t>::max()) { | ||
| return Status::Invalid("BitmapDeletionVector serialize size out of range: ", size); | ||
| } | ||
| PAIMON_RETURN_NOT_OK(out->WriteValue<int32_t>(static_cast<int32_t>(size))); | ||
| PAIMON_RETURN_NOT_OK(out->WriteBytes(data)); | ||
| uint32_t crc32 = 0; | ||
| crc32 = arrow::internal::crc32(crc32, data->data(), size); | ||
| PAIMON_RETURN_NOT_OK(out->WriteValue<int32_t>(static_cast<int32_t>(crc32))); | ||
| return static_cast<int32_t>(size); | ||
| } | ||
|
|
||
| Result<PAIMON_UNIQUE_PTR<Bytes>> BitmapDeletionVector::SerializeToBytes( | ||
| const std::shared_ptr<MemoryPool>& pool) { | ||
| std::shared_ptr<Bytes> bitmap_bytes = roaring_bitmap_.Serialize(pool.get()); | ||
| if (bitmap_bytes == nullptr) { | ||
| assert(bitmap_bytes); | ||
| return Status::Invalid("roaring bitmap serialize failed"); | ||
| } | ||
| MemorySegmentOutputStream output(MemorySegmentOutputStream::DEFAULT_SEGMENT_SIZE, pool); | ||
| output.WriteValue<int32_t>(MAGIC_NUMBER); | ||
| output.WriteBytes(bitmap_bytes); | ||
| return MemorySegmentUtils::CopyToBytes(output.Segments(), /*offset=*/0, output.CurrentSize(), | ||
| pool.get()); | ||
| } | ||
|
|
||
| Status BitmapDeletionVector::CheckPosition(int64_t position) const { | ||
| if (position > RoaringBitmap32::MAX_VALUE) { | ||
| return Status::Invalid(fmt::format( | ||
| "The file has too many rows, RoaringBitmap32 only supports files with row count " | ||
| "not exceeding {}.", | ||
| RoaringBitmap32::MAX_VALUE)); | ||
| } | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Result<PAIMON_UNIQUE_PTR<DeletionVector>> BitmapDeletionVector::Deserialize(const char* buffer, | ||
| int32_t length, | ||
| MemoryPool* pool) { | ||
| auto in = std::make_shared<ByteArrayInputStream>(buffer, length); | ||
| DataInputStream input(in); | ||
| PAIMON_ASSIGN_OR_RAISE(int32_t magic_num, input.ReadValue<int32_t>()); | ||
| if (magic_num != MAGIC_NUMBER) { | ||
| return Status::Invalid(fmt::format( | ||
| "Unable to deserialize deletion vector, invalid magic number: {}", magic_num)); | ||
| } | ||
| RoaringBitmap32 roaring_bitmap; | ||
| PAIMON_RETURN_NOT_OK(roaring_bitmap.Deserialize(buffer + MAGIC_NUMBER_SIZE_BYTES, | ||
| length - MAGIC_NUMBER_SIZE_BYTES)); | ||
| return pool->AllocateUnique<BitmapDeletionVector>(roaring_bitmap); | ||
| } | ||
|
|
||
| } // namespace paimon | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/paimon/core/deletionvectors/bitmap_deletion_vector_test.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /* | ||
| * Copyright 2026-present Alibaba Inc. | ||
| * | ||
| * Licensed 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 "paimon/core/deletionvectors/bitmap_deletion_vector.h" | ||
|
|
||
| #include <set> | ||
|
|
||
| #include "gtest/gtest.h" | ||
| #include "paimon/common/io/memory_segment_output_stream.h" | ||
| #include "paimon/common/utils/path_util.h" | ||
| #include "paimon/fs/file_system_factory.h" | ||
| #include "paimon/testing/utils/testharness.h" | ||
|
|
||
| namespace paimon::test { | ||
|
|
||
| TEST(BitmapDeletionVectorTest, BasicOperations) { | ||
| RoaringBitmap32 roaring; | ||
| BitmapDeletionVector dv(roaring); | ||
| ASSERT_TRUE(dv.IsEmpty()); | ||
| for (int32_t i = 0; i < 2000; i += 2) { | ||
| ASSERT_OK(dv.Delete(i)); | ||
| } | ||
| ASSERT_EQ(dv.GetCardinality(), 1000); | ||
| for (int32_t i = 0; i < 2000; ++i) { | ||
| if (i % 2 == 0) { | ||
| ASSERT_TRUE(dv.IsDeleted(i).value()); | ||
| } else { | ||
| ASSERT_FALSE(dv.IsDeleted(i).value()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| TEST(BitmapDeletionVectorTest, CheckedDelete) { | ||
| RoaringBitmap32 roaring; | ||
| BitmapDeletionVector dv(roaring); | ||
| ASSERT_TRUE(dv.CheckedDelete(42).value()); | ||
| ASSERT_FALSE(dv.CheckedDelete(42).value()); | ||
| ASSERT_TRUE(dv.IsDeleted(42).value()); | ||
| } | ||
|
|
||
| TEST(BitmapDeletionVectorTest, SerializeAndDeserialize) { | ||
| RoaringBitmap32 roaring; | ||
| for (int32_t i = 0; i < 100; i += 3) { | ||
| roaring.Add(i); | ||
| } | ||
| BitmapDeletionVector dv(roaring); | ||
| auto pool = GetDefaultPool(); | ||
| ASSERT_OK_AND_ASSIGN(auto bytes, dv.SerializeToBytes(pool)); | ||
| ASSERT_OK_AND_ASSIGN( | ||
| auto dv2, BitmapDeletionVector::Deserialize(bytes->data(), bytes->size(), pool.get())); | ||
| for (int32_t i = 0; i < 100; ++i) { | ||
| ASSERT_EQ(dv.IsDeleted(i).value(), dv2->IsDeleted(i).value()); | ||
| } | ||
| } | ||
|
|
||
| TEST(BitmapDeletionVectorTest, SerializeToOutputStream) { | ||
| RoaringBitmap32 roaring; | ||
| for (int32_t i = 0; i < 50; i += 2) { | ||
| roaring.Add(i); | ||
| } | ||
| BitmapDeletionVector dv(roaring); | ||
| auto pool = GetDefaultPool(); | ||
| auto dir = UniqueTestDirectory::Create(); | ||
| auto path = PathUtil::JoinPath(dir->Str(), "dv"); | ||
| ASSERT_OK_AND_ASSIGN(auto fs, FileSystemFactory::Get("local", path, {})); | ||
| ASSERT_OK_AND_ASSIGN(std::shared_ptr<OutputStream> output_stream, | ||
| fs->Create(path, /*overwrite=*/false)); | ||
| DataOutputStream out(output_stream); | ||
| ASSERT_OK_AND_ASSIGN(int64_t size, dv.SerializeTo(pool, &out)); | ||
| ASSERT_OK(output_stream->Flush()); | ||
| ASSERT_OK(output_stream->Close()); | ||
| ASSERT_OK_AND_ASSIGN(std::shared_ptr<InputStream> input_stream, fs->Open(path)); | ||
| DataInputStream in(input_stream); | ||
| ASSERT_OK_AND_ASSIGN(int32_t byte_len, in.ReadValue<int32_t>()); | ||
| auto bytes = Bytes::AllocateBytes(byte_len, pool.get()); | ||
| ASSERT_OK(in.Read(bytes->data(), bytes->size())); | ||
| const char* data = bytes->data(); | ||
| ASSERT_EQ(bytes->size(), size); | ||
| ASSERT_OK_AND_ASSIGN(auto dv2, | ||
| BitmapDeletionVector::Deserialize(data, bytes->size(), pool.get())); | ||
| for (int32_t i = 0; i < 50; ++i) { | ||
| ASSERT_EQ(dv.IsDeleted(i).value(), dv2->IsDeleted(i).value()); | ||
| } | ||
| } | ||
|
|
||
| TEST(BitmapDeletionVectorTest, GetCardinality) { | ||
| RoaringBitmap32 empty; | ||
| BitmapDeletionVector dv_empty(empty); | ||
| ASSERT_EQ(dv_empty.GetCardinality(), 0); | ||
|
|
||
| RoaringBitmap32 cont; | ||
| for (int i = 0; i < 100; ++i) cont.Add(i); | ||
| BitmapDeletionVector dv_cont(cont); | ||
| ASSERT_EQ(dv_cont.GetCardinality(), 100); | ||
|
|
||
| RoaringBitmap32 gap; | ||
| for (int i = 0; i < 1000; i += 10) gap.Add(i); | ||
| BitmapDeletionVector dv_gap(gap); | ||
| ASSERT_EQ(dv_gap.GetCardinality(), 100); | ||
|
|
||
| RoaringBitmap32 del; | ||
| for (int i = 0; i < 10; ++i) del.Add(i); | ||
| BitmapDeletionVector dv_del(del); | ||
| ASSERT_EQ(dv_del.GetCardinality(), 10); | ||
| ASSERT_OK(dv_del.Delete(100)); | ||
| ASSERT_EQ(dv_del.GetCardinality(), 11); | ||
| } | ||
|
|
||
| } // namespace paimon::test |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.