Skip to content

Commit c36f221

Browse files
authored
Python: Fix OracleCollection search with multiple filters (microsoft#14541)
### Motivation and Context `OracleCollection._inner_search_vector` only handled a single filter. With a list of filters, `_build_filter` returns a list of `(sql, binds)` tuples and the code raised `AttributeError: 'tuple' object has no attribute 'values'`. Each filter was also parsed with its own `BindCounter`, so bind names would have collided (`:bind_val1` in every filter). Fixes microsoft#14539 ### Description Override `_build_filter` in `OracleCollection` so all filters share one `BindCounter` and are combined with AND into one `(sql, binds)` tuple, as `VectorSearchOptions` documents. A single filter returns the same tuple as before. Added two tests in `test_oracle.py`: multiple filters (fails on main with the AttributeError) and a single filter regression check. Checked locally: `tests/unit/connectors/memory/test_oracle.py` passes, ruff check and format are clean, mypy is clean on `oracle.py`. No database was needed, the tests build the query only. ### Contribution Checklist - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄
1 parent 530f2e8 commit c36f221

2 files changed

Lines changed: 67 additions & 2 deletions

File tree

‎python/semantic_kernel/connectors/oracle.py‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
import re
1010
import sys
1111
import uuid
12-
from collections.abc import AsyncIterable, Mapping, Sequence
12+
from collections.abc import AsyncIterable, Callable, Mapping, Sequence
13+
from inspect import getsource
1314
from typing import Any, ClassVar, Final, Generic, TypeVar
1415

1516
# Third-party Libraries
@@ -26,6 +27,7 @@
2627
GetFilteredRecordOptions,
2728
IndexKind,
2829
KernelSearchResults,
30+
LambdaVisitor,
2931
SearchType,
3032
VectorSearch,
3133
VectorSearchOptions,
@@ -45,7 +47,7 @@
4547

4648
# Semantic Kernel Utilities & Config
4749
from semantic_kernel.kernel_pydantic import KernelBaseSettings
48-
from semantic_kernel.kernel_types import OneOrMany
50+
from semantic_kernel.kernel_types import OneOrMany, OptionalOneOrMany
4951
from semantic_kernel.utils.feature_stage_decorator import release_candidate
5052

5153
oracledb.defaults.fetch_lobs = False
@@ -1010,6 +1012,29 @@ async def _inner_search_vector(
10101012
bind_values.extend(filter_binds)
10111013
return query, bind_values, columns
10121014

1015+
@override
1016+
def _build_filter(self, search_filter: OptionalOneOrMany[Callable | str] | None) -> OptionalOneOrMany[Any]:
1017+
"""Build one (sql_expression, bind_values_dict) tuple from one or more filters.
1018+
1019+
Multiple filters are combined with AND. All filters share one bind counter, so the generated
1020+
bind variable names stay unique across the combined WHERE clause.
1021+
"""
1022+
if not search_filter:
1023+
return None
1024+
filters = search_filter if isinstance(search_filter, list) else [search_filter]
1025+
bind_counter = BindCounter()
1026+
visitor = LambdaVisitor(lambda node: self._lambda_parser(node, bind_counter))
1027+
for filter_ in filters:
1028+
visitor.visit(ast.parse(filter_ if isinstance(filter_, str) else getsource(filter_).strip())) # type: ignore[arg-type]
1029+
if not visitor.output_filters:
1030+
raise VectorStoreOperationException("No filter strings found.")
1031+
if len(visitor.output_filters) == 1:
1032+
return visitor.output_filters[0]
1033+
combined_binds: dict[str, Any] = {}
1034+
for _, binds in visitor.output_filters:
1035+
combined_binds.update(binds)
1036+
return (" AND ".join(f"({sql})" for sql, _ in visitor.output_filters), combined_binds)
1037+
10131038
@override
10141039
def _lambda_parser(self, node: ast.AST, bind_counter: BindCounter | None = None) -> Any:
10151040
"""Parse a lambda AST node and return a tuple: (sql_expression, bind_values_dict).

‎python/tests/unit/connectors/memory/test_oracle.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from semantic_kernel.data.vector import (
1616
DistanceFunction,
1717
IndexKind,
18+
VectorSearchOptions,
1819
VectorStoreCollectionDefinition,
1920
VectorStoreField,
2021
vectorstoremodel,
@@ -419,3 +420,42 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):
419420
assert len(results) == 1
420421
assert results[0].record.id == 1
421422
assert results[0].record.vector == [0.1, 0.2, 0.3]
423+
424+
425+
@vectorstoremodel
426+
@dataclass
427+
class FilterModel:
428+
id: Annotated[int, VectorStoreField("key")]
429+
color: Annotated[str, VectorStoreField("data", is_indexed=True)]
430+
size: Annotated[int, VectorStoreField("data", is_indexed=True)]
431+
vector: Annotated[
432+
list[float] | None,
433+
VectorStoreField("vector", type="float", dimensions=3, distance_function=DistanceFunction.COSINE_DISTANCE),
434+
] = None
435+
436+
437+
@pytest.mark.asyncio
438+
async def test_search_multiple_filters_are_combined_with_and(oracle_store):
439+
collection = oracle_store.get_collection(
440+
model=FilterModel, record_type=FilterModel, collection_name="MY_COLLECTION"
441+
)
442+
options = VectorSearchOptions(
443+
vector_property_name="vector",
444+
filter=["lambda x: x.color == 'red'", "lambda x: x.size > 5 or x.size < 2"],
445+
)
446+
sql, binds, _ = await collection._inner_search_vector(options, None, [0.1, 0.2, 0.3])
447+
448+
assert 'WHERE ("color" = :bind_val1) AND (("size" > :bind_val2 OR "size" < :bind_val3))' in sql
449+
assert binds[1:] == ["red", 5, 2]
450+
451+
452+
@pytest.mark.asyncio
453+
async def test_search_single_filter_is_unchanged(oracle_store):
454+
collection = oracle_store.get_collection(
455+
model=FilterModel, record_type=FilterModel, collection_name="MY_COLLECTION"
456+
)
457+
options = VectorSearchOptions(vector_property_name="vector", filter="lambda x: x.color == 'red'")
458+
sql, binds, _ = await collection._inner_search_vector(options, None, [0.1, 0.2, 0.3])
459+
460+
assert 'WHERE "color" = :bind_val1' in sql
461+
assert binds[1:] == ["red"]

0 commit comments

Comments
 (0)