Skip to content

fix(postgrest): escape special chars in array filters so values with commas don't split - #1611

Closed
twelfthlabor wants to merge 1 commit into
supabase:mainfrom
twelfthlabor:fix/array-escaping-1592
Closed

fix(postgrest): escape special chars in array filters so values with commas don't split#1611
twelfthlabor wants to merge 1 commit into
supabase:mainfrom
twelfthlabor:fix/array-escaping-1592

Conversation

@twelfthlabor

@twelfthlabor twelfthlabor commented Sep 4, 2026

Copy link
Copy Markdown

#1592

contains("tags", ["a,b", "c"]) was sending tags=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 in cs, cd, contained_by, and ov/overlaps since they all build the {...} literal the same way.

This PR adds a small sanitize_array_element helper in utils.py following 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, elem1 a,b (was 3 before)
  • quote, backslash, empty string, NULL, space, and brace cases all round-trip correctly

One note: test_contained_by_mixed_items currently 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

…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
@twelfthlabor
twelfthlabor requested review from a team and o-santi as code owners September 4, 2026 01:12
@twelfthlabor

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Array filters (cs/cd/contains/contained_by/overlaps) don't escape elements, corrupting values containing the comma delimiter

1 participant