Skip to content

Commit 43b9f45

Browse files
committed
Code review feedback
1 parent cdba474 commit 43b9f45

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
@@ -9569,14 +9569,7 @@ impl<'a> Parser<'a> {
95699569
}
95709570
opts
95719571
}),
9572-
Some(Keyword::IAM_ROLE) => {
9573-
if self.parse_keyword(Keyword::DEFAULT) {
9574-
CopyLegacyOption::IamRole(None)
9575-
} else {
9576-
let role = self.parse_literal_string()?;
9577-
CopyLegacyOption::IamRole(Some(role))
9578-
}
9579-
}
9572+
Some(Keyword::IAM_ROLE) => CopyLegacyOption::IamRole(self.parse_iam_role_kind()?),
95809573
Some(Keyword::IGNOREHEADER) => {
95819574
let _ = self.parse_keyword(Keyword::AS);
95829575
let num_rows = self.parse_literal_uint()?;
@@ -9587,6 +9580,15 @@ impl<'a> Parser<'a> {
95879580
Ok(ret)
95889581
}
95899582

9583+
fn parse_iam_role_kind(&mut self) -> Result<IamRoleKind, ParserError> {
9584+
if self.parse_keyword(Keyword::DEFAULT) {
9585+
Ok(IamRoleKind::Default)
9586+
} else {
9587+
let arn = self.parse_literal_string()?;
9588+
Ok(IamRoleKind::Arn(arn))
9589+
}
9590+
}
9591+
95909592
fn parse_copy_legacy_csv_option(&mut self) -> Result<CopyLegacyCsvOption, ParserError> {
95919593
let ret = match self.parse_one_of_keywords(&[
95929594
Keyword::HEADER,

tests/sqlparser_common.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16731,7 +16731,7 @@ fn parse_create_table_like() {
1673116731
}
1673216732

1673316733
#[test]
16734-
fn pares_copy_options() {
16734+
fn parse_copy_options() {
1673516735
let copy = verified_stmt(
1673616736
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"#,
1673716737
);
@@ -16740,7 +16740,7 @@ fn pares_copy_options() {
1674016740
assert_eq!(
1674116741
legacy_options,
1674216742
vec![
16743-
CopyLegacyOption::IamRole(Some(
16743+
CopyLegacyOption::IamRole(IamRoleKind::Arn(
1674416744
"arn:aws:iam::123456789:role/role1".to_string()
1674516745
)),
1674616746
CopyLegacyOption::Csv(vec![]),
@@ -16750,4 +16750,22 @@ fn pares_copy_options() {
1675016750
}
1675116751
_ => unreachable!(),
1675216752
}
16753+
16754+
let copy = one_statement_parses_to(
16755+
r#"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' IAM_ROLE DEFAULT CSV IGNOREHEADER AS 1"#,
16756+
r#"COPY dst (c1, c2, c3) FROM 's3://redshift-downloads/tickit/category_pipe.txt' IAM_ROLE DEFAULT CSV IGNOREHEADER 1"#,
16757+
);
16758+
match copy {
16759+
Statement::Copy { legacy_options, .. } => {
16760+
assert_eq!(
16761+
legacy_options,
16762+
vec![
16763+
CopyLegacyOption::IamRole(IamRoleKind::Default),
16764+
CopyLegacyOption::Csv(vec![]),
16765+
CopyLegacyOption::IgnoreHeader(1),
16766+
]
16767+
);
16768+
}
16769+
_ => unreachable!(),
16770+
}
1675316771
}

0 commit comments

Comments
 (0)