risingwave_frontend/catalog/
mod.rs

1// Copyright 2025 RisingWave Labs
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//! Definitions of catalog structs.
16//!
17//! The main struct is [`root_catalog::Catalog`], which is the root containing other catalog
18//! structs. It is accessed via [`catalog_service::CatalogReader`] and
19//! [`catalog_service::CatalogWriter`], which is held by [`crate::session::FrontendEnv`].
20
21use risingwave_common::catalog::{
22    ROW_ID_COLUMN_NAME, RW_RESERVED_COLUMN_NAME_PREFIX, is_system_schema,
23};
24use risingwave_common::error::code::PostgresErrorCode;
25use risingwave_connector::sink::catalog::SinkCatalog;
26use risingwave_pb::user::grant_privilege::Object as PbGrantObject;
27use thiserror::Error;
28
29use crate::error::{ErrorCode, Result, RwError};
30pub(crate) mod catalog_service;
31pub mod purify;
32
33pub(crate) mod connection_catalog;
34pub(crate) mod database_catalog;
35pub(crate) mod function_catalog;
36pub(crate) mod index_catalog;
37pub(crate) mod root_catalog;
38pub(crate) mod schema_catalog;
39pub(crate) mod source_catalog;
40pub(crate) mod subscription_catalog;
41pub(crate) mod system_catalog;
42pub(crate) mod table_catalog;
43pub(crate) mod view_catalog;
44
45pub(crate) mod secret_catalog;
46
47pub(crate) use catalog_service::CatalogReader;
48pub use index_catalog::IndexCatalog;
49pub use table_catalog::TableCatalog;
50
51use crate::user::UserId;
52
53pub(crate) type ConnectionId = u32;
54pub(crate) type SourceId = u32;
55pub(crate) type SinkId = u32;
56pub(crate) type SubscriptionId = u32;
57pub(crate) type ViewId = u32;
58pub(crate) type DatabaseId = u32;
59pub(crate) type SchemaId = u32;
60pub(crate) type TableId = risingwave_common::catalog::TableId;
61pub(crate) type ColumnId = risingwave_common::catalog::ColumnId;
62pub(crate) type FragmentId = u32;
63pub(crate) type SecretId = risingwave_common::catalog::SecretId;
64
65/// Check if the column name does not conflict with the internally reserved column name.
66pub fn check_column_name_not_reserved(column_name: &str) -> Result<()> {
67    if column_name.starts_with(ROW_ID_COLUMN_NAME) {
68        return Err(ErrorCode::InternalError(format!(
69            "column name prefixed with {:?} are reserved word.",
70            ROW_ID_COLUMN_NAME
71        ))
72        .into());
73    }
74
75    if column_name.starts_with(RW_RESERVED_COLUMN_NAME_PREFIX) {
76        return Err(ErrorCode::InternalError(format!(
77            "column name prefixed with {:?} are reserved word.",
78            RW_RESERVED_COLUMN_NAME_PREFIX
79        ))
80        .into());
81    }
82
83    if ["tableoid", "xmin", "cmin", "xmax", "cmax", "ctid"].contains(&column_name) {
84        return Err(ErrorCode::InvalidInputSyntax(format!(
85            "column name \"{column_name}\" conflicts with a system column name"
86        ))
87        .into());
88    }
89
90    Ok(())
91}
92
93/// Check if modifications happen to system catalog.
94pub fn check_schema_writable(schema: &str) -> Result<()> {
95    if is_system_schema(schema) {
96        Err(ErrorCode::ProtocolError(format!(
97            "permission denied to write on \"{}\", System catalog modifications are currently disallowed.",
98            schema
99        )).into())
100    } else {
101        Ok(())
102    }
103}
104
105pub type CatalogResult<T> = std::result::Result<T, CatalogError>;
106
107// TODO(error-handling): provide more concrete error code for different object types.
108#[derive(Error, Debug)]
109pub enum CatalogError {
110    #[provide(PostgresErrorCode => PostgresErrorCode::UndefinedObject)]
111    #[error("{0} not found: {1}")]
112    NotFound(&'static str, String),
113
114    #[provide(PostgresErrorCode => PostgresErrorCode::DuplicateObject)]
115    #[error(
116        "{0} with name {1} exists{under_creation}", under_creation = (.2).then_some(" but under creation").unwrap_or("")
117    )]
118    Duplicated(
119        &'static str,
120        String,
121        // whether the object is under creation (only used for StreamingJob type and Subscription for now)
122        bool,
123    ),
124}
125
126impl CatalogError {
127    pub fn duplicated(object_type: &'static str, name: String) -> Self {
128        Self::Duplicated(object_type, name, false)
129    }
130}
131
132impl From<CatalogError> for RwError {
133    fn from(e: CatalogError) -> Self {
134        ErrorCode::CatalogError(Box::new(e)).into()
135    }
136}
137
138/// A trait for the catalog with owners, including relations (table, index, sink, etc.) and
139/// function, connection.
140///
141/// This trait can be used to reduce code duplication and can be extended if needed in the future.
142pub trait OwnedByUserCatalog {
143    /// Returns the owner of the catalog.
144    fn owner(&self) -> UserId;
145}
146
147impl OwnedByUserCatalog for SinkCatalog {
148    fn owner(&self) -> UserId {
149        self.owner.user_id
150    }
151}
152
153pub struct OwnedGrantObject {
154    pub owner: UserId,
155    pub object: PbGrantObject,
156}