-
Notifications
You must be signed in to change notification settings - Fork 752
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4532 from kevinw66/alter_table_rename
ISSUE-4459: Add support for alter table rename statement
- Loading branch information
Showing
17 changed files
with
210 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2021 Datafuse Labs. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::sync::Arc; | ||
|
||
use common_exception::ErrorCode; | ||
use common_exception::Result; | ||
use common_planners::PlanNode; | ||
use common_planners::RenameTableEntity; | ||
use common_planners::RenameTablePlan; | ||
use common_tracing::tracing; | ||
use sqlparser::ast::ObjectName; | ||
|
||
use crate::sessions::QueryContext; | ||
use crate::sql::statements::AnalyzableStatement; | ||
use crate::sql::statements::AnalyzedResult; | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct DfAlterTable { | ||
pub if_exists: bool, | ||
pub table_name: ObjectName, | ||
pub action: AlterTableAction, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq)] | ||
pub enum AlterTableAction { | ||
RenameTable(ObjectName), | ||
// TODO AddColumn etc. | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl AnalyzableStatement for DfAlterTable { | ||
#[tracing::instrument(level = "debug", skip(self, ctx), fields(ctx.id = ctx.get_id().as_str()))] | ||
async fn analyze(&self, ctx: Arc<QueryContext>) -> Result<AnalyzedResult> { | ||
let tenant = ctx.get_tenant(); | ||
let (db, table_name) = self.resolve_table(ctx.clone(), &self.table_name)?; | ||
|
||
match &self.action { | ||
AlterTableAction::RenameTable(o) => { | ||
let mut entities = Vec::new(); | ||
let (new_db, new_table_name) = self.resolve_table(ctx, o)?; | ||
entities.push(RenameTableEntity { | ||
if_exists: self.if_exists, | ||
db, | ||
table_name, | ||
new_db, | ||
new_table_name, | ||
}); | ||
|
||
Ok(AnalyzedResult::SimpleQuery(Box::new( | ||
PlanNode::RenameTable(RenameTablePlan { tenant, entities }), | ||
))) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl DfAlterTable { | ||
fn resolve_table( | ||
&self, | ||
ctx: Arc<QueryContext>, | ||
table_name: &ObjectName, | ||
) -> Result<(String, String)> { | ||
let idents = &table_name.0; | ||
match idents.len() { | ||
0 => Err(ErrorCode::SyntaxException("Alter table name is empty")), | ||
1 => Ok((ctx.get_current_database(), idents[0].value.clone())), | ||
2 => Ok((idents[0].value.clone(), idents[1].value.clone())), | ||
_ => Err(ErrorCode::SyntaxException( | ||
"Alter table name must be [`db`].`table`", | ||
)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
tests/suites/0_stateless/05_ddl/05_0003_ddl_alter_table.result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1 | ||
1 |
Oops, something went wrong.