vibex-wang commented on code in PR #9315:
URL: https://github.com/apache/paimon/pull/9315#discussion_r4120617826


##########
paimon-python/pypaimon/tests/benchmark_vector_search_standalone.py:
##########
@@ -0,0 +1,247 @@
+# 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.
+
+"""
+Standalone benchmark for Phase 0 vector search optimizations.
+No pypaimon imports needed — tests the algorithm directly.
+
+Usage:
+    python3 benchmark_vector_search_standalone.py
+    python3 benchmark_vector_search_standalone.py --num-rows 100000 --dim 768
+"""
+
+import argparse
+import time
+
+import numpy as np
+
+
+# ============================================================
+# Original pure-Python implementation (copied from vector_search_read.py)
+# ============================================================
+
+def _compute_score_python(query, stored, metric):
+    if metric == "l2":
+        sum_sq = 0.0
+        for q, s in zip(query, stored):
+            diff = float(q) - float(s)
+            sum_sq += diff * diff
+        return 1.0 / (1.0 + sum_sq)
+    if metric == "cosine":
+        dot = 0.0
+        norm_a = 0.0
+        norm_b = 0.0
+        for q, s in zip(query, stored):
+            q = float(q)
+            s = float(s)
+            dot += q * s
+            norm_a += q * q
+            norm_b += s * s
+        denominator = (norm_a ** 0.5) * (norm_b ** 0.5)
+        return 0.0 if denominator == 0 else dot / denominator
+    if metric == "inner_product":
+        return sum(float(q) * float(s) for q, s in zip(query, stored))
+    raise ValueError("Unknown metric: %s" % metric)
+
+
+def raw_search_python(row_ids, vectors, query_vector, metric, limit):
+    """Original pure-Python raw search with heap."""
+    import heapq
+    top_k_heap = []
+    for row_id, stored in zip(row_ids, vectors):
+        if stored is None:
+            continue
+        score = _compute_score_python(query_vector, stored, metric)
+        entry = (score, -row_id, row_id)
+        if len(top_k_heap) < limit:
+            heapq.heappush(top_k_heap, entry)
+        elif entry[:2] > top_k_heap[0][:2]:
+            heapq.heapreplace(top_k_heap, entry)
+    return {row_id: score for score, _, row_id in top_k_heap}
+
+
+# ============================================================
+# New numpy-vectorized implementation
+# ============================================================
+
+def raw_search_numpy(row_ids_list, vectors_list, query_vector, metric, limit):
+    """Numpy-vectorized raw search."""
+    # Filter nulls.
+    filtered = [(rid, vec) for rid, vec in zip(row_ids_list, vectors_list)
+                if vec is not None]
+    if not filtered:
+        return {}
+
+    filtered_ids, filtered_vecs = zip(*filtered)
+    row_id_array = np.array(filtered_ids, dtype=np.int64)
+    stored_matrix = np.array(filtered_vecs, dtype=np.float32)
+    query_np = np.asarray(query_vector, dtype=np.float32)
+
+    return _numpy_distance_topk(row_id_array, stored_matrix, query_np, metric, 
limit)
+
+
+def raw_search_numpy_fast(row_id_array, stored_matrix, query_np, metric, 
limit):
+    """Numpy fast path: data already in numpy arrays (simulates Arrow 
zero-copy)."""
+    return _numpy_distance_topk(row_id_array, stored_matrix, query_np, metric, 
limit)
+
+
+def _numpy_distance_topk(row_id_array, stored_matrix, query_np, metric, limit):

Review Comment:
   Just pushed, please take another look.



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