copy_dumps corrupts array elements through COPY#67
Merged
Conversation
Array elements were quoted only when they contained braces, so ["a,b"] became {a,b} (two elements), [""] became {} (the empty array), [" a"] lost its space, and ["NULL"] became an SQL NULL — silent data corruption through copy_to/copy_from/reload and LMFDB's uploader. A None element was written as the field-level marker \N, which COPY unescapes to the letter N.
Elements are now quoted whenever PostgreSQL's array parser needs it (empty, NULL case-insensitively, or containing braces, commas, quotes, backslashes or whitespace), with array-level escaping applied inside COPY's field-level escaping in the order COPY undoes them, and None becomes the array literal NULL. Verified beyond the unit tests by driving 27 nasty values — embedded tabs, newlines, quotes, backslashes, unicode, [None, 1], 2-D arrays — through an actual COPY FROM and back: all round-trip exactly. Output changes only for values that could not round-trip before.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Array elements were quoted only when they contained braces, so
["a,b"]became{a,b}(two elements),[""]became{}(the empty array),[" a"]lost its space, and["NULL"]became an SQL NULL — silent data corruption throughcopy_to/copy_from/reloadand LMFDB's uploader. ANoneelement was written as the field-level marker\N, which COPY unescapes to the letterN.Elements are now quoted whenever PostgreSQL's array parser needs it (empty,
NULLcase-insensitively, or containing braces, commas, quotes, backslashes or whitespace), with array-level escaping applied inside COPY's field-level escaping in the order COPY undoes them, andNonebecomes the array literalNULL. Verified beyond the unit tests by driving 27 nasty values — embedded tabs, newlines, quotes, backslashes, unicode,[None, 1], 2-D arrays — through an actualCOPY FROMand back: all round-trip exactly. Output changes only for values that could not round-trip before.Split out of #60 (one PR per issue). This branch carries only this issue's fix and its tests; the rest of the known-bug inventory stays
xfailed, so the full suite is green here and the strict markers guarantee no cross-issue leakage.