fix(postgrest): escape special chars in array filters so values with commas don't split - #1611
Closed
twelfthlabor wants to merge 1 commit into
Closed
fix(postgrest): escape special chars in array filters so values with commas don't split#1611twelfthlabor wants to merge 1 commit into
twelfthlabor wants to merge 1 commit into
Conversation
…commas don't split
I ran into this while filtering on a tags column - contains("tags", ["a,b", "c"]) was sending tags=cs.{a,b,c}, which Postgres reads as three elements instead of two. Silent wrong results, no error, took me a while to spot.
This adds a small sanitize_array_element helper following the Postgres array literal rules (quote on empty, NULL, braces, comma, quote, backslash, whitespace, with backslash-escaping inside quotes) and uses it in cs, cd, contains, contained_by, and ov/overlaps. Simple values like ["a","b"] and [1,2,3] are untouched.
Fixes supabase#1592
Author
|
Closing in favor of #1593, which covers the same array-literal escaping with tests in both async and sync mirrors plus ruff/mypy. Happy to help review or test that one. |
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.
#1592
contains("tags", ["a,b", "c"])was sendingtags=cs.{a,b,c}, which Postgres parses as three elements (a,b,c) instead of the two I passed. No error, just silently wrong matches. Same issue incs,cd,contained_by, andov/overlapssince they all build the{...}literal the same way.This PR adds a small
sanitize_array_elementhelper inutils.pyfollowing the Postgres array literal rules - quote when empty, NULL, or containing braces, comma, quote, backslash, or whitespace, with backslash-escaping for quotes/backslashes inside. Simple values like["a","b"]and[1,2,3]come out exactly as before.I verified it against a local Postgres:
'{"a,b",c}'::text[]-> length 2, elem1a,b(was 3 before)One note:
test_contained_by_mixed_itemscurrently asserts the old malformed output{a,["b", "c"]}, which splits on the inner comma. That expectation will need updating to the quoted form.Fixes #1592