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 8 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;
2 changes: 1 addition & 1 deletion benchmark/clickbench/hits/create.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TRANSIENT TABLE hits
CREATE TABLE hits
(
WatchID BIGINT NOT NULL,
JavaEnable SMALLINT NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion benchmark/clickbench/hits/create_local.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TRANSIENT TABLE hits
CREATE TABLE hits
(
WatchID BIGINT NOT NULL,
JavaEnable SMALLINT NOT NULL,
Expand Down
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
54 changes: 5 additions & 49 deletions docs/doc/14-sql-commands/00-ddl/20-table/10-ddl-create-table.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ Databend aims to be easy to use by design and does NOT require any of those oper
- [CREATE TABLE](#create-table): Creates a table from scratch.
- [CREATE TABLE ... LIKE](#create-table--like): Creates a table with the same column definitions as an existing one.
- [CREATE TABLE ... AS](#create-table--as): Creates a table and inserts data with the results of a SELECT query.
- [CREATE TRANSIENT TABLE](#create-transient-table): Creates a table without storing its historical data for Time Travel..
- [CREATE TABLE ... EXTERNAL_LOCATION](#create-table--external_location): Creates a table and specifies an S3 bucket for the data storage instead of the FUSE engine.

## CREATE TABLE

```sql
CREATE [TRANSIENT] TABLE [IF NOT EXISTS] [db.]table_name
CREATE TABLE [IF NOT EXISTS] [db.]table_name
(
<column_name> <data_type> [ NOT NULL | NULL] [ { DEFAULT <expr> }] [AS (<expr>) STORED | VIRTUAL],
<column_name> <data_type> [ NOT NULL | NULL] [ { DEFAULT <expr> }] [AS (<expr>) STORED | VIRTUAL],
Expand All @@ -54,14 +53,12 @@ CREATE TABLE [IF NOT EXISTS] [db.]table_name
LIKE [db.]origin_table_name
```

This command does not include any data or attributes (such as `CLUSTER BY`, `TRANSIENT`, and `COMPRESSION`) from the original table, and instead creates a new table using the default system settings.
This command does not include any data or attributes (such as `CLUSTER BY`, `COMPRESSION`) from the original table, and instead creates a new table using the default system settings.

:::note WORKAROUND
- `TRANSIENT` and `COMPRESSION` can be explicitly specified when you create a new table with this command. For example,
- `COMPRESSION` can be explicitly specified when you create a new table with this command. For example,

```sql
create transient table t_new like t_old;

create table t_new compression='lz4' like t_old;
```
:::
Expand All @@ -76,31 +73,16 @@ CREATE TABLE [IF NOT EXISTS] [db.]table_name
AS SELECT query
```

This command does not include any attributes (such as CLUSTER BY, TRANSIENT, and COMPRESSION) from the original table, and instead creates a new table using the default system settings.
This command does not include any attributes (such as CLUSTER BY, COMPRESSION) from the original table, and instead creates a new table using the default system settings.

:::note WORKAROUND
- `TRANSIENT` and `COMPRESSION` can be explicitly specified when you create a new table with this command. For example,
- `COMPRESSION` can be explicitly specified when you create a new table with this command. For example,

```sql
create transient table t_new as select * from t_old;

create table t_new compression='lz4' as select * from t_old;
```
:::

## CREATE TRANSIENT TABLE

Creates a transient table.

Transient tables are used to hold transitory data that does not require a data protection or recovery mechanism. Dataebend does not hold historical data for a transient table so you will not be able to query from a previous version of the transient table with the Time Travel feature, for example, the [AT](./../../20-query-syntax/03-query-at.md) clause in the SELECT statement will not work for transient tables. Please note that you can still [drop](./20-ddl-drop-table.md) and [undrop](./21-ddl-undrop-table.md) a transient table.

Transient tables help save your storage expenses because they do not need extra space for historical data compared to non-transient tables. See [example](#create-transient-table-1) for detailed explanations.

Syntax:
```sql
CREATE TRANSIENT TABLE ...
```

## CREATE TABLE ... EXTERNAL_LOCATION

Creates a table and specifies an S3 bucket for the data storage instead of the FUSE engine.
Expand Down Expand Up @@ -349,32 +331,6 @@ SELECT * FROM books_backup;
+----+----------------+---------+
```

### Create Transient Table

Create a transient table (temporary table) that automatically deletes data after a specified period of time:

```sql
-- Create a transient table
CREATE TRANSIENT TABLE visits (
visitor_id BIGINT
);

-- Insert values
INSERT INTO visits VALUES(1);
INSERT INTO visits VALUES(2);
INSERT INTO visits VALUES(3);

-- Check the inserted data
SELECT * FROM visits;
+-----------+
| visitor_id |
+-----------+
| 1 |
| 2 |
| 3 |
+-----------+
```

### Create Table ... External_Location

Create a table with data stored on an external location, such as Amazon S3:
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)
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The capability to flash back a table is subject to these conditions:

- The command only existing tables to their prior states. To recover a dropped table, use [UNDROP TABLE](21-ddl-undrop-table.md).

- Flashback a table is part of Databend's time travel feature. Before using the command, make sure the table you want to flashback is eligible for time travel. For example, the command doesn't work for transient tables because Databend does not create or store snapshots for such tables.
- Flashback a table is part of Databend's time travel feature. Before using the command, make sure the table you want to flashback is eligible for time travel.

- You cannot roll back after flashback a table to a prior state, but you can flash back the table again to an earlier state.

Expand Down
8 changes: 6 additions & 2 deletions scripts/benchmark/query/load/hits.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ select version();
SQL

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

cat <<SQL | bendsql
CREATE TRANSIENT TABLE hits (
VACUUM DROP TABLE retain 0 hours;
SQL

cat <<SQL | bendsql
CREATE TABLE hits (
WatchID BIGINT NOT NULL,
JavaEnable SMALLINT NOT NULL,
Title TEXT NOT NULL,
Expand Down
8 changes: 1 addition & 7 deletions src/query/ast/src/ast/format/syntax/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ use crate::ast::CreateViewStmt;
use crate::ast::TimeTravelPoint;

pub(crate) fn pretty_create_table(stmt: CreateTableStmt) -> RcDoc<'static> {
RcDoc::text("CREATE")
.append(if stmt.transient {
RcDoc::space().append(RcDoc::text("TRANSIENT"))
} else {
RcDoc::nil()
})
.append(RcDoc::space().append(RcDoc::text("TABLE")))
RcDoc::text("CREATE TABLE")
.append(if stmt.if_not_exists {
RcDoc::space().append(RcDoc::text("IF NOT EXISTS"))
} else {
Expand Down
15 changes: 1 addition & 14 deletions src/query/ast/src/ast/statements/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,11 @@ pub struct CreateTableStmt {
pub cluster_by: Vec<Expr>,
pub table_options: BTreeMap<String, String>,
pub as_query: Option<Box<Query>>,
pub transient: bool,
}

impl Display for CreateTableStmt {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "CREATE ")?;
if self.transient {
write!(f, "TRANSIENT ")?;
}
write!(f, "TABLE ")?;
write!(f, "CREATE TABLE ")?;
if self.if_not_exists {
write!(f, "IF NOT EXISTS ")?;
}
Expand Down Expand Up @@ -256,7 +251,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 +266,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 +460,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 +472,6 @@ impl Display for TruncateTableStmt {
.chain(&self.database)
.chain(Some(&self.table)),
)?;
if self.purge {
write!(f, " PURGE")?;
}

Ok(())
}
Expand Down
16 changes: 6 additions & 10 deletions src/query/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
);
let create_table = map(
rule! {
CREATE ~ TRANSIENT? ~ TABLE ~ ( IF ~ ^NOT ~ ^EXISTS )?
CREATE ~ TABLE ~ ( IF ~ ^NOT ~ ^EXISTS )?
~ #dot_separated_idents_1_to_3
~ #create_table_source?
~ ( #engine )?
Expand All @@ -547,7 +547,6 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
},
|(
_,
opt_transient,
_,
opt_if_not_exists,
(catalog, database, table),
Expand All @@ -571,21 +570,19 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
.unwrap_or_default(),
table_options: opt_table_options.unwrap_or_default(),
as_query: opt_as_query.map(|(_, query)| Box::new(query)),
transient: opt_transient.is_some(),
})
},
);
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 +635,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 +1491,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
2 changes: 0 additions & 2 deletions src/query/ast/src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,6 @@ pub enum TokenKind {
TOKEN,
#[token("TRAILING", ignore(ascii_case))]
TRAILING,
#[token("TRANSIENT", ignore(ascii_case))]
TRANSIENT,
#[token("TRIM", ignore(ascii_case))]
TRIM,
#[token("TRUE", ignore(ascii_case))]
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
Loading
Loading