Skip to content

Python: Fix Postgres filter predicates treated as string literals - #13603

Open
Gadipally Sai Teja (tejasai97) wants to merge 4 commits into
microsoft:mainfrom
tejasai97:fix/postgres-filter-predicates-13595
Open

Gadipally Sai Teja (tejasai97) wants to merge 4 commits into
microsoft:mainfrom
tejasai97:fix/postgres-filter-predicates-13595

Conversation

@tejasai97

Copy link
Copy Markdown

Summary

  • Fixes Python postgres connector #13595: _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-string quoting
  • 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 in _construct_vector_query

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

  • Added 8 new filter tests; all 28 tests pass

Contribution Checklist

Change _lambda_parser to return sql.Composable objects instead of plain
strings so psycopg's sql.SQL.format() embeds them as SQL fragments
rather than wrapping them as sql.Literal.

Fixes microsoft#13595

Co-Authored-By: Claude <noreply@anthropic.com>
@moonbox3 Evan Mattson (moonbox3) added the python Pull requests for the Python Semantic Kernel label Feb 27, 2026
@tejasai97

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree company="Microsoft"

@tejasai97
Gadipally Sai Teja (tejasai97) marked this pull request as ready for review February 28, 2026 02:46
@eavanvalkenburg

Copy link
Copy Markdown
Member

Gadipally Sai Teja (@tejasai97) some of the checks are failing, please have a look

Copilot AI left a comment

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.

Copilot review overview

🟢 Approval recommended

No blocking issues were identified; the remaining feedback is a minor test-strengthening nit.

Review effort: Lite
Findings: None

What changed in this PR

Fixes PostgreSQL lambda filters being treated as quoted string literals instead of SQL predicates.

Changes:

  • Composes predicates with psycopg SQL objects.
  • Uses safe identifiers and literals.
  • Fixes WHERE spacing and adds filter tests.
File Summary
python/​tests/​unit/​connectors/​memory/​test_postgres_store.py Adds coverage for filter predicates and combinations.
python/​semantic_kernel/​connectors/​postgres.py Builds composable SQL predicates and corrects query spacing.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

This branch had an error being deployed

1 failed deployment
github-app-auth — b27108d9 Deployed Sep 28, 2026 by eavanvalkenburg via add_label #29086
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

4 participants