Skip to content

Capture the COPY ... FROM STDIN payload verbatim - #2428

Open
LucaCappelletti94 wants to merge 1 commit into
apache:mainfrom
LucaCappelletti94:copy-stdin-verbatim-payload
Open

LucaCappelletti94 wants to merge 1 commit into
apache:mainfrom
LucaCappelletti94:copy-stdin-verbatim-payload

Conversation

@LucaCappelletti94

Copy link
Copy Markdown
Contributor

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 \N interpreted, which is a partial implementation of the PostgreSQL text format. This removes that rather than completing it. Statement::Copy now carries payload: Option<String>, holding the text between the command and the \. terminator exactly as written, and Display writes it back unchanged. What a tab or a \N means is left to whoever consumes the statement. None means the statement carried no inline data, so a bare COPY t FROM STDIN; still prints without an invented terminator, while Some("") is an explicitly empty payload.

This breaks Statement::Copy, and Parser::parse_tsv and Parser::parse_tab_value are removed along with the format handling. Every other COPY sub-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.

@LucaCappelletti94
LucaCappelletti94 marked this pull request as ready for review August 4, 2026 20:28
@LucaCappelletti94
LucaCappelletti94 force-pushed the copy-stdin-verbatim-payload branch 2 times, most recently from 5c2e10e to de77ad2 Compare August 24, 2026 11:50
@LucaCappelletti94
LucaCappelletti94 force-pushed the copy-stdin-verbatim-payload branch from de77ad2 to 35a7fe0 Compare September 3, 2026 17:05
@LucaCappelletti94
LucaCappelletti94 force-pushed the copy-stdin-verbatim-payload branch 4 times, most recently from 01250ba to 75ca5f6 Compare September 22, 2026 21:20
LucaCappelletti94 added a commit to LucaCappelletti94/sqlparser-rs that referenced this pull request Oct 2, 2026
@codecov-commenter

codecov-commenter commented Oct 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.16981% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.40%. Comparing base (d32825d) to head (8b2d212).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/tokenizer.rs 97.86% 1 Missing and 3 partials ⚠️
src/ast/mod.rs 60.00% 0 Missing and 2 partials ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LucaCappelletti94
LucaCappelletti94 force-pushed the copy-stdin-verbatim-payload branch from d9944b2 to 652c896 Compare October 2, 2026 16:15
@LucaCappelletti94
LucaCappelletti94 force-pushed the copy-stdin-verbatim-payload branch 2 times, most recently from d3d3881 to 74d64d8 Compare October 18, 2026 14:08

@sabir-akhadov-localstack sabir-akhadov-localstack left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three non-blocking comments inline.

Comment thread src/parser/mod.rs Outdated
}
/// Parse the inline payload of `COPY ... FROM STDIN` as written.
///
/// The payload is data rather than SQL, so it is captured uninterpreted.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/parser/mod.rs Outdated
values.push(None);
}
// `\.` terminates the payload, every other backslash is data.
if self.peek_nth_token_no_skip_ref(0).token == Token::Period {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/parser/mod.rs Outdated
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 \..

@LucaCappelletti94
LucaCappelletti94 force-pushed the copy-stdin-verbatim-payload branch from 74d64d8 to 8b2d212 Compare October 19, 2026 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants