Skip to content

Commit bf0f8f4

Browse files
committed
Snowflake create database
1 parent 3b4e6bb commit bf0f8f4

File tree

7 files changed

+52
-34
lines changed

7 files changed

+52
-34
lines changed

src/ast/ddl.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ use serde::{Deserialize, Serialize};
2929
use sqlparser_derive::{Visit, VisitMut};
3030

3131
use crate::ast::value::escape_single_quote_string;
32-
use crate::ast::{display_comma_separated, display_separated, CatalogSyncNamespaceMode, CommentDef, CreateFunctionBody, CreateFunctionUsing, DataType, Expr, FunctionBehavior, FunctionCalledOnNull, FunctionDeterminismSpecifier, FunctionParallel, Ident, MySQLColumnPosition, ObjectName, OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, SqlOption, StorageSerializationPolicy, Tag, Value, ValueWithSpan};
32+
use crate::ast::{
33+
display_comma_separated, display_separated, CatalogSyncNamespaceMode, CommentDef,
34+
CreateFunctionBody, CreateFunctionUsing, DataType, Expr, FunctionBehavior,
35+
FunctionCalledOnNull, FunctionDeterminismSpecifier, FunctionParallel, Ident,
36+
MySQLColumnPosition, ObjectName, OperateFunctionArg, OrderByExpr, ProjectionSelect,
37+
SequenceOptions, SqlOption, StorageSerializationPolicy, Tag, Value, ValueWithSpan,
38+
};
3339
use crate::keywords::Keyword;
3440
use crate::tokenizer::Token;
3541

@@ -2298,7 +2304,11 @@ impl fmt::Display for CreateSnowflakeDatabase {
22982304
"CREATE {or_replace}{transient}DATABASE {if_not_exists}{name}",
22992305
or_replace = if self.or_replace { "OR REPLACE " } else { "" },
23002306
transient = if self.transient { "TRANSIENT " } else { "" },
2301-
if_not_exists = if self.if_not_exists { "IF NOT EXISTS " } else { "" },
2307+
if_not_exists = if self.if_not_exists {
2308+
"IF NOT EXISTS "
2309+
} else {
2310+
""
2311+
},
23022312
name = self.name,
23032313
)?;
23042314

src/ast/helpers/stmt_create_database.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,11 @@ impl TryFrom<Statement> for CreateDatabaseBuilder {
272272
}
273273
}
274274

275-
276275
#[cfg(test)]
277276
mod tests {
277+
use crate::ast::helpers::stmt_create_database::CreateDatabaseBuilder;
278278
use crate::ast::helpers::stmt_create_table::CreateTableBuilder;
279279
use crate::ast::{Ident, ObjectName, Statement};
280-
use crate::ast::helpers::stmt_create_database::CreateDatabaseBuilder;
281280
use crate::parser::ParserError;
282281

283282
#[test]

src/ast/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ pub mod helpers;
104104
mod operator;
105105
mod query;
106106
mod spans;
107-
pub use spans::Spanned;
108107
use crate::ast::ddl::CreateSnowflakeDatabase;
108+
pub use spans::Spanned;
109109

110110
mod trigger;
111111
mod value;

src/ast/spans.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use crate::ast::query::SelectItemQualifiedWildcardKind;
19-
use core::iter;
2018
use crate::ast::ddl::CreateSnowflakeDatabase;
19+
use crate::ast::query::SelectItemQualifiedWildcardKind;
2120
use crate::tokenizer::Span;
21+
use core::iter;
2222

2323
use super::{
2424
dcl::SecondaryRoles, value::ValueWithSpan, AccessExpr, AlterColumnOperation,
@@ -604,10 +604,7 @@ impl Spanned for CreateTable {
604604

605605
impl Spanned for CreateSnowflakeDatabase {
606606
fn span(&self) -> Span {
607-
union_spans(
608-
core::iter::once(self.name.span())
609-
.chain(self.clone.iter().map(|c| c.span()))
610-
)
607+
union_spans(core::iter::once(self.name.span()).chain(self.clone.iter().map(|c| c.span())))
611608
}
612609
}
613610

src/dialect/snowflake.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ use crate::ast::helpers::stmt_create_table::CreateTableBuilder;
2222
use crate::ast::helpers::stmt_data_loading::{
2323
FileStagingCommand, StageLoadSelectItem, StageParamsObject,
2424
};
25-
use crate::ast::{CatalogSyncNamespaceMode, ColumnOption, ColumnPolicy, ColumnPolicyProperty, CopyIntoSnowflakeKind, Ident, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder, ObjectName, RowAccessPolicy, ShowObjects, Statement, TagsColumnOption, WrappedCollection};
25+
use crate::ast::{
26+
CatalogSyncNamespaceMode, ColumnOption, ColumnPolicy, ColumnPolicyProperty,
27+
CopyIntoSnowflakeKind, Ident, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind,
28+
IdentityPropertyKind, IdentityPropertyOrder, ObjectName, RowAccessPolicy, ShowObjects,
29+
Statement, TagsColumnOption, WrappedCollection,
30+
};
2631
use crate::dialect::{Dialect, Precedence};
2732
use crate::keywords::Keyword;
2833
use crate::parser::{Parser, ParserError};
@@ -37,8 +42,8 @@ use alloc::vec::Vec;
3742
use alloc::{format, vec};
3843

3944
use super::keywords::RESERVED_FOR_IDENTIFIER;
40-
use sqlparser::ast::StorageSerializationPolicy;
4145
use crate::ast::helpers::stmt_create_database::CreateDatabaseBuilder;
46+
use sqlparser::ast::StorageSerializationPolicy;
4247

4348
/// A [`Dialect`] for [Snowflake](https://www.snowflake.com/)
4449
#[derive(Debug, Default)]
@@ -613,11 +618,13 @@ pub fn parse_create_database(
613618
}
614619
Keyword::DATA_RETENTION_TIME_IN_DAYS => {
615620
parser.expect_token(&Token::Eq)?;
616-
builder = builder.data_retention_time_in_days(Some(parser.parse_literal_uint()?));
621+
builder =
622+
builder.data_retention_time_in_days(Some(parser.parse_literal_uint()?));
617623
}
618624
Keyword::MAX_DATA_EXTENSION_TIME_IN_DAYS => {
619625
parser.expect_token(&Token::Eq)?;
620-
builder = builder.max_data_extension_time_in_days(Some(parser.parse_literal_uint()?));
626+
builder =
627+
builder.max_data_extension_time_in_days(Some(parser.parse_literal_uint()?));
621628
}
622629
Keyword::EXTERNAL_VOLUME => {
623630
parser.expect_token(&Token::Eq)?;
@@ -629,7 +636,8 @@ pub fn parse_create_database(
629636
}
630637
Keyword::REPLACE_INVALID_CHARACTERS => {
631638
parser.expect_token(&Token::Eq)?;
632-
builder = builder.replace_invalid_characters(Some(parser.parse_boolean_string()?));
639+
builder =
640+
builder.replace_invalid_characters(Some(parser.parse_boolean_string()?));
633641
}
634642
Keyword::DEFAULT_DDL_COLLATION => {
635643
parser.expect_token(&Token::Eq)?;
@@ -650,13 +658,15 @@ pub fn parse_create_database(
650658
}
651659
Keyword::CATALOG_SYNC_NAMESPACE_FLATTEN_DELIMITER => {
652660
parser.expect_token(&Token::Eq)?;
653-
builder = builder.catalog_sync_namespace_flatten_delimiter(Some(parser.parse_literal_string()?));
661+
builder = builder.catalog_sync_namespace_flatten_delimiter(Some(
662+
parser.parse_literal_string()?,
663+
));
654664
}
655665
Keyword::CATALOG_SYNC_NAMESPACE_MODE => {
656666
parser.expect_token(&Token::Eq)?;
657667
let mode =
658668
match parser.parse_one_of_keywords(&[Keyword::NEST, Keyword::FLATTEN]) {
659-
Some(Keyword::NEST) => CatalogSyncNamespaceMode::Nest,
669+
Some(Keyword::NEST) => CatalogSyncNamespaceMode::Nest,
660670
Some(Keyword::FLATTEN) => CatalogSyncNamespaceMode::Flatten,
661671
_ => {
662672
return parser.expected("NEST or FLATTEN", next_token);
@@ -693,7 +703,6 @@ pub fn parse_create_database(
693703
Ok(builder.build())
694704
}
695705

696-
697706
pub fn parse_storage_serialization_policy(
698707
parser: &mut Parser,
699708
) -> Result<StorageSerializationPolicy, ParserError> {
@@ -1139,12 +1148,15 @@ fn parse_session_options(
11391148
}
11401149
}
11411150
}
1142-
if options
1143-
.is_empty() { {
1151+
if options.is_empty() {
1152+
{
11441153
Err(ParserError::ParserError(
11451154
"expected at least one option".to_string(),
11461155
))
1147-
} } else { Ok(options) }
1156+
}
1157+
} else {
1158+
Ok(options)
1159+
}
11481160
}
11491161

11501162
/// Parses options provided within parentheses like:

src/parser/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8793,7 +8793,13 @@ impl<'a> Parser<'a> {
87938793

87948794
/// Parse a literal string
87958795
pub fn parse_literal_string(&mut self) -> Result<String, ParserError> {
8796-
let tokens = self.tokens.clone().iter().map(|t| t.to_string()).collect::<Vec<_>>().join(" ");
8796+
let tokens = self
8797+
.tokens
8798+
.clone()
8799+
.iter()
8800+
.map(|t| t.to_string())
8801+
.collect::<Vec<_>>()
8802+
.join(" ");
87978803
let next_token = self.next_token();
87988804
match next_token.token {
87998805
Token::Word(Word {
@@ -8807,7 +8813,7 @@ impl<'a> Parser<'a> {
88078813
Ok(s)
88088814
}
88098815
Token::UnicodeStringLiteral(s) => Ok(s),
8810-
_ => self.expected(&format!("literal string {tokens}"), next_token)
8816+
_ => self.expected(&format!("literal string {tokens}"), next_token),
88118817
}
88128818
}
88138819

tests/sqlparser_snowflake.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,7 +3539,7 @@ fn test_create_database_with_all_options() {
35393539
STORAGE_SERIALIZATION_POLICY = COMPATIBLE COMMENT = 'This is my database' \
35403540
CATALOG_SYNC = 'sync_integration' CATALOG_SYNC_NAMESPACE_FLATTEN_DELIMITER = '/' \
35413541
WITH TAG (env='prod', team='data') \
3542-
WITH CONTACT (owner = admin, dpo = compliance)"
3542+
WITH CONTACT (owner = admin, dpo = compliance)",
35433543
);
35443544
}
35453545

@@ -3549,17 +3549,11 @@ fn test_create_database_errors() {
35493549
.parse_sql_statements("CREATE DATABASE")
35503550
.unwrap_err()
35513551
.to_string();
3552-
assert!(
3553-
err.contains("Expected"),
3554-
"Unexpected error: {err}"
3555-
);
3552+
assert!(err.contains("Expected"), "Unexpected error: {err}");
35563553

35573554
let err = snowflake()
35583555
.parse_sql_statements("CREATE DATABASE my_db CLONE")
35593556
.unwrap_err()
35603557
.to_string();
3561-
assert!(
3562-
err.contains("Expected"),
3563-
"Unexpected error: {err}"
3564-
);
3565-
}
3558+
assert!(err.contains("Expected"), "Unexpected error: {err}");
3559+
}

0 commit comments

Comments
 (0)