Repository navigation
Capture the COPY ... FROM STDIN payload verbatim - #2428
LucaCappelletti94 wants to merge 1 commit into
Conversation
5c2e10e to
de77ad2
Compare
de77ad2 to
35a7fe0
Compare
01250ba to
75ca5f6
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2428 +/- ##
==========================================
+ Coverage 81.29% 81.40% +0.11%
==========================================
Files 42 42
Lines 33950 34120 +170
Branches 33950 34120 +170
==========================================
+ Hits 27601 27777 +176
+ Misses 2785 2775 -10
- Partials 3564 3568 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
d9944b2 to
652c896
Compare
d3d3881 to
74d64d8
Compare
sabir-akhadov-localstack
left a comment
There was a problem hiding this comment.
Three non-blocking comments inline.
| } | ||
| /// Parse the inline payload of `COPY ... FROM STDIN` as written. | ||
| /// | ||
| /// The payload is data rather than SQL, so it is captured uninterpreted. |
There was a problem hiding this comment.
The payload is re-rendered from tokens rather than sliced from the source, so it is not byte-for-byte: 0x1F comes back as X'1F', E'\x41' as E'A', CRLF as LF, and 'a''b' as 'a'b', which no longer re-parses. Worth softening "as written" here and in the Statement::Copy doc unless the tokenizer hands over the raw text.
There was a problem hiding this comment.
Rendering tokens could never be exact, and data such as O'Brien did not even tokenize.
The tokenizer now cuts the payload out of the source before tokenizing, so it comes back byte for byte, including 0x1F, E'\x41', CRLF and 'a''b'.
Objectively, the tokenizer needs a complete overhaul, but I will tackle that after most of the fuzzer bugs are squashed or the new tokenizer will be a mess to fuzz.
| values.push(None); | ||
| } | ||
| // `\.` terminates the payload, every other backslash is data. | ||
| if self.peek_nth_token_no_skip_ref(0).token == Token::Period { |
There was a problem hiding this comment.
PostgreSQL only ends the data at a line containing just \., so 1\tfoo\.bar (literal foo.bar in text format, plain data in CSV) terminates the payload here and then fails on bar. Requiring the previous token to be a newline (or payload start) and the next to be newline/EOF would match the docs; the same spot could also treat \. as the statement terminator, since \.\nSELECT 1; currently fails with "Expected: end of statement".
There was a problem hiding this comment.
The payload now ends only at a line holding just \., and a statement after it parses.
That said, while your claim is very reasonable, PostgreSQL 16.15 rejects 1\tfoo\.bar in text format with end-of-copy marker corrupt, so a mid-line \. stays in the payload and lets the server decides.
| values | ||
| // An unterminated payload keeps whatever data it holds, but an empty one | ||
| // means the statement had no inline data at all. | ||
| (!payload.is_empty()).then_some(payload) |
There was a problem hiding this comment.
COPY t FROM STDIN;\n\n yields Some("\n") and prints a \., while ;\n yields None; a whitespace-only unterminated trail should probably be None too.
There was a problem hiding this comment.
psql loads that blank line as a row. Running COPY t FROM STDIN; followed by an empty line at end of file through psql 16.15 inserts one row holding '', so the payload keeps it. It is recorded as unterminated and prints without an invented \..
74d64d8 to
8b2d212
Compare
Parsing
COPY t (a, b) FROM stdin;with two rows of two columns produced six values with nothing marking where the first row ended, so the rows could not be recovered even by counting columns. Printing the statement did not reproduce its input either, and every print and re-parse cycle inserted one more tab before each field, without bound.The cause is that the payload was being split on tabs and newlines with
\Ninterpreted, which is a partial implementation of the PostgreSQL text format. This removes that rather than completing it.Statement::Copynow carriespayload: Option<String>, holding the text between the command and the\.terminator exactly as written, andDisplaywrites it back unchanged. What a tab or a\Nmeans is left to whoever consumes the statement.Nonemeans the statement carried no inline data, so a bareCOPY t FROM STDIN;still prints without an invented terminator, whileSome("")is an explicitly empty payload.This breaks
Statement::Copy, andParser::parse_tsvandParser::parse_tab_valueare removed along with the format handling. Every otherCOPYsub-parser was already private.My suggestion here is that either we go full csv parsing (which I agree might be a bit much), or no parsing at all (which is what I am suggesting we do in this PR), as the current impl was neither and broken.