Python: Fix Postgres filter predicates treated as string literals#13602
Closed
tejasai97 wants to merge 1 commit intomicrosoft:mainfrom
Closed
Python: Fix Postgres filter predicates treated as string literals#13602tejasai97 wants to merge 1 commit intomicrosoft:mainfrom
tejasai97 wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…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>
Author
|
Closing this PR - submitted in error. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_lambda_parserin the Postgres connector returned plain Python strings for SQL WHERE clauses, which psycopg'ssql.SQL.format()auto-wrapped assql.Literal(quoted string literals) instead of embedding as SQL expressions_lambda_parserto returnsql.Composableobjects (sql.Identifier,sql.Literal,sql.SQL) so predicates are correctly embedded as SQL fragmentsMotivation 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:Description
python/semantic_kernel/connectors/postgres.py:_lambda_parsernow returnssql.Composableinstead ofstrsql.Identifier()instead of manualf'"{name}"'sql.Literal()instead of manual string escapingsql.SQL("TRUE")/sql.SQL("NULL")sql.SQL("{} op {}").format(left, right)WHEREclausepython/tests/unit/connectors/memory/test_postgres_store.py:Test plan
pytest tests/unit/connectors/memory/test_postgres_store.py)ruff checkpasses with no errorsruff formatproduces no changes🤖 Generated with Claude Code