Skip to content

Commit 5e2095c

Browse files
authored
chore: purge dropped view metadata during vacuum (#16819)
* chore: purge view metadata during vacuum * tweak logic test
1 parent 3023243 commit 5e2095c

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

src/query/service/src/interpreters/interpreter_vacuum_drop_tables.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use databend_common_meta_app::schema::DroppedId;
3131
use databend_common_meta_app::schema::GcDroppedTableReq;
3232
use databend_common_meta_app::schema::ListDroppedTableReq;
3333
use databend_common_sql::plans::VacuumDropTablePlan;
34+
use databend_common_storages_view::view_table::VIEW_ENGINE;
3435
use databend_enterprise_vacuum_handler::get_vacuum_handler;
3536
use log::info;
3637

@@ -156,13 +157,17 @@ impl Interpreter for VacuumDropTablesInterpreter {
156157
drop_ids
157158
);
158159

159-
// TODO buggy, table as catalog obj should be allowed to drop
160-
// also drop ids
161-
// filter out read-only tables
162-
let tables = tables
160+
// Filter out read-only tables and views.
161+
// Note: The drop_ids list still includes view IDs
162+
let (views, tables): (Vec<_>, Vec<_>) = tables
163163
.into_iter()
164164
.filter(|tbl| !tbl.as_ref().is_read_only())
165-
.collect::<Vec<_>>();
165+
.partition(|tbl| tbl.get_table_info().meta.engine == VIEW_ENGINE);
166+
167+
{
168+
let view_ids = views.into_iter().map(|v| v.get_id()).collect::<Vec<_>>();
169+
info!("view ids excluded from purging data: {:?}", view_ids);
170+
}
166171

167172
let handler = get_vacuum_handler();
168173
let threads_nums = self.ctx.get_settings().get_max_threads()? as usize;
@@ -187,6 +192,8 @@ impl Interpreter for VacuumDropTablesInterpreter {
187192
// gc metadata only when not dry run
188193
if self.plan.option.dry_run.is_none() {
189194
let mut success_dropped_ids = vec![];
195+
// Since drop_ids contains view IDs, any views (if present) will be added to
196+
// the success_dropped_id list, with removal from the meta-server attempted later.
190197
for drop_id in drop_ids {
191198
match &drop_id {
192199
DroppedId::Db { db_id, db_name: _ } => {
@@ -205,6 +212,7 @@ impl Interpreter for VacuumDropTablesInterpreter {
205212
"failed dbs:{:?}, failed_tables:{:?}, success_drop_ids:{:?}",
206213
failed_db_ids, failed_tables, success_dropped_ids
207214
);
215+
208216
self.gc_drop_tables(catalog, success_dropped_ids).await?;
209217
}
210218

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## Copyright 2023 Databend Cloud
2+
##
3+
## Licensed under the Elastic License, Version 2.0 (the "License");
4+
## you may not use this file except in compliance with the License.
5+
## You may obtain a copy of the License at
6+
##
7+
## https://www.elastic.co/licensing/elastic-license
8+
##
9+
## Unless required by applicable law or agreed to in writing, software
10+
## distributed under the License is distributed on an "AS IS" BASIS,
11+
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
## See the License for the specific language governing permissions and
13+
## limitations under the License.
14+
15+
statement ok
16+
drop database if exists vacuum_view_test;
17+
18+
statement ok
19+
create database vacuum_view_test;
20+
21+
statement ok
22+
use vacuum_view_test;
23+
24+
statement ok
25+
create table t as select * from numbers(1);
26+
27+
statement ok
28+
create view v as select * from t;
29+
30+
statement ok
31+
drop view v;
32+
33+
# the dropped view vacuum_view_test.v should be there
34+
query I
35+
select count() from system.tables_with_history where database = 'vacuum_view_test' and name = 'v';
36+
----
37+
1
38+
39+
statement ok
40+
set data_retention_time_in_days = 0;
41+
42+
statement ok
43+
vacuum drop table from vacuum_view_test;
44+
45+
# the dropped view vacuum_view_test.v should be vacuumed
46+
query I
47+
select count() from system.tables_with_history where database = 'vacuum_view_test' and name = 'v';
48+
----
49+
0
50+
51+
statement ok
52+
drop database vacuum_view_test;

0 commit comments

Comments
 (0)