Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3038,6 +3038,9 @@ pub struct CreateTable {
/// Redshift `DISTKEY` option
/// <https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html>
pub distkey: Option<Ident>,
/// Redshift `BACKUP` option: `BACKUP { YES | NO }`
/// <https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html>
pub backup: Option<bool>,
}

impl fmt::Display for CreateTable {
Expand Down Expand Up @@ -3336,6 +3339,9 @@ impl fmt::Display for CreateTable {
if self.strict {
write!(f, " STRICT")?;
}
if let Some(backup) = self.backup {
write!(f, " BACKUP {}", if backup { "YES" } else { "NO" })?;
}
if let Some(diststyle) = &self.diststyle {
write!(f, " DISTSTYLE {diststyle}")?;
}
Expand Down
10 changes: 10 additions & 0 deletions src/ast/helpers/stmt_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ pub struct CreateTableBuilder {
pub diststyle: Option<DistStyle>,
/// Redshift `DISTKEY` option.
pub distkey: Option<Ident>,
/// Redshift `BACKUP` option.
pub backup: Option<bool>,
}

impl CreateTableBuilder {
Expand Down Expand Up @@ -236,6 +238,7 @@ impl CreateTableBuilder {
require_user: false,
diststyle: None,
distkey: None,
backup: None,
}
}
/// Set `OR REPLACE` for the CREATE TABLE statement.
Expand Down Expand Up @@ -521,6 +524,11 @@ impl CreateTableBuilder {
self.distkey = distkey;
self
}
/// Set the Redshift `BACKUP` option.
pub fn backup(mut self, backup: Option<bool>) -> Self {
self.backup = backup;
self
}
/// Consume the builder and produce a `CreateTable`.
pub fn build(self) -> CreateTable {
CreateTable {
Expand Down Expand Up @@ -579,6 +587,7 @@ impl CreateTableBuilder {
require_user: self.require_user,
diststyle: self.diststyle,
distkey: self.distkey,
backup: self.backup,
}
}
}
Expand Down Expand Up @@ -656,6 +665,7 @@ impl From<CreateTable> for CreateTableBuilder {
require_user: table.require_user,
diststyle: table.diststyle,
distkey: table.distkey,
backup: table.backup,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,7 @@ impl Spanned for CreateTable {
require_user: _,
diststyle: _, // enum, no span
distkey: _, // Ident, todo
backup: _, // bool, no span
} = self;

union_spans(
Expand Down
2 changes: 2 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ define_keywords!(
AVG,
AVG_ROW_LENGTH,
AVRO,
BACKUP,
BACKWARD,
BASE64,
BASE_LOCATION,
Expand Down Expand Up @@ -1167,6 +1168,7 @@ define_keywords!(
XOR,
YEAR,
YEARS,
YES,
ZONE,
ZORDER,
ZSTD
Expand Down
9 changes: 9 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8384,6 +8384,14 @@ impl<'a> Parser<'a> {

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

// Redshift: BACKUP YES|NO
let backup = if self.parse_keyword(Keyword::BACKUP) {
let keyword = self.expect_one_of_keywords(&[Keyword::YES, Keyword::NO])?;
Some(keyword == Keyword::YES)
} else {
None
};

// Redshift: DISTSTYLE, DISTKEY
let diststyle = if self.parse_keyword(Keyword::DISTSTYLE) {
Some(self.parse_dist_style()?)
Expand Down Expand Up @@ -8438,6 +8446,7 @@ impl<'a> Parser<'a> {
.table_options(create_table_config.table_options)
.primary_key(primary_key)
.strict(strict)
.backup(backup)
.diststyle(diststyle)
.distkey(distkey)
.build())
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ fn test_duckdb_union_datatype() {
require_user: Default::default(),
diststyle: Default::default(),
distkey: Default::default(),
backup: Default::default(),
}),
stmt
);
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2008,6 +2008,7 @@ fn parse_create_table_with_valid_options() {
require_user: false,
diststyle: None,
distkey: None,
backup: None,
})
);
}
Expand Down Expand Up @@ -2178,6 +2179,7 @@ fn parse_create_table_with_identity_column() {
require_user: false,
diststyle: None,
distkey: None,
backup: None,
}),
);
}
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6476,6 +6476,7 @@ fn parse_trigger_related_functions() {
require_user: false,
diststyle: None,
distkey: None,
backup: None,
}
);

Expand Down
11 changes: 11 additions & 0 deletions tests/sqlparser_redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,14 @@ fn test_copy_credentials() {
"COPY t1 FROM 's3://bucket/file.csv' CREDENTIALS 'aws_access_key_id=AK;aws_secret_access_key=SK' CSV",
);
}

#[test]
fn test_create_table_backup() {
redshift().verified_stmt("CREATE TABLE public.users (id INT, name VARCHAR(255)) BACKUP YES");

redshift().verified_stmt("CREATE TABLE staging.events (event_id INT) BACKUP NO");

redshift().verified_stmt(
"CREATE TABLE public.users_backup_test BACKUP YES DISTSTYLE AUTO AS SELECT id, name, email FROM public.users",
);
}