mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-24 07:54:06 +00:00
Support ALTER TABLE ... SET LOCATION
(#1154)
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
parent
6f090e5547
commit
991dbab755
5 changed files with 59 additions and 7 deletions
|
@ -2001,6 +2001,7 @@ pub enum Statement {
|
||||||
if_exists: bool,
|
if_exists: bool,
|
||||||
only: bool,
|
only: bool,
|
||||||
operations: Vec<AlterTableOperation>,
|
operations: Vec<AlterTableOperation>,
|
||||||
|
location: Option<HiveSetLocation>,
|
||||||
},
|
},
|
||||||
/// ```sql
|
/// ```sql
|
||||||
/// ALTER INDEX
|
/// ALTER INDEX
|
||||||
|
@ -3249,12 +3250,10 @@ impl fmt::Display for Statement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if *external {
|
if *external {
|
||||||
write!(
|
if let Some(file_format) = &file_format {
|
||||||
f,
|
write!(f, " STORED AS {file_format}")?;
|
||||||
" STORED AS {} LOCATION '{}'",
|
}
|
||||||
file_format.as_ref().unwrap(),
|
write!(f, " LOCATION '{}'", location.as_ref().unwrap())?;
|
||||||
location.as_ref().unwrap()
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
if !table_properties.is_empty() {
|
if !table_properties.is_empty() {
|
||||||
write!(
|
write!(
|
||||||
|
@ -3504,6 +3503,7 @@ impl fmt::Display for Statement {
|
||||||
if_exists,
|
if_exists,
|
||||||
only,
|
only,
|
||||||
operations,
|
operations,
|
||||||
|
location,
|
||||||
} => {
|
} => {
|
||||||
write!(f, "ALTER TABLE ")?;
|
write!(f, "ALTER TABLE ")?;
|
||||||
if *if_exists {
|
if *if_exists {
|
||||||
|
@ -3516,7 +3516,11 @@ impl fmt::Display for Statement {
|
||||||
f,
|
f,
|
||||||
"{name} {operations}",
|
"{name} {operations}",
|
||||||
operations = display_comma_separated(operations)
|
operations = display_comma_separated(operations)
|
||||||
)
|
)?;
|
||||||
|
if let Some(loc) = location {
|
||||||
|
write!(f, " {loc}")?
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
Statement::AlterIndex { name, operation } => {
|
Statement::AlterIndex { name, operation } => {
|
||||||
write!(f, "ALTER INDEX {name} {operation}")
|
write!(f, "ALTER INDEX {name} {operation}")
|
||||||
|
@ -5840,6 +5844,23 @@ impl fmt::Display for LockTableType {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
|
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
|
||||||
|
pub struct HiveSetLocation {
|
||||||
|
pub has_set: bool,
|
||||||
|
pub location: Ident,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for HiveSetLocation {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
if self.has_set {
|
||||||
|
write!(f, "SET ")?;
|
||||||
|
}
|
||||||
|
write!(f, "LOCATION {}", self.location)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -5529,11 +5529,26 @@ impl<'a> Parser<'a> {
|
||||||
let only = self.parse_keyword(Keyword::ONLY); // [ ONLY ]
|
let only = self.parse_keyword(Keyword::ONLY); // [ ONLY ]
|
||||||
let table_name = self.parse_object_name(false)?;
|
let table_name = self.parse_object_name(false)?;
|
||||||
let operations = self.parse_comma_separated(Parser::parse_alter_table_operation)?;
|
let operations = self.parse_comma_separated(Parser::parse_alter_table_operation)?;
|
||||||
|
|
||||||
|
let mut location = None;
|
||||||
|
if self.parse_keyword(Keyword::LOCATION) {
|
||||||
|
location = Some(HiveSetLocation {
|
||||||
|
has_set: false,
|
||||||
|
location: self.parse_identifier(false)?,
|
||||||
|
});
|
||||||
|
} else if self.parse_keywords(&[Keyword::SET, Keyword::LOCATION]) {
|
||||||
|
location = Some(HiveSetLocation {
|
||||||
|
has_set: true,
|
||||||
|
location: self.parse_identifier(false)?,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Statement::AlterTable {
|
Ok(Statement::AlterTable {
|
||||||
name: table_name,
|
name: table_name,
|
||||||
if_exists,
|
if_exists,
|
||||||
only,
|
only,
|
||||||
operations,
|
operations,
|
||||||
|
location,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
Keyword::INDEX => {
|
Keyword::INDEX => {
|
||||||
|
|
|
@ -256,6 +256,7 @@ pub fn alter_table_op_with_name(stmt: Statement, expected_name: &str) -> AlterTa
|
||||||
if_exists,
|
if_exists,
|
||||||
only: is_only,
|
only: is_only,
|
||||||
operations,
|
operations,
|
||||||
|
location: _,
|
||||||
} => {
|
} => {
|
||||||
assert_eq!(name.to_string(), expected_name);
|
assert_eq!(name.to_string(), expected_name);
|
||||||
assert!(!if_exists);
|
assert!(!if_exists);
|
||||||
|
@ -265,6 +266,7 @@ pub fn alter_table_op_with_name(stmt: Statement, expected_name: &str) -> AlterTa
|
||||||
_ => panic!("Expected ALTER TABLE statement"),
|
_ => panic!("Expected ALTER TABLE statement"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn alter_table_op(stmt: Statement) -> AlterTableOperation {
|
pub fn alter_table_op(stmt: Statement) -> AlterTableOperation {
|
||||||
alter_table_op_with_name(stmt, "tab")
|
alter_table_op_with_name(stmt, "tab")
|
||||||
}
|
}
|
||||||
|
|
|
@ -124,6 +124,19 @@ fn test_alter_partition() {
|
||||||
hive().verified_stmt(alter);
|
hive().verified_stmt(alter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_alter_with_location() {
|
||||||
|
let alter =
|
||||||
|
"ALTER TABLE db.table PARTITION (a = 2) RENAME TO PARTITION (a = 1) LOCATION 's3://...'";
|
||||||
|
hive().verified_stmt(alter);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_alter_with_set_location() {
|
||||||
|
let alter = "ALTER TABLE db.table PARTITION (a = 2) RENAME TO PARTITION (a = 1) SET LOCATION 's3://...'";
|
||||||
|
hive().verified_stmt(alter);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_add_partition() {
|
fn test_add_partition() {
|
||||||
let add = "ALTER TABLE db.table ADD IF NOT EXISTS PARTITION (a = 'asdf', b = 2)";
|
let add = "ALTER TABLE db.table ADD IF NOT EXISTS PARTITION (a = 'asdf', b = 2)";
|
||||||
|
|
|
@ -677,6 +677,7 @@ fn parse_alter_table_add_columns() {
|
||||||
if_exists,
|
if_exists,
|
||||||
only,
|
only,
|
||||||
operations,
|
operations,
|
||||||
|
location: _,
|
||||||
} => {
|
} => {
|
||||||
assert_eq!(name.to_string(), "tab");
|
assert_eq!(name.to_string(), "tab");
|
||||||
assert!(if_exists);
|
assert!(if_exists);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue