Skip to content

Commit dd6b53a

Browse files
liadgiladialamb
authored andcommitted
Support ALTER VIEW, MySQL syntax (apache#907)
Co-authored-by: Andrew Lamb <[email protected]>
1 parent 855ad5a commit dd6b53a

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

src/ast/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,15 @@ pub enum Statement {
14711471
name: ObjectName,
14721472
operation: AlterIndexOperation,
14731473
},
1474+
/// ALTER VIEW
1475+
AlterView {
1476+
/// View name
1477+
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
1478+
name: ObjectName,
1479+
columns: Vec<Ident>,
1480+
query: Box<Query>,
1481+
with_options: Vec<SqlOption>,
1482+
},
14741483
/// DROP
14751484
Drop {
14761485
/// The type of the object to drop: TABLE, VIEW, etc.
@@ -2724,6 +2733,21 @@ impl fmt::Display for Statement {
27242733
Statement::AlterIndex { name, operation } => {
27252734
write!(f, "ALTER INDEX {name} {operation}")
27262735
}
2736+
Statement::AlterView {
2737+
name,
2738+
columns,
2739+
query,
2740+
with_options,
2741+
} => {
2742+
write!(f, "ALTER VIEW {name}")?;
2743+
if !with_options.is_empty() {
2744+
write!(f, " WITH ({})", display_comma_separated(with_options))?;
2745+
}
2746+
if !columns.is_empty() {
2747+
write!(f, " ({})", display_comma_separated(columns))?;
2748+
}
2749+
write!(f, " AS {query}")
2750+
}
27272751
Statement::Drop {
27282752
object_type,
27292753
if_exists,

src/parser.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3941,8 +3941,10 @@ impl<'a> Parser<'a> {
39413941
}
39423942

39433943
pub fn parse_alter(&mut self) -> Result<Statement, ParserError> {
3944-
let object_type = self.expect_one_of_keywords(&[Keyword::TABLE, Keyword::INDEX])?;
3944+
let object_type =
3945+
self.expect_one_of_keywords(&[Keyword::VIEW, Keyword::TABLE, Keyword::INDEX])?;
39453946
match object_type {
3947+
Keyword::VIEW => self.parse_alter_view(),
39463948
Keyword::TABLE => {
39473949
let _ = self.parse_keyword(Keyword::ONLY); // [ ONLY ]
39483950
let table_name = self.parse_object_name()?;
@@ -4140,6 +4142,23 @@ impl<'a> Parser<'a> {
41404142
}
41414143
}
41424144

4145+
pub fn parse_alter_view(&mut self) -> Result<Statement, ParserError> {
4146+
let name = self.parse_object_name()?;
4147+
let columns = self.parse_parenthesized_column_list(Optional, false)?;
4148+
4149+
let with_options = self.parse_options(Keyword::WITH)?;
4150+
4151+
self.expect_keyword(Keyword::AS)?;
4152+
let query = Box::new(self.parse_query()?);
4153+
4154+
Ok(Statement::AlterView {
4155+
name,
4156+
columns,
4157+
query,
4158+
with_options,
4159+
})
4160+
}
4161+
41434162
/// Parse a copy statement
41444163
pub fn parse_copy(&mut self) -> Result<Statement, ParserError> {
41454164
let source;

tests/sqlparser_common.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,6 +2869,67 @@ fn parse_alter_index() {
28692869
};
28702870
}
28712871

2872+
#[test]
2873+
fn parse_alter_view() {
2874+
let sql = "ALTER VIEW myschema.myview AS SELECT foo FROM bar";
2875+
match verified_stmt(sql) {
2876+
Statement::AlterView {
2877+
name,
2878+
columns,
2879+
query,
2880+
with_options,
2881+
} => {
2882+
assert_eq!("myschema.myview", name.to_string());
2883+
assert_eq!(Vec::<Ident>::new(), columns);
2884+
assert_eq!("SELECT foo FROM bar", query.to_string());
2885+
assert_eq!(with_options, vec![]);
2886+
}
2887+
_ => unreachable!(),
2888+
}
2889+
}
2890+
2891+
#[test]
2892+
fn parse_alter_view_with_options() {
2893+
let sql = "ALTER VIEW v WITH (foo = 'bar', a = 123) AS SELECT 1";
2894+
match verified_stmt(sql) {
2895+
Statement::AlterView { with_options, .. } => {
2896+
assert_eq!(
2897+
vec![
2898+
SqlOption {
2899+
name: "foo".into(),
2900+
value: Value::SingleQuotedString("bar".into()),
2901+
},
2902+
SqlOption {
2903+
name: "a".into(),
2904+
value: number("123"),
2905+
},
2906+
],
2907+
with_options
2908+
);
2909+
}
2910+
_ => unreachable!(),
2911+
}
2912+
}
2913+
2914+
#[test]
2915+
fn parse_alter_view_with_columns() {
2916+
let sql = "ALTER VIEW v (has, cols) AS SELECT 1, 2";
2917+
match verified_stmt(sql) {
2918+
Statement::AlterView {
2919+
name,
2920+
columns,
2921+
query,
2922+
with_options,
2923+
} => {
2924+
assert_eq!("v", name.to_string());
2925+
assert_eq!(columns, vec![Ident::new("has"), Ident::new("cols")]);
2926+
assert_eq!("SELECT 1, 2", query.to_string());
2927+
assert_eq!(with_options, vec![]);
2928+
}
2929+
_ => unreachable!(),
2930+
}
2931+
}
2932+
28722933
#[test]
28732934
fn parse_alter_table_add_column() {
28742935
match verified_stmt("ALTER TABLE tab ADD foo TEXT") {

0 commit comments

Comments
 (0)