|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/deletes/position_delete_range_consumer.h" |
| 21 | + |
| 22 | +#include <algorithm> |
| 23 | +#include <cstdint> |
| 24 | +#include <span> |
| 25 | +#include <vector> |
| 26 | + |
| 27 | +#include "iceberg/deletes/position_delete_index.h" |
| 28 | +#include "iceberg/deletes/roaring_position_bitmap.h" |
| 29 | + |
| 30 | +namespace iceberg { |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +bool IsValidPosition(int64_t pos) { |
| 35 | + return pos >= 0 && pos <= RoaringPositionBitmap::kMaxPosition; |
| 36 | +} |
| 37 | + |
| 38 | +// Unsigned subtraction so negative or wrap-around input can't |
| 39 | +// false-positive via signed overflow. |
| 40 | +bool IsAdjacent(int64_t prev, int64_t next) { |
| 41 | + return (static_cast<uint64_t>(next) - static_cast<uint64_t>(prev)) == 1; |
| 42 | +} |
| 43 | + |
| 44 | +// `RoaringPositionBitmap` shards positions by their high 32 bits; the |
| 45 | +// bulk path groups by this key before flushing via `BulkAddForKey`. |
| 46 | +int32_t HighKeyFromPosition(int64_t pos) { |
| 47 | + return static_cast<int32_t>(pos >> 32); |
| 48 | +} |
| 49 | + |
| 50 | +// Emit `[range_start, last_position]`, collapsing singletons. Callers |
| 51 | +// pre-filter via `IsValidPosition`, so `last_position + 1` cannot overflow. |
| 52 | +void EmitRange(PositionDeleteIndex& target, int64_t range_start, |
| 53 | + int64_t last_position) { |
| 54 | + if (range_start == last_position) { |
| 55 | + target.Delete(range_start); |
| 56 | + } else { |
| 57 | + target.Delete(range_start, last_position + 1); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Emit closed-interval runs; out-of-range positions are silently skipped |
| 62 | +// to match `Delete(pos)`. |
| 63 | +void CoalesceIntoRanges(std::span<const int64_t> positions, |
| 64 | + PositionDeleteIndex& target) { |
| 65 | + const size_t n = positions.size(); |
| 66 | + |
| 67 | + size_t i = 0; |
| 68 | + while (i < n && !IsValidPosition(positions[i])) { |
| 69 | + ++i; |
| 70 | + } |
| 71 | + if (i == n) { |
| 72 | + return; |
| 73 | + } |
| 74 | + |
| 75 | + int64_t range_start = positions[i]; |
| 76 | + int64_t last_position = range_start; |
| 77 | + ++i; |
| 78 | + |
| 79 | + for (; i < n; ++i) { |
| 80 | + const int64_t pos = positions[i]; |
| 81 | + if (!IsValidPosition(pos)) { |
| 82 | + continue; |
| 83 | + } |
| 84 | + if (!IsAdjacent(last_position, pos)) { |
| 85 | + EmitRange(target, range_start, last_position); |
| 86 | + range_start = pos; |
| 87 | + } |
| 88 | + last_position = pos; |
| 89 | + } |
| 90 | + |
| 91 | + EmitRange(target, range_start, last_position); |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace |
| 95 | + |
| 96 | +void ForEachPositionDelete(std::span<const int64_t> positions, |
| 97 | + PositionDeleteIndex& target) { |
| 98 | + if (positions.empty()) { |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + // Below this size the bulk path's fixed overhead exceeds coalescing |
| 103 | + // even on fully scattered input; skip the sniff. |
| 104 | + constexpr size_t kMinSniffSize = 64; |
| 105 | + if (positions.size() < kMinSniffSize) { |
| 106 | + CoalesceIntoRanges(positions, target); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + // Estimate boundary density (fraction of adjacent pairs where |
| 111 | + // `pos[i] != pos[i-1] + 1`) over a bounded prefix. Misclassification is |
| 112 | + // performance-only -- both paths produce identical contents. |
| 113 | + constexpr size_t kSniffSize = 1024; |
| 114 | + // 10% threshold: boundary-heavy inputs go to bulk addMany; run-heavy |
| 115 | + // inputs stay on coalesce where Roaring's addRange collapses runs. |
| 116 | + constexpr size_t kBulkThresholdPercent = 10; |
| 117 | + |
| 118 | + const size_t sniff = std::min(positions.size(), kSniffSize); |
| 119 | + size_t boundaries = 0; |
| 120 | + for (size_t i = 1; i < sniff; ++i) { |
| 121 | + boundaries += static_cast<size_t>(!IsAdjacent(positions[i - 1], positions[i])); |
| 122 | + } |
| 123 | + |
| 124 | + // boundaries / (sniff - 1) > kBulkThresholdPercent / 100, without FP. |
| 125 | + if (boundaries * 100 > (sniff - 1) * kBulkThresholdPercent) { |
| 126 | + // Bulk path: group by high-32-bit key, flush each group via CRoaring's |
| 127 | + // `addMany` (through `BulkAddForKey`). The thread-local buffer is |
| 128 | + // reused across calls; nested invocations on the same thread would |
| 129 | + // corrupt it -- see `\warning` on `ForEachPositionDelete`. |
| 130 | + thread_local std::vector<uint32_t> bulk_key_positions; |
| 131 | + const size_t n = positions.size(); |
| 132 | + size_t i = 0; |
| 133 | + while (i < n) { |
| 134 | + while (i < n && !IsValidPosition(positions[i])) { |
| 135 | + ++i; |
| 136 | + } |
| 137 | + if (i == n) { |
| 138 | + break; |
| 139 | + } |
| 140 | + const int32_t key = HighKeyFromPosition(positions[i]); |
| 141 | + bulk_key_positions.clear(); |
| 142 | + while (i < n && IsValidPosition(positions[i]) && |
| 143 | + HighKeyFromPosition(positions[i]) == key) { |
| 144 | + bulk_key_positions.push_back( |
| 145 | + static_cast<uint32_t>(positions[i] & 0xFFFFFFFFu)); |
| 146 | + ++i; |
| 147 | + } |
| 148 | + target.BulkAddForKey(key, bulk_key_positions.data(), |
| 149 | + bulk_key_positions.size()); |
| 150 | + } |
| 151 | + return; |
| 152 | + } |
| 153 | + |
| 154 | + CoalesceIntoRanges(positions, target); |
| 155 | +} |
| 156 | + |
| 157 | +} // namespace iceberg |
0 commit comments