catalog/
lib.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![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    // TODO(j0hn50n133): re-export to make it compatible with the legacy code, migrate to the new path later
41    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    /// Returns the table by catalog, schema and table name.
84    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    /// Returns the tables by table ids.
93    async fn tables_by_ids(
94        &self,
95        catalog: &str,
96        schema: &str,
97        table_ids: &[TableId],
98    ) -> Result<Vec<TableRef>>;
99
100    /// Returns all tables with a stream by catalog and schema.
101    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    /// Check if `schema` is a reserved schema name
109    fn is_reserved_schema_name(&self, schema: &str) -> bool {
110        // We have to check whether a schema name is reserved before create schema.
111        // We need this rather than use schema_exists directly because `pg_catalog` is
112        // only visible via postgres protocol. So if we don't check, a mysql client may
113        // create a schema named `pg_catalog` which is somehow malformed.
114        schema == INFORMATION_SCHEMA_NAME || schema == PG_CATALOG_NAME
115    }
116}
117
118pub type CatalogManagerRef = Arc<dyn CatalogManager>;
119
120/// Hook called after system table opening.
121pub type OpenSystemTableHook =
122    Box<dyn Fn(TableRef) -> BoxFuture<'static, Result<()>> + Send + Sync>;
123
124/// Register system table request:
125/// - When system table is already created and registered, the hook will be called
126///     with table ref after opening the system table
127/// - When system table is not exists, create and register the table by `create_table_expr` and calls `open_hook` with the created table.
128pub 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}