Skip to content

Support Unload statement #1150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2547,6 +2547,16 @@ pub enum Statement {
/// ```
/// Note: this is a MySQL-specific statement. See <https://dev.mysql.com/doc/refman/8.0/en/lock-tables.html>
UnlockTables,
/// ```sql
/// UNLOAD(statement) TO <destination> [ WITH options ]
/// ```
/// See Redshift <https://docs.aws.amazon.com/redshift/latest/dg/r_UNLOAD.html> and
// Athena <https://docs.aws.amazon.com/athena/latest/ug/unload.html>
Unload {
query: Box<Query>,
to: Ident,
with: Vec<SqlOption>,
},
}

impl fmt::Display for Statement {
Expand Down Expand Up @@ -4060,6 +4070,15 @@ impl fmt::Display for Statement {
Statement::UnlockTables => {
write!(f, "UNLOCK TABLES")
}
Statement::Unload { query, to, with } => {
write!(f, "UNLOAD({query}) TO {to}")?;

if !with.is_empty() {
write!(f, " WITH ({})", display_comma_separated(with))?;
}

Ok(())
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ define_keywords!(
UNION,
UNIQUE,
UNKNOWN,
UNLOAD,
UNLOCK,
UNLOGGED,
UNNEST,
Expand Down
19 changes: 18 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ impl<'a> Parser<'a> {
Keyword::MERGE => Ok(self.parse_merge()?),
// `PRAGMA` is sqlite specific https://www.sqlite.org/pragma.html
Keyword::PRAGMA => Ok(self.parse_pragma()?),
Keyword::UNLOAD => Ok(self.parse_unload()?),
// `INSTALL` is duckdb specific https://duckdb.org/docs/extensions/overview
Keyword::INSTALL if dialect_of!(self is DuckDbDialect | GenericDialect) => {
Ok(self.parse_install()?)
Expand All @@ -524,7 +525,6 @@ impl<'a> Parser<'a> {
Keyword::LOAD if dialect_of!(self is DuckDbDialect | GenericDialect) => {
Ok(self.parse_load()?)
}

_ => self.expected("an SQL statement", next_token),
},
Token::LParen => {
Expand Down Expand Up @@ -8947,6 +8947,23 @@ impl<'a> Parser<'a> {
})
}

pub fn parse_unload(&mut self) -> Result<Statement, ParserError> {
self.expect_token(&Token::LParen)?;
let query = self.parse_query()?;
self.expect_token(&Token::RParen)?;

self.expect_keyword(Keyword::TO)?;
let to = self.parse_identifier(false)?;

let with_options = self.parse_options(Keyword::WITH)?;

Ok(Statement::Unload {
query: Box::new(query),
to,
with: with_options,
})
}

pub fn parse_merge_clauses(&mut self) -> Result<Vec<MergeClause>, ParserError> {
let mut clauses: Vec<MergeClause> = vec![];
loop {
Expand Down
58 changes: 58 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8434,6 +8434,64 @@ fn parse_binary_operators_without_whitespace() {
);
}

#[test]
fn parse_unload() {
let unload = verified_stmt("UNLOAD(SELECT cola FROM tab) TO 's3://...' WITH (format = 'AVRO')");
Copy link
Contributor

Choose a reason for hiding this comment

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

fascinating -- I haven't seen UNLOAD before

assert_eq!(
unload,
Statement::Unload {
query: Box::new(Query {
body: Box::new(SetExpr::Select(Box::new(Select {
distinct: None,
top: None,
projection: vec![UnnamedExpr(Expr::Identifier(Ident::new("cola"))),],
into: None,
from: vec![TableWithJoins {
relation: TableFactor::Table {
name: ObjectName(vec![Ident::new("tab")]),
alias: None,
args: None,
with_hints: vec![],
version: None,
partitions: vec![],
},
joins: vec![],
}],
lateral_views: vec![],
selection: None,
group_by: GroupByExpr::Expressions(vec![]),
cluster_by: vec![],
distribute_by: vec![],
sort_by: vec![],
having: None,
named_window: vec![],
qualify: None,
value_table_mode: None,
}))),
with: None,
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
for_clause: None,
order_by: vec![],
}),
to: Ident {
value: "s3://...".to_string(),
quote_style: Some('\'')
},
with: vec![SqlOption {
name: Ident {
value: "format".to_string(),
quote_style: None
},
value: Expr::Value(Value::SingleQuotedString("AVRO".to_string()))
}]
}
);
}

#[test]
fn test_savepoint() {
match verified_stmt("SAVEPOINT test1") {
Expand Down