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::metadata::TableId;
28use table::TableRef;
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 table_source;
44
45#[async_trait::async_trait]
46pub trait CatalogManager: Send + Sync {
47 fn as_any(&self) -> &dyn Any;
48
49 async fn catalog_names(&self) -> Result<Vec<String>>;
50
51 async fn schema_names(
52 &self,
53 catalog: &str,
54 query_ctx: Option<&QueryContext>,
55 ) -> Result<Vec<String>>;
56
57 async fn table_names(
58 &self,
59 catalog: &str,
60 schema: &str,
61 query_ctx: Option<&QueryContext>,
62 ) -> Result<Vec<String>>;
63
64 async fn catalog_exists(&self, catalog: &str) -> Result<bool>;
65
66 async fn schema_exists(
67 &self,
68 catalog: &str,
69 schema: &str,
70 query_ctx: Option<&QueryContext>,
71 ) -> Result<bool>;
72
73 async fn table_exists(
74 &self,
75 catalog: &str,
76 schema: &str,
77 table: &str,
78 query_ctx: Option<&QueryContext>,
79 ) -> Result<bool>;
80
81 async fn table(
83 &self,
84 catalog: &str,
85 schema: &str,
86 table_name: &str,
87 query_ctx: Option<&QueryContext>,
88 ) -> Result<Option<TableRef>>;
89
90 async fn tables_by_ids(
92 &self,
93 catalog: &str,
94 schema: &str,
95 table_ids: &[TableId],
96 ) -> Result<Vec<TableRef>>;
97
98 fn tables<'a>(
100 &'a self,
101 catalog: &'a str,
102 schema: &'a str,
103 query_ctx: Option<&'a QueryContext>,
104 ) -> BoxStream<'a, Result<TableRef>>;
105
106 fn is_reserved_schema_name(&self, schema: &str) -> bool {
108 schema == INFORMATION_SCHEMA_NAME || schema == PG_CATALOG_NAME
113 }
114}
115
116pub type CatalogManagerRef = Arc<dyn CatalogManager>;
117
118pub type OpenSystemTableHook =
120 Box<dyn Fn(TableRef) -> BoxFuture<'static, Result<()>> + Send + Sync>;
121
122pub struct RegisterSystemTableRequest {
127 pub create_table_expr: CreateTableExpr,
128 pub open_hook: Option<OpenSystemTableHook>,
129}
130
131#[derive(Clone)]
132pub struct RegisterTableRequest {
133 pub catalog: String,
134 pub schema: String,
135 pub table_name: String,
136 pub table_id: TableId,
137 pub table: TableRef,
138}
139
140impl Debug for RegisterTableRequest {
141 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
142 f.debug_struct("RegisterTableRequest")
143 .field("catalog", &self.catalog)
144 .field("schema", &self.schema)
145 .field("table_name", &self.table_name)
146 .field("table_id", &self.table_id)
147 .field("table", &self.table.table_info())
148 .finish()
149 }
150}
151
152#[derive(Debug, Clone)]
153pub struct RenameTableRequest {
154 pub catalog: String,
155 pub schema: String,
156 pub table_name: String,
157 pub new_table_name: String,
158 pub table_id: TableId,
159}
160
161#[derive(Debug, Clone)]
162pub struct DeregisterTableRequest {
163 pub catalog: String,
164 pub schema: String,
165 pub table_name: String,
166}
167
168#[derive(Debug, Clone)]
169pub struct DeregisterSchemaRequest {
170 pub catalog: String,
171 pub schema: String,
172}
173
174#[derive(Debug, Clone)]
175pub struct RegisterSchemaRequest {
176 pub catalog: String,
177 pub schema: String,
178}