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