Skip to content

Commit 3c29c2e

Browse files
committed
Added dynamic keyword parsing for snowflake
1 parent 308a723 commit 3c29c2e

File tree

5 files changed

+89
-1
lines changed

5 files changed

+89
-1
lines changed

src/ast/ddl.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ pub enum AlterTableOperation {
365365
DropClusteringKey,
366366
SuspendRecluster,
367367
ResumeRecluster,
368+
/// `REFRESH`
369+
///
370+
/// Note: this is Snowflake specific for dynamic tables <https://docs.snowflake.com/en/sql-reference/sql/alter-table>
371+
Refresh,
368372
/// `ALGORITHM [=] { DEFAULT | INSTANT | INPLACE | COPY }`
369373
///
370374
/// [MySQL]-specific table alter algorithm.
@@ -845,6 +849,9 @@ impl fmt::Display for AlterTableOperation {
845849
write!(f, "RESUME RECLUSTER")?;
846850
Ok(())
847851
}
852+
AlterTableOperation::Refresh => {
853+
write!(f, "REFRESH")
854+
}
848855
AlterTableOperation::AutoIncrement { equals, value } => {
849856
write!(
850857
f,

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,7 @@ impl Spanned for AlterTableOperation {
11071107
AlterTableOperation::DropClusteringKey => Span::empty(),
11081108
AlterTableOperation::SuspendRecluster => Span::empty(),
11091109
AlterTableOperation::ResumeRecluster => Span::empty(),
1110+
AlterTableOperation::Refresh => Span::empty(),
11101111
AlterTableOperation::Algorithm { .. } => Span::empty(),
11111112
AlterTableOperation::AutoIncrement { value, .. } => value.span(),
11121113
AlterTableOperation::Lock { .. } => Span::empty(),

src/dialect/snowflake.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::ast::helpers::stmt_data_loading::{
2626
FileStagingCommand, StageLoadSelectItem, StageLoadSelectItemKind, StageParamsObject,
2727
};
2828
use crate::ast::{
29-
CatalogSyncNamespaceMode, ColumnOption, ColumnPolicy, ColumnPolicyProperty, ContactEntry,
29+
AlterTableOperation, CatalogSyncNamespaceMode, ColumnOption, ColumnPolicy, ColumnPolicyProperty, ContactEntry,
3030
CopyIntoSnowflakeKind, CreateTableLikeKind, DollarQuotedString, Ident, IdentityParameters,
3131
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
3232
InitializeKind, ObjectName, ObjectNamePart, RefreshModeKind, RowAccessPolicy, ShowObjects,
@@ -214,6 +214,11 @@ impl Dialect for SnowflakeDialect {
214214
return Some(parser.parse_begin_exception_end());
215215
}
216216

217+
if parser.parse_keywords(&[Keyword::ALTER, Keyword::DYNAMIC, Keyword::TABLE]) {
218+
// ALTER DYNAMIC TABLE
219+
return Some(parse_alter_dynamic_table(parser));
220+
}
221+
217222
if parser.parse_keywords(&[Keyword::ALTER, Keyword::SESSION]) {
218223
// ALTER SESSION
219224
let set = match parser.parse_one_of_keywords(&[Keyword::SET, Keyword::UNSET]) {
@@ -604,6 +609,27 @@ fn parse_file_staging_command(kw: Keyword, parser: &mut Parser) -> Result<Statem
604609
}
605610
}
606611

612+
/// Parse snowflake alter dynamic table.
613+
/// <https://docs.snowflake.com/en/sql-reference/sql/alter-table>
614+
fn parse_alter_dynamic_table(parser: &mut Parser) -> Result<Statement, ParserError> {
615+
let table_name = parser.parse_object_name(false)?;
616+
617+
// Parse the operation (currently only REFRESH is supported)
618+
if parser.parse_keyword(Keyword::REFRESH) {
619+
Ok(Statement::AlterTable {
620+
name: table_name,
621+
if_exists: false,
622+
only: false,
623+
operations: vec![AlterTableOperation::Refresh],
624+
location: None,
625+
on_cluster: None,
626+
iceberg: false,
627+
})
628+
} else {
629+
parser.expected("REFRESH after ALTER DYNAMIC TABLE", parser.peek_token())
630+
}
631+
}
632+
607633
/// Parse snowflake alter session.
608634
/// <https://docs.snowflake.com/en/sql-reference/sql/alter-session>
609635
fn parse_alter_session(parser: &mut Parser, set: bool) -> Result<Statement, ParserError> {

src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ define_keywords!(
784784
REFERENCES,
785785
REFERENCING,
786786
REFRESH_MODE,
787+
REFRESH,
787788
REGCLASS,
788789
REGEXP,
789790
REGION,

tests/sqlparser_snowflake.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4662,3 +4662,56 @@ fn test_drop_constraints() {
46624662
snowflake().verified_stmt("ALTER TABLE tbl DROP FOREIGN KEY k1 RESTRICT");
46634663
snowflake().verified_stmt("ALTER TABLE tbl DROP CONSTRAINT c1 CASCADE");
46644664
}
4665+
4666+
#[test]
4667+
fn test_snowflake_alter_dynamic_table_refresh() {
4668+
// Test with simple table name
4669+
let sql = "ALTER DYNAMIC TABLE MY_DYNAMIC_TABLE REFRESH";
4670+
let result = snowflake().parse_sql_statements(sql);
4671+
4672+
assert!(
4673+
result.is_ok(),
4674+
"Expected ALTER DYNAMIC TABLE to parse successfully"
4675+
);
4676+
4677+
match result.unwrap().first().unwrap() {
4678+
Statement::AlterTable {
4679+
name, operations, ..
4680+
} => {
4681+
assert_eq!(name.to_string(), "MY_DYNAMIC_TABLE");
4682+
assert_eq!(operations.len(), 1);
4683+
match &operations[0] {
4684+
AlterTableOperation::Refresh => {
4685+
// Success!
4686+
}
4687+
_ => panic!("Expected Refresh operation"),
4688+
}
4689+
}
4690+
_ => panic!("Expected AlterTable statement"),
4691+
}
4692+
4693+
// Test with qualified table name
4694+
let sql3 = "ALTER DYNAMIC TABLE my_database.my_schema.my_dynamic_table REFRESH";
4695+
let result3 = snowflake().parse_sql_statements(sql3);
4696+
4697+
assert!(
4698+
result3.is_ok(),
4699+
"Expected ALTER DYNAMIC TABLE to parse successfully"
4700+
);
4701+
4702+
match result3.unwrap().first().unwrap() {
4703+
Statement::AlterTable {
4704+
name, operations, ..
4705+
} => {
4706+
assert_eq!(name.to_string(), "my_database.my_schema.my_dynamic_table");
4707+
assert_eq!(operations.len(), 1);
4708+
match &operations[0] {
4709+
AlterTableOperation::Refresh => {
4710+
// Success!
4711+
}
4712+
_ => panic!("Expected Refresh operation"),
4713+
}
4714+
}
4715+
_ => panic!("Expected AlterTable statement"),
4716+
}
4717+
}

0 commit comments

Comments
 (0)