Skip to content

Commit 9a16128

Browse files
committed
Code review feedback
1 parent de019d9 commit 9a16128

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

src/ast/mod.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8570,8 +8570,8 @@ pub enum CopyLegacyOption {
85708570
Null(String),
85718571
/// CSV ...
85728572
Csv(Vec<CopyLegacyCsvOption>),
8573-
/// IAM_ROLE { default | 'arn:aws:iam::123456789:role/role1' }
8574-
IamRole(Option<String>),
8573+
/// IAM_ROLE { DEFAULT | 'arn:aws:iam::123456789:role/role1' }
8574+
IamRole(IamRoleKind),
85758575
/// IGNOREHEADER \[ AS \] number_rows
85768576
IgnoreHeader(u64),
85778577
}
@@ -8590,18 +8590,34 @@ impl fmt::Display for CopyLegacyOption {
85908590
}
85918591
Ok(())
85928592
}
8593-
IamRole(role) => {
8594-
write!(f, "IAM_ROLE")?;
8595-
match role {
8596-
Some(role) => write!(f, " '{role}'"),
8597-
None => write!(f, " default"),
8598-
}
8599-
}
8593+
IamRole(role) => write!(f, "IAM_ROLE {role}"),
86008594
IgnoreHeader(num_rows) => write!(f, "IGNOREHEADER {num_rows}"),
86018595
}
86028596
}
86038597
}
86048598

8599+
/// An `IAM_ROLE` option in the AWS ecosystem
8600+
///
8601+
/// [Redshift COPY](https://docs.aws.amazon.com/redshift/latest/dg/copy-parameters-authorization.html#copy-iam-role)
8602+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
8603+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8604+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
8605+
pub enum IamRoleKind {
8606+
/// Default role
8607+
Default,
8608+
/// Specific role ARN, for example: `arn:aws:iam::123456789:role/role1`
8609+
Arn(String),
8610+
}
8611+
8612+
impl fmt::Display for IamRoleKind {
8613+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8614+
match self {
8615+
IamRoleKind::Default => write!(f, "DEFAULT"),
8616+
IamRoleKind::Arn(arn) => write!(f, "'{arn}'"),
8617+
}
8618+
}
8619+
}
8620+
86058621
/// A `CSV` option in `COPY` statement before PostgreSQL version 9.0.
86068622
///
86078623
/// <https://www.postgresql.org/docs/8.4/sql-copy.html>

src/parser/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9379,14 +9379,7 @@ impl<'a> Parser<'a> {
93799379
}
93809380
opts
93819381
}),
9382-
Some(Keyword::IAM_ROLE) => {
9383-
if self.parse_keyword(Keyword::DEFAULT) {
9384-
CopyLegacyOption::IamRole(None)
9385-
} else {
9386-
let role = self.parse_literal_string()?;
9387-
CopyLegacyOption::IamRole(Some(role))
9388-
}
9389-
}
9382+
Some(Keyword::IAM_ROLE) => CopyLegacyOption::IamRole(self.parse_iam_role_kind()?),
93909383
Some(Keyword::IGNOREHEADER) => {
93919384
let _ = self.parse_keyword(Keyword::AS);
93929385
let num_rows = self.parse_literal_uint()?;
@@ -9397,6 +9390,15 @@ impl<'a> Parser<'a> {
93979390
Ok(ret)
93989391
}
93999392

9393+
fn parse_iam_role_kind(&mut self) -> Result<IamRoleKind, ParserError> {
9394+
if self.parse_keyword(Keyword::DEFAULT) {
9395+
Ok(IamRoleKind::Default)
9396+
} else {
9397+
let arn = self.parse_literal_string()?;
9398+
Ok(IamRoleKind::Arn(arn))
9399+
}
9400+
}
9401+
94009402
fn parse_copy_legacy_csv_option(&mut self) -> Result<CopyLegacyCsvOption, ParserError> {
94019403
let ret = match self.parse_one_of_keywords(&[
94029404
Keyword::HEADER,

tests/sqlparser_common.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16258,7 +16258,7 @@ fn parse_notnull() {
1625816258
}
1625916259

1626016260
#[test]
16261-
fn pares_copy_options() {
16261+
fn parse_copy_options() {
1626216262
let copy = verified_stmt(
1626316263
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"#,
1626416264
);
@@ -16267,7 +16267,7 @@ fn pares_copy_options() {
1626716267
assert_eq!(
1626816268
legacy_options,
1626916269
vec![
16270-
CopyLegacyOption::IamRole(Some(
16270+
CopyLegacyOption::IamRole(IamRoleKind::Arn(
1627116271
"arn:aws:iam::123456789:role/role1".to_string()
1627216272
)),
1627316273
CopyLegacyOption::Csv(vec![]),
@@ -16277,4 +16277,22 @@ fn pares_copy_options() {
1627716277
}
1627816278
_ => unreachable!(),
1627916279
}
16280+
16281+
let copy = one_statement_parses_to(
16282+
r#"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' IAM_ROLE DEFAULT CSV IGNOREHEADER AS 1"#,
16283+
r#"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' IAM_ROLE DEFAULT CSV IGNOREHEADER 1"#,
16284+
);
16285+
match copy {
16286+
Statement::Copy { legacy_options, .. } => {
16287+
assert_eq!(
16288+
legacy_options,
16289+
vec![
16290+
CopyLegacyOption::IamRole(IamRoleKind::Default),
16291+
CopyLegacyOption::Csv(vec![]),
16292+
CopyLegacyOption::IgnoreHeader(1),
16293+
]
16294+
);
16295+
}
16296+
_ => unreachable!(),
16297+
}
1628016298
}

0 commit comments

Comments
 (0)