Skip to content

Python: Fix Postgres filter predicates treated as string literals#13602

Closed
tejasai97 wants to merge 1 commit intomicrosoft:mainfrom
tejasai97:fix/postgres-filter-predicates-13595
Closed

Python: Fix Postgres filter predicates treated as string literals#13602
tejasai97 wants to merge 1 commit intomicrosoft:mainfrom
tejasai97:fix/postgres-filter-predicates-13595

Conversation

@tejasai97
Copy link

Summary

  • Fixes Python postgres connector #13595 where _lambda_parser in the Postgres connector returned plain Python strings for SQL WHERE clauses, which psycopg's sql.SQL.format() auto-wrapped as sql.Literal (quoted string literals) instead of embedding as SQL expressions
  • Changed _lambda_parser to return sql.Composable objects (sql.Identifier, sql.Literal, sql.SQL) so predicates are correctly embedded as SQL fragments
  • Added 8 unit tests covering equality, numeric comparison, AND/OR, IN, NOT, multiple filters, and chain comparisons

Motivation and Context

When using lambda filters with the Postgres vector search (e.g., filter=lambda x: x.content_type == "course"), the generated SQL was malformed:

-- Before (broken): predicate quoted as a string literal
WHERE '"content_type" = ''course'''

-- After (fixed): predicate embedded as a SQL expression
WHERE "content_type" = 'course'

Description

python/semantic_kernel/connectors/postgres.py:

  • _lambda_parser now returns sql.Composable instead of str
  • Column names use sql.Identifier() instead of manual f'"{name}"'
  • Constants use sql.Literal() instead of manual string escaping
  • Booleans/NULL use sql.SQL("TRUE") / sql.SQL("NULL")
  • All operators compose via sql.SQL("{} op {}").format(left, right)
  • Fixed missing leading space in WHERE clause

python/tests/unit/connectors/memory/test_postgres_store.py:

  • Added 8 new filter tests; all 28 tests pass

Test plan

  • All 28 existing + new unit tests pass (pytest tests/unit/connectors/memory/test_postgres_store.py)
  • ruff check passes with no errors
  • ruff format produces no changes
  • CI pipeline passes

🤖 Generated with Claude Code

…crosoft#13595)

The _lambda_parser method returned plain Python strings for SQL WHERE
clauses. When passed to psycopg's sql.SQL.format(), these were
auto-wrapped as sql.Literal, producing malformed queries like:
  WHERE '"content_type" = ''course'''
instead of:
  WHERE "content_type" = 'course'

Fix: return sql.Composable objects (sql.Identifier, sql.Literal,
sql.SQL) from _lambda_parser so psycopg correctly embeds them as SQL
fragments. Also adds 8 unit tests covering filter edge cases.

Co-Authored-By: Claude <noreply@anthropic.com>
@tejasai97 tejasai97 requested a review from a team as a code owner February 27, 2026 21:25
@moonbox3 moonbox3 added the python Pull requests for the Python Semantic Kernel label Feb 27, 2026
@tejasai97
Copy link
Author

Closing this PR - submitted in error.

@tejasai97 tejasai97 closed this Feb 27, 2026
@tejasai97 tejasai97 deleted the fix/postgres-filter-predicates-13595 branch February 27, 2026 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests for the Python Semantic Kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python postgres connector

2 participants