-
Notifications
You must be signed in to change notification settings - Fork 777
feat: Implement Modify Table Connection #18034
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3c6c5b5
feat: Implement alter table connection
Xuanwo 0395aaf
feat: Implement Modify Table Connection
Xuanwo 475ec31
Fix typo
Xuanwo 5cc82f0
Add new test cases
Xuanwo a8bdd1e
Make clippy happy
Xuanwo 6dee9f1
Merge branch 'main' into alter-table-connection
BohuTANG 651ff15
Merge remote-tracking branch 'origin/main' into alter-table-connection
Xuanwo c347b1b
Fix tests
Xuanwo 55f350c
Merge branch 'main' into alter-table-connection
Xuanwo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
143 changes: 143 additions & 0 deletions
143
src/query/service/src/interpreters/interpreter_table_modify_connection.rs
This file contains hidden or 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,143 @@ | ||
// 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 databend_common_ast::ast::UriLocation; | ||
use databend_common_catalog::table::TableExt; | ||
use databend_common_exception::ErrorCode; | ||
use databend_common_exception::Result; | ||
use databend_common_meta_app::schema::DatabaseType; | ||
use databend_common_sql::binder::parse_storage_params_from_uri; | ||
use databend_common_sql::plans::ModifyTableConnectionPlan; | ||
use databend_common_storage::check_operator; | ||
use databend_common_storage::init_operator; | ||
use databend_common_storages_stream::stream_table::STREAM_ENGINE; | ||
use databend_common_storages_view::view_table::VIEW_ENGINE; | ||
use log::debug; | ||
|
||
use crate::interpreters::interpreter_table_add_column::commit_table_meta; | ||
use crate::interpreters::Interpreter; | ||
use crate::pipelines::PipelineBuildResult; | ||
use crate::sessions::QueryContext; | ||
use crate::sessions::TableContext; | ||
|
||
pub struct ModifyTableConnectionInterpreter { | ||
ctx: Arc<QueryContext>, | ||
plan: ModifyTableConnectionPlan, | ||
} | ||
|
||
impl ModifyTableConnectionInterpreter { | ||
pub fn try_create(ctx: Arc<QueryContext>, plan: ModifyTableConnectionPlan) -> Result<Self> { | ||
Ok(ModifyTableConnectionInterpreter { ctx, plan }) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Interpreter for ModifyTableConnectionInterpreter { | ||
fn name(&self) -> &str { | ||
"ModifyTableConnectionInterpreter" | ||
} | ||
|
||
fn is_ddl(&self) -> bool { | ||
true | ||
} | ||
|
||
#[async_backtrace::framed] | ||
async fn execute2(&self) -> Result<PipelineBuildResult> { | ||
let catalog_name = self.plan.catalog.as_str(); | ||
let db_name = self.plan.database.as_str(); | ||
let tbl_name = self.plan.table.as_str(); | ||
|
||
let table = self | ||
.ctx | ||
.get_catalog(catalog_name) | ||
.await? | ||
.get_table(&self.ctx.get_tenant(), db_name, tbl_name) | ||
.await?; | ||
|
||
// check mutability | ||
table.check_mutable()?; | ||
|
||
let table_info = table.get_table_info(); | ||
let engine = table.engine(); | ||
if matches!(engine, VIEW_ENGINE | STREAM_ENGINE) { | ||
return Err(ErrorCode::TableEngineNotSupported(format!( | ||
"{}.{} engine is {} that doesn't support alter", | ||
&self.plan.database, &self.plan.table, engine | ||
))); | ||
} | ||
if table_info.db_type != DatabaseType::NormalDB { | ||
return Err(ErrorCode::TableEngineNotSupported(format!( | ||
"{}.{} doesn't support alter", | ||
&self.plan.database, &self.plan.table | ||
))); | ||
} | ||
let Some(old_sp) = table_info.meta.storage_params.clone() else { | ||
return Err(ErrorCode::TableEngineNotSupported(format!( | ||
"{}.{} is not an external table, cannot alter connection", | ||
&self.plan.database, &self.plan.table | ||
))); | ||
}; | ||
|
||
debug!("old storage params before update: {old_sp:?}"); | ||
|
||
// This location is used to parse the storage parameters from the URI. | ||
// | ||
// We don't really this this location to replace the old one, we just parse it out and change the storage parameters on needs. | ||
let mut location = UriLocation::new( | ||
// The storage type is not changeable, we just use the old one. | ||
old_sp.storage_type(), | ||
// name is not changeable, we just use a dummy value here. | ||
"test".to_string(), | ||
// root is not changeable, we just use a dummy value here. | ||
"/".to_string(), | ||
self.plan.new_connection.clone(), | ||
); | ||
// NOTE: never use this storage params directly. | ||
let updated_sp = parse_storage_params_from_uri( | ||
&mut location, | ||
Some(self.ctx.as_ref() as _), | ||
"when ALTER TABLE CONNECTION", | ||
) | ||
.await?; | ||
|
||
debug!("storage params used for update: {updated_sp:?}"); | ||
let new_sp = old_sp.apply_update(updated_sp)?; | ||
debug!("new storage params been updated: {new_sp:?}"); | ||
|
||
// Check the storage params via init operator. | ||
let op = init_operator(&new_sp).map_err(|err| { | ||
ErrorCode::InvalidConfig(format!( | ||
"Input storage config for stage is invalid: {err:?}" | ||
)) | ||
})?; | ||
check_operator(&op, &new_sp).await?; | ||
|
||
let catalog = self.ctx.get_catalog(self.plan.catalog.as_str()).await?; | ||
let mut new_table_meta = table_info.meta.clone(); | ||
new_table_meta.storage_params = Some(new_sp); | ||
|
||
commit_table_meta( | ||
&self.ctx, | ||
table.as_ref(), | ||
table_info, | ||
new_table_meta, | ||
catalog, | ||
) | ||
.await?; | ||
|
||
Ok(PipelineBuildResult::create()) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.