Elffuss BraidHub

kikocis/lancedb public

feat(python): allow explicit hybrid search query pattern in SaaS (feat parity) (#1698)

-  fixes https://github.com/lancedb/lancedb/issues/1697.
- unifies vector column inference logic for remote and local table to
prevent future disparities.
- Updates docstring in RemoteTable to specify empty queries are not
supported
Changesetffc27a8ff454402e1a15f587006a2fd430fcc89d38fb998fe0219aaca67e8620
AuthorAyush Chaurasia <ayush.chaurarsia@gmail.com> · 2024-09-25 21:04 +05:30
CommitterGitHub <noreply@github.com> · 2024-09-25 21:04 +05:30
Parentsf371856ec79a

Browse the files at this changeset

3 files changed

modified python/python/lancedb/remote/table.py +11 −9
@@ -26,7 +26,7 @@
2626
2727 from ..query import LanceVectorQueryBuilder, LanceQueryBuilder
2828 from ..table import Query, Table, _sanitize_data
29-from ..util import inf_vector_column_query, value_to_sql
29+from ..util import value_to_sql, infer_vector_column_name
3030 from .arrow import to_ipc_binary
3131 from .client import ARROW_STREAM_CONTENT_TYPE
3232 from .db import RemoteDBConnection
@@ -266,7 +266,7 @@
266266
267267 def search(
268268 self,
269- query: Union[VEC, str],
269+ query: Union[VEC, str] = None,
270270 vector_column_name: Optional[str] = None,
271271 query_type="auto",
272272 fts_columns: Optional[Union[str, List[str]]] = None,
@@ -305,8 +305,6 @@
305305 - *default None*.
306306 Acceptable types are: list, np.ndarray, PIL.Image.Image
307307
308- - If None then the select/where/limit clauses are applied to filter
309- the table
310308 vector_column_name: str, optional
311309 The name of the vector column to search.
312310
@@ -329,11 +327,15 @@
329327 - and also the "_distance" column which is the distance between the query
330328 vector and the returned vector.
331329 """
332- if vector_column_name is None and query is not None and query_type != "fts":
333- try:
334- vector_column_name = inf_vector_column_query(self.schema)
335- except Exception as e:
336- raise e
330+ # empty query builder is not supported in saas, raise error
331+ if query is None and query_type != "hybrid":
332+ raise ValueError("Empty query is not supported")
333+ vector_column_name = infer_vector_column_name(
334+ schema=self.schema,
335+ query_type=query_type,
336+ query=query,
337+ vector_column_name=vector_column_name,
338+ )
337339
338340 return LanceQueryBuilder.create(
339341 self,
modified python/python/lancedb/table.py +7 −8
@@ -50,7 +50,7 @@
5050 from .util import (
5151 fs_from_uri,
5252 get_uri_scheme,
53- inf_vector_column_query,
53+ infer_vector_column_name,
5454 join_uri,
5555 safe_import_pandas,
5656 safe_import_polars,
@@ -1630,13 +1630,12 @@
16301630 and also the "_distance" column which is the distance between the query
16311631 vector and the returned vector.
16321632 """
1633- if (
1634- vector_column_name is None and query is not None and query_type != "fts"
1635- ) or (vector_column_name is None and query_type == "hybrid"):
1636- try:
1637- vector_column_name = inf_vector_column_query(self.schema)
1638- except Exception as e:
1639- raise e
1633+ vector_column_name = infer_vector_column_name(
1634+ schema=self.schema,
1635+ query_type=query_type,
1636+ query=query,
1637+ vector_column_name=vector_column_name,
1638+ )
16401639
16411640 return LanceQueryBuilder.create(
16421641 self,
modified python/python/lancedb/util.py +18 −1
@@ -9,7 +9,7 @@
99 import warnings
1010 from datetime import date, datetime
1111 from functools import singledispatch
12-from typing import Tuple, Union
12+from typing import Tuple, Union, Optional, Any
1313 from urllib.parse import urlparse
1414
1515 import numpy as np
@@ -212,6 +212,23 @@
212212 return vector_col_name
213213
214214
215+def infer_vector_column_name(
216+ schema: pa.Schema,
217+ query_type: str,
218+ query: Optional[Any], # inferred later in query builder
219+ vector_column_name: Optional[str],
220+):
221+ if (vector_column_name is None and query is not None and query_type != "fts") or (
222+ vector_column_name is None and query_type == "hybrid"
223+ ):
224+ try:
225+ vector_column_name = inf_vector_column_query(schema)
226+ except Exception as e:
227+ raise e
228+
229+ return vector_column_name
230+
231+
215232 @singledispatch
216233 def value_to_sql(value):
217234 raise NotImplementedError("SQL conversion is not implemented for this type")