Skip to content

PostgreSQL: Escape quotes in COPY option values - #2784

Open
LucaCappelletti94 wants to merge 1 commit into
mainfrom
copy-options-escape-quotes
Open

LucaCappelletti94 wants to merge 1 commit into
mainfrom
copy-options-escape-quotes

Conversation

@LucaCappelletti94

Copy link
Copy Markdown
Contributor

COPY y TO stdout WITH CSV QUOTE '''' DELIMITER '|' rendered QUOTE ''', which does not parse. Every COPY option that prints a character or string between single quotes now escapes it. That covers DELIMITER, QUOTE and ESCAPE in both option syntaxes and the IAM_ROLE ARN. The string options already escaped.

These are all friends of #2783 and the same consideration thereof apply also here.

https://www.postgresql.org/docs/current/sql-copy.html

@LucaCappelletti94 LucaCappelletti94 added bug Something isn't working fuzzer A bug found by the fuzzer PostgreSQL Redshift labels Oct 31, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.85%. Comparing base (45fe354) to head (e5789f0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2784      +/-   ##
==========================================
+ Coverage   82.83%   82.85%   +0.01%     
==========================================
  Files          42       42              
  Lines       35900    35908       +8     
  Branches    35900    35908       +8     
==========================================
+ Hits        29739    29750      +11     
+ Misses       2654     2651       -3     
  Partials     3507     3507              

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

@slopgobblercobbler slopgobblercobbler left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Audited single-quote character escaping in COPY options across src/ast/value.rs and src/ast/mod.rs:

  • Root cause resolution:
    • In PostgreSQL and Redshift COPY syntax, a literal single-quote option value (e.g. QUOTE ''' or ESCAPE ''') must be escaped by doubling the quote inside single quotes, producing ''''.
    • Previously, formatting char directly inside single quotes (write!(f, "QUOTE '{char}'")) yielded ''', creating an unclosed string literal syntax error on re-parsing.
  • Display implementation and escaping:
    • Introducing EscapeSingleQuoteChar in src/ast/value.rs provides a lightweight, zero-allocation display adapter that writes '' when char == '\'' and writes the character unchanged otherwise.
    • Applying escape_single_quote_char to DELIMITER, QUOTE, and ESCAPE in CopyOption, CopyLegacyOption, and CopyLegacyCsvOption ensures uniform serialization across modern and legacy COPY option syntaxes.
    • In Redshift IAM options, applying escape_single_quote_string to IamRoleKind::Arn(arn) ensures ARNs containing single quotes round-trip safely.
  • Roundtrip invariance and test coverage:
    • Unit tests in sqlparser_postgres.rs assert exact AST identity and string roundtrips across standard options, legacy options, and Redshift IAM role strings.
    • Verified parse_copy_option_quoted_chars_round_trip passes with 0 errors.

Clean, minimal fix that restores roundtrip invariance for quote-delimited COPY configurations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working fuzzer A bug found by the fuzzer PostgreSQL Redshift

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants