Skip to content
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

refactor: alter view only need to modify table option #15012

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Changes from all 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
50 changes: 13 additions & 37 deletions src/query/service/src/interpreters/interpreter_view_alter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::sync::Arc;

use databend_common_exception::ErrorCode;
use databend_common_exception::Result;
use databend_common_meta_app::schema::CreateOption;
use databend_common_meta_app::schema::CreateTableReq;
use databend_common_meta_app::schema::DropTableByIdReq;
use databend_common_meta_app::schema::TableMeta;
use databend_common_meta_app::schema::TableNameIdent;
use databend_common_meta_app::schema::UpsertTableOptionReq;
use databend_common_meta_types::MatchSeq;
use databend_common_sql::plans::AlterViewPlan;
use databend_common_sql::Planner;
use databend_common_storages_view::view_table::VIEW_ENGINE;

use crate::interpreters::Interpreter;
use crate::pipelines::PipelineBuildResult;
Expand Down Expand Up @@ -54,27 +50,12 @@ impl Interpreter for AlterViewInterpreter {

#[async_backtrace::framed]
async fn execute2(&self) -> Result<PipelineBuildResult> {
// drop view
let catalog = self.ctx.get_catalog(&self.plan.catalog).await?;
if let Ok(tbl) = catalog
.get_table(&self.plan.tenant, &self.plan.database, &self.plan.view_name)
.await
{
let db = catalog
.get_database(&self.plan.tenant, &self.plan.database)
.await?;
catalog
.drop_table_by_id(DropTableByIdReq {
if_exists: true,
tenant: self.plan.tenant.clone(),
table_name: self.plan.view_name.clone(),
tb_id: tbl.get_id(),
db_id: db.get_db_info().ident.db_id,
})
.await?;

// create new view
let mut options = BTreeMap::new();
let mut options = HashMap::new();
let subquery = if self.plan.column_names.is_empty() {
self.plan.subquery.clone()
} else {
Expand All @@ -94,22 +75,17 @@ impl Interpreter for AlterViewInterpreter {
self.plan.column_names.join(", ")
)
};
options.insert("query".to_string(), subquery);
options.insert("query".to_string(), Some(subquery));

let plan = CreateTableReq {
create_option: CreateOption::CreateIfNotExists,
name_ident: TableNameIdent {
tenant: self.plan.tenant.clone(),
db_name: self.plan.database.clone(),
table_name: self.plan.view_name.clone(),
},
table_meta: TableMeta {
engine: VIEW_ENGINE.to_string(),
options,
..Default::default()
},
let req = UpsertTableOptionReq {
table_id: tbl.get_id(),
seq: MatchSeq::Exact(tbl.get_table_info().ident.seq),
options,
};
catalog.create_table(plan).await?;

catalog
.upsert_table_option(&self.plan.tenant, &self.plan.database, req)
.await?;

Ok(PipelineBuildResult::create())
} else {
Expand Down
Loading