risingwave_frontend/expr/function_impl/
cast_regclass.rsuse risingwave_common::session_config::SearchPath;
use risingwave_expr::{capture_context, function, ExprError};
use risingwave_sqlparser::parser::{Parser, ParserError};
use thiserror::Error;
use thiserror_ext::AsReport;
use super::context::{AUTH_CONTEXT, CATALOG_READER, DB_NAME, SEARCH_PATH};
use crate::catalog::root_catalog::SchemaPath;
use crate::catalog::{CatalogError, CatalogReader};
use crate::session::AuthContext;
#[derive(Error, Debug)]
enum ResolveRegclassError {
#[error("parse object name failed: {0}")]
Parser(#[from] ParserError),
#[error("catalog error: {0}")]
Catalog(#[from] CatalogError),
}
impl From<ResolveRegclassError> for ExprError {
fn from(e: ResolveRegclassError) -> Self {
match e {
ResolveRegclassError::Parser(e) => ExprError::Parse(e.to_report_string().into()),
ResolveRegclassError::Catalog(e) => ExprError::InvalidParam {
name: "name",
reason: e.to_report_string().into(),
},
}
}
}
#[capture_context(CATALOG_READER, AUTH_CONTEXT, SEARCH_PATH, DB_NAME)]
fn resolve_regclass_impl(
catalog: &CatalogReader,
auth_context: &AuthContext,
search_path: &SearchPath,
db_name: &str,
class_name: &str,
) -> Result<u32, ExprError> {
resolve_regclass_inner(catalog, auth_context, search_path, db_name, class_name)
.map_err(Into::into)
}
fn resolve_regclass_inner(
catalog: &CatalogReader,
auth_context: &AuthContext,
search_path: &SearchPath,
db_name: &str,
class_name: &str,
) -> Result<u32, ResolveRegclassError> {
let obj = Parser::parse_object_name_str(class_name)?;
if obj.0.len() == 1 {
let class_name = obj.0[0].real_value();
let schema_path = SchemaPath::Path(search_path, &auth_context.user_name);
Ok(catalog
.read_guard()
.get_id_by_class_name(db_name, schema_path, &class_name)?)
} else {
let schema = obj.0[0].real_value();
let class_name = obj.0[1].real_value();
let schema_path = SchemaPath::Name(&schema);
Ok(catalog
.read_guard()
.get_id_by_class_name(db_name, schema_path, &class_name)?)
}
}
#[function("cast_regclass(varchar) -> int4")]
fn cast_regclass(class_name: &str) -> Result<i32, ExprError> {
let oid = resolve_regclass_impl_captured(class_name)?;
Ok(oid as i32)
}