Skip to content

Commit d753af7

Browse files
Aleksei Piianinayman-sigma
Aleksei Piianin
authored andcommitted
Snowflake: Support dollar quoted comments (apache#1755)
1 parent cdd8b3b commit d753af7

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

src/dialect/snowflake.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,7 @@ pub fn parse_create_stage(
644644
// [ comment ]
645645
if parser.parse_keyword(Keyword::COMMENT) {
646646
parser.expect_token(&Token::Eq)?;
647-
comment = Some(match parser.next_token().token {
648-
Token::SingleQuotedString(word) => Ok(word),
649-
_ => parser.expected("a comment statement", parser.peek_token()),
650-
}?)
647+
comment = Some(parser.parse_comment_value()?);
651648
}
652649

653650
Ok(Statement::CreateStage {

src/parser/mod.rs

+18-19
Original file line numberDiff line numberDiff line change
@@ -5461,11 +5461,7 @@ impl<'a> Parser<'a> {
54615461
&& self.parse_keyword(Keyword::COMMENT)
54625462
{
54635463
self.expect_token(&Token::Eq)?;
5464-
let next_token = self.next_token();
5465-
match next_token.token {
5466-
Token::SingleQuotedString(str) => Some(str),
5467-
_ => self.expected("string literal", next_token)?,
5468-
}
5464+
Some(self.parse_comment_value()?)
54695465
} else {
54705466
None
54715467
};
@@ -7069,21 +7065,28 @@ impl<'a> Parser<'a> {
70697065
pub fn parse_optional_inline_comment(&mut self) -> Result<Option<CommentDef>, ParserError> {
70707066
let comment = if self.parse_keyword(Keyword::COMMENT) {
70717067
let has_eq = self.consume_token(&Token::Eq);
7072-
let next_token = self.next_token();
7073-
match next_token.token {
7074-
Token::SingleQuotedString(str) => Some(if has_eq {
7075-
CommentDef::WithEq(str)
7076-
} else {
7077-
CommentDef::WithoutEq(str)
7078-
}),
7079-
_ => self.expected("comment", next_token)?,
7080-
}
7068+
let comment = self.parse_comment_value()?;
7069+
Some(if has_eq {
7070+
CommentDef::WithEq(comment)
7071+
} else {
7072+
CommentDef::WithoutEq(comment)
7073+
})
70817074
} else {
70827075
None
70837076
};
70847077
Ok(comment)
70857078
}
70867079

7080+
pub fn parse_comment_value(&mut self) -> Result<String, ParserError> {
7081+
let next_token = self.next_token();
7082+
let value = match next_token.token {
7083+
Token::SingleQuotedString(str) => str,
7084+
Token::DollarQuotedString(str) => str.value,
7085+
_ => self.expected("string literal", next_token)?,
7086+
};
7087+
Ok(value)
7088+
}
7089+
70877090
pub fn parse_optional_procedure_parameters(
70887091
&mut self,
70897092
) -> Result<Option<Vec<ProcedureParam>>, ParserError> {
@@ -7219,11 +7222,7 @@ impl<'a> Parser<'a> {
72197222
} else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
72207223
Ok(Some(ColumnOption::NotNull))
72217224
} else if self.parse_keywords(&[Keyword::COMMENT]) {
7222-
let next_token = self.next_token();
7223-
match next_token.token {
7224-
Token::SingleQuotedString(value, ..) => Ok(Some(ColumnOption::Comment(value))),
7225-
_ => self.expected("string", next_token),
7226-
}
7225+
Ok(Some(ColumnOption::Comment(self.parse_comment_value()?)))
72277226
} else if self.parse_keyword(Keyword::NULL) {
72287227
Ok(Some(ColumnOption::Null))
72297228
} else if self.parse_keyword(Keyword::DEFAULT) {

tests/sqlparser_snowflake.rs

+15
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,21 @@ fn parse_sf_create_or_replace_with_comment_for_snowflake() {
976976
}
977977
}
978978

979+
#[test]
980+
fn parse_sf_create_table_or_view_with_dollar_quoted_comment() {
981+
// Snowflake transforms dollar quoted comments into a common comment in DDL representation of creation
982+
snowflake()
983+
.one_statement_parses_to(
984+
r#"CREATE OR REPLACE TEMPORARY VIEW foo.bar.baz ("COL_1" COMMENT $$comment 1$$) COMMENT = $$view comment$$ AS (SELECT 1)"#,
985+
r#"CREATE OR REPLACE TEMPORARY VIEW foo.bar.baz ("COL_1" COMMENT 'comment 1') COMMENT = 'view comment' AS (SELECT 1)"#
986+
);
987+
988+
snowflake().one_statement_parses_to(
989+
r#"CREATE TABLE my_table (a STRING COMMENT $$comment 1$$) COMMENT = $$table comment$$"#,
990+
r#"CREATE TABLE my_table (a STRING COMMENT 'comment 1') COMMENT = 'table comment'"#,
991+
);
992+
}
993+
979994
#[test]
980995
fn test_sf_derived_table_in_parenthesis() {
981996
// Nesting a subquery in an extra set of parentheses is non-standard,

0 commit comments

Comments
 (0)