This is an automated email from the ASF dual-hosted git repository. github-bot pushed a commit to branch gh-readonly-queue/main/pr-2270-50921b173a3ca7a3183398cc83de1dfd1cd165f5 in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git
commit 1097a0d5a6a852737b55f96bd38fba3f2ece9bd0 Author: Andriy Romanov <[email protected]> AuthorDate: Fri Mar 20 03:03:55 2026 -0700 Fixed BACKUP parsing for redshift (#2270) --- src/ast/ddl.rs | 6 ++++++ src/ast/helpers/stmt_create_table.rs | 10 ++++++++++ src/ast/spans.rs | 1 + src/keywords.rs | 2 ++ src/parser/mod.rs | 9 +++++++++ tests/sqlparser_duckdb.rs | 1 + tests/sqlparser_mssql.rs | 2 ++ tests/sqlparser_postgres.rs | 1 + tests/sqlparser_redshift.rs | 11 +++++++++++ 9 files changed, 43 insertions(+) diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index f0e79e73..879740f0 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -3057,6 +3057,9 @@ pub struct CreateTable { /// Redshift `SORTKEY` option /// <https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html> pub sortkey: Option<Vec<Expr>>, + /// 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 { @@ -3360,6 +3363,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}")?; } diff --git a/src/ast/helpers/stmt_create_table.rs b/src/ast/helpers/stmt_create_table.rs index 29589e21..ab2feb69 100644 --- a/src/ast/helpers/stmt_create_table.rs +++ b/src/ast/helpers/stmt_create_table.rs @@ -181,6 +181,8 @@ pub struct CreateTableBuilder { pub distkey: Option<Expr>, /// Redshift `SORTKEY` option. pub sortkey: Option<Vec<Expr>>, + /// Redshift `BACKUP` option. + pub backup: Option<bool>, } impl CreateTableBuilder { @@ -245,6 +247,7 @@ impl CreateTableBuilder { diststyle: None, distkey: None, sortkey: None, + backup: None, } } /// Set `OR REPLACE` for the CREATE TABLE statement. @@ -548,6 +551,11 @@ impl CreateTableBuilder { self.sortkey = sortkey; 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 { @@ -609,6 +617,7 @@ impl CreateTableBuilder { diststyle: self.diststyle, distkey: self.distkey, sortkey: self.sortkey, + backup: self.backup, } } } @@ -689,6 +698,7 @@ impl From<CreateTable> for CreateTableBuilder { diststyle: table.diststyle, distkey: table.distkey, sortkey: table.sortkey, + backup: table.backup, } } } diff --git a/src/ast/spans.rs b/src/ast/spans.rs index 78698bbe..2af57d98 100644 --- a/src/ast/spans.rs +++ b/src/ast/spans.rs @@ -589,6 +589,7 @@ impl Spanned for CreateTable { diststyle: _, distkey: _, sortkey: _, + backup: _, } = self; union_spans( diff --git a/src/keywords.rs b/src/keywords.rs index 94458ccb..f0f37b1c 100644 --- a/src/keywords.rs +++ b/src/keywords.rs @@ -145,6 +145,7 @@ define_keywords!( AVG, AVG_ROW_LENGTH, AVRO, + BACKUP, BACKWARD, BASE64, BASE_LOCATION, @@ -1171,6 +1172,7 @@ define_keywords!( XOR, YEAR, YEARS, + YES, ZONE, ZORDER, ZSTD diff --git a/src/parser/mod.rs b/src/parser/mod.rs index f617cadd..cefc0c6f 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -8443,6 +8443,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, SORTKEY let diststyle = if self.parse_keyword(Keyword::DISTSTYLE) { Some(self.parse_dist_style()?) @@ -8505,6 +8513,7 @@ impl<'a> Parser<'a> { .table_options(create_table_config.table_options) .primary_key(primary_key) .strict(strict) + .backup(backup) .diststyle(diststyle) .distkey(distkey) .sortkey(sortkey) diff --git a/tests/sqlparser_duckdb.rs b/tests/sqlparser_duckdb.rs index 671e92b9..df626858 100644 --- a/tests/sqlparser_duckdb.rs +++ b/tests/sqlparser_duckdb.rs @@ -793,6 +793,7 @@ fn test_duckdb_union_datatype() { diststyle: Default::default(), distkey: Default::default(), sortkey: Default::default(), + backup: Default::default(), }), stmt ); diff --git a/tests/sqlparser_mssql.rs b/tests/sqlparser_mssql.rs index 07dd0fcb..733923f4 100644 --- a/tests/sqlparser_mssql.rs +++ b/tests/sqlparser_mssql.rs @@ -2011,6 +2011,7 @@ fn parse_create_table_with_valid_options() { diststyle: None, distkey: None, sortkey: None, + backup: None, }) ); } @@ -2184,6 +2185,7 @@ fn parse_create_table_with_identity_column() { diststyle: None, distkey: None, sortkey: None, + backup: None, }), ); } diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 6b4f35d7..9a4ff418 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -6503,6 +6503,7 @@ fn parse_trigger_related_functions() { diststyle: None, distkey: None, sortkey: None, + backup: None, } ); diff --git a/tests/sqlparser_redshift.rs b/tests/sqlparser_redshift.rs index 184aa5b6..319a818c 100644 --- a/tests/sqlparser_redshift.rs +++ b/tests/sqlparser_redshift.rs @@ -500,3 +500,14 @@ fn test_alter_table_alter_sortkey() { redshift().verified_stmt("ALTER TABLE users ALTER SORTKEY(created_at)"); redshift().verified_stmt("ALTER TABLE users ALTER SORTKEY(c1, c2)"); } + +#[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", + ); +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
