|
| 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, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +################################################################################ |
| 18 | + |
| 19 | +"""A GlobalIndexReader that wraps another reader and applies an offset to all row IDs.""" |
| 20 | + |
| 21 | +from typing import List, Optional |
| 22 | + |
| 23 | +from pypaimon.globalindex.global_index_reader import GlobalIndexReader, FieldRef |
| 24 | +from pypaimon.globalindex.global_index_result import GlobalIndexResult |
| 25 | + |
| 26 | + |
| 27 | +class OffsetGlobalIndexReader(GlobalIndexReader): |
| 28 | + """ |
| 29 | + A GlobalIndexReader that wraps another reader and applies an offset |
| 30 | + to all row IDs in the results. |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__(self, wrapped: GlobalIndexReader, offset: int, to: int): |
| 34 | + self._wrapped = wrapped |
| 35 | + self._offset = offset |
| 36 | + self._to = to |
| 37 | + |
| 38 | + def visit_vector_search(self, vector_search) -> Optional[GlobalIndexResult]: |
| 39 | + result = self._wrapped.visit_vector_search( |
| 40 | + vector_search.offset_range(self._offset, self._to)) |
| 41 | + if result is not None: |
| 42 | + return result.offset(self._offset) |
| 43 | + return None |
| 44 | + |
| 45 | + def visit_full_text_search(self, full_text_search) -> Optional[GlobalIndexResult]: |
| 46 | + result = self._wrapped.visit_full_text_search(full_text_search) |
| 47 | + if result is not None: |
| 48 | + return result.offset(self._offset) |
| 49 | + return None |
| 50 | + |
| 51 | + def visit_equal(self, field_ref: FieldRef, literal: object) -> Optional[GlobalIndexResult]: |
| 52 | + return self._apply_offset(self._wrapped.visit_equal(field_ref, literal)) |
| 53 | + |
| 54 | + def visit_not_equal(self, field_ref: FieldRef, literal: object) -> Optional[GlobalIndexResult]: |
| 55 | + return self._apply_offset(self._wrapped.visit_not_equal(field_ref, literal)) |
| 56 | + |
| 57 | + def visit_less_than(self, field_ref: FieldRef, literal: object) -> Optional[GlobalIndexResult]: |
| 58 | + return self._apply_offset(self._wrapped.visit_less_than(field_ref, literal)) |
| 59 | + |
| 60 | + def visit_less_or_equal(self, field_ref: FieldRef, literal: object) -> Optional[GlobalIndexResult]: |
| 61 | + return self._apply_offset(self._wrapped.visit_less_or_equal(field_ref, literal)) |
| 62 | + |
| 63 | + def visit_greater_than(self, field_ref: FieldRef, literal: object) -> Optional[GlobalIndexResult]: |
| 64 | + return self._apply_offset(self._wrapped.visit_greater_than(field_ref, literal)) |
| 65 | + |
| 66 | + def visit_greater_or_equal(self, field_ref: FieldRef, literal: object) -> Optional[GlobalIndexResult]: |
| 67 | + return self._apply_offset(self._wrapped.visit_greater_or_equal(field_ref, literal)) |
| 68 | + |
| 69 | + def visit_is_null(self, field_ref: FieldRef) -> Optional[GlobalIndexResult]: |
| 70 | + return self._apply_offset(self._wrapped.visit_is_null(field_ref)) |
| 71 | + |
| 72 | + def visit_is_not_null(self, field_ref: FieldRef) -> Optional[GlobalIndexResult]: |
| 73 | + return self._apply_offset(self._wrapped.visit_is_not_null(field_ref)) |
| 74 | + |
| 75 | + def visit_in(self, field_ref: FieldRef, literals: List[object]) -> Optional[GlobalIndexResult]: |
| 76 | + return self._apply_offset(self._wrapped.visit_in(field_ref, literals)) |
| 77 | + |
| 78 | + def visit_not_in(self, field_ref: FieldRef, literals: List[object]) -> Optional[GlobalIndexResult]: |
| 79 | + return self._apply_offset(self._wrapped.visit_not_in(field_ref, literals)) |
| 80 | + |
| 81 | + def visit_starts_with(self, field_ref: FieldRef, literal: object) -> Optional[GlobalIndexResult]: |
| 82 | + return self._apply_offset(self._wrapped.visit_starts_with(field_ref, literal)) |
| 83 | + |
| 84 | + def visit_ends_with(self, field_ref: FieldRef, literal: object) -> Optional[GlobalIndexResult]: |
| 85 | + return self._apply_offset(self._wrapped.visit_ends_with(field_ref, literal)) |
| 86 | + |
| 87 | + def visit_contains(self, field_ref: FieldRef, literal: object) -> Optional[GlobalIndexResult]: |
| 88 | + return self._apply_offset(self._wrapped.visit_contains(field_ref, literal)) |
| 89 | + |
| 90 | + def visit_like(self, field_ref: FieldRef, literal: object) -> Optional[GlobalIndexResult]: |
| 91 | + return self._apply_offset(self._wrapped.visit_like(field_ref, literal)) |
| 92 | + |
| 93 | + def visit_between(self, field_ref: FieldRef, min_v: object, max_v: object) -> Optional[GlobalIndexResult]: |
| 94 | + return self._apply_offset(self._wrapped.visit_between(field_ref, min_v, max_v)) |
| 95 | + |
| 96 | + def _apply_offset(self, result: Optional[GlobalIndexResult]) -> Optional[GlobalIndexResult]: |
| 97 | + if result is not None: |
| 98 | + return result.offset(self._offset) |
| 99 | + return None |
| 100 | + |
| 101 | + def close(self) -> None: |
| 102 | + self._wrapped.close() |
0 commit comments