leaves12138 commented on code in PR #10476:
URL: https://github.com/apache/paimon/pull/10476#discussion_r4231176134


##########
paimon-python/pypaimon/table/source/native_vector_search.py:
##########
@@ -0,0 +1,93 @@
+# 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.
+
+"""Local vector search through the Rust core Scan -> Plan -> Read path."""
+
+import logging
+
+from pypaimon.catalog.table_query_auth import reject_search_under_query_auth
+from pypaimon.globalindex.vector_search_result import 
DictBasedScoredIndexResult
+from pypaimon.read.native_plan import (
+    _catalog_metastore, _native_table, _partition_fields, _predicate_to_native,
+    _raise_if_native_fork_safety_error, native_method_available)
+from pypaimon.read.split_serializer import deserialize_split_v1
+from pypaimon.table.source.primary_key_scored_result import (
+    PrimaryKeyScoredResult, PrimaryKeySearchPosition)
+
+logger = logging.getLogger(__name__)
+
+
+def try_native_vector_search(builder, queries, batch=False):
+    """Return existing Python result types, or None to use the Python reader.
+
+    Search, scalar filtering, refinement and physical-position selection all
+    belong to Rust core. Python only converts the scored result metadata.
+    """
+    table = builder._table
+    reject_search_under_query_auth(table)
+    try:
+        native_enabled = getattr(table.options, 'native_read_enabled', None)
+        if not callable(native_enabled) or not native_enabled():
+            return None
+        loader = getattr(getattr(table, 'catalog_environment', None), 
'catalog_loader', None)
+        if loader is None or _catalog_metastore(loader) != 'rest':
+            return None
+        context_fn = getattr(loader, 'context', None)
+        if not callable(context_fn):
+            return None
+        context = context_fn()
+        if any(getattr(context, attr, None) is not None for attr in (
+                'hadoop_conf', 'prefer_io_loader', 'fallback_io_loader')):
+            return None
+        # Python's batch reader currently supports only global row-ID results.
+        if batch and builder._is_primary_key_vector_search():
+            return None
+        method = 'new_batch_vector_search_builder' if batch else 
'new_vector_search_builder'
+        if not native_method_available('Table', method):
+            return None
+        native = getattr(_native_table(table), method)()
+        if builder._vector_column is not None:
+            native.with_vector_column(builder._vector_column.name)
+        native.with_limit(builder._limit).with_options(builder._options)
+        if builder._filter is not None:
+            native.with_filter(_predicate_to_native(builder._filter))

Review Comment:
   [P2] Preserve global-index.filter.refine-from-data semantics during 
delegation
   
   With an inexact scalar-index answer, Java and this PR's classic reader 
exclude candidates when `global-index.filter.refine-from-data=false`. The 
delegated core path instead reads the data to build an exact allow-list, 
ignoring that switch and changing the existing public API's result and I/O 
behavior just by enabling Native reading.
   
   On a real REST table with a BTREE index on `name`, an IVF-flat vector index, 
`name LIKE '%zeta%'`, and limit 1, classic returns 0 hits with refinement 
disabled whereas the new Native path returns 1 (batch: `[0, 0]` versus `[1, 
1]`). All FAST/FULL/DETAIL cases reproduce it; enabled-refinement controls 
agree. Please require the companion core fix, or decline Native delegation for 
these unsupported refinement semantics.



##########
paimon-python/pypaimon/table/source/native_vector_search.py:
##########
@@ -0,0 +1,93 @@
+# 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.
+
+"""Local vector search through the Rust core Scan -> Plan -> Read path."""
+
+import logging
+
+from pypaimon.catalog.table_query_auth import reject_search_under_query_auth
+from pypaimon.globalindex.vector_search_result import 
DictBasedScoredIndexResult
+from pypaimon.read.native_plan import (
+    _catalog_metastore, _native_table, _partition_fields, _predicate_to_native,
+    _raise_if_native_fork_safety_error, native_method_available)
+from pypaimon.read.split_serializer import deserialize_split_v1
+from pypaimon.table.source.primary_key_scored_result import (
+    PrimaryKeyScoredResult, PrimaryKeySearchPosition)
+
+logger = logging.getLogger(__name__)
+
+
+def try_native_vector_search(builder, queries, batch=False):
+    """Return existing Python result types, or None to use the Python reader.
+
+    Search, scalar filtering, refinement and physical-position selection all
+    belong to Rust core. Python only converts the scored result metadata.
+    """
+    table = builder._table
+    reject_search_under_query_auth(table)
+    try:
+        native_enabled = getattr(table.options, 'native_read_enabled', None)
+        if not callable(native_enabled) or not native_enabled():
+            return None
+        loader = getattr(getattr(table, 'catalog_environment', None), 
'catalog_loader', None)
+        if loader is None or _catalog_metastore(loader) != 'rest':
+            return None
+        context_fn = getattr(loader, 'context', None)
+        if not callable(context_fn):
+            return None
+        context = context_fn()
+        if any(getattr(context, attr, None) is not None for attr in (
+                'hadoop_conf', 'prefer_io_loader', 'fallback_io_loader')):
+            return None
+        # Python's batch reader currently supports only global row-ID results.
+        if batch and builder._is_primary_key_vector_search():
+            return None
+        method = 'new_batch_vector_search_builder' if batch else 
'new_vector_search_builder'
+        if not native_method_available('Table', method):
+            return None
+        native = getattr(_native_table(table), method)()
+        if builder._vector_column is not None:
+            native.with_vector_column(builder._vector_column.name)
+        native.with_limit(builder._limit).with_options(builder._options)
+        if builder._filter is not None:
+            native.with_filter(_predicate_to_native(builder._filter))
+        if builder._partition_filter is not None:
+            
native.with_partition_filter(_predicate_to_native(builder._partition_filter))
+        if batch:
+            native.with_query_vectors(queries)
+            results = native.execute_batch_local()
+            return [DictBasedScoredIndexResult(result.row_ids()) for result in 
results]
+        native.with_query_vector(queries)
+        result = native.execute_local()

Review Comment:
   [P1] Do not enable Native DE filtering before matching Java's raw fallback
   
   For a scalar predicate that cannot be evaluated by a scalar index, the 
classic reader added in this PR correctly follows Java 
`AbstractDataEvolutionVectorRead.prepareSplits`: it reroutes 
vector-index-covered ranges to raw scoring. This new delegation bypasses that 
policy and accepts an empty Native ANN result as a successful result, so the 
exception fallback does not help.
   
   Using a real REST IVF-flat index with four clusters, `ivf.nprobe=1`, query 
`[0, 0]`, limit 3, and `id >= 100` without an `id` scalar index, the classic 
path returns 3 hits while the new public Native path returns 0. This reproduces 
for single/batch and FAST/FULL/DETAIL; probing all four clusters recovers the 
three Native hits. The core issue is detailed on apache/paimon-rust#1087. 
Please land the Java-equivalent core routing before enabling these filtered 
searches, or keep unsupported cases on the classic lane until then.



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