Skip to content
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
54 changes: 47 additions & 7 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8759,41 +8759,81 @@ impl fmt::Display for CopyOption {

/// An option in `COPY` statement before PostgreSQL version 9.0.
///
/// <https://www.postgresql.org/docs/8.4/sql-copy.html>
/// [PostgreSQL](https://www.postgresql.org/docs/8.4/sql-copy.html)
/// [Redshift](https://docs.aws.amazon.com/redshift/latest/dg/r_COPY-alphabetical-parm-list.html)
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum CopyLegacyOption {
/// ACCEPTANYDATE
AcceptAnyDate,
/// ACCEPTINVCHARS
AcceptInvChars(Option<String>),
/// BINARY
Binary,
/// DELIMITER \[ AS \] 'delimiter_character'
Delimiter(char),
/// NULL \[ AS \] 'null_string'
Null(String),
/// BLANKSASNULL
BlankAsNull,
/// CSV ...
Csv(Vec<CopyLegacyCsvOption>),
/// DATEFORMAT \[ AS \] {'dateformat_string' | 'auto' }
DateFormat(Option<String>),
/// DELIMITER \[ AS \] 'delimiter_character'
Delimiter(char),
/// EMPTYASNULL
EmptyAsNull,
/// IAM_ROLE { DEFAULT | 'arn:aws:iam::123456789:role/role1' }
IamRole(IamRoleKind),
/// IGNOREHEADER \[ AS \] number_rows
IgnoreHeader(u64),
/// NULL \[ AS \] 'null_string'
Null(String),
/// TIMEFORMAT \[ AS \] {'timeformat_string' | 'auto' | 'epochsecs' | 'epochmillisecs' }
TimeFormat(Option<String>),
/// TRUNCATECOLUMNS
TruncateColumns,
}

impl fmt::Display for CopyLegacyOption {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use CopyLegacyOption::*;
match self {
AcceptAnyDate => write!(f, "ACCEPTANYDATE"),
AcceptInvChars(ch) => {
write!(f, "ACCEPTINVCHARS")?;
if let Some(ch) = ch {
write!(f, " '{}'", value::escape_single_quote_string(ch))?;
}
Ok(())
}
Binary => write!(f, "BINARY"),
Delimiter(char) => write!(f, "DELIMITER '{char}'"),
Null(string) => write!(f, "NULL '{}'", value::escape_single_quote_string(string)),
BlankAsNull => write!(f, "BLANKSASNULL"),
Csv(opts) => {
write!(f, "CSV")?;
if !opts.is_empty() {
write!(f, " {}", display_separated(opts, " "))?;
}
Ok(())
}
DateFormat(fmt) => {
write!(f, "DATEFORMAT")?;
if let Some(fmt) = fmt {
write!(f, " '{}'", value::escape_single_quote_string(fmt))?;
}
Ok(())
}
Delimiter(char) => write!(f, "DELIMITER '{char}'"),
EmptyAsNull => write!(f, "EMPTYASNULL"),
IamRole(role) => write!(f, "IAM_ROLE {role}"),
IgnoreHeader(num_rows) => write!(f, "IGNOREHEADER {num_rows}"),
Null(string) => write!(f, "NULL '{}'", value::escape_single_quote_string(string)),
TimeFormat(fmt) => {
write!(f, "TIMEFORMAT")?;
if let Some(fmt) = fmt {
write!(f, " '{}'", value::escape_single_quote_string(fmt))?;
}
Ok(())
}
TruncateColumns => write!(f, "TRUNCATECOLUMNS"),
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ define_keywords!(
ABS,
ABSENT,
ABSOLUTE,
ACCEPTANYDATE,
ACCEPTINVCHARS,
ACCESS,
ACCOUNT,
ACTION,
Expand Down Expand Up @@ -138,6 +140,7 @@ define_keywords!(
BIND,
BINDING,
BIT,
BLANKSASNULL,
BLOB,
BLOCK,
BLOOM,
Expand Down Expand Up @@ -254,6 +257,7 @@ define_keywords!(
DATA_RETENTION_TIME_IN_DAYS,
DATE,
DATE32,
DATEFORMAT,
DATETIME,
DATETIME64,
DAY,
Expand Down Expand Up @@ -312,6 +316,7 @@ define_keywords!(
ELSE,
ELSEIF,
EMPTY,
EMPTYASNULL,
ENABLE,
ENABLE_SCHEMA_EVOLUTION,
ENCODING,
Expand Down Expand Up @@ -927,6 +932,7 @@ define_keywords!(
THEN,
TIES,
TIME,
TIMEFORMAT,
TIMESTAMP,
TIMESTAMPTZ,
TIMESTAMP_NTZ,
Expand Down Expand Up @@ -955,6 +961,7 @@ define_keywords!(
TRIM_ARRAY,
TRUE,
TRUNCATE,
TRUNCATECOLUMNS,
TRY,
TRY_CAST,
TRY_CONVERT,
Expand Down
61 changes: 52 additions & 9 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9543,23 +9543,38 @@ impl<'a> Parser<'a> {
}

fn parse_copy_legacy_option(&mut self) -> Result<CopyLegacyOption, ParserError> {
// FORMAT \[ AS \] is optional
if self.parse_keyword(Keyword::FORMAT) {
let _ = self.parse_keyword(Keyword::AS);
}

let ret = match self.parse_one_of_keywords(&[
Keyword::ACCEPTANYDATE,
Keyword::ACCEPTINVCHARS,
Keyword::BINARY,
Keyword::DELIMITER,
Keyword::NULL,
Keyword::BLANKSASNULL,
Keyword::CSV,
Keyword::DATEFORMAT,
Keyword::DELIMITER,
Keyword::EMPTYASNULL,
Keyword::IAM_ROLE,
Keyword::IGNOREHEADER,
Keyword::NULL,
Keyword::TIMEFORMAT,
Keyword::TRUNCATECOLUMNS,
]) {
Some(Keyword::BINARY) => CopyLegacyOption::Binary,
Some(Keyword::DELIMITER) => {
Some(Keyword::ACCEPTANYDATE) => CopyLegacyOption::AcceptAnyDate,
Some(Keyword::ACCEPTINVCHARS) => {
let _ = self.parse_keyword(Keyword::AS); // [ AS ]
CopyLegacyOption::Delimiter(self.parse_literal_char()?)
}
Some(Keyword::NULL) => {
let _ = self.parse_keyword(Keyword::AS); // [ AS ]
CopyLegacyOption::Null(self.parse_literal_string()?)
let ch = if matches!(self.peek_token().token, Token::SingleQuotedString(_)) {
Some(self.parse_literal_string()?)
} else {
None
};
CopyLegacyOption::AcceptInvChars(ch)
}
Some(Keyword::BINARY) => CopyLegacyOption::Binary,
Some(Keyword::BLANKSASNULL) => CopyLegacyOption::BlankAsNull,
Some(Keyword::CSV) => CopyLegacyOption::Csv({
let mut opts = vec![];
while let Some(opt) =
Expand All @@ -9569,12 +9584,40 @@ impl<'a> Parser<'a> {
}
opts
}),
Some(Keyword::DATEFORMAT) => {
let _ = self.parse_keyword(Keyword::AS);
let fmt = if matches!(self.peek_token().token, Token::SingleQuotedString(_)) {
Some(self.parse_literal_string()?)
} else {
None
};
CopyLegacyOption::DateFormat(fmt)
}
Some(Keyword::DELIMITER) => {
let _ = self.parse_keyword(Keyword::AS);
CopyLegacyOption::Delimiter(self.parse_literal_char()?)
}
Some(Keyword::EMPTYASNULL) => CopyLegacyOption::EmptyAsNull,
Some(Keyword::IAM_ROLE) => CopyLegacyOption::IamRole(self.parse_iam_role_kind()?),
Some(Keyword::IGNOREHEADER) => {
let _ = self.parse_keyword(Keyword::AS);
let num_rows = self.parse_literal_uint()?;
CopyLegacyOption::IgnoreHeader(num_rows)
}
Some(Keyword::NULL) => {
let _ = self.parse_keyword(Keyword::AS);
CopyLegacyOption::Null(self.parse_literal_string()?)
}
Some(Keyword::TIMEFORMAT) => {
let _ = self.parse_keyword(Keyword::AS);
let fmt = if matches!(self.peek_token().token, Token::SingleQuotedString(_)) {
Some(self.parse_literal_string()?)
} else {
None
};
CopyLegacyOption::TimeFormat(fmt)
}
Some(Keyword::TRUNCATECOLUMNS) => CopyLegacyOption::TruncateColumns,
_ => self.expected("option", self.peek_token())?,
};
Ok(ret)
Expand Down
32 changes: 32 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16768,4 +16768,36 @@ fn parse_copy_options() {
}
_ => unreachable!(),
}
one_statement_parses_to(
concat!(
"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' ",
"ACCEPTANYDATE ",
"ACCEPTINVCHARS AS '*' ",
"BLANKSASNULL ",
"CSV ",
"DATEFORMAT AS 'DD-MM-YYYY' ",
"EMPTYASNULL ",
"IAM_ROLE DEFAULT ",
"IGNOREHEADER AS 1 ",
"TIMEFORMAT AS 'auto' ",
"TRUNCATECOLUMNS",
),
concat!(
"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' ",
"ACCEPTANYDATE ",
"ACCEPTINVCHARS '*' ",
"BLANKSASNULL ",
"CSV ",
"DATEFORMAT 'DD-MM-YYYY' ",
"EMPTYASNULL ",
"IAM_ROLE DEFAULT ",
"IGNOREHEADER 1 ",
"TIMEFORMAT 'auto' ",
"TRUNCATECOLUMNS",
),
);
one_statement_parses_to(
"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' FORMAT AS CSV",
"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' CSV",
);
}