Skip to content

Commit

Permalink
start, support an optional INTO keyword in COPY (INTO)? <table> FROM
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkMyCar committed Jan 6, 2025
1 parent 0a847c8 commit ee0498a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/sql-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6299,6 +6299,13 @@ impl<'a> Parser<'a> {

/// Parse a copy statement
fn parse_copy(&mut self) -> Result<Statement<Raw>, ParserStatementError> {
// We support an optional "INTO" keyword for COPY INTO <table> FROM
let maybe_into_pos = if self.parse_keyword(Keyword::Into) {
Some(self.peek_prev_pos())
} else {
None
};

let relation = if self.consume_token(&Token::LParen) {
let query = self.parse_statement()?.ast;
self.expect_token(&Token::RParen)
Expand Down Expand Up @@ -6338,6 +6345,13 @@ impl<'a> Parser<'a> {
(CopyDirection::From, CopyTarget::Stdin)
}
TO => {
// We only support the INTO keyword for 'COPY FROM'.
if let Some(into_pos) = maybe_into_pos {
return self
.expected(into_pos, "identifier", Some(Token::Keyword(Keyword::Into)))
.map_parser_err(StatementKind::Copy);
}

if self.parse_keyword(STDOUT) {
(CopyDirection::To, CopyTarget::Stdout)
} else {
Expand Down
14 changes: 14 additions & 0 deletions src/sql-parser/tests/testdata/copy
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,17 @@ COPY (select * from t order by 1) TO 's3://path/' || repeat('1', 2)
COPY (SELECT * FROM t ORDER BY 1) TO 's3://path/' || repeat('1', 2)
=>
Copy(CopyStatement { relation: Select(SelectStatement { query: Query { ctes: Simple([]), body: Select(Select { distinct: None, projection: [Wildcard], from: [TableWithJoins { relation: Table { name: Name(UnresolvedItemName([Ident("t")])), alias: None }, joins: [] }], selection: None, group_by: [], having: None, options: [] }), order_by: [OrderByExpr { expr: Value(Number("1")), asc: None, nulls_last: None }], limit: None, offset: None }, as_of: None }), direction: To, target: Expr(Op { op: Op { namespace: None, op: "||" }, expr1: Value(String("s3://path/")), expr2: Some(Function(Function { name: Name(UnresolvedItemName([Ident("repeat")])), args: Args { args: [Value(String("1")), Value(Number("2"))], order_by: [] }, filter: None, over: None, distinct: false })) }), options: [] })

parse-statement
COPY INTO t1 FROM STDIN
----
COPY t1 FROM STDIN
=>
Copy(CopyStatement { relation: Named { name: Name(UnresolvedItemName([Ident("t1")])), columns: [] }, direction: From, target: Stdin, options: [] })

parse-statement
COPY INTO t(a, b) TO '/any/path'
----
error: Expected identifier, found INTO
COPY INTO t(a, b) TO '/any/path'
^

0 comments on commit ee0498a

Please sign in to comment.