Skip to content

Commit

Permalink
refactor(catalog): use ctor in prepare_sys_catalog for further `cat…
Browse files Browse the repository at this point in the history
…alog` macro (#13491)

Signed-off-by: TennyZhuang <[email protected]>
  • Loading branch information
TennyZhuang authored Dec 11, 2023
1 parent ccf8be5 commit 98b0ebd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions src/expr/impl/src/scalar/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ use risingwave_common::cast;
use risingwave_common::row::OwnedRow;
use risingwave_common::types::{Int256, IntoOrdered, JsonbRef, ToText, F64};
use risingwave_common::util::iter_util::ZipEqFast;
use risingwave_expr::expr::{
build_func, Context, Expression, ExpressionBoxExt, InputRefExpression,
};
use risingwave_expr::expr::{build_func, Context, ExpressionBoxExt, InputRefExpression};
use risingwave_expr::{function, ExprError, Result};
use risingwave_pb::expr::expr_node::PbType;
use thiserror_ext::AsReport;
Expand Down
1 change: 1 addition & 0 deletions src/frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ auto_impl = "1"
bk-tree = "0.5.0"
bytes = "1"
clap = { version = "4", features = ["derive"] }
ctor = "0.2"
downcast-rs = "1.2"
dyn-clone = "1.0.14"
easy-ext = "1"
Expand Down
74 changes: 51 additions & 23 deletions src/frontend/src/catalog/system_catalog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,34 +283,62 @@ pub fn get_sys_views_in_schema(schema_name: &str) -> Option<Vec<Arc<ViewCatalog>
.map(Clone::clone)
}

/// The global registry of all builtin catalogs.
pub static SYS_CATALOGS: LazyLock<SystemCatalog> = LazyLock::new(|| {
// SAFETY: this function is called after all `#[ctor]` functions are called.
let mut table_by_schema_name = HashMap::new();
let mut table_name_by_id = HashMap::new();
let mut view_by_schema_name = HashMap::new();
tracing::info!("found {} catalogs", unsafe { SYS_CATALOGS_INIT.len() });
for (id, catalog) in unsafe { SYS_CATALOGS_INIT.drain(..) } {
match catalog {
BuiltinCatalog::Table(table) => {
let sys_table: SystemTableCatalog = table.into();
table_by_schema_name
.entry(table.schema)
.or_insert(vec![])
.push(Arc::new(sys_table.with_id(id.into())));
table_name_by_id.insert(id.into(), table.name);
}
BuiltinCatalog::View(view) => {
let sys_view: ViewCatalog = view.into();
view_by_schema_name
.entry(view.schema)
.or_insert(vec![])
.push(Arc::new(sys_view.with_id(id)));
}
}
}
SystemCatalog {
table_by_schema_name,
table_name_by_id,
view_by_schema_name,
}
});

pub static mut SYS_CATALOGS_INIT: Vec<(u32, BuiltinCatalog)> = vec![];

/// Register the catalog to global registry.
///
/// Note: The function is used by macro generated code. Don't call it directly.
#[doc(hidden)]
pub(super) unsafe fn _register(id: u32, builtin_catalog: BuiltinCatalog) {
assert!(id < NON_RESERVED_SYS_CATALOG_ID as u32);

SYS_CATALOGS_INIT.push((id, builtin_catalog));
}

macro_rules! prepare_sys_catalog {
($( { $builtin_catalog:expr $(, $func:ident $($await:ident)?)? } ),* $(,)?) => {
pub(crate) static SYS_CATALOGS: LazyLock<SystemCatalog> = LazyLock::new(|| {
let mut table_by_schema_name = HashMap::new();
let mut table_name_by_id = HashMap::new();
let mut view_by_schema_name = HashMap::new();
paste::paste! {
$(
let id = (${index()} + 1) as u32;
match $builtin_catalog {
BuiltinCatalog::Table(table) => {
let sys_table: SystemTableCatalog = table.into();
table_by_schema_name.entry(table.schema).or_insert(vec![]).push(Arc::new(sys_table.with_id(id.into())));
table_name_by_id.insert(id.into(), table.name);
},
BuiltinCatalog::View(view) => {
let sys_view: ViewCatalog = view.into();
view_by_schema_name.entry(view.schema).or_insert(vec![]).push(Arc::new(sys_view.with_id(id)));
},
#[ctor::ctor]
#[allow(non_snake_case)]
unsafe fn [<register_${index()}>]() {
_register((${index()} + 1) as u32, $builtin_catalog);
}
)*
assert!(table_name_by_id.len() < NON_RESERVED_SYS_CATALOG_ID as usize, "too many system catalogs");

SystemCatalog {
table_by_schema_name,
table_name_by_id,
view_by_schema_name,
}
});
}

#[async_trait]
impl SysCatalogReader for SysCatalogReaderImpl {
Expand Down

0 comments on commit 98b0ebd

Please sign in to comment.