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

feat: tweak table data life-cycle related sql stmts #13015

Merged
merged 12 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion benchmark/clickbench/hits/clear.sql
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
drop table hits all;
drop table hits;
VACUUM DROP TABLE retain 0 hours;
17 changes: 9 additions & 8 deletions benchmark/clickbench/tpch/clear.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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;
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;
4 changes: 3 additions & 1 deletion benchmark/tpcds/load_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ tables=(
# Clear Data
for t in ${tables[@]}
do
echo "DROP TABLE IF EXISTS $t ALL" | $MYSQL_CLIENT_CONNECT
echo "DROP TABLE IF EXISTS $t" | $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: 0 additions & 3 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,9 +18,6 @@ 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. 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.
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.

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

## Syntax

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

## Examples
Expand Down Expand Up @@ -50,10 +50,4 @@ 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: 5 additions & 1 deletion scripts/benchmark/query/load/hits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ select version();
SQL

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

cat <<SQL | bendsql
VACUUM DROP TABLE retain 0 hours;
SQL

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

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

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

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

Ok(())
}
Expand Down
12 changes: 5 additions & 7 deletions src/query/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,15 +577,14 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
);
let drop_table = map(
rule! {
DROP ~ TABLE ~ ( IF ~ ^EXISTS )? ~ #dot_separated_idents_1_to_3 ~ ALL?
DROP ~ TABLE ~ ( IF ~ ^EXISTS )? ~ #dot_separated_idents_1_to_3
},
|(_, _, opt_if_exists, (catalog, database, table), opt_all)| {
|(_, _, opt_if_exists, (catalog, database, table))| {
Statement::DropTable(DropTableStmt {
if_exists: opt_if_exists.is_some(),
catalog,
database,
table,
all: opt_all.is_some(),
})
},
);
Expand Down Expand Up @@ -638,14 +637,13 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
);
let truncate_table = map(
rule! {
TRUNCATE ~ TABLE ~ #dot_separated_idents_1_to_3 ~ PURGE?
TRUNCATE ~ TABLE ~ #dot_separated_idents_1_to_3
},
|(_, _, (catalog, database, table), opt_purge)| {
|(_, _, (catalog, database, table))| {
Statement::TruncateTable(TruncateTableStmt {
catalog,
database,
table,
purge: opt_purge.is_some(),
})
},
);
Expand Down Expand Up @@ -1495,7 +1493,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> [PURGE]`"
| #truncate_table : "`TRUNCATE TABLE [<database>.]<table>`"
| #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 `PURGE`, `FORMAT`, or `;`
| ^ expected `FORMAT` or `;`


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


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

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

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

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

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

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

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

Expand All @@ -2432,7 +2425,6 @@ 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>, purge: bool) -> Result<()> {
let (_, _) = (ctx, purge);
async fn truncate(&self, ctx: Arc<dyn TableContext>) -> Result<()> {
let _ = ctx;
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 @@ -1085,8 +1085,8 @@ async fn test_fuzz_impl(format: &str) -> Result<()> {
}

// Clear data
execute_sql(fixture.ctx(), "DROP TABLE rt ALL").await?;
execute_sql(fixture.ctx(), "DROP TABLE t ALL").await?;
execute_sql(fixture.ctx(), "DROP TABLE rt").await?;
execute_sql(fixture.ctx(), "DROP TABLE t").await?;
}
}
Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ 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 @@ -38,10 +37,8 @@ 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: 0 additions & 10 deletions src/query/service/src/interpreters/interpreter_table_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

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 @@ -78,15 +77,6 @@ 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,7 +27,6 @@ use crate::sessions::TableContext;

pub struct TruncateTableInterpreter {
ctx: Arc<QueryContext>,
purge: bool,
table_name: String,
catalog_name: String,
database_name: String,
Expand All @@ -39,7 +38,6 @@ 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 @@ -50,7 +48,6 @@ 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 @@ -84,14 +81,13 @@ 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(), self.purge).await?;
table.truncate(self.ctx.clone()).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, false).await?;
fuse_table.truncate(ctx).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, // 0 segments
1, // 0 blocks
1, // 0 index
1, // 1 segments
1, // 1 blocks
1, // 1 index
Some(()),
None,
)
Expand Down
1 change: 0 additions & 1 deletion src/query/service/tests/it/storages/fuse/operations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ mod mutation;
mod navigate;
mod optimize;
mod purge_drop;
mod purge_truncate;
mod read_plan;
mod replace_into;
mod table_analyze;
Expand Down
Loading
Loading