Copilot commented on code in PR #6995: URL: https://github.com/apache/paimon/pull/6995#discussion_r2688766080
########## paimon-python/pypaimon/globalindex/global_index_scan_builder.py: ########## @@ -0,0 +1,204 @@ +################################################################################ +# 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. +################################################################################ + +"""Builder for scanning global indexes.""" + +from abc import ABC, abstractmethod +from typing import List, Optional, Collection +from concurrent.futures import ThreadPoolExecutor, as_completed + +from pypaimon.globalindex import GlobalIndexIOMeta, GlobalIndexReader, GlobalIndexEvaluator +from pypaimon.globalindex.range import Range +from pypaimon.globalindex.global_index_result import GlobalIndexResult + + +class GlobalIndexScanBuilder(ABC): + """Builder for scanning global indexes.""" + + @abstractmethod + def with_snapshot(self, snapshot_or_id) -> 'GlobalIndexScanBuilder': + """Set the snapshot to scan.""" + pass + + @abstractmethod + def with_partition_predicate(self, partition_predicate) -> 'GlobalIndexScanBuilder': + """Set the partition predicate.""" + pass + + @abstractmethod + def with_row_range(self, row_range: Range) -> 'GlobalIndexScanBuilder': + """Set the row range to scan.""" + pass + + @abstractmethod + def build(self) -> 'RowRangeGlobalIndexScanner': + """Build the scanner.""" + pass + + @abstractmethod + def shard_list(self) -> List[Range]: + """Return sorted and non-overlapping ranges.""" + pass + + @staticmethod + def parallel_scan( + ranges: List[Range], + builder: 'GlobalIndexScanBuilder', + filter_predicate: Optional['Predicate'], + vector_search: Optional['VectorSearch'], + thread_num: Optional[int] = None + ) -> Optional[GlobalIndexResult]: + + if not ranges: + return None + + scanners = [] + try: + # Build scanners for each range + for row_range in ranges: + scanner = builder.with_row_range(row_range).build() + scanners.append((row_range, scanner)) + + # Execute scans in parallel + results: List[Optional[GlobalIndexResult]] = [None] * len(ranges) + + def scan_range(idx: int, scanner: 'RowRangeGlobalIndexScanner') -> tuple: + result = scanner.scan(filter_predicate, vector_search) + return idx, result + + with ThreadPoolExecutor(max_workers=thread_num) as executor: Review Comment: The `thread_num` parameter can be None, which allows ThreadPoolExecutor to use its default behavior (typically 5x the number of CPU cores). While this is acceptable, consider documenting this behavior or setting a reasonable maximum to prevent resource exhaustion when many ranges are scanned in parallel. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
