Skip to content

Commit

Permalink
chore: ignore stdin/stdout typo in COPY commands (#1976)
Browse files Browse the repository at this point in the history
PostgreSQL accepts both stdin and stdout for COPY TO and COPY FROM.
The PHP PDO driver actually contains this typo, and therefore failed
when used against PGAdapter.

Fixes #1975
  • Loading branch information
olavloite committed Jun 19, 2024
1 parent fa92c7c commit ce7387e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -617,13 +617,19 @@ static ParsedCopyStatement parse(String sql) {
}
builder.direction = Direction.valueOf(parser.readKeyword().toUpperCase());
if (builder.direction == Direction.FROM) {
if (!parser.eatKeyword("stdin")) {
// Silently ignore typo 'copy from stdout'.
// See
// https://github.com/postgres/postgres/blob/03ec203164119f11f0eab4c83c97a8527e2b108d/src/backend/parser/gram.y#L3463
if (!parser.eatKeyword("stdin") && !parser.eatKeyword("stdout")) {
throw PGExceptionFactory.newPGException(
"missing 'STDIN' keyword. PGAdapter only supports COPY ... FROM STDIN: " + sql,
SQLState.SyntaxError);
}
} else {
if (!parser.eatKeyword("stdout")) {
// Silently ignore typo 'copy to stdin'.
// See
// https://github.com/postgres/postgres/blob/03ec203164119f11f0eab4c83c97a8527e2b108d/src/backend/parser/gram.y#L3463
if (!parser.eatKeyword("stdout") && !parser.eatKeyword("stdin")) {
throw PGExceptionFactory.newPGException(
"missing 'STDOUT' keyword. PGAdapter only supports COPY ... TO STDOUT: " + sql,
SQLState.SyntaxError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public void testParseNonCopy() {
public void testParseDirection() {
assertEquals(Direction.FROM, parse("copy my_table from stdin").direction);
assertEquals(Direction.TO, parse("copy my_table to stdout").direction);

// PostgreSQL accepts this kind of typo.
// See
// https://github.com/postgres/postgres/blob/03ec203164119f11f0eab4c83c97a8527e2b108d/src/backend/parser/gram.y#L3463
assertEquals(Direction.FROM, parse("copy my_table from stdout").direction);
assertEquals(Direction.TO, parse("copy my_table to stdin").direction);

// COPY BOTH is not supported.
assertThrows(PGException.class, () -> parse("copy my_table both stdin"));
}

Expand Down

0 comments on commit ce7387e

Please sign in to comment.