Skip to content

Commit 932b30d

Browse files
authored
refactor: catalog crate (#331)
* chore: refactor dir for local catalog manager * refactor: CatalogProvider returns Result * refactor: SchemaProvider returns Result * feat: add kv operations to remote catalog * chore: refactor some code * feat: impl catalog initialization * feat: add register table and register system table function * refactor: add table_info method for Table trait * chore: add some tests * chore: add register schema test * chore: fix build issue after rebase onto develop * refactor: mock to separate file * build: failed to compile * fix: use a container struct to bridge KvBackend and Accessor trait * feat: upgrade opendal to 0.17 * test: add more tests * chore: add catalog name and schema name to table info * chore: add catalog name and schema name to table info * chore: rebase onto develop * refactor: common-catalog crate * refactor: remove remote catalog related files * fix: compilation * feat: add table version to TableKey * feat: add node id to TableValue * fix: some CR comments * chore: change async fn create_expr_to_request to sync * fix: add backtrace to errors * fix: code style * fix: CatalogManager::table also requires both catalog_name and schema_name * chore: merge develop
1 parent 7fe39e9 commit 932b30d

52 files changed

Lines changed: 919 additions & 314 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ members = [
55
"src/client",
66
"src/cmd",
77
"src/common/base",
8+
"src/common/catalog",
89
"src/common/error",
910
"src/common/function",
1011
"src/common/function-macro",

src/catalog/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ edition = "2021"
77
[dependencies]
88
async-stream = "0.3"
99
async-trait = "0.1"
10+
common-catalog = { path = "../common/catalog" }
1011
common-error = { path = "../common/error" }
1112
common-query = { path = "../common/query" }
1213
common-recordbatch = { path = "../common/recordbatch" }
14+
common-runtime = { path = "../common/runtime" }
1315
common-telemetry = { path = "../common/telemetry" }
1416
common-time = { path = "../common/time" }
1517
datafusion = { git = "https://github.com/apache/arrow-datafusion.git", branch = "arrow2", features = [
@@ -18,12 +20,18 @@ datafusion = { git = "https://github.com/apache/arrow-datafusion.git", branch =
1820
datatypes = { path = "../datatypes" }
1921
futures = "0.3"
2022
futures-util = "0.3"
23+
lazy_static = "1.4"
24+
opendal = "0.17"
25+
regex = "1.6"
2126
serde = "1.0"
2227
serde_json = "1.0"
2328
snafu = { version = "0.7", features = ["backtraces"] }
29+
storage = { path = "../storage" }
2430
table = { path = "../table" }
31+
tokio = { version = "1.18", features = ["full"] }
2532

2633
[dev-dependencies]
34+
chrono = "0.4"
2735
log-store = { path = "../log-store" }
2836
object-store = { path = "../object-store" }
2937
opendal = "0.17"

src/catalog/src/error.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ pub enum Error {
115115
#[snafu(backtrace)]
116116
source: common_query::error::Error,
117117
},
118+
#[snafu(display("Cannot parse catalog value, source: {}", source))]
119+
InvalidCatalogValue {
120+
#[snafu(backtrace)]
121+
source: common_catalog::error::Error,
122+
},
123+
124+
#[snafu(display("IO error occurred while fetching catalog info, source: {}", source))]
125+
Io {
126+
backtrace: Backtrace,
127+
source: std::io::Error,
128+
},
118129
}
119130

120131
pub type Result<T> = std::result::Result<T, Error>;
@@ -129,12 +140,14 @@ impl ErrorExt for Error {
129140
| Error::CatalogNotFound { .. }
130141
| Error::InvalidEntryType { .. } => StatusCode::Unexpected,
131142

132-
Error::SystemCatalog { .. } | Error::EmptyValue | Error::ValueDeserialize { .. } => {
133-
StatusCode::StorageUnavailable
134-
}
143+
Error::SystemCatalog { .. }
144+
| Error::EmptyValue
145+
| Error::ValueDeserialize { .. }
146+
| Error::Io { .. } => StatusCode::StorageUnavailable,
135147

136148
Error::ReadSystemCatalog { source, .. } => source.status_code(),
137149
Error::SystemCatalogTypeMismatch { source, .. } => source.status_code(),
150+
Error::InvalidCatalogValue { source, .. } => source.status_code(),
138151

139152
Error::RegisterTable { .. } => StatusCode::Internal,
140153
Error::TableExists { .. } => StatusCode::TableAlreadyExists,

src/catalog/src/lib.rs

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33
use std::any::Any;
44
use std::sync::Arc;
55

6+
use common_telemetry::info;
7+
use snafu::ResultExt;
8+
use table::engine::{EngineContext, TableEngineRef};
69
use table::metadata::TableId;
710
use table::requests::CreateTableRequest;
811
use table::TableRef;
912

10-
pub use crate::consts::{DEFAULT_CATALOG_NAME, DEFAULT_SCHEMA_NAME, MIN_USER_TABLE_ID};
11-
pub use crate::manager::LocalCatalogManager;
13+
use crate::error::{CreateTableSnafu, Result};
1214
pub use crate::schema::{SchemaProvider, SchemaProviderRef};
1315

14-
pub mod consts;
1516
pub mod error;
16-
mod manager;
17-
pub mod memory;
17+
pub mod local;
1818
pub mod schema;
1919
pub mod system;
2020
pub mod tables;
@@ -31,13 +31,13 @@ pub trait CatalogList: Sync + Send {
3131
&self,
3232
name: String,
3333
catalog: CatalogProviderRef,
34-
) -> Option<CatalogProviderRef>;
34+
) -> Result<Option<CatalogProviderRef>>;
3535

3636
/// Retrieves the list of available catalog names
37-
fn catalog_names(&self) -> Vec<String>;
37+
fn catalog_names(&self) -> Result<Vec<String>>;
3838

3939
/// Retrieves a specific catalog by name, provided it exists.
40-
fn catalog(&self, name: &str) -> Option<CatalogProviderRef>;
40+
fn catalog(&self, name: &str) -> Result<Option<CatalogProviderRef>>;
4141
}
4242

4343
/// Represents a catalog, comprising a number of named schemas.
@@ -47,14 +47,17 @@ pub trait CatalogProvider: Sync + Send {
4747
fn as_any(&self) -> &dyn Any;
4848

4949
/// Retrieves the list of available schema names in this catalog.
50-
fn schema_names(&self) -> Vec<String>;
50+
fn schema_names(&self) -> Result<Vec<String>>;
5151

5252
/// Registers schema to this catalog.
53-
fn register_schema(&self, name: String, schema: SchemaProviderRef)
54-
-> Option<SchemaProviderRef>;
53+
fn register_schema(
54+
&self,
55+
name: String,
56+
schema: SchemaProviderRef,
57+
) -> Result<Option<SchemaProviderRef>>;
5558

5659
/// Retrieves a specific schema from the catalog by name, provided it exists.
57-
fn schema(&self, name: &str) -> Option<SchemaProviderRef>;
60+
fn schema(&self, name: &str) -> Result<Option<SchemaProviderRef>>;
5861
}
5962

6063
pub type CatalogListRef = Arc<dyn CatalogList>;
@@ -79,8 +82,8 @@ pub trait CatalogManager: CatalogList {
7982
/// Returns the table by catalog, schema and table name.
8083
fn table(
8184
&self,
82-
catalog: Option<&str>,
83-
schema: Option<&str>,
85+
catalog: &str,
86+
schema: &str,
8487
table_name: &str,
8588
) -> error::Result<Option<TableRef>>;
8689
}
@@ -99,9 +102,10 @@ pub struct RegisterSystemTableRequest {
99102
pub open_hook: Option<OpenSystemTableHook>,
100103
}
101104

105+
#[derive(Clone)]
102106
pub struct RegisterTableRequest {
103-
pub catalog: Option<String>,
104-
pub schema: Option<String>,
107+
pub catalog: String,
108+
pub schema: String,
105109
pub table_name: String,
106110
pub table_id: TableId,
107111
pub table: TableRef,
@@ -111,3 +115,53 @@ pub struct RegisterTableRequest {
111115
pub fn format_full_table_name(catalog: &str, schema: &str, table: &str) -> String {
112116
format!("{}.{}.{}", catalog, schema, table)
113117
}
118+
119+
pub trait CatalogProviderFactory {
120+
fn create(&self, catalog_name: String) -> CatalogProviderRef;
121+
}
122+
123+
pub trait SchemaProviderFactory {
124+
fn create(&self, catalog_name: String, schema_name: String) -> SchemaProviderRef;
125+
}
126+
127+
pub(crate) async fn handle_system_table_request<'a, M: CatalogManager>(
128+
manager: &'a M,
129+
engine: TableEngineRef,
130+
sys_table_requests: &'a mut Vec<RegisterSystemTableRequest>,
131+
) -> Result<()> {
132+
for req in sys_table_requests.drain(..) {
133+
let catalog_name = &req.create_table_request.catalog_name;
134+
let schema_name = &req.create_table_request.schema_name;
135+
let table_name = &req.create_table_request.table_name;
136+
let table_id = req.create_table_request.id;
137+
138+
let table = if let Some(table) = manager.table(catalog_name, schema_name, table_name)? {
139+
table
140+
} else {
141+
let table = engine
142+
.create_table(&EngineContext::default(), req.create_table_request.clone())
143+
.await
144+
.with_context(|_| CreateTableSnafu {
145+
table_info: format!(
146+
"{}.{}.{}, id: {}",
147+
catalog_name, schema_name, table_name, table_id,
148+
),
149+
})?;
150+
manager
151+
.register_table(RegisterTableRequest {
152+
catalog: catalog_name.clone(),
153+
schema: schema_name.clone(),
154+
table_name: table_name.clone(),
155+
table_id,
156+
table: table.clone(),
157+
})
158+
.await?;
159+
info!("Created and registered system table: {}", table_name);
160+
table
161+
};
162+
if let Some(hook) = req.open_hook {
163+
(hook)(table)?;
164+
}
165+
}
166+
Ok(())
167+
}

src/catalog/src/local.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pub mod manager;
2+
pub mod memory;
3+
4+
pub use manager::LocalCatalogManager;
5+
pub use memory::{
6+
new_memory_catalog_list, MemoryCatalogList, MemoryCatalogProvider, MemorySchemaProvider,
7+
};

0 commit comments

Comments
 (0)