Skip to content
Merged
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
41 changes: 41 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4511,6 +4511,24 @@ pub enum Statement {
comment: Option<String>,
},
/// ```sql
/// CREATE FILE FORMAT
/// ```
/// See <https://docs.snowflake.com/en/sql-reference/sql/create-file-format>
CreateFileFormat {
/// `OR REPLACE` flag.
or_replace: bool,
/// Whether the file format is temporary or volatile.
temporary: bool,
/// `IF NOT EXISTS` flag.
if_not_exists: bool,
/// File format name.
name: ObjectName,
/// Format type and format-specific options.
options: KeyValueOptions,
/// Optional comment.
comment: Option<String>,
},
/// ```sql
/// ASSERT <condition> [AS <message>]
/// ```
Assert {
Expand Down Expand Up @@ -6228,6 +6246,29 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::CreateFileFormat {
or_replace,
temporary,
if_not_exists,
name,
options,
comment,
} => {
write!(
f,
"CREATE {or_replace}{temporary}FILE FORMAT {if_not_exists}{name}",
or_replace = if *or_replace { "OR REPLACE " } else { "" },
temporary = if *temporary { "TEMPORARY " } else { "" },
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
)?;
if !options.options.is_empty() {
write!(f, " {options}")?;
}
if let Some(comment) = comment {
write!(f, " COMMENT='{comment}'")?;
}
Ok(())
}
Statement::CopyIntoSnowflake {
kind,
into,
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ impl Spanned for Statement {
Statement::CreateProcedure { .. } => Span::empty(),
Statement::CreateMacro { .. } => Span::empty(),
Statement::CreateStage { .. } => Span::empty(),
Statement::CreateFileFormat { .. } => Span::empty(),
Statement::Assert { .. } => Span::empty(),
Statement::Grant { .. } => Span::empty(),
Statement::Deny { .. } => Span::empty(),
Expand Down
31 changes: 31 additions & 0 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ impl Dialect for SnowflakeDialect {
if parser.parse_keyword(Keyword::STAGE) {
// OK - this is CREATE STAGE statement
return Some(parse_create_stage(or_replace, temporary, parser));
} else if parser.parse_keywords(&[Keyword::FILE, Keyword::FORMAT]) {
return Some(parse_create_file_format(
or_replace,
temporary || volatile,
parser,
));
} else if parser.parse_keyword(Keyword::TABLE) {
return Some(
parse_create_table(
Expand Down Expand Up @@ -1426,6 +1432,31 @@ pub fn parse_create_stage(
})
}

pub fn parse_create_file_format(
or_replace: bool,
temporary: bool,
parser: &mut Parser,
) -> Result<Statement, ParserError> {
let if_not_exists = parser.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
let name = parser.parse_object_name(false)?;
let options = parser.parse_key_value_options(false, &[Keyword::COMMENT])?;
let comment = if parser.parse_keyword(Keyword::COMMENT) {
parser.expect_token(&Token::Eq)?;
Some(parser.parse_comment_value()?)
} else {
None
};

Ok(Statement::CreateFileFormat {
or_replace,
temporary,
if_not_exists,
name,
options,
comment,
})
}

pub fn parse_stage_name_identifier(parser: &mut Parser) -> Result<Ident, ParserError> {
let mut ident = String::new();
while let Some(next_token) = parser.next_token_no_skip() {
Expand Down
58 changes: 58 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2149,6 +2149,64 @@ fn test_create_stage() {
);
}

#[test]
fn test_create_file_format() {
let sql = "CREATE FILE FORMAT analytics.formats.parquet TYPE=PARQUET";
match snowflake().verified_stmt(sql) {
Statement::CreateFileFormat {
or_replace,
temporary,
if_not_exists,
name,
options,
comment,
} => {
assert!(!or_replace);
assert!(!temporary);
assert!(!if_not_exists);
assert_eq!(name.to_string(), "analytics.formats.parquet");
assert_eq!(options.options.len(), 1);
assert!(comment.is_none());
}
_ => unreachable!(),
}
assert_eq!(snowflake().verified_stmt(sql).to_string(), sql);

let sql = "CREATE FILE FORMAT IF NOT EXISTS existing_format TYPE=JSON";
match snowflake().verified_stmt(sql) {
Statement::CreateFileFormat { if_not_exists, .. } => assert!(if_not_exists),
_ => unreachable!(),
}

let sql = concat!(
"CREATE OR REPLACE TEMPORARY FILE FORMAT csv_format ",
"TYPE=CSV FIELD_DELIMITER='|' NULL_IF=('NULL', 'null') ",
"COMMENT='pipe-delimited input'"
);
match snowflake().verified_stmt(sql) {
Statement::CreateFileFormat {
or_replace,
temporary,
if_not_exists,
name,
options,
comment,
} => {
assert!(or_replace);
assert!(temporary);
assert!(!if_not_exists);
assert_eq!(name.to_string(), "csv_format");
assert_eq!(options.options.len(), 3);
assert_eq!(comment.as_deref(), Some("pipe-delimited input"));
}
_ => unreachable!(),
}
assert_eq!(snowflake().verified_stmt(sql).to_string(), sql);

let sql = "CREATE VOLATILE FILE FORMAT json_format TYPE=JSON";
snowflake().one_statement_parses_to(sql, "CREATE TEMPORARY FILE FORMAT json_format TYPE=JSON");
}

#[test]
fn test_create_stage_with_stage_params() {
let sql = concat!(
Expand Down
Loading