Skip to content

Snowflake: Support dollar quoted comments #1755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 10 additions & 1 deletion src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ impl Dialect for SnowflakeDialect {
.map(|p| Some(ColumnOption::Policy(ColumnPolicy::ProjectionPolicy(p)))))
} else if parser.parse_keywords(&[Keyword::TAG]) {
Ok(parse_column_tags(parser, with).map(|p| Some(ColumnOption::Tags(p))))
} else if parser.parse_keywords(&[Keyword::COMMENT]) {
let next_token = parser.next_token();
match next_token.token {
Token::DollarQuotedString(value, ..) => {
Ok(Ok(Some(ColumnOption::Comment(value.value))))
}
_ => Err(ParserError::ParserError("not found match".to_string())),
}
} else {
Err(ParserError::ParserError("not found match".to_string()))
}
Expand Down Expand Up @@ -422,7 +430,7 @@ pub fn parse_create_table(
Keyword::COMMENT => {
// Rewind the COMMENT keyword
parser.prev_token();
builder = builder.comment(parser.parse_optional_inline_comment()?);
builder = builder.comment(parser.parse_optional_inline_comment(true)?);
}
Keyword::AS => {
let query = parser.parse_query()?;
Expand Down Expand Up @@ -646,6 +654,7 @@ pub fn parse_create_stage(
parser.expect_token(&Token::Eq)?;
comment = Some(match parser.next_token().token {
Token::SingleQuotedString(word) => Ok(word),
Token::DollarQuotedString(word) => Ok(word.value),
_ => parser.expected("a comment statement", parser.peek_token()),
}?)
}
Expand Down
27 changes: 17 additions & 10 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5323,6 +5323,7 @@ impl<'a> Parser<'a> {
let next_token = self.next_token();
match next_token.token {
Token::SingleQuotedString(str) => Some(str),
Token::DollarQuotedString(str) => Some(str.value),
_ => self.expected("string literal", next_token)?,
}
} else {
Expand Down Expand Up @@ -5764,7 +5765,7 @@ impl<'a> Parser<'a> {
None
};

let comment = self.parse_optional_inline_comment()?;
let comment = self.parse_optional_inline_comment(false)?;

let with_dcproperties =
match self.parse_options_with_keywords(&[Keyword::WITH, Keyword::DCPROPERTIES])? {
Expand Down Expand Up @@ -6821,7 +6822,8 @@ impl<'a> Parser<'a> {
if !dialect_of!(self is HiveDialect) && self.parse_keyword(Keyword::COMMENT) {
// rewind the COMMENT keyword
self.prev_token();
comment = self.parse_optional_inline_comment()?
let support_dollar_quoted_comment = dialect_of!(self is SnowflakeDialect);
comment = self.parse_optional_inline_comment(support_dollar_quoted_comment)?
};

// Parse optional `AS ( query )`
Expand Down Expand Up @@ -6922,18 +6924,23 @@ impl<'a> Parser<'a> {
})
}

pub fn parse_optional_inline_comment(&mut self) -> Result<Option<CommentDef>, ParserError> {
pub fn parse_optional_inline_comment(
&mut self,
support_dollar_quoted_comment: bool,
) -> Result<Option<CommentDef>, ParserError> {
let comment = if self.parse_keyword(Keyword::COMMENT) {
let has_eq = self.consume_token(&Token::Eq);
let next_token = self.next_token();
match next_token.token {
Token::SingleQuotedString(str) => Some(if has_eq {
CommentDef::WithEq(str)
} else {
CommentDef::WithoutEq(str)
}),
let comment = match next_token.token {
Token::SingleQuotedString(str) => str,
Token::DollarQuotedString(str) if support_dollar_quoted_comment => str.value,
_ => self.expected("comment", next_token)?,
}
};
Some(if has_eq {
CommentDef::WithEq(comment)
} else {
CommentDef::WithoutEq(comment)
})
} else {
None
};
Expand Down
21 changes: 21 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,27 @@ fn parse_sf_create_or_replace_with_comment_for_snowflake() {
}
}

#[test]
fn parse_sf_create_table_or_view_with_dollar_quoted_comment() {
assert!(snowflake()
.parse_sql_statements(
r#"CREATE OR REPLACE TEMPORARY VIEW foo.bar.baz (
"COL_1" COMMENT $$comment 1$$
) COMMENT = $$view comment$$ AS (
SELECT 1
)"#
)
.is_ok());

assert!(snowflake()
.parse_sql_statements(
r#"CREATE TABLE my_table (
a STRING COMMENT $$comment 1$$
) COMMENT = $$table comment$$"#
)
.is_ok());
}

#[test]
fn test_sf_derived_table_in_parenthesis() {
// Nesting a subquery in an extra set of parentheses is non-standard,
Expand Down