Skip to content

Commit b1b9fc5

Browse files
authored
feat: support show temp tables from all sessions (#18183)
* feat: support show temp tables from all sessions * make lint * fix ut
1 parent a18d1f0 commit b1b9fc5

File tree

8 files changed

+66
-13
lines changed

8 files changed

+66
-13
lines changed

src/query/catalog/src/table_context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ pub trait TableContext: Send + Sync {
217217
fn get_current_session_id(&self) -> String {
218218
unimplemented!()
219219
}
220+
fn get_current_client_session_id(&self) -> Option<String> {
221+
unimplemented!()
222+
}
220223
async fn get_all_effective_roles(&self) -> Result<Vec<RoleInfo>>;
221224

222225
async fn validate_privilege(

src/query/service/src/databases/system/system_database.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ use databend_common_storages_system::TablesTableWithoutHistory;
6161
use databend_common_storages_system::TaskHistoryTable;
6262
use databend_common_storages_system::TasksTable;
6363
use databend_common_storages_system::TempFilesTable;
64-
use databend_common_storages_system::TemporaryTablesTable;
6564
use databend_common_storages_system::TerseStreamsTable;
6665
use databend_common_storages_system::UserFunctionsTable;
6766
use databend_common_storages_system::UsersTable;
@@ -77,6 +76,7 @@ use databend_common_version::VERGEN_CARGO_FEATURES;
7776

7877
use crate::catalogs::InMemoryMetas;
7978
use crate::databases::Database;
79+
use crate::table_functions::TemporaryTablesTable;
8080

8181
#[derive(Clone)]
8282
pub struct SystemDatabase {

src/query/service/src/servers/http/v1/session/client_session_manager.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,21 @@ impl ClientSessionManager {
438438
}
439439
Ok(false)
440440
}
441+
442+
/// Get all temporary tables from all sessions
443+
pub fn get_all_temp_tables(
444+
&self,
445+
) -> Result<Vec<(String, databend_common_meta_app::schema::TableInfo)>> {
446+
let mut all_temp_tables = Vec::new();
447+
let session_states = self.session_state.lock();
448+
for (session_key, session_state) in session_states.iter() {
449+
let temp_tables = session_state.temp_tbl_mgr.lock().list_tables()?;
450+
for table in temp_tables {
451+
all_temp_tables.push((session_key.clone(), table));
452+
}
453+
}
454+
Ok(all_temp_tables)
455+
}
441456
}
442457

443458
pub async fn drop_all_temp_tables_with_logging(

src/query/service/src/sessions/query_ctx.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,10 @@ impl TableContext for QueryContext {
900900
self.get_current_session().get_id()
901901
}
902902

903+
fn get_current_client_session_id(&self) -> Option<String> {
904+
self.get_current_session().get_client_session_id()
905+
}
906+
903907
async fn get_visibility_checker(
904908
&self,
905909
ignore_ownership: bool,

src/query/service/src/table_functions/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod sync_crash_me;
3030
mod system;
3131
mod table_function;
3232
mod table_function_factory;
33+
mod temporary_tables_table;
3334

3435
pub use numbers::generate_numbers_parts;
3536
pub use numbers::NumbersPartInfo;
@@ -42,3 +43,4 @@ pub use system::get_fuse_table_statistics;
4243
pub use system::TableStatisticsFunc;
4344
pub use table_function::TableFunction;
4445
pub use table_function_factory::TableFunctionFactory;
46+
pub use temporary_tables_table::TemporaryTablesTable;

src/query/storages/system/src/temporary_tables_table.rs renamed to src/query/service/src/table_functions/temporary_tables_table.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::sync::Arc;
1717
use databend_common_catalog::table::Table;
1818
use databend_common_catalog::table_context::TableContext;
1919
use databend_common_exception::Result;
20+
use databend_common_expression::types::BooleanType;
2021
use databend_common_expression::types::NumberDataType;
2122
use databend_common_expression::types::StringType;
2223
use databend_common_expression::types::UInt64Type;
@@ -28,9 +29,10 @@ use databend_common_expression::TableSchemaRefExt;
2829
use databend_common_meta_app::schema::TableIdent;
2930
use databend_common_meta_app::schema::TableInfo;
3031
use databend_common_meta_app::schema::TableMeta;
32+
use databend_common_storages_system::SyncOneBlockSystemTable;
33+
use databend_common_storages_system::SyncSystemTable;
3134

32-
use crate::SyncOneBlockSystemTable;
33-
use crate::SyncSystemTable;
35+
use crate::servers::http::v1::ClientSessionManager;
3436

3537
pub struct TemporaryTablesTable {
3638
table_info: TableInfo,
@@ -44,13 +46,22 @@ impl SyncSystemTable for TemporaryTablesTable {
4446
}
4547

4648
fn get_full_data(&self, ctx: Arc<dyn TableContext>) -> Result<DataBlock> {
47-
let catalog = ctx.get_default_catalog()?;
48-
let tables = catalog.list_temporary_tables()?;
49-
let mut dbs = Vec::with_capacity(tables.len());
50-
let mut names = Vec::with_capacity(tables.len());
51-
let mut table_ids = Vec::with_capacity(tables.len());
52-
let mut engines = Vec::with_capacity(tables.len());
53-
for table in tables {
49+
let mut dbs = Vec::new();
50+
let mut names = Vec::new();
51+
let mut table_ids = Vec::new();
52+
let mut engines = Vec::new();
53+
let mut users = Vec::new();
54+
let mut session_ids = Vec::new();
55+
let mut is_current_sessions = Vec::new();
56+
57+
let client_session_manager = ClientSessionManager::instance();
58+
let all_temp_tables = client_session_manager.get_all_temp_tables()?;
59+
60+
let current_session_id = ctx.get_current_client_session_id();
61+
log::info!("current_session_id: {:?}", current_session_id);
62+
63+
for (session_key, table) in all_temp_tables {
64+
log::info!("session_key: {:?}", session_key);
5465
let desc = table.desc;
5566
let db_name = desc
5667
.split('.')
@@ -63,16 +74,30 @@ impl SyncSystemTable for TemporaryTablesTable {
6374
}
6475
})
6576
.ok_or_else(|| format!("Invalid table desc: {}", desc))?;
77+
78+
let user = session_key.split('/').next().unwrap().to_string();
79+
let session_id = session_key.split('/').nth(1).unwrap().to_string();
80+
let is_current_session = current_session_id
81+
.as_ref()
82+
.map(|id| id == &session_id)
83+
.unwrap_or(false);
6684
dbs.push(db_name.to_string());
6785
names.push(table.name);
6886
table_ids.push(table.ident.table_id);
69-
engines.push(table.meta.engine.clone());
87+
engines.push(table.meta.engine);
88+
users.push(user);
89+
session_ids.push(session_id);
90+
is_current_sessions.push(is_current_session);
7091
}
92+
7193
Ok(DataBlock::new_from_columns(vec![
7294
StringType::from_data(dbs),
7395
StringType::from_data(names),
7496
UInt64Type::from_data(table_ids),
7597
StringType::from_data(engines),
98+
StringType::from_data(users),
99+
StringType::from_data(session_ids),
100+
BooleanType::from_data(is_current_sessions),
76101
]))
77102
}
78103
}
@@ -84,6 +109,9 @@ impl TemporaryTablesTable {
84109
TableField::new("name", TableDataType::String),
85110
TableField::new("table_id", TableDataType::Number(NumberDataType::UInt64)),
86111
TableField::new("engine", TableDataType::String),
112+
TableField::new("user", TableDataType::String),
113+
TableField::new("session_id", TableDataType::String),
114+
TableField::new("is_current_session", TableDataType::Boolean),
87115
]);
88116
let table_info = TableInfo {
89117
ident: TableIdent::new(table_id, 0),

src/query/service/tests/it/storages/testdata/columns_table.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ DB.Table: 'system'.'columns', Table: columns-table_id:1, ver:0, Engine: SystemCo
201201
| 'is_attach' | 'system' | 'tables' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' |
202202
| 'is_attach' | 'system' | 'tables_with_history' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' |
203203
| 'is_configured' | 'system' | 'users' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' |
204+
| 'is_current_session' | 'system' | 'temporary_tables' | 'Boolean' | 'BOOLEAN' | '' | '' | 'NO' | '' |
204205
| 'is_external' | 'system' | 'tables' | 'Boolean' | 'BOOLEAN' | '' | '' | 'NO' | '' |
205206
| 'is_external' | 'system' | 'tables_with_history' | 'Boolean' | 'BOOLEAN' | '' | '' | 'NO' | '' |
206207
| 'is_insertable_into' | 'information_schema' | 'views' | 'Boolean' | 'BOOLEAN' | '' | '' | 'NO' | '' |
@@ -331,6 +332,7 @@ DB.Table: 'system'.'columns', Table: columns-table_id:1, ver:0, Engine: SystemCo
331332
| 'schema_name' | 'information_schema' | 'schemata' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' |
332333
| 'schema_owner' | 'information_schema' | 'schemata' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' |
333334
| 'seq_in_index' | 'information_schema' | 'statistics' | 'NULL' | 'NULL' | '' | '' | 'NO' | '' |
335+
| 'session_id' | 'system' | 'temporary_tables' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' |
334336
| 'session_parameters' | 'system' | 'task_history' | 'Nullable(Variant)' | 'VARIANT' | '' | '' | 'YES' | '' |
335337
| 'session_parameters' | 'system' | 'tasks' | 'Nullable(Variant)' | 'VARIANT' | '' | '' | 'YES' | '' |
336338
| 'size' | 'system' | 'caches' | 'UInt64' | 'BIGINT UNSIGNED' | '' | '' | 'NO' | '' |
@@ -415,6 +417,7 @@ DB.Table: 'system'.'columns', Table: columns-table_id:1, ver:0, Engine: SystemCo
415417
| 'updated_on' | 'system' | 'views_with_history' | 'Timestamp' | 'TIMESTAMP' | '' | '' | 'NO' | '' |
416418
| 'user' | 'system' | 'locks' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' |
417419
| 'user' | 'system' | 'processes' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' |
420+
| 'user' | 'system' | 'temporary_tables' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' |
418421
| 'value' | 'system' | 'configs' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' |
419422
| 'value' | 'system' | 'malloc_stats_totals' | 'UInt64' | 'BIGINT UNSIGNED' | '' | '' | 'NO' | '' |
420423
| 'value' | 'system' | 'metrics' | 'String' | 'VARCHAR' | '' | '' | 'NO' | '' |

src/query/storages/system/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ mod tables_table;
6060
mod task_history_table;
6161
mod tasks_table;
6262
mod temp_files_table;
63-
mod temporary_tables_table;
6463
mod user_functions_table;
6564
mod users_table;
6665
mod util;
@@ -122,7 +121,6 @@ pub use task_history_table::TaskHistoryTable;
122121
pub use tasks_table::parse_tasks_to_datablock;
123122
pub use tasks_table::TasksTable;
124123
pub use temp_files_table::TempFilesTable;
125-
pub use temporary_tables_table::TemporaryTablesTable;
126124
pub use user_functions_table::UserFunctionsTable;
127125
pub use users_table::UsersTable;
128126
pub use util::generate_catalog_meta;

0 commit comments

Comments
 (0)