Skip to content

Commit ed0859d

Browse files
committed
Code review feedback
1 parent 2144d92 commit ed0859d

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::123456789:role/role1' }
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
@@ -9529,14 +9529,7 @@ impl<'a> Parser<'a> {
95299529
}
95309530
opts
95319531
}),
9532-
Some(Keyword::IAM_ROLE) => {
9533-
if self.parse_keyword(Keyword::DEFAULT) {
9534-
CopyLegacyOption::IamRole(None)
9535-
} else {
9536-
let role = self.parse_literal_string()?;
9537-
CopyLegacyOption::IamRole(Some(role))
9538-
}
9539-
}
9532+
Some(Keyword::IAM_ROLE) => CopyLegacyOption::IamRole(self.parse_iam_role_kind()?),
95409533
Some(Keyword::IGNOREHEADER) => {
95419534
let _ = self.parse_keyword(Keyword::AS);
95429535
let num_rows = self.parse_literal_uint()?;
@@ -9547,6 +9540,15 @@ impl<'a> Parser<'a> {
95479540
Ok(ret)
95489541
}
95499542

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

tests/sqlparser_common.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16600,7 +16600,7 @@ fn parse_create_view_if_not_exists() {
1660016600
}
1660116601

1660216602
#[test]
16603-
fn pares_copy_options() {
16603+
fn parse_copy_options() {
1660416604
let copy = verified_stmt(
1660516605
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"#,
1660616606
);
@@ -16609,7 +16609,7 @@ fn pares_copy_options() {
1660916609
assert_eq!(
1661016610
legacy_options,
1661116611
vec![
16612-
CopyLegacyOption::IamRole(Some(
16612+
CopyLegacyOption::IamRole(IamRoleKind::Arn(
1661316613
"arn:aws:iam::123456789:role/role1".to_string()
1661416614
)),
1661516615
CopyLegacyOption::Csv(vec![]),
@@ -16619,4 +16619,22 @@ fn pares_copy_options() {
1661916619
}
1662016620
_ => unreachable!(),
1662116621
}
16622+
16623+
let copy = one_statement_parses_to(
16624+
r#"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' IAM_ROLE DEFAULT CSV IGNOREHEADER AS 1"#,
16625+
r#"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' IAM_ROLE DEFAULT CSV IGNOREHEADER 1"#,
16626+
);
16627+
match copy {
16628+
Statement::Copy { legacy_options, .. } => {
16629+
assert_eq!(
16630+
legacy_options,
16631+
vec![
16632+
CopyLegacyOption::IamRole(IamRoleKind::Default),
16633+
CopyLegacyOption::Csv(vec![]),
16634+
CopyLegacyOption::IgnoreHeader(1),
16635+
]
16636+
);
16637+
}
16638+
_ => unreachable!(),
16639+
}
1662216640
}

0 commit comments

Comments
 (0)