Skip to content

Commit 1097a0d

Browse files
authored
Fixed BACKUP parsing for redshift (#2270)
1 parent 50921b1 commit 1097a0d

File tree

9 files changed

+43
-0
lines changed

9 files changed

+43
-0
lines changed

src/ast/ddl.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3057,6 +3057,9 @@ pub struct CreateTable {
30573057
/// Redshift `SORTKEY` option
30583058
/// <https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html>
30593059
pub sortkey: Option<Vec<Expr>>,
3060+
/// Redshift `BACKUP` option: `BACKUP { YES | NO }`
3061+
/// <https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html>
3062+
pub backup: Option<bool>,
30603063
}
30613064

30623065
impl fmt::Display for CreateTable {
@@ -3360,6 +3363,9 @@ impl fmt::Display for CreateTable {
33603363
if self.strict {
33613364
write!(f, " STRICT")?;
33623365
}
3366+
if let Some(backup) = self.backup {
3367+
write!(f, " BACKUP {}", if backup { "YES" } else { "NO" })?;
3368+
}
33633369
if let Some(diststyle) = &self.diststyle {
33643370
write!(f, " DISTSTYLE {diststyle}")?;
33653371
}

src/ast/helpers/stmt_create_table.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ pub struct CreateTableBuilder {
181181
pub distkey: Option<Expr>,
182182
/// Redshift `SORTKEY` option.
183183
pub sortkey: Option<Vec<Expr>>,
184+
/// Redshift `BACKUP` option.
185+
pub backup: Option<bool>,
184186
}
185187

186188
impl CreateTableBuilder {
@@ -245,6 +247,7 @@ impl CreateTableBuilder {
245247
diststyle: None,
246248
distkey: None,
247249
sortkey: None,
250+
backup: None,
248251
}
249252
}
250253
/// Set `OR REPLACE` for the CREATE TABLE statement.
@@ -548,6 +551,11 @@ impl CreateTableBuilder {
548551
self.sortkey = sortkey;
549552
self
550553
}
554+
/// Set the Redshift `BACKUP` option.
555+
pub fn backup(mut self, backup: Option<bool>) -> Self {
556+
self.backup = backup;
557+
self
558+
}
551559
/// Consume the builder and produce a `CreateTable`.
552560
pub fn build(self) -> CreateTable {
553561
CreateTable {
@@ -609,6 +617,7 @@ impl CreateTableBuilder {
609617
diststyle: self.diststyle,
610618
distkey: self.distkey,
611619
sortkey: self.sortkey,
620+
backup: self.backup,
612621
}
613622
}
614623
}
@@ -689,6 +698,7 @@ impl From<CreateTable> for CreateTableBuilder {
689698
diststyle: table.diststyle,
690699
distkey: table.distkey,
691700
sortkey: table.sortkey,
701+
backup: table.backup,
692702
}
693703
}
694704
}

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ impl Spanned for CreateTable {
589589
diststyle: _,
590590
distkey: _,
591591
sortkey: _,
592+
backup: _,
592593
} = self;
593594

594595
union_spans(

src/keywords.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ define_keywords!(
145145
AVG,
146146
AVG_ROW_LENGTH,
147147
AVRO,
148+
BACKUP,
148149
BACKWARD,
149150
BASE64,
150151
BASE_LOCATION,
@@ -1171,6 +1172,7 @@ define_keywords!(
11711172
XOR,
11721173
YEAR,
11731174
YEARS,
1175+
YES,
11741176
ZONE,
11751177
ZORDER,
11761178
ZSTD

src/parser/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8443,6 +8443,14 @@ impl<'a> Parser<'a> {
84438443

84448444
let strict = self.parse_keyword(Keyword::STRICT);
84458445

8446+
// Redshift: BACKUP YES|NO
8447+
let backup = if self.parse_keyword(Keyword::BACKUP) {
8448+
let keyword = self.expect_one_of_keywords(&[Keyword::YES, Keyword::NO])?;
8449+
Some(keyword == Keyword::YES)
8450+
} else {
8451+
None
8452+
};
8453+
84468454
// Redshift: DISTSTYLE, DISTKEY, SORTKEY
84478455
let diststyle = if self.parse_keyword(Keyword::DISTSTYLE) {
84488456
Some(self.parse_dist_style()?)
@@ -8505,6 +8513,7 @@ impl<'a> Parser<'a> {
85058513
.table_options(create_table_config.table_options)
85068514
.primary_key(primary_key)
85078515
.strict(strict)
8516+
.backup(backup)
85088517
.diststyle(diststyle)
85098518
.distkey(distkey)
85108519
.sortkey(sortkey)

tests/sqlparser_duckdb.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,7 @@ fn test_duckdb_union_datatype() {
793793
diststyle: Default::default(),
794794
distkey: Default::default(),
795795
sortkey: Default::default(),
796+
backup: Default::default(),
796797
}),
797798
stmt
798799
);

tests/sqlparser_mssql.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,6 +2011,7 @@ fn parse_create_table_with_valid_options() {
20112011
diststyle: None,
20122012
distkey: None,
20132013
sortkey: None,
2014+
backup: None,
20142015
})
20152016
);
20162017
}
@@ -2184,6 +2185,7 @@ fn parse_create_table_with_identity_column() {
21842185
diststyle: None,
21852186
distkey: None,
21862187
sortkey: None,
2188+
backup: None,
21872189
}),
21882190
);
21892191
}

tests/sqlparser_postgres.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6503,6 +6503,7 @@ fn parse_trigger_related_functions() {
65036503
diststyle: None,
65046504
distkey: None,
65056505
sortkey: None,
6506+
backup: None,
65066507
}
65076508
);
65086509

tests/sqlparser_redshift.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,14 @@ fn test_alter_table_alter_sortkey() {
500500
redshift().verified_stmt("ALTER TABLE users ALTER SORTKEY(created_at)");
501501
redshift().verified_stmt("ALTER TABLE users ALTER SORTKEY(c1, c2)");
502502
}
503+
504+
#[test]
505+
fn test_create_table_backup() {
506+
redshift().verified_stmt("CREATE TABLE public.users (id INT, name VARCHAR(255)) BACKUP YES");
507+
508+
redshift().verified_stmt("CREATE TABLE staging.events (event_id INT) BACKUP NO");
509+
510+
redshift().verified_stmt(
511+
"CREATE TABLE public.users_backup_test BACKUP YES DISTSTYLE AUTO AS SELECT id, name, email FROM public.users",
512+
);
513+
}

0 commit comments

Comments
 (0)