mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-19 05:30:19 +00:00
feat: comment and alter column support (#381)
* feat: add support for postgresql comment keyword * feat: add alter column and rename constraint
This commit is contained in:
parent
823635d2fc
commit
9569d1b215
6 changed files with 355 additions and 3 deletions
|
@ -191,6 +191,9 @@ impl<'a> Parser<'a> {
|
|||
self.prev_token();
|
||||
Ok(self.parse_insert()?)
|
||||
}
|
||||
Keyword::COMMENT if dialect_of!(self is PostgreSqlDialect) => {
|
||||
Ok(self.parse_comment()?)
|
||||
}
|
||||
_ => self.expected("an SQL statement", Token::Word(w)),
|
||||
},
|
||||
Token::LParen => {
|
||||
|
@ -1944,7 +1947,12 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
}
|
||||
} else if self.parse_keyword(Keyword::RENAME) {
|
||||
if self.parse_keyword(Keyword::TO) {
|
||||
if dialect_of!(self is PostgreSqlDialect) && self.parse_keyword(Keyword::CONSTRAINT) {
|
||||
let old_name = self.parse_identifier()?;
|
||||
self.expect_keyword(Keyword::TO)?;
|
||||
let new_name = self.parse_identifier()?;
|
||||
AlterTableOperation::RenameConstraint { old_name, new_name }
|
||||
} else if self.parse_keyword(Keyword::TO) {
|
||||
let table_name = self.parse_object_name()?;
|
||||
AlterTableOperation::RenameTable { table_name }
|
||||
} else {
|
||||
|
@ -2014,6 +2022,38 @@ impl<'a> Parser<'a> {
|
|||
data_type,
|
||||
options,
|
||||
}
|
||||
} else if self.parse_keyword(Keyword::ALTER) {
|
||||
let _ = self.parse_keyword(Keyword::COLUMN);
|
||||
let column_name = self.parse_identifier()?;
|
||||
let is_postgresql = dialect_of!(self is PostgreSqlDialect);
|
||||
|
||||
let op = if self.parse_keywords(&[Keyword::SET, Keyword::NOT, Keyword::NULL]) {
|
||||
AlterColumnOperation::SetNotNull {}
|
||||
} else if self.parse_keywords(&[Keyword::DROP, Keyword::NOT, Keyword::NULL]) {
|
||||
AlterColumnOperation::DropNotNull {}
|
||||
} else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT]) {
|
||||
AlterColumnOperation::SetDefault {
|
||||
value: self.parse_expr()?,
|
||||
}
|
||||
} else if self.parse_keywords(&[Keyword::DROP, Keyword::DEFAULT]) {
|
||||
AlterColumnOperation::DropDefault {}
|
||||
} else if self.parse_keywords(&[Keyword::SET, Keyword::DATA, Keyword::TYPE])
|
||||
|| (is_postgresql && self.parse_keyword(Keyword::TYPE))
|
||||
{
|
||||
let data_type = self.parse_data_type()?;
|
||||
let using = if is_postgresql && self.parse_keyword(Keyword::USING) {
|
||||
Some(self.parse_expr()?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
AlterColumnOperation::SetDataType { data_type, using }
|
||||
} else {
|
||||
return self.expected(
|
||||
"SET/DROP NOT NULL, SET DEFAULT, SET DATA TYPE after ALTER COLUMN",
|
||||
self.peek_token(),
|
||||
);
|
||||
};
|
||||
AlterTableOperation::AlterColumn { column_name, op }
|
||||
} else {
|
||||
return self.expected(
|
||||
"ADD, RENAME, PARTITION or DROP after ALTER TABLE",
|
||||
|
@ -3547,6 +3587,35 @@ impl<'a> Parser<'a> {
|
|||
statement,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_comment(&mut self) -> Result<Statement, ParserError> {
|
||||
self.expect_keyword(Keyword::ON)?;
|
||||
let token = self.next_token();
|
||||
|
||||
let (object_type, object_name) = match token {
|
||||
Token::Word(w) if w.keyword == Keyword::COLUMN => {
|
||||
let object_name = self.parse_object_name()?;
|
||||
(CommentObject::Column, object_name)
|
||||
}
|
||||
Token::Word(w) if w.keyword == Keyword::TABLE => {
|
||||
let object_name = self.parse_object_name()?;
|
||||
(CommentObject::Table, object_name)
|
||||
}
|
||||
_ => self.expected("comment object_type", token)?,
|
||||
};
|
||||
|
||||
self.expect_keyword(Keyword::IS)?;
|
||||
let comment = if self.parse_keyword(Keyword::NULL) {
|
||||
None
|
||||
} else {
|
||||
Some(self.parse_literal_string()?)
|
||||
};
|
||||
Ok(Statement::Comment {
|
||||
object_type,
|
||||
object_name,
|
||||
comment,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Word {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue