Skip to content

fix: bind an empty JSON array as the quoted '{}' literal (#86) - #105

Merged
aesslinger merged 1 commit into
mainfrom
fix/86-empty-array-literal
Sep 16, 2026
Merged

aesslinger merged 1 commit into
mainfrom
fix/86-empty-array-literal

Conversation

@aesslinger

Copy link
Copy Markdown
Collaborator

Summary

  • 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 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_literalbind_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.

Fixes #86.

Test plan

  • TDD: added 3 new unit tests to 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 passed
  • cargo clippy --all-targets -- -D warnings — clean
  • cargo fmt --all -- --check — clean
  • Live end-to-end verification against a real PostgreSQL instance:
    • The issue's exact repro (INSERT of {"ids": []} into an int[] column) now succeeds and round-trips as an empty array instead of erroring
    • A non-empty array still binds unchanged (no regression)
    • The string-embedded "[]" path binds identically to the native array path
    • 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 the same error against bare psql using 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

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.
@aesslinger aesslinger added the prerelease:rc Version suggestion targets a release candidate label Sep 16, 2026
@aesslinger aesslinger self-assigned this Sep 16, 2026
@github-actions

Copy link
Copy Markdown

Version suggestion

Based on this PR's title (fix) and the prerelease:rc label:

Current 1.0.0-rc.3
Suggested next tag v1.0.0-rc.4

This is informational only — no tag or release is created automatically yet.

1 similar comment
@github-actions

Copy link
Copy Markdown

Version suggestion

Based on this PR's title (fix) and the prerelease:rc label:

Current 1.0.0-rc.3
Suggested next tag v1.0.0-rc.4

This is informational only — no tag or release is created automatically yet.

@aesslinger
aesslinger merged commit 6ced427 into main Sep 16, 2026
14 checks passed
@aesslinger
aesslinger deleted the fix/86-empty-array-literal branch September 16, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

prerelease:rc Version suggestion targets a release candidate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty JSON array binds as ARRAY[] instead of '{}' (server errors in many array-column contexts)

1 participant