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
36 changes: 35 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8772,6 +8772,10 @@ pub enum CopyLegacyOption {
Null(String),
/// CSV ...
Csv(Vec<CopyLegacyCsvOption>),
/// IAM_ROLE { DEFAULT | 'arn:aws:iam::123456789:role/role1' }
IamRole(IamRoleKind),
/// IGNOREHEADER \[ AS \] number_rows
IgnoreHeader(u64),
}

impl fmt::Display for CopyLegacyOption {
Expand All @@ -8781,7 +8785,37 @@ 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 {role}"),
IgnoreHeader(num_rows) => write!(f, "IGNOREHEADER {num_rows}"),
}
}
}

/// An `IAM_ROLE` option in the AWS ecosystem
///
/// [Redshift COPY](https://docs.aws.amazon.com/redshift/latest/dg/copy-parameters-authorization.html#copy-iam-role)
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum IamRoleKind {
/// Default role
Default,
/// Specific role ARN, for example: `arn:aws:iam::123456789:role/role1`
Arn(String),
}

impl fmt::Display for IamRoleKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
IamRoleKind::Default => write!(f, "DEFAULT"),
IamRoleKind::Arn(arn) => write!(f, "'{arn}'"),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,12 +427,14 @@ define_keywords!(
HOUR,
HOURS,
HUGEINT,
IAM_ROLE,
ICEBERG,
ID,
IDENTITY,
IDENTITY_INSERT,
IF,
IGNORE,
IGNOREHEADER,
ILIKE,
IMMEDIATE,
IMMUTABLE,
Expand Down
17 changes: 17 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9509,6 +9509,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 @@ -9528,11 +9530,26 @@ impl<'a> Parser<'a> {
}
opts
}),
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)
}
_ => self.expected("option", self.peek_token())?,
};
Ok(ret)
}

fn parse_iam_role_kind(&mut self) -> Result<IamRoleKind, ParserError> {
if self.parse_keyword(Keyword::DEFAULT) {
Ok(IamRoleKind::Default)
} else {
let arn = self.parse_literal_string()?;
Ok(IamRoleKind::Arn(arn))
}
}

fn parse_copy_legacy_csv_option(&mut self) -> Result<CopyLegacyCsvOption, ParserError> {
let ret = match self.parse_one_of_keywords(&[
Keyword::HEADER,
Expand Down
40 changes: 40 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16655,3 +16655,43 @@ fn test_parse_default_with_collate_column_option() {
panic!("Expected create table statement");
}
}

#[test]
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(IamRoleKind::Arn(
"arn:aws:iam::123456789:role/role1".to_string()
)),
CopyLegacyOption::Csv(vec![]),
CopyLegacyOption::IgnoreHeader(1),
]
);
}
_ => unreachable!(),
}

let copy = one_statement_parses_to(
r#"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' IAM_ROLE DEFAULT CSV IGNOREHEADER AS 1"#,
r#"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' IAM_ROLE DEFAULT CSV IGNOREHEADER 1"#,
);
match copy {
Statement::Copy { legacy_options, .. } => {
assert_eq!(
legacy_options,
vec![
CopyLegacyOption::IamRole(IamRoleKind::Default),
CopyLegacyOption::Csv(vec![]),
CopyLegacyOption::IgnoreHeader(1),
]
);
}
_ => unreachable!(),
}
}
Loading