Skip to content

Commit 18c466d

Browse files
committed
Code review feedback
1 parent 2094afc commit 18c466d

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
@@ -8772,8 +8772,8 @@ pub enum CopyLegacyOption {
87728772
Null(String),
87738773
/// CSV ...
87748774
Csv(Vec<CopyLegacyCsvOption>),
8775-
/// IAM_ROLE { DEFAULT | 'arn:aws:iam::AWS_ACCOUNT_ID:role/ROLE_NAME' }
8776-
IamRole(Option<String>),
8775+
/// IAM_ROLE { DEFAULT | 'arn:aws:iam::123456789:role/role1' }
8776+
IamRole(IamRoleKind),
87778777
/// IGNOREHEADER \[ AS \] number_rows
87788778
IgnoreHeader(u64),
87798779
}
@@ -8792,18 +8792,34 @@ impl fmt::Display for CopyLegacyOption {
87928792
}
87938793
Ok(())
87948794
}
8795-
IamRole(role) => {
8796-
write!(f, "IAM_ROLE")?;
8797-
match role {
8798-
Some(role) => write!(f, " '{role}'"),
8799-
None => write!(f, " default"),
8800-
}
8801-
}
8795+
IamRole(role) => write!(f, "IAM_ROLE {role}"),
88028796
IgnoreHeader(num_rows) => write!(f, "IGNOREHEADER {num_rows}"),
88038797
}
88048798
}
88058799
}
88068800

8801+
/// An `IAM_ROLE` option in the AWS ecosystem
8802+
///
8803+
/// [Redshift COPY](https://docs.aws.amazon.com/redshift/latest/dg/copy-parameters-authorization.html#copy-iam-role)
8804+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
8805+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
8806+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
8807+
pub enum IamRoleKind {
8808+
/// Default role
8809+
Default,
8810+
/// Specific role ARN, for example: `arn:aws:iam::123456789:role/role1`
8811+
Arn(String),
8812+
}
8813+
8814+
impl fmt::Display for IamRoleKind {
8815+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8816+
match self {
8817+
IamRoleKind::Default => write!(f, "DEFAULT"),
8818+
IamRoleKind::Arn(arn) => write!(f, "'{arn}'"),
8819+
}
8820+
}
8821+
}
8822+
88078823
/// A `CSV` option in `COPY` statement before PostgreSQL version 9.0.
88088824
///
88098825
/// <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
@@ -9530,14 +9530,7 @@ impl<'a> Parser<'a> {
95309530
}
95319531
opts
95329532
}),
9533-
Some(Keyword::IAM_ROLE) => {
9534-
if self.parse_keyword(Keyword::DEFAULT) {
9535-
CopyLegacyOption::IamRole(None)
9536-
} else {
9537-
let role = self.parse_literal_string()?;
9538-
CopyLegacyOption::IamRole(Some(role))
9539-
}
9540-
}
9533+
Some(Keyword::IAM_ROLE) => CopyLegacyOption::IamRole(self.parse_iam_role_kind()?),
95419534
Some(Keyword::IGNOREHEADER) => {
95429535
let _ = self.parse_keyword(Keyword::AS);
95439536
let num_rows = self.parse_literal_uint()?;
@@ -9548,6 +9541,15 @@ impl<'a> Parser<'a> {
95489541
Ok(ret)
95499542
}
95509543

9544+
fn parse_iam_role_kind(&mut self) -> Result<IamRoleKind, ParserError> {
9545+
if self.parse_keyword(Keyword::DEFAULT) {
9546+
Ok(IamRoleKind::Default)
9547+
} else {
9548+
let arn = self.parse_literal_string()?;
9549+
Ok(IamRoleKind::Arn(arn))
9550+
}
9551+
}
9552+
95519553
fn parse_copy_legacy_csv_option(&mut self) -> Result<CopyLegacyCsvOption, ParserError> {
95529554
let ret = match self.parse_one_of_keywords(&[
95539555
Keyword::HEADER,

tests/sqlparser_common.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16657,7 +16657,7 @@ fn test_parse_default_with_collate_column_option() {
1665716657
}
1665816658

1665916659
#[test]
16660-
fn pares_copy_options() {
16660+
fn parse_copy_options() {
1666116661
let copy = verified_stmt(
1666216662
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"#,
1666316663
);
@@ -16666,7 +16666,7 @@ fn pares_copy_options() {
1666616666
assert_eq!(
1666716667
legacy_options,
1666816668
vec![
16669-
CopyLegacyOption::IamRole(Some(
16669+
CopyLegacyOption::IamRole(IamRoleKind::Arn(
1667016670
"arn:aws:iam::123456789:role/role1".to_string()
1667116671
)),
1667216672
CopyLegacyOption::Csv(vec![]),
@@ -16676,4 +16676,22 @@ fn pares_copy_options() {
1667616676
}
1667716677
_ => unreachable!(),
1667816678
}
16679+
16680+
let copy = one_statement_parses_to(
16681+
r#"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' IAM_ROLE DEFAULT CSV IGNOREHEADER AS 1"#,
16682+
r#"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' IAM_ROLE DEFAULT CSV IGNOREHEADER 1"#,
16683+
);
16684+
match copy {
16685+
Statement::Copy { legacy_options, .. } => {
16686+
assert_eq!(
16687+
legacy_options,
16688+
vec![
16689+
CopyLegacyOption::IamRole(IamRoleKind::Default),
16690+
CopyLegacyOption::Csv(vec![]),
16691+
CopyLegacyOption::IgnoreHeader(1),
16692+
]
16693+
);
16694+
}
16695+
_ => unreachable!(),
16696+
}
1667916697
}

0 commit comments

Comments
 (0)