|
| 1 | +/* |
| 2 | + * Copyright 2026-present Alibaba Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "paimon/utils/row_range_index.h" |
| 18 | + |
| 19 | +#include <algorithm> |
| 20 | +#include <cassert> |
| 21 | + |
| 22 | +namespace paimon { |
| 23 | + |
| 24 | +RowRangeIndex::RowRangeIndex(std::vector<Range> ranges) : ranges_(std::move(ranges)) { |
| 25 | + starts_.reserve(ranges_.size()); |
| 26 | + ends_.reserve(ranges_.size()); |
| 27 | + for (const auto& range : ranges_) { |
| 28 | + starts_.push_back(range.from); |
| 29 | + ends_.push_back(range.to); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +Result<RowRangeIndex> RowRangeIndex::Create(const std::vector<Range>& ranges) { |
| 34 | + if (ranges.empty()) { |
| 35 | + return Status::Invalid("Ranges cannot be empty in RowRangeIndex"); |
| 36 | + } |
| 37 | + return RowRangeIndex(Range::SortAndMergeOverlap(ranges, /*adjacent=*/true)); |
| 38 | +} |
| 39 | + |
| 40 | +const std::vector<Range>& RowRangeIndex::Ranges() const { |
| 41 | + return ranges_; |
| 42 | +} |
| 43 | + |
| 44 | +bool RowRangeIndex::Intersects(int64_t start, int64_t end) const { |
| 45 | + int32_t candidate = LowerBound(start); |
| 46 | + return candidate < static_cast<int32_t>(starts_.size()) && starts_[candidate] <= end; |
| 47 | +} |
| 48 | + |
| 49 | +std::vector<Range> RowRangeIndex::IntersectedRanges(int64_t start, int64_t end) const { |
| 50 | + int32_t left = LowerBound(start); |
| 51 | + if (left >= static_cast<int32_t>(ranges_.size())) { |
| 52 | + return {}; |
| 53 | + } |
| 54 | + |
| 55 | + int32_t right = LowerBound(end); |
| 56 | + if (right >= static_cast<int32_t>(ranges_.size())) { |
| 57 | + right = static_cast<int32_t>(ranges_.size()) - 1; |
| 58 | + } |
| 59 | + |
| 60 | + if (starts_[left] > end) { |
| 61 | + return {}; |
| 62 | + } |
| 63 | + |
| 64 | + std::vector<Range> expected; |
| 65 | + |
| 66 | + // Add the first intersecting range, clipped to [start, end]. |
| 67 | + const Range& first_range = ranges_[left]; |
| 68 | + expected.emplace_back(std::max(start, first_range.from), std::min(end, first_range.to)); |
| 69 | + |
| 70 | + // Add all fully contained ranges between first and last. |
| 71 | + for (int32_t i = left + 1; i < right; ++i) { |
| 72 | + expected.push_back(ranges_[i]); |
| 73 | + } |
| 74 | + |
| 75 | + // Add the last intersecting range (if different from the first), clipped to [start, end]. |
| 76 | + if (right != left) { |
| 77 | + const Range& last_range = ranges_[right]; |
| 78 | + if (last_range.from <= end) { |
| 79 | + expected.emplace_back(std::max(start, last_range.from), std::min(end, last_range.to)); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return expected; |
| 84 | +} |
| 85 | + |
| 86 | +int32_t RowRangeIndex::LowerBound(int64_t target) const { |
| 87 | + int32_t left = 0; |
| 88 | + auto right = static_cast<int32_t>(ends_.size()); |
| 89 | + while (left < right) { |
| 90 | + int32_t mid = left + (right - left) / 2; |
| 91 | + if (ends_[mid] < target) { |
| 92 | + left = mid + 1; |
| 93 | + } else { |
| 94 | + right = mid; |
| 95 | + } |
| 96 | + } |
| 97 | + return left; |
| 98 | +} |
| 99 | + |
| 100 | +} // namespace paimon |
0 commit comments