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