Support ALTER VIEW, MySQL syntax (#907)

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
liadgiladi 2023-06-29 20:33:51 +03:00 committed by GitHub
parent 9effeba0d8
commit f05f71e20d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 105 additions and 1 deletions

View file

@ -1375,6 +1375,15 @@ pub enum Statement {
name: ObjectName,
operation: AlterIndexOperation,
},
/// ALTER VIEW
AlterView {
/// View name
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
name: ObjectName,
columns: Vec<Ident>,
query: Box<Query>,
with_options: Vec<SqlOption>,
},
/// DROP
Drop {
/// The type of the object to drop: TABLE, VIEW, etc.
@ -2534,6 +2543,21 @@ impl fmt::Display for Statement {
Statement::AlterIndex { name, operation } => {
write!(f, "ALTER INDEX {name} {operation}")
}
Statement::AlterView {
name,
columns,
query,
with_options,
} => {
write!(f, "ALTER VIEW {name}")?;
if !with_options.is_empty() {
write!(f, " WITH ({})", display_comma_separated(with_options))?;
}
if !columns.is_empty() {
write!(f, " ({})", display_comma_separated(columns))?;
}
write!(f, " AS {query}")
}
Statement::Drop {
object_type,
if_exists,

View file

@ -3898,8 +3898,10 @@ impl<'a> Parser<'a> {
}
pub fn parse_alter(&mut self) -> Result<Statement, ParserError> {
let object_type = self.expect_one_of_keywords(&[Keyword::TABLE, Keyword::INDEX])?;
let object_type =
self.expect_one_of_keywords(&[Keyword::VIEW, Keyword::TABLE, Keyword::INDEX])?;
match object_type {
Keyword::VIEW => self.parse_alter_view(),
Keyword::TABLE => {
let _ = self.parse_keyword(Keyword::ONLY); // [ ONLY ]
let table_name = self.parse_object_name()?;
@ -4097,6 +4099,23 @@ impl<'a> Parser<'a> {
}
}
pub fn parse_alter_view(&mut self) -> Result<Statement, ParserError> {
let name = self.parse_object_name()?;
let columns = self.parse_parenthesized_column_list(Optional, false)?;
let with_options = self.parse_options(Keyword::WITH)?;
self.expect_keyword(Keyword::AS)?;
let query = Box::new(self.parse_query()?);
Ok(Statement::AlterView {
name,
columns,
query,
with_options,
})
}
/// Parse a copy statement
pub fn parse_copy(&mut self) -> Result<Statement, ParserError> {
let source;

View file

@ -2869,6 +2869,67 @@ fn parse_alter_index() {
};
}
#[test]
fn parse_alter_view() {
let sql = "ALTER VIEW myschema.myview AS SELECT foo FROM bar";
match verified_stmt(sql) {
Statement::AlterView {
name,
columns,
query,
with_options,
} => {
assert_eq!("myschema.myview", name.to_string());
assert_eq!(Vec::<Ident>::new(), columns);
assert_eq!("SELECT foo FROM bar", query.to_string());
assert_eq!(with_options, vec![]);
}
_ => unreachable!(),
}
}
#[test]
fn parse_alter_view_with_options() {
let sql = "ALTER VIEW v WITH (foo = 'bar', a = 123) AS SELECT 1";
match verified_stmt(sql) {
Statement::AlterView { with_options, .. } => {
assert_eq!(
vec![
SqlOption {
name: "foo".into(),
value: Value::SingleQuotedString("bar".into()),
},
SqlOption {
name: "a".into(),
value: number("123"),
},
],
with_options
);
}
_ => unreachable!(),
}
}
#[test]
fn parse_alter_view_with_columns() {
let sql = "ALTER VIEW v (has, cols) AS SELECT 1, 2";
match verified_stmt(sql) {
Statement::AlterView {
name,
columns,
query,
with_options,
} => {
assert_eq!("v", name.to_string());
assert_eq!(columns, vec![Ident::new("has"), Ident::new("cols")]);
assert_eq!("SELECT 1, 2", query.to_string());
assert_eq!(with_options, vec![]);
}
_ => unreachable!(),
}
}
#[test]
fn parse_alter_table_add_column() {
match verified_stmt("ALTER TABLE tab ADD foo TEXT") {