Skip to content

Commit 66c5f2e

Browse files
authored
feat: support temporary table (#16250)
* create temporary table * create as select * fix create as select * rename table * get_table_meta_by_id * get_table_name_by_id * not support stream on temp table * fix get_table_name_by_id * add comment * check access * forbid grant and revoke * get table * not support inverted index * not support virtual column * not support agg index * not support data mask * , * drop table by id * refactor * update table meta * fix get table meta by id * get_table * fix missing header * add create table test * rename folder * commit before refactor * refactor * make lint * regenerate golden file * fix catalog and add drop table test * fix catalog * add alter temp table test * fix show create table * add rename temp table test * add truncate test * add copy into test * fix copied file * add log * add some check * add drop_all_temp_tables() * refine error message * forbid alter temp prefix * remove useless println * adjust slt * add privilege test * fix * add assert * change print to log * add temp table storage prefix * make lint * fix prefix
1 parent 14053ee commit 66c5f2e

File tree

90 files changed

+2388
-315
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+2388
-315
lines changed

Cargo.lock

Lines changed: 22 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ members = [
5252
"src/query/storages/common/io",
5353
"src/query/storages/common/pruner",
5454
"src/query/storages/common/stage",
55-
"src/query/storages/common/txn",
55+
"src/query/storages/common/session",
5656
"src/query/storages/common/table_meta",
5757
"src/query/storages/delta",
5858
"src/query/storages/factory",
@@ -197,9 +197,9 @@ databend-storages-common-cache = { path = "src/query/storages/common/cache" }
197197
databend-storages-common-index = { path = "src/query/storages/common/index" }
198198
databend-storages-common-io = { path = "src/query/storages/common/io" }
199199
databend-storages-common-pruner = { path = "src/query/storages/common/pruner" }
200+
databend-storages-common-session = { path = "src/query/storages/common/session" }
200201
databend-storages-common-stage = { path = "src/query/storages/common/stage" }
201202
databend-storages-common-table-meta = { path = "src/query/storages/common/table_meta" }
202-
databend-storages-common-txn = { path = "src/query/storages/common/txn" }
203203

204204
# Crates.io dependencies
205205
anyerror = { version = "=0.1.10" }

scripts/ci/ci-run-sqllogic-tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ fi
1919
echo "Run suites using argument: $RUN_DIR"
2020

2121
echo "Starting databend-sqllogic tests"
22-
target/${BUILD_PROFILE}/databend-sqllogictests --handlers ${TEST_HANDLERS} ${RUN_DIR} --skip_dir management,explain_native,ee --enable_sandbox --parallel 8
22+
target/${BUILD_PROFILE}/databend-sqllogictests --handlers "mysql" --run_dir temp_table --enable_sandbox --parallel 8
23+
target/${BUILD_PROFILE}/databend-sqllogictests --handlers ${TEST_HANDLERS} ${RUN_DIR} --skip_dir management,explain_native,ee,temp_table --enable_sandbox --parallel 8

src/meta/api/src/schema_api_impl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,7 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
30163016
copied_files,
30173017
update_stream_metas,
30183018
deduplicated_labels,
3019+
update_temp_tables: _,
30193020
} = req;
30203021

30213022
let mut tbl_seqs = HashMap::new();

src/meta/app/src/schema/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pub use table::UpdateMultiTableMetaResult;
136136
pub use table::UpdateStreamMetaReq;
137137
pub use table::UpdateTableMetaReply;
138138
pub use table::UpdateTableMetaReq;
139+
pub use table::UpdateTempTableReq;
139140
pub use table::UpsertTableCopiedFileReply;
140141
pub use table::UpsertTableCopiedFileReq;
141142
pub use table::UpsertTableOptionReply;

src/meta/app/src/schema/table.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Display for TableIdent {
7272
}
7373
}
7474

75-
#[derive(Clone, Debug, Eq, PartialEq)]
75+
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
7676
pub struct TableNameIdent {
7777
pub tenant: Tenant,
7878
pub db_name: String,
@@ -188,6 +188,9 @@ impl Display for DatabaseType {
188188

189189
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Eq, PartialEq, Default)]
190190
pub struct TableInfo {
191+
/// For a temp table,
192+
/// `ident.seq` is always 0.
193+
/// `id.table_id` is set as value of `TempTblId`.
191194
pub ident: TableIdent,
192195

193196
/// For a table it is `db_name.table_name`.
@@ -581,7 +584,7 @@ impl Display for DropTableByIdReq {
581584
}
582585
}
583586

584-
#[derive(Clone, Debug, PartialEq, Eq)]
587+
#[derive(Clone, Debug, PartialEq, Eq, Default)]
585588
pub struct DropTableReply {
586589
// db id, share spec vector
587590
pub spec_vec: Option<(u64, Vec<ShareSpec>)>,
@@ -720,12 +723,31 @@ pub struct UpdateTableMetaReq {
720723
pub new_table_meta: TableMeta,
721724
}
722725

726+
#[derive(Clone, Debug, PartialEq, Eq)]
727+
pub struct UpdateTempTableReq {
728+
pub table_id: u64,
729+
pub desc: String,
730+
pub new_table_meta: TableMeta,
731+
pub copied_files: BTreeMap<String, TableCopiedFileInfo>,
732+
}
733+
723734
#[derive(Clone, Debug, PartialEq, Eq, Default)]
724735
pub struct UpdateMultiTableMetaReq {
725736
pub update_table_metas: Vec<(UpdateTableMetaReq, TableInfo)>,
726737
pub copied_files: Vec<(u64, UpsertTableCopiedFileReq)>,
727738
pub update_stream_metas: Vec<UpdateStreamMetaReq>,
728739
pub deduplicated_labels: Vec<String>,
740+
pub update_temp_tables: Vec<UpdateTempTableReq>,
741+
}
742+
743+
impl UpdateMultiTableMetaReq {
744+
pub fn is_empty(&self) -> bool {
745+
self.update_table_metas.is_empty()
746+
&& self.copied_files.is_empty()
747+
&& self.update_stream_metas.is_empty()
748+
&& self.deduplicated_labels.is_empty()
749+
&& self.update_temp_tables.is_empty()
750+
}
729751
}
730752

731753
/// The result of updating multiple table meta

src/query/ast/src/ast/format/syntax/ddl.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::ast::CreateStreamStmt;
3131
use crate::ast::CreateTableSource;
3232
use crate::ast::CreateTableStmt;
3333
use crate::ast::CreateViewStmt;
34+
use crate::ast::TableType;
3435
use crate::ast::TimeTravelPoint;
3536

3637
pub(crate) fn pretty_create_table(stmt: CreateTableStmt) -> RcDoc<'static> {
@@ -40,10 +41,10 @@ pub(crate) fn pretty_create_table(stmt: CreateTableStmt) -> RcDoc<'static> {
4041
} else {
4142
RcDoc::nil()
4243
})
43-
.append(if stmt.transient {
44-
RcDoc::space().append(RcDoc::text("TRANSIENT"))
45-
} else {
46-
RcDoc::nil()
44+
.append(match stmt.table_type {
45+
TableType::Transient => RcDoc::space().append(RcDoc::text("TRANSIENT")),
46+
TableType::Temporary => RcDoc::space().append(RcDoc::text("TEMPORARY")),
47+
TableType::Normal => RcDoc::nil(),
4748
})
4849
.append(RcDoc::space().append(RcDoc::text("TABLE")))
4950
.append(match stmt.create_option {

src/query/ast/src/ast/statements/table.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,14 @@ pub struct CreateTableStmt {
140140
pub cluster_by: Vec<Expr>,
141141
pub table_options: BTreeMap<String, String>,
142142
pub as_query: Option<Box<Query>>,
143-
pub transient: bool,
143+
pub table_type: TableType,
144+
}
145+
146+
#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
147+
pub enum TableType {
148+
Normal,
149+
Transient,
150+
Temporary,
144151
}
145152

146153
impl Display for CreateTableStmt {
@@ -149,9 +156,11 @@ impl Display for CreateTableStmt {
149156
if let CreateOption::CreateOrReplace = self.create_option {
150157
write!(f, "OR REPLACE ")?;
151158
}
152-
if self.transient {
153-
write!(f, "TRANSIENT ")?;
154-
}
159+
match self.table_type {
160+
TableType::Normal => {}
161+
TableType::Transient => write!(f, "TRANSIENT ")?,
162+
TableType::Temporary => write!(f, "TEMPORARY ")?,
163+
};
155164
write!(f, "TABLE ")?;
156165
if let CreateOption::CreateIfNotExists = self.create_option {
157166
write!(f, "IF NOT EXISTS ")?;

src/query/ast/src/parser/statement.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
697697
);
698698
let create_table = map_res(
699699
rule! {
700-
CREATE ~ ( OR ~ ^REPLACE )? ~ TRANSIENT? ~ TABLE ~ ( IF ~ ^NOT ~ ^EXISTS )?
700+
CREATE ~ ( OR ~ ^REPLACE )? ~ (TEMP| TEMPORARY|TRANSIENT)? ~ TABLE ~ ( IF ~ ^NOT ~ ^EXISTS )?
701701
~ #dot_separated_idents_1_to_3
702702
~ #create_table_source?
703703
~ ( #engine )?
@@ -709,7 +709,7 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
709709
|(
710710
_,
711711
opt_or_replace,
712-
opt_transient,
712+
opt_type,
713713
_,
714714
opt_if_not_exists,
715715
(catalog, database, table),
@@ -722,6 +722,12 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
722722
)| {
723723
let create_option =
724724
parse_create_option(opt_or_replace.is_some(), opt_if_not_exists.is_some())?;
725+
let table_type = match opt_type.map(|t| t.kind) {
726+
None => TableType::Normal,
727+
Some(TRANSIENT) => TableType::Transient,
728+
Some(TEMP) | Some(TEMPORARY) => TableType::Temporary,
729+
_ => unreachable!(),
730+
};
725731
Ok(Statement::CreateTable(CreateTableStmt {
726732
create_option,
727733
catalog,
@@ -735,7 +741,7 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
735741
.unwrap_or_default(),
736742
table_options: opt_table_options.unwrap_or_default(),
737743
as_query: opt_as_query.map(|(_, query)| Box::new(query)),
738-
transient: opt_transient.is_some(),
744+
table_type,
739745
}))
740746
},
741747
);

src/query/ast/src/parser/token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,6 +1305,8 @@ pub enum TokenKind {
13051305
ROLLBACK,
13061306
#[token("TEMPORARY", ignore(ascii_case))]
13071307
TEMPORARY,
1308+
#[token("TEMP", ignore(ascii_case))]
1309+
TEMP,
13081310
#[token("SECONDS", ignore(ascii_case))]
13091311
SECONDS,
13101312
#[token("DAYS", ignore(ascii_case))]

0 commit comments

Comments
 (0)