Skip to content

Support ALTER TABLE ... SET LOCATION #1154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,6 +1747,7 @@ pub enum Statement {
if_exists: bool,
only: bool,
operations: Vec<AlterTableOperation>,
location: Option<HiveSetLocation>,
},
/// ```sql
/// ALTER INDEX
Expand Down Expand Up @@ -3000,12 +3001,10 @@ impl fmt::Display for Statement {
}
}
if *external {
write!(
f,
" STORED AS {} LOCATION '{}'",
file_format.as_ref().unwrap(),
location.as_ref().unwrap()
)?;
if file_format.is_some() {
write!(f, " STORED AS {}", file_format.as_ref().unwrap())?;
}
write!(f, " LOCATION '{}'", location.as_ref().unwrap())?;
}
if !table_properties.is_empty() {
write!(
Expand Down Expand Up @@ -3255,6 +3254,7 @@ impl fmt::Display for Statement {
if_exists,
only,
operations,
location,
} => {
write!(f, "ALTER TABLE ")?;
if *if_exists {
Expand All @@ -3267,7 +3267,11 @@ impl fmt::Display for Statement {
f,
"{name} {operations}",
operations = display_comma_separated(operations)
)
)?;
if let Some(loc) = location {
write!(f, " {loc}")?
}
Ok(())
}
Statement::AlterIndex { name, operation } => {
write!(f, "ALTER INDEX {name} {operation}")
Expand Down Expand Up @@ -5481,6 +5485,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)]
mod tests {
use super::*;
Expand Down
15 changes: 15 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5145,11 +5145,26 @@ impl<'a> Parser<'a> {
let only = self.parse_keyword(Keyword::ONLY); // [ ONLY ]
let table_name = self.parse_object_name(false)?;
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 {
name: table_name,
if_exists,
only,
operations,
location,
})
}
Keyword::INDEX => {
Expand Down
2 changes: 2 additions & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ pub fn alter_table_op_with_name(stmt: Statement, expected_name: &str) -> AlterTa
if_exists,
only: is_only,
operations,
location: _,
} => {
assert_eq!(name.to_string(), expected_name);
assert!(!if_exists);
Expand All @@ -251,6 +252,7 @@ pub fn alter_table_op_with_name(stmt: Statement, expected_name: &str) -> AlterTa
_ => panic!("Expected ALTER TABLE statement"),
}
}

pub fn alter_table_op(stmt: Statement) -> AlterTableOperation {
alter_table_op_with_name(stmt, "tab")
}
Expand Down
13 changes: 13 additions & 0 deletions tests/sqlparser_hive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ fn test_alter_partition() {
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]
fn test_add_partition() {
let add = "ALTER TABLE db.table ADD IF NOT EXISTS PARTITION (a = 'asdf', b = 2)";
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ fn parse_alter_table_add_columns() {
if_exists,
only,
operations,
location: _,
} => {
assert_eq!(name.to_string(), "tab");
assert!(if_exists);
Expand Down