Skip to content

Commit 4ff3aeb

Browse files
support IF EXISTS in COMMENT statements (apache#831)
* support IF EXISTS in COMMENT statements Signed-off-by: Pawel Leszczynski <[email protected]> * Update src/ast/mod.rs Co-authored-by: Andrew Lamb <[email protected]> --------- Signed-off-by: Pawel Leszczynski <[email protected]> Co-authored-by: Andrew Lamb <[email protected]>
1 parent 5481918 commit 4ff3aeb

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/ast/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,9 @@ pub enum Statement {
14871487
object_type: CommentObject,
14881488
object_name: ObjectName,
14891489
comment: Option<String>,
1490+
/// An optional `IF EXISTS` clause. (Non-standard.)
1491+
/// See <https://docs.snowflake.com/en/sql-reference/sql/comment>
1492+
if_exists: bool,
14901493
},
14911494
/// `COMMIT [ TRANSACTION | WORK ] [ AND [ NO ] CHAIN ]`
14921495
Commit { chain: bool },
@@ -2637,8 +2640,13 @@ impl fmt::Display for Statement {
26372640
object_type,
26382641
object_name,
26392642
comment,
2643+
if_exists,
26402644
} => {
2641-
write!(f, "COMMENT ON {object_type} {object_name} IS ")?;
2645+
write!(f, "COMMENT ")?;
2646+
if *if_exists {
2647+
write!(f, "IF EXISTS ")?
2648+
};
2649+
write!(f, "ON {object_type} {object_name} IS ")?;
26422650
if let Some(c) = comment {
26432651
write!(f, "'{c}'")
26442652
} else {

src/dialect/postgresql.rs

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ impl Dialect for PostgreSqlDialect {
4949
}
5050

5151
pub fn parse_comment(parser: &mut Parser) -> Result<Statement, ParserError> {
52+
let if_exists = parser.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
53+
5254
parser.expect_keyword(Keyword::ON)?;
5355
let token = parser.next_token();
5456

@@ -74,5 +76,6 @@ pub fn parse_comment(parser: &mut Parser) -> Result<Statement, ParserError> {
7476
object_type,
7577
object_name,
7678
comment,
79+
if_exists,
7780
})
7881
}

tests/sqlparser_postgres.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1790,10 +1790,12 @@ fn parse_comments() {
17901790
object_type,
17911791
object_name,
17921792
comment: Some(comment),
1793+
if_exists,
17931794
} => {
17941795
assert_eq!("comment", comment);
17951796
assert_eq!("tab.name", object_name.to_string());
17961797
assert_eq!(CommentObject::Column, object_type);
1798+
assert!(!if_exists);
17971799
}
17981800
_ => unreachable!(),
17991801
}
@@ -1803,22 +1805,26 @@ fn parse_comments() {
18031805
object_type,
18041806
object_name,
18051807
comment: Some(comment),
1808+
if_exists,
18061809
} => {
18071810
assert_eq!("comment", comment);
18081811
assert_eq!("public.tab", object_name.to_string());
18091812
assert_eq!(CommentObject::Table, object_type);
1813+
assert!(!if_exists);
18101814
}
18111815
_ => unreachable!(),
18121816
}
18131817

1814-
match pg().verified_stmt("COMMENT ON TABLE public.tab IS NULL") {
1818+
match pg().verified_stmt("COMMENT IF EXISTS ON TABLE public.tab IS NULL") {
18151819
Statement::Comment {
18161820
object_type,
18171821
object_name,
18181822
comment: None,
1823+
if_exists,
18191824
} => {
18201825
assert_eq!("public.tab", object_name.to_string());
18211826
assert_eq!(CommentObject::Table, object_type);
1827+
assert!(if_exists);
18221828
}
18231829
_ => unreachable!(),
18241830
}

0 commit comments

Comments
 (0)