Python: Fix Postgres filter SQL composition
Motivation and Context
Fixes #13595.
Postgres vector-search lambda filters were parsed into plain strings and then passed through psycopg.sql.SQL.format(). psycopg treats non-composable values as SQL literals, so filter predicates could be embedded as quoted strings instead of SQL expressions. The filtered query also missed a leading space before WHERE.
Description
- Build Postgres filter clauses with
psycopg.sqlcomposables instead of plain strings - Use
sql.Identifierfor field names andsql.Literalfor constants - Preserve existing boolean/comparison/list filter behavior while composing
AND,OR,NOT,IN, and chained comparisons as SQL fragments - Add regression tests for single and multiple filtered vector-search queries
Contribution Checklist
- [x] The code builds clean without any errors or warnings
- [x] The PR follows the SK Contribution Guidelines
- [x] The code follows the Python coding conventions
- [x] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone :smile:
Validation
From python/:
.venv/bin/python -m pytest tests/unit/connectors/memory/test_postgres_store.py -q-> 22 passed.venv/bin/ruff check semantic_kernel/connectors/postgres.py tests/unit/connectors/memory/test_postgres_store.py-> passed.venv/bin/ruff format --check semantic_kernel/connectors/postgres.py tests/unit/connectors/memory/test_postgres_store.py-> passedgit diff --check-> passed
Nice fix — using psycopg.sql composables instead of SQL.format() with plain strings is exactly the right approach here. The root issue (psycopg treating non-composable values as literals rather than SQL expressions) is a subtle but critical correctness bug. A few additional observations:
SQL Injection Safety
Beyond correctness, this migration also closes a potential SQL injection vector. Raw strings passed through SQL.format() with user-controlled field names or filter values could allow injection if input validation upstream is ever relaxed. Using sql.Identifier and sql.Literal makes the intent explicit and safe by construction.
NULL Comparison Handling
Good call on the null commit. The canonical SQL rule is:
field = NULL→ always false (never matches anything in standard SQL)- Correct form:
field IS NULL/field IS NOT NULL
The fix should special-case None comparisons in _build_filter before reaching the generic sql.Literal path:
if value is None:
op_sql = sql.SQL("IS NULL" if op == "==" else "IS NOT NULL")
return sql.SQL("{} {}").format(sql.Identifier(field), op_sql)
This also means != with None should emit IS NOT NULL, not != NULL.
Edge Cases to Test
Beyond basic comparison and list filters, it would be worth testing:
Noneequality / inequality (already being addressed)- Field names that are Postgres reserved words (e.g.,
"order","limit") —sql.Identifierhandles quoting automatically, but a test would confirm - Nested boolean logic:
AND/ORcombinations with mixed types - Empty list in
INfilter:field IN ()is invalid SQL in Postgres; should this raise early?
Leading Space Fix
The missing space before WHERE is a classic string concatenation bug. This confirms why composable building is safer — sql.SQL(" WHERE ").join(clauses) or explicit spacing in the composable chain never silently drops whitespace.
Overall this is a well-targeted fix for a real data correctness bug. The addition of the null comparison commit shows good attention to edge cases. +1 from me once the test coverage for null filters lands.