Skip to content

Redshift: Add support for IAM_ROLE and IGNOREHEADER COPY options #1968

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8570,6 +8570,10 @@ pub enum CopyLegacyOption {
Null(String),
/// CSV ...
Csv(Vec<CopyLegacyCsvOption>),
/// IAM_ROLE { default | 'arn:aws:iam::123456789:role/role1' }
IamRole(Option<String>),
/// IGNOREHEADER \[ AS \] number_rows
IgnoreHeader(u64),
}

impl fmt::Display for CopyLegacyOption {
Expand All @@ -8579,7 +8583,21 @@ impl fmt::Display for CopyLegacyOption {
Binary => write!(f, "BINARY"),
Delimiter(char) => write!(f, "DELIMITER '{char}'"),
Null(string) => write!(f, "NULL '{}'", value::escape_single_quote_string(string)),
Csv(opts) => write!(f, "CSV {}", display_separated(opts, " ")),
Csv(opts) => {
write!(f, "CSV")?;
if !opts.is_empty() {
write!(f, " {}", display_separated(opts, " "))?;
}
Ok(())
}
IamRole(role) => {
write!(f, "IAM_ROLE")?;
match role {
Some(role) => write!(f, " '{role}'"),
None => write!(f, " default"),
}
}
IgnoreHeader(num_rows) => write!(f, "IGNOREHEADER {num_rows}"),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,14 @@ define_keywords!(
HOUR,
HOURS,
HUGEINT,
IAM_ROLE,
ICEBERG,
ID,
IDENTITY,
IDENTITY_INSERT,
IF,
IGNORE,
IGNOREHEADER,
ILIKE,
IMMEDIATE,
IMMUTABLE,
Expand Down
15 changes: 15 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9358,6 +9358,8 @@ impl<'a> Parser<'a> {
Keyword::DELIMITER,
Keyword::NULL,
Keyword::CSV,
Keyword::IAM_ROLE,
Keyword::IGNOREHEADER,
]) {
Some(Keyword::BINARY) => CopyLegacyOption::Binary,
Some(Keyword::DELIMITER) => {
Expand All @@ -9377,6 +9379,19 @@ impl<'a> Parser<'a> {
}
opts
}),
Some(Keyword::IAM_ROLE) => {
if self.parse_keyword(Keyword::DEFAULT) {
CopyLegacyOption::IamRole(None)
} else {
let role = self.parse_literal_string()?;
CopyLegacyOption::IamRole(Some(role))
}
}
Some(Keyword::IGNOREHEADER) => {
let _ = self.parse_keyword(Keyword::AS);
let num_rows = self.parse_literal_uint()?;
CopyLegacyOption::IgnoreHeader(num_rows)
}
_ => self.expected("option", self.peek_token())?,
};
Ok(ret)
Expand Down
22 changes: 22 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16256,3 +16256,25 @@ fn parse_notnull() {
// for unsupported dialects, parsing should stop at `NOT NULL`
notnull_unsupported_dialects.expr_parses_to("NOT NULL NOTNULL", "NOT NULL");
}

#[test]
fn pares_copy_options() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
fn pares_copy_options() {
fn parse_copy_options() {

let copy = verified_stmt(
r#"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' IAM_ROLE 'arn:aws:iam::123456789:role/role1' CSV IGNOREHEADER 1"#,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add tests to cover the IAM ROLE DEFAULT and CSV IGNOREHEADER AS ... scenarios?

);
match copy {
Statement::Copy { legacy_options, .. } => {
assert_eq!(
legacy_options,
vec![
CopyLegacyOption::IamRole(Some(
"arn:aws:iam::123456789:role/role1".to_string()
)),
CopyLegacyOption::Csv(vec![]),
CopyLegacyOption::IgnoreHeader(1),
]
);
}
_ => unreachable!(),
}
}
Loading