fix: bind an empty JSON array as the quoted '{}' literal (#86) - #105
Merged
Merged
Conversation
json_array_to_pg_literal had no empty-array special case, so an empty
JSON array [] produced ARRAY[] (an ARRAY constructor with no elements),
which PostgreSQL rejects in many contexts -- a syntax error, or "cannot
determine type of empty array" -- since the server can't infer an
element type from zero elements. The builtin driver special-cases this
to '{}', the canonical PostgreSQL empty-array literal the server always
accepts regardless of context.
Note: the issue's suggested snippet used a bare, unquoted {}, but that's
itself a syntax error on its own (confirmed live: "syntax error at or
near \"{\""). Checked the builtin's actual current source and its git
history -- it has always quoted this as '{}', which I verified live
works correctly. Matched that exactly rather than the issue's snippet.
Both call sites that reach json_array_to_pg_literal (bind_pg_value's
native Value::Array arm, and bind_pg_string's inline "[...]"-embedded-in-
a-string parse) share this one function, so the fix covers both without
duplication.
Verified end-to-end against a live database: the issue's exact
repro (INSERT of {"ids": []} into an int[] column) now succeeds and
round-trips as an empty array rather than erroring; a non-empty array
still binds unchanged (no regression); and the string-embedded "[]"
path binds identically. Also confirmed a pre-existing, shared limitation
carries over unchanged from the builtin: a uniform nested empty array
like [[],[]] still hits PostgreSQL's own type-inference gap for
ARRAY['{}', '{}'] (each '{}' looks like text to the parser) -- reproduced
against bare psql with the builtin's exact algorithm, confirming this is
inherited, not introduced by this fix, and out of scope for #86.
Version suggestionBased on this PR's title (
This is informational only — no tag or release is created automatically yet. |
1 similar comment
Version suggestionBased on this PR's title (
This is informational only — no tag or release is created automatically yet. |
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
json_array_to_pg_literalhad no empty-array special case, so an empty JSON array[]producedARRAY[](an ARRAY constructor with no elements), which PostgreSQL rejects in many contexts — a syntax error, or "cannot determine type of empty array" — since the server can't infer an element type from zero elements.'{}', the canonical PostgreSQL empty-array literal the server always accepts regardless of context.Note on the fix value: the issue's suggested code snippet used a bare, unquoted
{}, but that's itself a syntax error on its own (confirmed live:syntax error at or near "{"). I checked the builtin's actual current source (src-tauri/src/drivers/postgres/helpers.rs::json_array_to_pg_literal) and its git history — it has always quoted this as'{}', going back to when the function was first introduced. Matched that exactly (and verified the quoted form works live) rather than the issue's snippet.Both call sites that reach
json_array_to_pg_literal—bind_pg_value's nativeValue::Arrayarm, andbind_pg_string's inline"[...]"-embedded-in-a-string parse — share this one function, so the fix covers both without duplication.Fixes #86.
Test plan
binding_tests.rs(native empty array, empty array embedded in a string, nested empty array). Confirmed the first two fail against the pre-fix code (ARRAY[]vs expected'{}') before the fix, pass after.cargo test --lib— 312 passedcargo clippy --all-targets -- -D warnings— cleancargo fmt --all -- --check— cleanINSERTof{"ids": []}into anint[]column) now succeeds and round-trips as an empty array instead of erroring"[]"path binds identically to the native array path[[],[]]still hits PostgreSQL's own type-inference gap forARRAY['{}', '{}'](each'{}'looks liketextto the parser) — reproduced the same error against barepsqlusing the builtin's exact algorithm, confirming this is inherited behavior, not introduced by this fix, and out of scope for Empty JSON array binds as ARRAY[] instead of '{}' (server errors in many array-column contexts) #86