Skip to content

Commit ed740cb

Browse files
authored
refactor: alter view only need to modify table option (#15012)
1 parent c85eec1 commit ed740cb

File tree

1 file changed

+13
-37
lines changed

1 file changed

+13
-37
lines changed

src/query/service/src/interpreters/interpreter_view_alter.rs

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::collections::BTreeMap;
15+
use std::collections::HashMap;
1616
use std::sync::Arc;
1717

1818
use databend_common_exception::ErrorCode;
1919
use databend_common_exception::Result;
20-
use databend_common_meta_app::schema::CreateOption;
21-
use databend_common_meta_app::schema::CreateTableReq;
22-
use databend_common_meta_app::schema::DropTableByIdReq;
23-
use databend_common_meta_app::schema::TableMeta;
24-
use databend_common_meta_app::schema::TableNameIdent;
20+
use databend_common_meta_app::schema::UpsertTableOptionReq;
21+
use databend_common_meta_types::MatchSeq;
2522
use databend_common_sql::plans::AlterViewPlan;
2623
use databend_common_sql::Planner;
27-
use databend_common_storages_view::view_table::VIEW_ENGINE;
2824

2925
use crate::interpreters::Interpreter;
3026
use crate::pipelines::PipelineBuildResult;
@@ -54,27 +50,12 @@ impl Interpreter for AlterViewInterpreter {
5450

5551
#[async_backtrace::framed]
5652
async fn execute2(&self) -> Result<PipelineBuildResult> {
57-
// drop view
5853
let catalog = self.ctx.get_catalog(&self.plan.catalog).await?;
5954
if let Ok(tbl) = catalog
6055
.get_table(&self.plan.tenant, &self.plan.database, &self.plan.view_name)
6156
.await
6257
{
63-
let db = catalog
64-
.get_database(&self.plan.tenant, &self.plan.database)
65-
.await?;
66-
catalog
67-
.drop_table_by_id(DropTableByIdReq {
68-
if_exists: true,
69-
tenant: self.plan.tenant.clone(),
70-
table_name: self.plan.view_name.clone(),
71-
tb_id: tbl.get_id(),
72-
db_id: db.get_db_info().ident.db_id,
73-
})
74-
.await?;
75-
76-
// create new view
77-
let mut options = BTreeMap::new();
58+
let mut options = HashMap::new();
7859
let subquery = if self.plan.column_names.is_empty() {
7960
self.plan.subquery.clone()
8061
} else {
@@ -94,22 +75,17 @@ impl Interpreter for AlterViewInterpreter {
9475
self.plan.column_names.join(", ")
9576
)
9677
};
97-
options.insert("query".to_string(), subquery);
78+
options.insert("query".to_string(), Some(subquery));
9879

99-
let plan = CreateTableReq {
100-
create_option: CreateOption::CreateIfNotExists,
101-
name_ident: TableNameIdent {
102-
tenant: self.plan.tenant.clone(),
103-
db_name: self.plan.database.clone(),
104-
table_name: self.plan.view_name.clone(),
105-
},
106-
table_meta: TableMeta {
107-
engine: VIEW_ENGINE.to_string(),
108-
options,
109-
..Default::default()
110-
},
80+
let req = UpsertTableOptionReq {
81+
table_id: tbl.get_id(),
82+
seq: MatchSeq::Exact(tbl.get_table_info().ident.seq),
83+
options,
11184
};
112-
catalog.create_table(plan).await?;
85+
86+
catalog
87+
.upsert_table_option(&self.plan.tenant, &self.plan.database, req)
88+
.await?;
11389

11490
Ok(PipelineBuildResult::create())
11591
} else {

0 commit comments

Comments
 (0)