Skip to content

Fix always uses CommentDef::WithoutEq while parsing the inline comment #1453

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 1 commit into from
Oct 4, 2024
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
14 changes: 4 additions & 10 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ use crate::ast::helpers::stmt_data_loading::{
DataLoadingOption, DataLoadingOptionType, DataLoadingOptions, StageLoadSelectItem,
StageParamsObject,
};
use crate::ast::{
CommentDef, Ident, ObjectName, RowAccessPolicy, Statement, Tag, WrappedCollection,
};
use crate::ast::{Ident, ObjectName, RowAccessPolicy, Statement, Tag, WrappedCollection};
use crate::dialect::{Dialect, Precedence};
use crate::keywords::Keyword;
use crate::parser::{Parser, ParserError};
Expand Down Expand Up @@ -210,13 +208,9 @@ pub fn parse_create_table(
builder = builder.copy_grants(true);
}
Keyword::COMMENT => {
parser.expect_token(&Token::Eq)?;
let next_token = parser.next_token();
let comment = match next_token.token {
Token::SingleQuotedString(str) => Some(CommentDef::WithEq(str)),
_ => parser.expected("comment", next_token)?,
};
builder = builder.comment(comment);
// Rewind the COMMENT keyword
parser.prev_token();
builder = builder.comment(parser.parse_optional_inline_comment()?);
}
Keyword::AS => {
let query = parser.parse_boxed_query()?;
Expand Down
27 changes: 21 additions & 6 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5871,12 +5871,9 @@ impl<'a> Parser<'a> {

// Excludes Hive dialect here since it has been handled after table column definitions.
if !dialect_of!(self is HiveDialect) && self.parse_keyword(Keyword::COMMENT) {
let _ = self.consume_token(&Token::Eq);
let next_token = self.next_token();
comment = match next_token.token {
Token::SingleQuotedString(str) => Some(CommentDef::WithoutEq(str)),
_ => self.expected("comment", next_token)?,
}
// rewind the COMMENT keyword
self.prev_token();
comment = self.parse_optional_inline_comment()?
};

// Parse optional `AS ( query )`
Expand Down Expand Up @@ -5957,6 +5954,24 @@ impl<'a> Parser<'a> {
})
}

pub fn parse_optional_inline_comment(&mut self) -> 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)
}),
_ => self.expected("comment", next_token)?,
}
} else {
None
};
Ok(comment)
}

pub fn parse_optional_procedure_parameters(
&mut self,
) -> Result<Option<Vec<ProcedureParam>>, ParserError> {
Expand Down
32 changes: 32 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod test_utils;

#[cfg(test)]
use pretty_assertions::assert_eq;
use sqlparser::ast::ColumnOption::Comment;
use sqlparser::ast::Expr::{Identifier, UnaryOp};
use sqlparser::test_utils::all_dialects_except;

Expand Down Expand Up @@ -9841,6 +9842,37 @@ fn test_comment_hash_syntax() {
dialects.verified_only_select_with_canonical(sql, canonical);
}

#[test]
fn test_parse_inline_comment() {
let sql =
"CREATE TABLE t0 (id INT COMMENT 'comment without equal') COMMENT = 'comment with equal'";
// Hive dialect doesn't support `=` in table comment, please refer:
// [Hive](https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable)
match all_dialects_except(|d| d.is::<HiveDialect>()).verified_stmt(sql) {
Statement::CreateTable(CreateTable {
columns, comment, ..
}) => {
assert_eq!(
columns,
vec![ColumnDef {
name: Ident::new("id".to_string()),
data_type: DataType::Int(None),
collation: None,
options: vec![ColumnOptionDef {
name: None,
option: Comment("comment without equal".to_string()),
}]
}]
);
assert_eq!(
comment.unwrap(),
CommentDef::WithEq("comment with equal".to_string())
);
}
_ => unreachable!(),
}
}

#[test]
fn test_buffer_reuse() {
let d = GenericDialect {};
Expand Down
6 changes: 3 additions & 3 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,11 @@ fn parse_create_table_primary_and_unique_key_characteristic_test() {

#[test]
fn parse_create_table_comment() {
let canonical = "CREATE TABLE foo (bar INT) COMMENT 'baz'";
let without_equal = "CREATE TABLE foo (bar INT) COMMENT 'baz'";
let with_equal = "CREATE TABLE foo (bar INT) COMMENT = 'baz'";

for sql in [canonical, with_equal] {
match mysql().one_statement_parses_to(sql, canonical) {
for sql in [without_equal, with_equal] {
match mysql().verified_stmt(sql) {
Statement::CreateTable(CreateTable { name, comment, .. }) => {
assert_eq!(name.to_string(), "foo");
assert_eq!(comment.expect("Should exist").to_string(), "baz");
Expand Down
Loading