Skip to content

Commit ca6ead2

Browse files
committed
Snowflake: ORDER/NOORDER as a real sequence property; CREATE SEQUENCE WITH/comma options; ALTER SEQUENCE SET ORDER|NOORDER
1 parent 14fe810 commit ca6ead2

4 files changed

Lines changed: 40 additions & 7 deletions

File tree

src/ast/dcl.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,11 @@ impl fmt::Display for CreateRole {
428428
write!(f, " AUTHORIZATION {owner}")?;
429429
}
430430
if !self.with_tags.is_empty() {
431-
write!(f, " WITH TAG ({})", display_comma_separated(&self.with_tags))?;
431+
write!(
432+
f,
433+
" WITH TAG ({})",
434+
display_comma_separated(&self.with_tags)
435+
)?;
432436
}
433437
Ok(())
434438
}

src/ast/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8567,6 +8567,8 @@ pub enum SequenceOptions {
85678567
Cache(Expr),
85688568
/// `CYCLE` or `NO CYCLE` option.
85698569
Cycle(bool),
8570+
/// `ORDER` or `NOORDER` option (Snowflake); `true` = `ORDER`, `false` = `NOORDER`.
8571+
Order(bool),
85708572
}
85718573

85728574
impl fmt::Display for SequenceOptions {
@@ -8606,6 +8608,9 @@ impl fmt::Display for SequenceOptions {
86068608
SequenceOptions::Cycle(no) => {
86078609
write!(f, " {}CYCLE", if *no { "NO " } else { "" })
86088610
}
8611+
SequenceOptions::Order(order) => {
8612+
write!(f, " {}", if *order { "ORDER" } else { "NOORDER" })
8613+
}
86098614
}
86108615
}
86118616
}
@@ -13795,6 +13800,8 @@ pub enum AlterSequenceOperation {
1379513800
SetComment(String),
1379613801
/// `UNSET COMMENT`
1379713802
UnsetComment,
13803+
/// `SET ORDER` or `SET NOORDER`; `true` = `ORDER`, `false` = `NOORDER`.
13804+
SetOrder(bool),
1379813805
}
1379913806

1380013807
impl fmt::Display for AlterSequenceOperation {
@@ -13804,6 +13811,9 @@ impl fmt::Display for AlterSequenceOperation {
1380413811
AlterSequenceOperation::SetIncrement(value) => write!(f, "SET INCREMENT BY {value}"),
1380513812
AlterSequenceOperation::SetComment(value) => write!(f, "SET COMMENT = '{value}'"),
1380613813
AlterSequenceOperation::UnsetComment => write!(f, "UNSET COMMENT"),
13814+
AlterSequenceOperation::SetOrder(order) => {
13815+
write!(f, "SET {}", if *order { "ORDER" } else { "NOORDER" })
13816+
}
1380713817
}
1380813818
}
1380913819
}

src/parser/mod.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12231,9 +12231,13 @@ impl<'a> Parser<'a> {
1223112231
let _ = self.parse_keyword(Keyword::BY);
1223212232
let _ = self.consume_token(&Token::Eq);
1223312233
AlterSequenceOperation::SetIncrement(self.parse_number()?)
12234+
} else if self.parse_keyword(Keyword::ORDER) {
12235+
AlterSequenceOperation::SetOrder(true)
12236+
} else if self.parse_keyword(Keyword::NOORDER) {
12237+
AlterSequenceOperation::SetOrder(false)
1223412238
} else {
1223512239
return self.expected(
12236-
"RENAME TO, INCREMENT, SET COMMENT, or UNSET COMMENT after ALTER SEQUENCE",
12240+
"RENAME TO, INCREMENT, ORDER, NOORDER, SET COMMENT, or UNSET COMMENT after ALTER SEQUENCE",
1223712241
self.peek_token(),
1223812242
);
1223912243
}
@@ -21288,10 +21292,14 @@ impl<'a> Parser<'a> {
2128821292

2128921293
fn parse_create_sequence_options(&mut self) -> Result<Vec<SequenceOptions>, ParserError> {
2129021294
// Options may appear in any order (Snowflake) and each keyword may use an
21291-
// optional `=` assignment form (`START = 1`, `INCREMENT = 1`). Snowflake's
21292-
// `ORDER`/`NOORDER` ordering guarantee is accepted and ignored.
21295+
// optional `=` assignment form (`START = 1`, `INCREMENT = 1`). A leading
21296+
// `WITH` and comma separators between options are both accepted.
2129321297
let mut sequence_options = vec![];
21298+
let _ = self.parse_keyword(Keyword::WITH);
2129421299
loop {
21300+
if !sequence_options.is_empty() {
21301+
let _ = self.consume_token(&Token::Comma);
21302+
}
2129521303
if self.parse_keyword(Keyword::INCREMENT) {
2129621304
//[ INCREMENT [ BY ] [ = ] increment ]
2129721305
let by = self.parse_keyword(Keyword::BY);
@@ -21322,8 +21330,10 @@ impl<'a> Parser<'a> {
2132221330
sequence_options.push(SequenceOptions::Cycle(true));
2132321331
} else if self.parse_keyword(Keyword::CYCLE) {
2132421332
sequence_options.push(SequenceOptions::Cycle(false));
21325-
} else if self.parse_keyword(Keyword::ORDER) || self.parse_keyword(Keyword::NOORDER) {
21326-
// Snowflake ordering guarantee — accepted, no effect on emulation.
21333+
} else if self.parse_keyword(Keyword::ORDER) {
21334+
sequence_options.push(SequenceOptions::Order(true));
21335+
} else if self.parse_keyword(Keyword::NOORDER) {
21336+
sequence_options.push(SequenceOptions::Order(false));
2132721337
} else {
2132821338
break;
2132921339
}

tests/sqlparser_snowflake.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ fn parse_sf_create_stream_append_only() {
137137
// and on an `ON VIEW` source.
138138
for (sql, expected) in [
139139
("CREATE STREAM s ON TABLE t APPEND_ONLY = TRUE", Some(true)),
140-
("CREATE STREAM s ON TABLE t APPEND_ONLY = FALSE", Some(false)),
140+
(
141+
"CREATE STREAM s ON TABLE t APPEND_ONLY = FALSE",
142+
Some(false),
143+
),
141144
("CREATE STREAM s ON VIEW v APPEND_ONLY = TRUE", Some(true)),
142145
(
143146
"CREATE STREAM s ON TABLE t AT(STREAM => 'S_BASE') APPEND_ONLY = TRUE",
@@ -1885,6 +1888,12 @@ fn parse_create_sequence_snowflake_options() {
18851888
"CREATE SEQUENCE seq0 START = 1 INCREMENT = 1 ORDER",
18861889
"CREATE SEQUENCE seq0 INCREMENT = 2 START = 5 NOORDER",
18871890
"CREATE SEQUENCE seq0 START WITH 1 INCREMENT 1",
1891+
"CREATE SEQUENCE seq0 WITH START = 1, INCREMENT = 1",
1892+
"CREATE SEQUENCE seq0 WITH START = 1, INCREMENT = 1, ORDER",
1893+
"CREATE SEQUENCE seq0 WITH NOORDER",
1894+
"ALTER SEQUENCE seq0 SET ORDER",
1895+
"ALTER SEQUENCE seq0 SET NOORDER",
1896+
"ALTER SEQUENCE IF EXISTS seq0 SET NOORDER",
18881897
] {
18891898
snowflake()
18901899
.parse_sql_statements(sql)

0 commit comments

Comments
 (0)