| # 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 to build vector search.""" | |
| from abc import ABC, abstractmethod | |
| from copy import copy | |
| from math import isfinite | |
| from pypaimon.common.options.core_options import CoreOptions | |
| from pypaimon.common.options.vector_index_options import VectorIndexOptions | |
| from pypaimon.catalog.table_query_auth import reject_search_under_query_auth | |
| from pypaimon.common.predicate_builder import PredicateBuilder | |
| from pypaimon.common.options.options import Options | |
| from pypaimon.globalindex.vindex_options import is_multi_vector_type | |
| class VectorSearchBuilder(ABC): | |
| """Builder to build vector search.""" | |
| @abstractmethod | |
| def with_limit(self, limit): | |
| # type: (int) -> VectorSearchBuilder | |
| """The top k results to return.""" | |
| pass | |
| @abstractmethod | |
| def with_vector_column(self, name): | |
| # type: (str) -> VectorSearchBuilder | |
| """The vector column to search.""" | |
| pass | |
| @abstractmethod | |
| def with_query_vector(self, vector): | |
| # type: (list) -> VectorSearchBuilder | |
| """The query vector (list of floats).""" | |
| pass | |
| def with_multi_vector(self, vectors): | |
| """Rank each row's embedding list against a query matrix using cosine MaxSim.""" | |
| raise NotImplementedError("%s does not support MultiVector search." % type(self).__name__) | |
| def with_option(self, key, value): | |
| # type: (str, str) -> VectorSearchBuilder | |
| """Option for vector indexes.""" | |
| raise NotImplementedError( | |
| "%s does not support vector options." | |
| % self.__class__.__name__) | |
| def with_options(self, options): | |
| # type: (dict) -> VectorSearchBuilder | |
| """Options for vector indexes.""" | |
| raise NotImplementedError( | |
| "%s does not support vector options." | |
| % self.__class__.__name__) | |
| @abstractmethod | |
| def with_filter(self, predicate): | |
| # type: (Predicate) -> VectorSearchBuilder | |
| """Scalar predicate used to pre-filter rows before vector search.""" | |
| pass | |
| @abstractmethod | |
| def with_partition_filter(self, partition_filter): | |
| # type: (Predicate) -> VectorSearchBuilder | |
| """Partition predicate used to prune index manifest entries.""" | |
| pass | |
| @abstractmethod | |
| def new_vector_search_scan(self): | |
| # type: () -> VectorSearchScan | |
| """Create vector search scan to scan index files.""" | |
| pass | |
| @abstractmethod | |
| def new_vector_search_read(self): | |
| # type: () -> VectorSearchRead | |
| """Create vector search read to read index files.""" | |
| pass | |
| def execute_local(self): | |
| # type: () -> GlobalIndexResult | |
| """Execute vector search locally.""" | |
| return self.new_vector_search_read().read_plan( | |
| self.new_vector_search_scan().scan() | |
| ) | |
| class AbstractVectorSearchBuilderImpl: | |
| """Shared state and filter/partition handling for the vector search builders.""" | |
| _native_batch_search = False | |
| def __init__(self, table): | |
| self._table = table | |
| self._limit = 0 | |
| self._vector_column = None | |
| self._filter = None | |
| self._partition_filter = None | |
| self._options = {} | |
| def with_limit(self, limit): | |
| # type: (int) -> VectorSearchBuilder | |
| self._limit = limit | |
| return self | |
| def with_vector_column(self, name): | |
| # type: (str) -> VectorSearchBuilder | |
| field_dict = {f.name: f for f in self._table.fields} | |
| if name not in field_dict: | |
| raise ValueError("Vector column '%s' not found in table schema" % name) | |
| self._vector_column = field_dict[name] | |
| return self | |
| def with_option(self, key, value): | |
| # type: (str, str) -> VectorSearchBuilder | |
| return self.with_options({key: value}) | |
| def with_options(self, options): | |
| # type: (dict) -> VectorSearchBuilder | |
| if options is not None: | |
| self._options.update(options) | |
| prefilter_key = CoreOptions.SCALAR_INDEX_PREFILTER_SEARCH_MODE.key() | |
| if self._options.get(prefilter_key) is None: | |
| self._options.pop(prefilter_key, None) | |
| return self | |
| def with_filter(self, predicate): | |
| # type: (Predicate) -> VectorSearchBuilder | |
| if predicate is None: | |
| return self | |
| partition_filter, data_filter = self._split_partition_filter(predicate) | |
| if partition_filter is not None: | |
| self._add_partition_filter(partition_filter) | |
| if data_filter is not None: | |
| if self._filter is None: | |
| self._filter = data_filter | |
| else: | |
| self._filter = PredicateBuilder.and_predicates( | |
| [self._filter, data_filter]) | |
| return self | |
| def with_partition_filter(self, partition_filter): | |
| # type: (Predicate) -> VectorSearchBuilder | |
| if partition_filter is None: | |
| return self | |
| # Strict: every referenced field must be a partition key, otherwise a | |
| # non-partition conjunct would be silently dropped (with_filter has | |
| # the scalar fallback; with_partition_filter does not). | |
| partition_keys = list(self._table.partition_keys or []) | |
| if not partition_keys: | |
| raise ValueError( | |
| "with_partition_filter called on a non-partitioned table") | |
| from pypaimon.read.push_down_utils import _get_all_fields | |
| referenced = _get_all_fields(partition_filter) | |
| extras = referenced - set(partition_keys) | |
| if extras: | |
| raise ValueError( | |
| "Partition filter must reference only partition keys " | |
| "(%s); got non-partition field(s): %s" | |
| % (partition_keys, sorted(extras))) | |
| self._add_partition_filter( | |
| self._rebuild_leaf_indices_by_name( | |
| partition_filter, | |
| {name: idx for idx, name in enumerate(partition_keys)}, | |
| )) | |
| return self | |
| def _split_partition_filter(self, predicate): | |
| """Split partition-only and data conjuncts.""" | |
| partition_keys = list(self._table.partition_keys or []) | |
| if not partition_keys: | |
| return None, predicate | |
| from pypaimon.read.push_down_utils import _split_and, _get_all_fields | |
| partition_key_set = set(partition_keys) | |
| pk_to_idx = {name: idx for idx, name in enumerate(partition_keys)} | |
| partition_parts = [] | |
| data_parts = [] | |
| for part in _split_and(predicate): | |
| if _get_all_fields(part).issubset(partition_key_set): | |
| partition_parts.append( | |
| self._rebuild_leaf_indices_by_name(part, pk_to_idx)) | |
| else: | |
| data_parts.append(part) | |
| return ( | |
| PredicateBuilder.and_predicates(partition_parts), | |
| PredicateBuilder.and_predicates(data_parts), | |
| ) | |
| def _add_partition_filter(self, partition_filter): | |
| if partition_filter is None: | |
| return | |
| if self._partition_filter is None: | |
| self._partition_filter = partition_filter | |
| else: | |
| self._partition_filter = PredicateBuilder.and_predicates( | |
| [self._partition_filter, partition_filter]) | |
| @classmethod | |
| def _rebuild_leaf_indices_by_name(cls, predicate, pk_to_idx): | |
| """Return a copy of ``predicate`` with every leaf's ``index`` set to | |
| its position in ``pk_to_idx`` (field-name lookup). Input predicate may | |
| have been built against any schema — we key off ``Predicate.field`` | |
| rather than ``Predicate.index`` so positional convention doesn't | |
| matter. | |
| """ | |
| if predicate.method in ('and', 'or'): | |
| new_children = [cls._rebuild_leaf_indices_by_name(c, pk_to_idx) | |
| for c in (predicate.literals or [])] | |
| return predicate.new_literals(new_children) | |
| return predicate.new_index(pk_to_idx[predicate.field]) | |
| def new_vector_search_scan(self): | |
| # type: () -> VectorSearchScan | |
| reject_search_under_query_auth(self._table) | |
| if self._vector_column is None: | |
| raise ValueError("Vector column must be set via with_vector_column()") | |
| self._validate_query_matrix_column() | |
| from pypaimon.table.source.search_dispatch import create_vector_scan | |
| execution = copy(self) | |
| execution._options = dict(self._options) | |
| return create_vector_scan(self, execution, self._native_batch_search) | |
| def _is_primary_key_vector_search(self): | |
| if self._vector_column is None or not getattr(self._table.table_schema, 'primary_keys', None): | |
| return False | |
| return self._vector_column.name in VectorIndexOptions.columns(Options(dict(self._table.table_schema.options))) | |
| def _validate_query_matrix_column(self): | |
| if getattr(self, '_multi_vector', None) is not None and ( | |
| not self._table.options.data_evolution_enabled() | |
| or not is_multi_vector_type(getattr(self._vector_column, 'type', None))): | |
| raise ValueError("A query matrix requires a MultiVector column on a Data Evolution table.") | |
| class VectorSearchBuilderImpl(AbstractVectorSearchBuilderImpl, VectorSearchBuilder): | |
| """Implementation for VectorSearchBuilder.""" | |
| def __init__(self, table): | |
| super().__init__(table) | |
| self._query_vector = None | |
| self._multi_vector = None | |
| def with_query_vector(self, vector): | |
| # type: (list) -> VectorSearchBuilder | |
| self._query_vector = vector | |
| self._multi_vector = None | |
| return self | |
| def with_multi_vector(self, vectors): | |
| try: | |
| query = tuple(tuple(vector) for vector in vectors) | |
| valid = (query and query[0] and all(len(vector) == len(query[0]) for vector in query) | |
| and all(isfinite(value) for vector in query for value in vector)) | |
| except (TypeError, ValueError, OverflowError) as exc: | |
| raise ValueError("A query matrix requires numeric vectors with finite coordinates.") from exc | |
| if not valid: | |
| raise ValueError("A query matrix requires non-empty, equally sized vectors with finite coordinates.") | |
| self._multi_vector = query | |
| self._query_vector = None | |
| return self | |
| def execute_local(self): | |
| if self._multi_vector is not None: | |
| reject_search_under_query_auth(self._table) | |
| self._validate_query_matrix_column() | |
| from pypaimon.table.source.search_dispatch import execute_vector_search | |
| return execute_vector_search(self, self._query_vector) | |
| def new_vector_search_read(self): | |
| # type: () -> VectorSearchRead | |
| reject_search_under_query_auth(self._table) | |
| if self._limit <= 0: | |
| raise ValueError("Limit must be positive, set via with_limit()") | |
| if self._vector_column is None: | |
| raise ValueError("Vector column must be set via with_vector_column()") | |
| self._validate_query_matrix_column() | |
| if self._query_vector is None and self._multi_vector is None: | |
| raise ValueError("Query must be set via with_query_vector() or with_multi_vector()") | |
| from pypaimon.table.source.search_dispatch import create_vector_read | |
| execution = copy(self) | |
| execution._options = dict(self._options) | |
| execution._query_vector = copy(self._query_vector) | |
| return create_vector_read(execution) |