-
Notifications
You must be signed in to change notification settings - Fork 106
feat(data): coalesce position deletes into range inserts #645
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
Open
Baunsgaard
wants to merge
2
commits into
apache:main
Choose a base branch
from
Baunsgaard:coalesce-position-deletes-loader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /* | ||
| * 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 "iceberg/deletes/position_delete_range_consumer.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cstdint> | ||
| #include <span> | ||
| #include <vector> | ||
|
|
||
| #include "iceberg/deletes/position_delete_index.h" | ||
| #include "iceberg/deletes/roaring_position_bitmap.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| namespace { | ||
|
|
||
| bool IsValidPosition(int64_t pos) { | ||
| return pos >= 0 && pos <= RoaringPositionBitmap::kMaxPosition; | ||
| } | ||
|
|
||
| // Unsigned subtraction so negative or wrap-around input can't | ||
| // false-positive via signed overflow. | ||
| bool IsAdjacent(int64_t prev, int64_t next) { | ||
| return (static_cast<uint64_t>(next) - static_cast<uint64_t>(prev)) == 1; | ||
| } | ||
|
|
||
| // `RoaringPositionBitmap` shards positions by their high 32 bits; the | ||
| // bulk path groups by this key before flushing via `BulkAddForKey`. | ||
| int32_t HighKeyFromPosition(int64_t pos) { return static_cast<int32_t>(pos >> 32); } | ||
|
|
||
| // Emit `[range_start, last_position]`, collapsing singletons. Callers | ||
| // pre-filter via `IsValidPosition`, so `last_position + 1` cannot overflow. | ||
| void EmitRange(PositionDeleteIndex& target, int64_t range_start, int64_t last_position) { | ||
| if (range_start == last_position) { | ||
| target.Delete(range_start); | ||
| } else { | ||
| target.Delete(range_start, last_position + 1); | ||
| } | ||
| } | ||
|
|
||
| // Emit closed-interval runs; out-of-range positions are silently skipped | ||
| // to match `Delete(pos)`. | ||
| void CoalesceIntoRanges(std::span<const int64_t> positions, PositionDeleteIndex& target) { | ||
| const size_t n = positions.size(); | ||
|
|
||
| size_t i = 0; | ||
| while (i < n && !IsValidPosition(positions[i])) { | ||
| ++i; | ||
| } | ||
| if (i == n) { | ||
| return; | ||
| } | ||
|
|
||
| int64_t range_start = positions[i]; | ||
| int64_t last_position = range_start; | ||
| ++i; | ||
|
|
||
| for (; i < n; ++i) { | ||
| const int64_t pos = positions[i]; | ||
| if (!IsValidPosition(pos)) { | ||
| continue; | ||
| } | ||
| if (!IsAdjacent(last_position, pos)) { | ||
| EmitRange(target, range_start, last_position); | ||
| range_start = pos; | ||
| } | ||
| last_position = pos; | ||
| } | ||
|
|
||
| EmitRange(target, range_start, last_position); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| void ForEachPositionDelete(std::span<const int64_t> positions, | ||
| PositionDeleteIndex& target) { | ||
| if (positions.empty()) { | ||
| return; | ||
| } | ||
|
|
||
| // Below this size the bulk path's fixed overhead beats any coalescing win. | ||
| constexpr size_t kMinSniffSize = 64; | ||
| if (positions.size() < kMinSniffSize) { | ||
| CoalesceIntoRanges(positions, target); | ||
| return; | ||
| } | ||
|
|
||
| // Bounded prefix size for the boundary-density estimate. | ||
| constexpr size_t kSniffSize = 1024; | ||
| // Above this boundary density take the bulk path; below it stay on coalesce. | ||
| constexpr size_t kBulkThresholdPercent = 10; | ||
|
|
||
| const size_t sniff = std::min(positions.size(), kSniffSize); | ||
| size_t boundaries = 0; | ||
| for (size_t i = 1; i < sniff; ++i) { | ||
| boundaries += static_cast<size_t>(!IsAdjacent(positions[i - 1], positions[i])); | ||
| } | ||
|
|
||
| // boundaries / (sniff - 1) > kBulkThresholdPercent / 100, without FP. | ||
| if (boundaries * 100 > (sniff - 1) * kBulkThresholdPercent) { | ||
| // Bulk path: group by high-32-bit key, flush each group via CRoaring's | ||
| // `addMany` (through `BulkAddForKey`). The thread-local buffer is | ||
| // reused across calls; nested invocations on the same thread would | ||
| // corrupt it -- see `\warning` on `ForEachPositionDelete`. | ||
| thread_local std::vector<uint32_t> bulk_key_positions; | ||
| const size_t n = positions.size(); | ||
| size_t i = 0; | ||
| while (i < n) { | ||
| while (i < n && !IsValidPosition(positions[i])) { | ||
| ++i; | ||
| } | ||
| if (i == n) { | ||
| break; | ||
| } | ||
| const int32_t key = HighKeyFromPosition(positions[i]); | ||
| bulk_key_positions.clear(); | ||
| while (i < n && IsValidPosition(positions[i]) && | ||
| HighKeyFromPosition(positions[i]) == key) { | ||
| bulk_key_positions.push_back(static_cast<uint32_t>(positions[i] & 0xFFFFFFFFu)); | ||
| ++i; | ||
| } | ||
| target.BulkAddForKey(key, bulk_key_positions.data(), bulk_key_positions.size()); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| CoalesceIntoRanges(positions, target); | ||
| } | ||
|
|
||
| } // namespace iceberg | ||
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,42 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
| #include <span> | ||
|
|
||
| #include "iceberg/iceberg_data_export.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| class PositionDeleteIndex; | ||
|
|
||
| /// \brief Apply `positions` to `target` as deletes; semantically equivalent | ||
| /// to calling `target.Delete(pos)` for each entry. Out-of-range positions | ||
| /// are silently ignored. Sorted, mostly-contiguous input is fastest. | ||
| /// | ||
| /// \warning Not safe to call recursively or interleaved on the same thread: | ||
| /// the bulk dispatch path uses a thread-local staging buffer that a | ||
| /// nested invocation would corrupt. Concurrent calls on different | ||
| /// threads are safe with disjoint `target`. | ||
| void ICEBERG_DATA_EXPORT ForEachPositionDelete(std::span<const int64_t> positions, | ||
| PositionDeleteIndex& target); | ||
|
|
||
| } // namespace iceberg |
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -33,6 +33,8 @@ | |||||
|
|
||||||
| namespace iceberg { | ||||||
|
|
||||||
| class PositionDeleteIndex; | ||||||
|
|
||||||
| /// \brief A bitmap that supports positive 64-bit positions, optimized | ||||||
| /// for cases where most positions fit in 32 bits. | ||||||
| /// | ||||||
|
|
@@ -110,6 +112,12 @@ class ICEBERG_DATA_EXPORT RoaringPositionBitmap { | |||||
| std::unique_ptr<Impl> impl_; | ||||||
|
|
||||||
| explicit RoaringPositionBitmap(std::unique_ptr<Impl> impl); | ||||||
|
|
||||||
| // Bulk-add positions sharing high-32-bit `key`. Internal hook for | ||||||
| // `PositionDeleteIndex::BulkAddForKey`; per-key grouping is the caller's | ||||||
| // job, keeping this a thin wrapper around CRoaring's `addMany`. | ||||||
| void AddManyForKey(int32_t key, const uint32_t* positions, size_t n); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I'd prefer |
||||||
| friend class PositionDeleteIndex; | ||||||
| }; | ||||||
|
|
||||||
| } // namespace iceberg | ||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks hacky and limits its use. How about passing
bulk_key_positionsas a parameter so that caller can take full control over it?