1use std::any::Any;
16use std::fmt::{Debug, Formatter};
17use std::sync::Arc;
18
19use api::v1::CreateTableExpr;
20use common_catalog::consts::system_schema_name;
21use futures::future::BoxFuture;
22use futures_util::stream::BoxStream;
23use session::context::QueryContext;
24use table::TableRef;
25use table::metadata::{TableId, TableInfoRef};
26
27use crate::error::Result;
28
29pub mod error;
30pub mod information_extension;
31pub mod kvbackend;
32#[cfg(any(test, feature = "testing"))]
33pub mod memory;
34mod metrics;
35pub mod system_schema;
36pub mod information_schema {
37 pub use crate::system_schema::information_schema::*;
39}
40
41pub mod process_manager;
42pub mod table_source;
43
44#[async_trait::async_trait]
45pub trait CatalogManager: Send + Sync {
46 fn as_any(&self) -> &dyn Any;
47
48 async fn catalog_names(&self) -> Result<Vec<String>>;
49
50 async fn schema_names(
51 &self,
52 catalog: &str,
53 query_ctx: Option<&QueryContext>,
54 ) -> Result<Vec<String>>;
55
56 async fn table_names(
57 &self,
58 catalog: &str,
59 schema: &str,
60 query_ctx: Option<&QueryContext>,
61 ) -> Result<Vec<String>>;
62
63 async fn catalog_exists(&self, catalog: &str) -> Result<bool>;
64
65 async fn schema_exists(
66 &self,
67 catalog: &str,
68 schema: &str,
69 query_ctx: Option<&QueryContext>,
70 ) -> Result<bool>;
71
72 async fn table_exists(
73 &self,
74 catalog: &str,
75 schema: &str,
76 table: &str,
77 query_ctx: Option<&QueryContext>,
78 ) -> Result<bool>;
79
80 async fn table(
82 &self,
83 catalog: &str,
84 schema: &str,
85 table_name: &str,
86 query_ctx: Option<&QueryContext>,
87 ) -> Result<Option<TableRef>>;
88
89 async fn table_id(
91 &self,
92 catalog: &str,
93 schema: &str,
94 table_name: &str,
95 query_ctx: Option<&QueryContext>,
96 ) -> Result<Option<TableId>> {
97 Ok(self
98 .table(catalog, schema, table_name, query_ctx)
99 .await?
100 .map(|t| t.table_info().ident.table_id))
101 }
102
103 async fn table_info_by_id(&self, table_id: TableId) -> Result<Option<TableInfoRef>>;
105
106 async fn tables_by_ids(
108 &self,
109 catalog: &str,
110 schema: &str,
111 table_ids: &[TableId],
112 ) -> Result<Vec<TableRef>>;
113
114 fn tables<'a>(
116 &'a self,
117 catalog: &'a str,
118 schema: &'a str,
119 query_ctx: Option<&'a QueryContext>,
120 ) -> BoxStream<'a, Result<TableRef>>;
121
122 fn is_reserved_schema_name(&self, schema: &str) -> bool {
124 system_schema_name(schema).is_some()
132 }
133}
134
135pub type CatalogManagerRef = Arc<dyn CatalogManager>;
136
137pub type OpenSystemTableHook =
139 Box<dyn Fn(TableRef) -> BoxFuture<'static, Result<()>> + Send + Sync>;
140
141pub struct RegisterSystemTableRequest {
146 pub create_table_expr: CreateTableExpr,
147 pub open_hook: Option<OpenSystemTableHook>,
148}
149
150#[derive(Clone)]
151pub struct RegisterTableRequest {
152 pub catalog: String,
153 pub schema: String,
154 pub table_name: String,
155 pub table_id: TableId,
156 pub table: TableRef,
157}
158
159impl Debug for RegisterTableRequest {
160 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
161 f.debug_struct("RegisterTableRequest")
162 .field("catalog", &self.catalog)
163 .field("schema", &self.schema)
164 .field("table_name", &self.table_name)
165 .field("table_id", &self.table_id)
166 .field("table", &self.table.table_info())
167 .finish()
168 }
169}
170
171#[derive(Debug, Clone)]
172pub struct RenameTableRequest {
173 pub catalog: String,
174 pub schema: String,
175 pub table_name: String,
176 pub new_table_name: String,
177 pub table_id: TableId,
178}
179
180#[derive(Debug, Clone)]
181pub struct DeregisterTableRequest {
182 pub catalog: String,
183 pub schema: String,
184 pub table_name: String,
185}
186
187#[derive(Debug, Clone)]
188pub struct DeregisterSchemaRequest {
189 pub catalog: String,
190 pub schema: String,
191}
192
193#[derive(Debug, Clone)]
194pub struct RegisterSchemaRequest {
195 pub catalog: String,
196 pub schema: String,
197}