Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions python/semantic_kernel/connectors/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,9 +795,11 @@ def _construct_vector_query(

if where_clauses := self._build_filter(options.filter): # type: ignore
query += (
sql.SQL("WHERE {clause}").format(clause=sql.SQL(" AND ").join(where_clauses))
sql.SQL("WHERE {clause}").format(
clause=sql.SQL(" AND ").join(sql.SQL(clause) for clause in where_clauses)
)
if isinstance(where_clauses, list)
else sql.SQL("WHERE {clause}").format(clause=where_clauses)
else sql.SQL("WHERE {clause}").format(clause=sql.SQL(where_clauses))
Comment on lines +798 to +802

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where_clauses here is the non-list return of _build_filter, typed
Any | Sequence[Any] (OptionalOneOrMany), but sql.SQL(...) is annotated to
accept only str/LiteralString. mypy fails on this line with
error: Argument 1 to "SQL" has incompatible type "Any | Sequence[Any]"; expected "str" [arg-type]. The required lint workflow runs uv run mypy -p semantic_kernel
and connectors.postgres is not in mypy.ini's ignore list, so this check now
fails and blocks merge; the base commit was mypy-clean on this file. In the
single-clause path _build_filter returns a str, so this is a type-annotation
mismatch rather than a runtime fault - coerce or narrow the value so the argument
is a str, e.g. sql.SQL(str(where_clauses)), so the lint gate passes.

)

query += sql.SQL(" ORDER BY {dist_col} LIMIT {limit}").format(
Expand Down
20 changes: 19 additions & 1 deletion python/tests/unit/connectors/memory/test_postgres_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
PostgresSettings,
PostgresStore,
)
from semantic_kernel.data.vector import DistanceFunction, IndexKind, VectorStoreField, vectorstoremodel
from semantic_kernel.data.vector import (
DistanceFunction,
IndexKind,
VectorSearchOptions,
VectorStoreField,
vectorstoremodel,
)


@fixture(scope="function")
Expand Down Expand Up @@ -322,6 +328,18 @@ def model_post_init(self, context: Any) -> None:
assert statement_str == expected_statement


async def test_vector_search_filter_is_rendered_as_sql(vector_store: PostgresStore) -> None:
collection = vector_store.get_collection(collection_name="test_collection", record_type=SimpleDataModel)
assert isinstance(collection, PostgresCollection)

query, _, _ = collection._construct_vector_query(
[1.0, 2.0, 3.0],
VectorSearchOptions(filter="lambda x: x.id == 1"),
)

assert 'WHERE "id" = 1' in query.as_string()


async def test_model_post_init_conflicting_distance_column_name(vector_store: PostgresStore) -> None:
@vectorstoremodel
@dataclass
Expand Down
Loading