From b4ced32415b68bc393ca4a1dbecc3ad0ec0e873c Mon Sep 17 00:00:00 2001 From: xudong963 Date: Mon, 25 Nov 2024 19:26:32 +0800 Subject: [PATCH] clear cache --- src/query/catalog/src/table_context.rs | 2 ++ src/query/service/src/sessions/query_ctx.rs | 5 +++++ src/query/service/src/sessions/query_ctx_shared.rs | 10 ++++++++++ .../service/tests/it/sql/exec/get_table_bind_test.rs | 4 ++++ .../tests/it/storages/fuse/operations/commit.rs | 4 ++++ src/query/sql/src/planner/binder/bind_query/bind.rs | 8 +++++--- 6 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/query/catalog/src/table_context.rs b/src/query/catalog/src/table_context.rs index c7b9a05946fb..69d4ff3be489 100644 --- a/src/query/catalog/src/table_context.rs +++ b/src/query/catalog/src/table_context.rs @@ -261,6 +261,8 @@ pub trait TableContext: Send + Sync { async fn get_table(&self, catalog: &str, database: &str, table: &str) -> Result>; + fn remove_table_from_cache(&self, catalog: &str, database: &str, table: &str); + async fn get_table_with_batch( &self, catalog: &str, diff --git a/src/query/service/src/sessions/query_ctx.rs b/src/query/service/src/sessions/query_ctx.rs index 4e17bd5063f4..4055fe53b929 100644 --- a/src/query/service/src/sessions/query_ctx.rs +++ b/src/query/service/src/sessions/query_ctx.rs @@ -957,6 +957,11 @@ impl TableContext for QueryContext { .await } + fn remove_table_from_cache(&self, catalog: &str, database: &str, table: &str) { + self.shared + .remove_table_from_cache(catalog, database, table) + } + #[async_backtrace::framed] async fn get_table_with_batch( &self, diff --git a/src/query/service/src/sessions/query_ctx_shared.rs b/src/query/service/src/sessions/query_ctx_shared.rs index 18bbe3f1c22a..f119eccc38bd 100644 --- a/src/query/service/src/sessions/query_ctx_shared.rs +++ b/src/query/service/src/sessions/query_ctx_shared.rs @@ -353,6 +353,16 @@ impl QueryContextShared { Ok(res) } + pub fn remove_table_from_cache(&self, catalog_name: &str, database: &str, table: &str) { + let table_meta_key = ( + catalog_name.to_string(), + database.to_string(), + table.to_string(), + ); + let mut tables_refs = self.tables_refs.lock(); + tables_refs.remove(&table_meta_key); + } + #[async_backtrace::framed] async fn get_table_to_cache( &self, diff --git a/src/query/service/tests/it/sql/exec/get_table_bind_test.rs b/src/query/service/tests/it/sql/exec/get_table_bind_test.rs index 89ff1fc3aa23..d07e1e81b0d9 100644 --- a/src/query/service/tests/it/sql/exec/get_table_bind_test.rs +++ b/src/query/service/tests/it/sql/exec/get_table_bind_test.rs @@ -818,6 +818,10 @@ impl TableContext for CtxDelegation { } } + fn remove_table_from_cache(&self, _catalog: &str, _database: &str, _table: &str) { + todo!() + } + async fn get_table_with_batch( &self, catalog: &str, diff --git a/src/query/service/tests/it/storages/fuse/operations/commit.rs b/src/query/service/tests/it/storages/fuse/operations/commit.rs index efc889f29843..c23ba39e7930 100644 --- a/src/query/service/tests/it/storages/fuse/operations/commit.rs +++ b/src/query/service/tests/it/storages/fuse/operations/commit.rs @@ -697,6 +697,10 @@ impl TableContext for CtxDelegation { todo!() } + fn remove_table_from_cache(&self, _catalog: &str, _database: &str, _table: &str) { + todo!() + } + async fn get_table_with_batch( &self, _catalog: &str, diff --git a/src/query/sql/src/planner/binder/bind_query/bind.rs b/src/query/sql/src/planner/binder/bind_query/bind.rs index 62ac440ffb9a..b4f7c45ef4a0 100644 --- a/src/query/sql/src/planner/binder/bind_query/bind.rs +++ b/src/query/sql/src/planner/binder/bind_query/bind.rs @@ -184,10 +184,11 @@ impl Binder { Engine::Memory }; let database = self.ctx.get_current_database(); + let catalog = self.ctx.get_current_catalog(); let create_table_stmt = CreateTableStmt { create_option: CreateOption::CreateOrReplace, - catalog: None, - database: Some(Identifier::from_name(Span::None, database)), + catalog: Some(Identifier::from_name(Span::None, catalog.clone())), + database: Some(Identifier::from_name(Span::None, database.clone())), table: cte.alias.name.clone(), source: None, engine: Some(engine), @@ -210,7 +211,8 @@ impl Binder { }; // Todo: clear the table with the same name in table cache - + self.ctx + .remove_table_from_cache(&catalog, &database, &cte.alias.name.name); Ok(()) } }