Skip to content

Commit 673f258

Browse files
committed
Fixed BACKUP parsing for redshift
1 parent 3fa7114 commit 673f258

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
@@ -3051,6 +3051,9 @@ pub struct CreateTable {
30513051
/// Redshift `SORTKEY` option
30523052
/// <https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html>
30533053
pub sortkey: Option<Vec<Expr>>,
3054+
/// Redshift `BACKUP` option: `BACKUP { YES | NO }`
3055+
/// <https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html>
3056+
pub backup: Option<bool>,
30543057
}
30553058

30563059
impl fmt::Display for CreateTable {
@@ -3349,6 +3352,9 @@ impl fmt::Display for CreateTable {
33493352
if self.strict {
33503353
write!(f, " STRICT")?;
33513354
}
3355+
if let Some(backup) = self.backup {
3356+
write!(f, " BACKUP {}", if backup { "YES" } else { "NO" })?;
3357+
}
33523358
if let Some(diststyle) = &self.diststyle {
33533359
write!(f, " DISTSTYLE {diststyle}")?;
33543360
}

src/ast/helpers/stmt_create_table.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ pub struct CreateTableBuilder {
177177
pub distkey: Option<Expr>,
178178
/// Redshift `SORTKEY` option.
179179
pub sortkey: Option<Vec<Expr>>,
180+
/// Redshift `BACKUP` option.
181+
pub backup: Option<bool>,
180182
}
181183

182184
impl CreateTableBuilder {
@@ -239,6 +241,7 @@ impl CreateTableBuilder {
239241
diststyle: None,
240242
distkey: None,
241243
sortkey: None,
244+
backup: None,
242245
}
243246
}
244247
/// Set `OR REPLACE` for the CREATE TABLE statement.
@@ -529,6 +532,11 @@ impl CreateTableBuilder {
529532
self.sortkey = sortkey;
530533
self
531534
}
535+
/// Set the Redshift `BACKUP` option.
536+
pub fn backup(mut self, backup: Option<bool>) -> Self {
537+
self.backup = backup;
538+
self
539+
}
532540
/// Consume the builder and produce a `CreateTable`.
533541
pub fn build(self) -> CreateTable {
534542
CreateTable {
@@ -588,6 +596,7 @@ impl CreateTableBuilder {
588596
diststyle: self.diststyle,
589597
distkey: self.distkey,
590598
sortkey: self.sortkey,
599+
backup: self.backup,
591600
}
592601
}
593602
}
@@ -666,6 +675,7 @@ impl From<CreateTable> for CreateTableBuilder {
666675
diststyle: table.diststyle,
667676
distkey: table.distkey,
668677
sortkey: table.sortkey,
678+
backup: table.backup,
669679
}
670680
}
671681
}

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ impl Spanned for CreateTable {
585585
diststyle: _,
586586
distkey: _,
587587
sortkey: _,
588+
backup: _,
588589
} = self;
589590

590591
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,
@@ -1168,6 +1169,7 @@ define_keywords!(
11681169
XOR,
11691170
YEAR,
11701171
YEARS,
1172+
YES,
11711173
ZONE,
11721174
ZORDER,
11731175
ZSTD

src/parser/mod.rs

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

83898389
let strict = self.parse_keyword(Keyword::STRICT);
83908390

8391+
// Redshift: BACKUP YES|NO
8392+
let backup = if self.parse_keyword(Keyword::BACKUP) {
8393+
let keyword = self.expect_one_of_keywords(&[Keyword::YES, Keyword::NO])?;
8394+
Some(keyword == Keyword::YES)
8395+
} else {
8396+
None
8397+
};
8398+
83918399
// Redshift: DISTSTYLE, DISTKEY, SORTKEY
83928400
let diststyle = if self.parse_keyword(Keyword::DISTSTYLE) {
83938401
Some(self.parse_dist_style()?)
@@ -8450,6 +8458,7 @@ impl<'a> Parser<'a> {
84508458
.table_options(create_table_config.table_options)
84518459
.primary_key(primary_key)
84528460
.strict(strict)
8461+
.backup(backup)
84538462
.diststyle(diststyle)
84548463
.distkey(distkey)
84558464
.sortkey(sortkey)

tests/sqlparser_duckdb.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ fn test_duckdb_union_datatype() {
791791
diststyle: Default::default(),
792792
distkey: Default::default(),
793793
sortkey: Default::default(),
794+
backup: Default::default(),
794795
}),
795796
stmt
796797
);

tests/sqlparser_mssql.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,7 @@ fn parse_create_table_with_valid_options() {
20092009
diststyle: None,
20102010
distkey: None,
20112011
sortkey: None,
2012+
backup: None,
20122013
})
20132014
);
20142015
}
@@ -2180,6 +2181,7 @@ fn parse_create_table_with_identity_column() {
21802181
diststyle: None,
21812182
distkey: None,
21822183
sortkey: None,
2184+
backup: None,
21832185
}),
21842186
);
21852187
}

tests/sqlparser_postgres.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6477,6 +6477,7 @@ fn parse_trigger_related_functions() {
64776477
diststyle: None,
64786478
distkey: None,
64796479
sortkey: None,
6480+
backup: None,
64806481
}
64816482
);
64826483

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)