Skip to content

Commit

Permalink
Revert "feat: tweak table data life-cycle related sql stmts (databend…
Browse files Browse the repository at this point in the history
…labs#13015)"

This reverts commit e14dc7c.
  • Loading branch information
dantengsky committed Oct 12, 2023
1 parent 34025da commit 372899b
Show file tree
Hide file tree
Showing 95 changed files with 324 additions and 135 deletions.
3 changes: 1 addition & 2 deletions benchmark/clickbench/hits/clear.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
drop table hits;
VACUUM DROP TABLE retain 0 hours;
drop table hits all;
17 changes: 8 additions & 9 deletions benchmark/clickbench/tpch/clear.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
drop table customer;
drop table lineitem;
drop table nation;
drop table orders;
drop table partsupp;
drop table part;
drop table region;
drop table supplier;
VACUUM DROP TABLE retain 0 hours;
drop table customer all;
drop table lineitem all;
drop table nation all;
drop table orders all;
drop table partsupp all;
drop table part all;
drop table region all;
drop table supplier all;
4 changes: 1 addition & 3 deletions benchmark/tpcds/load_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ tables=(
# Clear Data
for t in ${tables[@]}
do
echo "DROP TABLE IF EXISTS $t" | $MYSQL_CLIENT_CONNECT
echo "DROP TABLE IF EXISTS $t ALL" | $MYSQL_CLIENT_CONNECT
done

echo "VACUUM DROP TABLE retain 0 hours" | $MYSQL_CLIENT_CONNECT

# Create Tables;
cat "$CURDIR"/tpcds.sql | $MYSQL_CLIENT_CONNECT

Expand Down
3 changes: 3 additions & 0 deletions docs/doc/14-sql-commands/00-ddl/20-table/20-ddl-drop-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ DROP TABLE [IF EXISTS] [db.]name
:::caution

`DROP TABLE` only remove the table schema from meta service, we do not remove the underlying data from the storage.
If you want to delete the data and table all, please use:

`DROP TABLE <table_name> ALL;`

:::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
title: TRUNCATE TABLE
---

Removes all data from a table while preserving the table's schema. It deletes all rows in the table, making it an empty table with the same columns and constraints. Please note that, it does not release the disk space allocated to the table.
Removes all data from a table while preserving the table's schema. It deletes all rows in the table, making it an empty table with the same columns and constraints. Please note that, it does not release the disk space allocated to the table. To release the disk space, include the PURGE option, which is used to release the disk space allocated to the table when the truncate operation is performed.

See also: [DROP TABLE](20-ddl-drop-table.md)

## Syntax

```sql
TRUNCATE TABLE [db.]table_name
TRUNCATE TABLE [db.]table_name [PURGE]
```

## Examples
Expand Down Expand Up @@ -50,4 +50,10 @@ FROM
test_truncate

0 row in 0.017 sec. Processed 0 rows, 0B (0 rows/s, 0B/s)

root@localhost> TRUNCATE TABLE test_truncate PURGE;

TRUNCATE TABLE test_truncate PURGE

0 row in 0.118 sec. Processed 0 rows, 0B (0 rows/s, 0B/s)
```
6 changes: 1 addition & 5 deletions scripts/benchmark/query/load/hits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ select version();
SQL

cat <<SQL | bendsql
DROP TABLE IF EXISTS hits;
SQL

cat <<SQL | bendsql
VACUUM DROP TABLE retain 0 hours;
DROP TABLE IF EXISTS hits ALL;
SQL

cat <<SQL | bendsql
Expand Down
8 changes: 8 additions & 0 deletions src/query/ast/src/ast/statements/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ pub struct DropTableStmt {
pub catalog: Option<Identifier>,
pub database: Option<Identifier>,
pub table: Identifier,
pub all: bool,
}

impl Display for DropTableStmt {
Expand All @@ -271,6 +272,9 @@ impl Display for DropTableStmt {
.chain(&self.database)
.chain(Some(&self.table)),
)?;
if self.all {
write!(f, " ALL")?;
}

Ok(())
}
Expand Down Expand Up @@ -464,6 +468,7 @@ pub struct TruncateTableStmt {
pub catalog: Option<Identifier>,
pub database: Option<Identifier>,
pub table: Identifier,
pub purge: bool,
}

impl Display for TruncateTableStmt {
Expand All @@ -476,6 +481,9 @@ impl Display for TruncateTableStmt {
.chain(&self.database)
.chain(Some(&self.table)),
)?;
if self.purge {
write!(f, " PURGE")?;
}

Ok(())
}
Expand Down
12 changes: 7 additions & 5 deletions src/query/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,15 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
);
let drop_table = map(
rule! {
DROP ~ TABLE ~ ( IF ~ ^EXISTS )? ~ #dot_separated_idents_1_to_3
DROP ~ TABLE ~ ( IF ~ ^EXISTS )? ~ #dot_separated_idents_1_to_3 ~ ALL?
},
|(_, _, opt_if_exists, (catalog, database, table))| {
|(_, _, opt_if_exists, (catalog, database, table), opt_all)| {
Statement::DropTable(DropTableStmt {
if_exists: opt_if_exists.is_some(),
catalog,
database,
table,
all: opt_all.is_some(),
})
},
);
Expand Down Expand Up @@ -635,13 +636,14 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
);
let truncate_table = map(
rule! {
TRUNCATE ~ TABLE ~ #dot_separated_idents_1_to_3
TRUNCATE ~ TABLE ~ #dot_separated_idents_1_to_3 ~ PURGE?
},
|(_, _, (catalog, database, table))| {
|(_, _, (catalog, database, table), opt_purge)| {
Statement::TruncateTable(TruncateTableStmt {
catalog,
database,
table,
purge: opt_purge.is_some(),
})
},
);
Expand Down Expand Up @@ -1457,7 +1459,7 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
| #undrop_table : "`UNDROP TABLE [<database>.]<table>`"
| #alter_table : "`ALTER TABLE [<database>.]<table> <action>`"
| #rename_table : "`RENAME TABLE [<database>.]<table> TO <new_table>`"
| #truncate_table : "`TRUNCATE TABLE [<database>.]<table>`"
| #truncate_table : "`TRUNCATE TABLE [<database>.]<table> [PURGE]`"
| #optimize_table : "`OPTIMIZE TABLE [<database>.]<table> (ALL | PURGE | COMPACT [SEGMENT])`"
| #vacuum_table : "`VACUUM TABLE [<database>.]<table> [RETAIN number HOURS] [DRY RUN]`"
| #vacuum_drop_table : "`VACUUM DROP TABLE [FROM [<catalog>.]<database>] [RETAIN number HOURS] [DRY RUN]`"
Expand Down
4 changes: 2 additions & 2 deletions src/query/ast/tests/it/testdata/statement-error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ error:
--> SQL:1:21
|
1 | truncate table a.b.c.d
| ^ expected `FORMAT` or `;`
| ^ expected `PURGE`, `FORMAT`, or `;`


---------- Input ----------
Expand All @@ -121,7 +121,7 @@ error:
1 | truncate a
| -------- ^ expected `TABLE`
| |
| while parsing `TRUNCATE TABLE [<database>.]<table>`
| while parsing `TRUNCATE TABLE [<database>.]<table> [PURGE]`


---------- Input ----------
Expand Down
8 changes: 8 additions & 0 deletions src/query/ast/tests/it/testdata/statement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1462,6 +1462,7 @@ TruncateTable(
15..16,
),
},
purge: false,
},
)

Expand Down Expand Up @@ -1492,6 +1493,7 @@ TruncateTable(
19..20,
),
},
purge: false,
},
)

Expand All @@ -1513,6 +1515,7 @@ DropTable(
11..12,
),
},
all: false,
},
)

Expand Down Expand Up @@ -1544,6 +1547,7 @@ DropTable(
23..26,
),
},
all: false,
},
)

Expand Down Expand Up @@ -2355,6 +2359,7 @@ TruncateTable(
15..19,
),
},
purge: false,
},
)

Expand Down Expand Up @@ -2383,6 +2388,7 @@ TruncateTable(
23..27,
),
},
purge: false,
},
)

Expand All @@ -2404,6 +2410,7 @@ DropTable(
11..17,
),
},
all: false,
},
)

Expand All @@ -2425,6 +2432,7 @@ DropTable(
21..27,
),
},
all: false,
},
)

Expand Down
4 changes: 2 additions & 2 deletions src/query/catalog/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ pub trait Table: Sync + Send {
}

#[async_backtrace::framed]
async fn truncate(&self, ctx: Arc<dyn TableContext>) -> Result<()> {
let _ = ctx;
async fn truncate(&self, ctx: Arc<dyn TableContext>, purge: bool) -> Result<()> {
let (_, _) = (ctx, purge);
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/query/ee/tests/it/aggregating_index/index_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1110,8 +1110,8 @@ async fn test_fuzz_impl(format: &str, spill: bool) -> Result<()> {
}

// Clear data
execute_sql(fixture.ctx(), "DROP TABLE rt").await?;
execute_sql(fixture.ctx(), "DROP TABLE t").await?;
execute_sql(fixture.ctx(), "DROP TABLE rt ALL").await?;
execute_sql(fixture.ctx(), "DROP TABLE t ALL").await?;
}
}
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::api::FlightAction;

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
pub struct TruncateTablePacket {
pub purge: bool,
pub table_name: String,
pub catalog_name: String,
pub database_name: String,
Expand All @@ -37,8 +38,10 @@ impl TruncateTablePacket {
table_name: String,
catalog_name: String,
database_name: String,
purge: bool,
) -> TruncateTablePacket {
TruncateTablePacket {
purge,
table_name,
catalog_name,
database_name,
Expand Down
10 changes: 10 additions & 0 deletions src/query/service/src/interpreters/interpreter_table_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use std::sync::Arc;

use common_catalog::table::TableExt;
use common_exception::ErrorCode;
use common_exception::Result;
use common_meta_app::schema::DropTableByIdReq;
Expand Down Expand Up @@ -77,6 +78,15 @@ impl Interpreter for DropTableInterpreter {
})
.await?;

// if `plan.all`, truncate, then purge the historical data
if self.plan.all {
let purge = true;
// the above `catalog.drop_table` operation changed the table meta version,
// thus if we do not refresh the table instance, `truncate` will fail
let latest = tbl.as_ref().refresh(self.ctx.as_ref()).await?;
latest.truncate(self.ctx.clone(), purge).await?
}

if let Some((spec_vec, share_table_info)) = resp.spec_vec {
save_share_spec(
&self.ctx.get_tenant(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::sessions::TableContext;

pub struct TruncateTableInterpreter {
ctx: Arc<QueryContext>,
purge: bool,
table_name: String,
catalog_name: String,
database_name: String,
Expand All @@ -38,6 +39,7 @@ impl TruncateTableInterpreter {
pub fn try_create(ctx: Arc<QueryContext>, plan: TruncateTablePlan) -> Result<Self> {
Ok(TruncateTableInterpreter {
ctx,
purge: plan.purge,
table_name: plan.table,
catalog_name: plan.catalog,
database_name: plan.database,
Expand All @@ -48,6 +50,7 @@ impl TruncateTableInterpreter {
pub fn from_flight(ctx: Arc<QueryContext>, packet: TruncateTablePacket) -> Result<Self> {
Ok(TruncateTableInterpreter {
ctx,
purge: packet.purge,
table_name: packet.table_name,
catalog_name: packet.catalog_name,
database_name: packet.database_name,
Expand Down Expand Up @@ -81,13 +84,14 @@ impl Interpreter for TruncateTableInterpreter {
self.table_name.clone(),
self.catalog_name.clone(),
self.database_name.clone(),
self.purge,
);
truncate_packet.commit(conf.as_ref(), timeout).await?;
}
}
}

table.truncate(self.ctx.clone()).await?;
table.truncate(self.ctx.clone(), self.purge).await?;
Ok(PipelineBuildResult::create())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async fn test_fuse_snapshot_analyze_and_truncate() -> Result<()> {
.get_table(ctx.get_tenant().as_str(), &db, &tbl)
.await?;
let fuse_table = FuseTable::try_from_table(table.as_ref())?;
fuse_table.truncate(ctx).await?;
fuse_table.truncate(ctx, false).await?;
}

// optimize after truncate table, ts file location will become None
Expand Down
6 changes: 3 additions & 3 deletions src/query/service/tests/it/storages/fuse/operations/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ async fn test_fuse_purge_normal_orphan_snapshot() -> Result<()> {
"do_gc: there should be 1 snapshot, 0 segment/block",
expected_num_of_snapshot,
0, // 0 snapshot statistic
1, // 1 segments
1, // 1 blocks
1, // 1 index
1, // 0 segments
1, // 0 blocks
1, // 0 index
Some(()),
None,
)
Expand Down
1 change: 1 addition & 0 deletions src/query/service/tests/it/storages/fuse/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod mutation;
mod navigate;
mod optimize;
mod purge_drop;
mod purge_truncate;
mod read_plan;
mod replace_into;
mod table_analyze;
Expand Down
Loading

0 comments on commit 372899b

Please sign in to comment.