wangzhigang1999 commented on code in PR #11254:
URL: https://github.com/apache/paimon/pull/11254#discussion_r4623415200


##########
paimon-python/pypaimon/execution/python/read/search/vector_search_read.py:
##########
@@ -356,20 +369,27 @@ def _read_raw_search(self, raw_row_ranges, pre_filter, 
query_vector,
         if not raw_row_ranges:
             return DictBasedScoredIndexResult({})
 
-        table = self._read_raw_arrow(raw_row_ranges, include_filter, snapshot)
-        if table is None or table.num_rows == 0:
-            return DictBasedScoredIndexResult({})
+        table_read: "TableRead"
+        splits: "List[Split]"
+        table_read, splits = self._plan_raw_read(raw_row_ranges, 
include_filter, snapshot)
+        metric = self._search_metric(index_type)
+        workers = min(len(splits), table_read._resolve_parallelism(None, 
len(splits)))
+        if workers <= 1:
+            source = _open_raw_batch_reader(table_read, splits)
+        else:
+            source = closing(_parallel_raw_batches(table_read, splits, 
workers))
 
-        return self._score_raw_batches(
-            table.to_batches(max_chunksize=_score_block_size(query_vector)),
-            query_vector, self._search_metric(index_type), score_candidates)
+        with source as batches:
+            return self._score_raw_batches(batches, query_vector, metric, 
score_candidates)
 
     def _score_raw_batches(self, batches, query_vector, metric, 
score_candidates=None, reject_nan=False):
         """Score bounded Arrow batches, shared by local and distributed 
reads."""
         import math
 
         top_k_heap = []
-        for block in batches:
+        # Bound the Arrow take as well as the scoring matrix for wide vectors.
+        blocks = batches if score_candidates is None else 
_slice_batches(batches, _score_block_size(query_vector))
+        for block in blocks:

Review Comment:
   Thanks for catching this. I could reproduce the previous Arrow buffer still 
being held when the reader was asked for the next batch. Fixed in 
eafb7eb14660376f3b00fb4c48f8b2474f0944ff. The regression test checks the buffer 
lifetime with filtering off, with a matching candidate, and with no matches.



##########
paimon-python/pypaimon/execution/python/read/search/vector_search_read.py:
##########
@@ -848,6 +868,67 @@ def _score_raw_batch_queries(self, batches, metric):
         return [_scored_result(heap) for heap in heaps]
 
 
+def _open_raw_batch_reader(table_read: "TableRead", splits: "List[Split]") -> 
"_ClosableArrowBatchReader":
+    from pypaimon.read.read_support import _ClosableArrowBatchReader
+
+    reader, source = table_read._new_arrow_batch_reader(splits)
+    return _ClosableArrowBatchReader(reader, source)
+
+
+def _parallel_raw_batches(
+    table_read: "TableRead", splits: "List[Split]", workers: int,
+) -> "_BatchStream":
+    """Yield ready batches, with at most one outstanding read per split 
group."""
+    # Readers may be suspended after yielding. Join all in-flight reads
+    # before closing their iterators, including when scoring fails.
+    with ExitStack() as streams, ThreadPoolExecutor(max_workers=workers) as 
executor:
+        pending: "Dict[Future[Optional[pa.RecordBatch]], 
Iterator[pa.RecordBatch]]" = {}
+        try:
+            for i in range(workers):
+                group = splits[i::workers]
+                batches = _open_prefetched_batches(table_read, group, streams)
+                pending[executor.submit(next, batches, None)] = batches
+            while pending:
+                completed = wait(pending, return_when=FIRST_COMPLETED).done
+                while completed:
+                    future = completed.pop()
+                    batches = pending.pop(future)
+                    batch = future.result()
+                    if batch is None:
+                        continue
+                    yield batch
+                    # Keep completed batches bounded, and let ready
+                    # readers progress while another reader is slow.
+                    del batch, future
+                    pending[executor.submit(next, batches, None)] = batches
+        finally:
+            for future in pending:
+                future.cancel()
+
+
+def _open_prefetched_batches(
+    table_read: "TableRead", splits: "List[Split]", streams: ExitStack,
+) -> "_BatchStream":
+    """Open on the caller thread and register cleanup before submitting the 
first read."""
+    reader_scope = streams.enter_context(ExitStack())
+    reader = reader_scope.enter_context(_open_raw_batch_reader(table_read, 
splits))

Review Comment:
   I tried this ordering. You’re right about the overlap: on split groups with 
multiple batches, submitting the next read before scoring brought the first 
batch to the scorer 6–24% sooner across the local Parquet and OSS cases. The 
caller gets its Top-K after the scan finishes, though, and median end-to-end 
times changed by less than 5% in all six cases (five measured runs each).
   
   The deciding case for me was 32 single-batch splits on OSS. Both versions 
passed 20/20 runs under a 6 GiB virtual address space limit, but median peak 
RSS rose from 299 to 347 MiB with early submission. The current lookahead 
reaches EOF and closes each reader before handing its last batch to the scorer. 
With early submission, that close can still be waiting on a worker. Memory 
results varied by layout (early submission did save 98 MiB on the large local 
table), so I’m keeping the current ordering for this PR to preserve early close 
on small splits. Thanks for suggesting the comparison.



-- 
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]

Reply via email to