Skip to content

Commit 6ad7811

Browse files
author
aleksei.p
committed
Snowflake: Support dollar quoted comment of table, view and field
1 parent ed41654 commit 6ad7811

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

src/dialect/snowflake.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ impl Dialect for SnowflakeDialect {
245245
.map(|p| Some(ColumnOption::Policy(ColumnPolicy::ProjectionPolicy(p)))))
246246
} else if parser.parse_keywords(&[Keyword::TAG]) {
247247
Ok(parse_column_tags(parser, with).map(|p| Some(ColumnOption::Tags(p))))
248+
} else if parser.parse_keywords(&[Keyword::COMMENT]) {
249+
let next_token = parser.next_token();
250+
match next_token.token {
251+
Token::DollarQuotedString(value, ..) => {
252+
Ok(Ok(Some(ColumnOption::Comment(value.value))))
253+
}
254+
_ => Err(ParserError::ParserError("not found match".to_string())),
255+
}
248256
} else {
249257
Err(ParserError::ParserError("not found match".to_string()))
250258
}
@@ -422,7 +430,7 @@ pub fn parse_create_table(
422430
Keyword::COMMENT => {
423431
// Rewind the COMMENT keyword
424432
parser.prev_token();
425-
builder = builder.comment(parser.parse_optional_inline_comment()?);
433+
builder = builder.comment(parser.parse_optional_inline_comment(true)?);
426434
}
427435
Keyword::AS => {
428436
let query = parser.parse_query()?;
@@ -646,6 +654,7 @@ pub fn parse_create_stage(
646654
parser.expect_token(&Token::Eq)?;
647655
comment = Some(match parser.next_token().token {
648656
Token::SingleQuotedString(word) => Ok(word),
657+
Token::DollarQuotedString(word) => Ok(word.value),
649658
_ => parser.expected("a comment statement", parser.peek_token()),
650659
}?)
651660
}

src/parser/mod.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -5323,6 +5323,7 @@ impl<'a> Parser<'a> {
53235323
let next_token = self.next_token();
53245324
match next_token.token {
53255325
Token::SingleQuotedString(str) => Some(str),
5326+
Token::DollarQuotedString(str) => Some(str.value),
53265327
_ => self.expected("string literal", next_token)?,
53275328
}
53285329
} else {
@@ -5764,7 +5765,7 @@ impl<'a> Parser<'a> {
57645765
None
57655766
};
57665767

5767-
let comment = self.parse_optional_inline_comment()?;
5768+
let comment = self.parse_optional_inline_comment(false)?;
57685769

57695770
let with_dcproperties =
57705771
match self.parse_options_with_keywords(&[Keyword::WITH, Keyword::DCPROPERTIES])? {
@@ -6821,7 +6822,8 @@ impl<'a> Parser<'a> {
68216822
if !dialect_of!(self is HiveDialect) && self.parse_keyword(Keyword::COMMENT) {
68226823
// rewind the COMMENT keyword
68236824
self.prev_token();
6824-
comment = self.parse_optional_inline_comment()?
6825+
let support_dollar_quoted_comment = dialect_of!(self is SnowflakeDialect);
6826+
comment = self.parse_optional_inline_comment(support_dollar_quoted_comment)?
68256827
};
68266828

68276829
// Parse optional `AS ( query )`
@@ -6922,18 +6924,23 @@ impl<'a> Parser<'a> {
69226924
})
69236925
}
69246926

6925-
pub fn parse_optional_inline_comment(&mut self) -> Result<Option<CommentDef>, ParserError> {
6927+
pub fn parse_optional_inline_comment(
6928+
&mut self,
6929+
support_dollar_quoted_comment: bool,
6930+
) -> Result<Option<CommentDef>, ParserError> {
69266931
let comment = if self.parse_keyword(Keyword::COMMENT) {
69276932
let has_eq = self.consume_token(&Token::Eq);
69286933
let next_token = self.next_token();
6929-
match next_token.token {
6930-
Token::SingleQuotedString(str) => Some(if has_eq {
6931-
CommentDef::WithEq(str)
6932-
} else {
6933-
CommentDef::WithoutEq(str)
6934-
}),
6934+
let comment = match next_token.token {
6935+
Token::SingleQuotedString(str) => str,
6936+
Token::DollarQuotedString(str) if support_dollar_quoted_comment => str.value,
69356937
_ => self.expected("comment", next_token)?,
6936-
}
6938+
};
6939+
Some(if has_eq {
6940+
CommentDef::WithEq(comment)
6941+
} else {
6942+
CommentDef::WithoutEq(comment)
6943+
})
69376944
} else {
69386945
None
69396946
};

tests/sqlparser_snowflake.rs

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

979+
#[test]
980+
fn parse_sf_create_table_or_view_with_dollars_comment() {
981+
assert!(snowflake()
982+
.parse_sql_statements(
983+
r#"CREATE OR REPLACE TEMPORARY VIEW foo.bar.baz (
984+
"COL_1" COMMENT $$comment 1$$
985+
) COMMENT = $$view comment$$ AS (
986+
SELECT 1
987+
)"#
988+
)
989+
.is_ok());
990+
991+
assert!(snowflake()
992+
.parse_sql_statements(
993+
r#"CREATE TABLE my_table (
994+
a STRING COMMENT $$comment 1$$
995+
) COMMENT = $$table comment$$"#
996+
)
997+
.is_ok());
998+
}
999+
9791000
#[test]
9801001
fn test_sf_derived_table_in_parenthesis() {
9811002
// Nesting a subquery in an extra set of parentheses is non-standard,

0 commit comments

Comments
 (0)