Skip to content

Commit 32a126b

Browse files
authored
Fix always uses CommentDef::WithoutEq while parsing the inline comment (#1453)
1 parent 1e0460a commit 32a126b

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed

src/dialect/snowflake.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ use crate::ast::helpers::stmt_data_loading::{
2222
DataLoadingOption, DataLoadingOptionType, DataLoadingOptions, StageLoadSelectItem,
2323
StageParamsObject,
2424
};
25-
use crate::ast::{
26-
CommentDef, Ident, ObjectName, RowAccessPolicy, Statement, Tag, WrappedCollection,
27-
};
25+
use crate::ast::{Ident, ObjectName, RowAccessPolicy, Statement, Tag, WrappedCollection};
2826
use crate::dialect::{Dialect, Precedence};
2927
use crate::keywords::Keyword;
3028
use crate::parser::{Parser, ParserError};
@@ -210,13 +208,9 @@ pub fn parse_create_table(
210208
builder = builder.copy_grants(true);
211209
}
212210
Keyword::COMMENT => {
213-
parser.expect_token(&Token::Eq)?;
214-
let next_token = parser.next_token();
215-
let comment = match next_token.token {
216-
Token::SingleQuotedString(str) => Some(CommentDef::WithEq(str)),
217-
_ => parser.expected("comment", next_token)?,
218-
};
219-
builder = builder.comment(comment);
211+
// Rewind the COMMENT keyword
212+
parser.prev_token();
213+
builder = builder.comment(parser.parse_optional_inline_comment()?);
220214
}
221215
Keyword::AS => {
222216
let query = parser.parse_boxed_query()?;

src/parser/mod.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -5871,12 +5871,9 @@ impl<'a> Parser<'a> {
58715871

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

58825879
// Parse optional `AS ( query )`
@@ -5957,6 +5954,24 @@ impl<'a> Parser<'a> {
59575954
})
59585955
}
59595956

5957+
pub fn parse_optional_inline_comment(&mut self) -> Result<Option<CommentDef>, ParserError> {
5958+
let comment = if self.parse_keyword(Keyword::COMMENT) {
5959+
let has_eq = self.consume_token(&Token::Eq);
5960+
let next_token = self.next_token();
5961+
match next_token.token {
5962+
Token::SingleQuotedString(str) => Some(if has_eq {
5963+
CommentDef::WithEq(str)
5964+
} else {
5965+
CommentDef::WithoutEq(str)
5966+
}),
5967+
_ => self.expected("comment", next_token)?,
5968+
}
5969+
} else {
5970+
None
5971+
};
5972+
Ok(comment)
5973+
}
5974+
59605975
pub fn parse_optional_procedure_parameters(
59615976
&mut self,
59625977
) -> Result<Option<Vec<ProcedureParam>>, ParserError> {

tests/sqlparser_common.rs

+32
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ mod test_utils;
4747

4848
#[cfg(test)]
4949
use pretty_assertions::assert_eq;
50+
use sqlparser::ast::ColumnOption::Comment;
5051
use sqlparser::ast::Expr::{Identifier, UnaryOp};
5152
use sqlparser::test_utils::all_dialects_except;
5253

@@ -9841,6 +9842,37 @@ fn test_comment_hash_syntax() {
98419842
dialects.verified_only_select_with_canonical(sql, canonical);
98429843
}
98439844

9845+
#[test]
9846+
fn test_parse_inline_comment() {
9847+
let sql =
9848+
"CREATE TABLE t0 (id INT COMMENT 'comment without equal') COMMENT = 'comment with equal'";
9849+
// Hive dialect doesn't support `=` in table comment, please refer:
9850+
// [Hive](https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable)
9851+
match all_dialects_except(|d| d.is::<HiveDialect>()).verified_stmt(sql) {
9852+
Statement::CreateTable(CreateTable {
9853+
columns, comment, ..
9854+
}) => {
9855+
assert_eq!(
9856+
columns,
9857+
vec![ColumnDef {
9858+
name: Ident::new("id".to_string()),
9859+
data_type: DataType::Int(None),
9860+
collation: None,
9861+
options: vec![ColumnOptionDef {
9862+
name: None,
9863+
option: Comment("comment without equal".to_string()),
9864+
}]
9865+
}]
9866+
);
9867+
assert_eq!(
9868+
comment.unwrap(),
9869+
CommentDef::WithEq("comment with equal".to_string())
9870+
);
9871+
}
9872+
_ => unreachable!(),
9873+
}
9874+
}
9875+
98449876
#[test]
98459877
fn test_buffer_reuse() {
98469878
let d = GenericDialect {};

tests/sqlparser_mysql.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -713,11 +713,11 @@ fn parse_create_table_primary_and_unique_key_characteristic_test() {
713713

714714
#[test]
715715
fn parse_create_table_comment() {
716-
let canonical = "CREATE TABLE foo (bar INT) COMMENT 'baz'";
716+
let without_equal = "CREATE TABLE foo (bar INT) COMMENT 'baz'";
717717
let with_equal = "CREATE TABLE foo (bar INT) COMMENT = 'baz'";
718718

719-
for sql in [canonical, with_equal] {
720-
match mysql().one_statement_parses_to(sql, canonical) {
719+
for sql in [without_equal, with_equal] {
720+
match mysql().verified_stmt(sql) {
721721
Statement::CreateTable(CreateTable { name, comment, .. }) => {
722722
assert_eq!(name.to_string(), "foo");
723723
assert_eq!(comment.expect("Should exist").to_string(), "baz");

0 commit comments

Comments
 (0)