risingwave_frontend/catalog/system_catalog/rw_catalog/
rw_description.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
15use std::iter;
16
17use risingwave_common::catalog::RW_CATALOG_SCHEMA_NAME;
18use risingwave_common::id::TableId;
19use risingwave_common::types::Fields;
20use risingwave_frontend_macro::system_catalog;
21
22use crate::catalog::system_catalog::SysCatalogReaderImpl;
23use crate::error::Result;
24
25#[derive(Fields)]
26#[primary_key(objoid, classoid, objsubid)]
27struct RwDescription {
28    // table_id, view_id, function_id, etc.
29    objoid: i32,
30    // rw_tables, rw_views, rw_functions, etc.
31    classoid: i32,
32    // If `objoid` is `table_id`, then non-null `objsubid` is column number.
33    objsubid: Option<i32>,
34    description: Option<String>,
35}
36
37#[system_catalog(table, "rw_catalog.rw_description")]
38fn read(reader: &SysCatalogReaderImpl) -> Result<Vec<RwDescription>> {
39    let build_row = |table_id: TableId,
40                     catalog_id,
41                     index: Option<i32>,
42                     description: Option<Box<str>>| RwDescription {
43        objoid: table_id.as_i32_id(),
44        classoid: catalog_id,
45        objsubid: index,
46        description: description.map(|s| s.into()),
47    };
48
49    let catalog_reader = reader.catalog_reader.read_guard();
50    let rw_catalog =
51        catalog_reader.get_schema_by_name(&reader.auth_context.database, RW_CATALOG_SCHEMA_NAME)?;
52    let schemas = catalog_reader
53        .iter_schemas(&reader.auth_context.database)?
54        .filter(|schema| schema.id() != rw_catalog.id());
55
56    let rw_tables_id: i32 = rw_catalog
57        .get_system_table_by_name("rw_tables")
58        .map(|st| st.id.as_raw_id())
59        .unwrap_or_default() as _;
60
61    Ok(schemas
62        .flat_map(|schema| {
63            schema.iter_user_table().flat_map(|table| {
64                iter::once(build_row(
65                    table.id,
66                    rw_tables_id,
67                    None,
68                    table.description.as_deref().map(Into::into),
69                ))
70                .chain(table.columns.iter().map(|col| {
71                    build_row(
72                        table.id,
73                        rw_tables_id,
74                        Some(col.column_id().get_id() as _),
75                        col.column_desc.description.as_deref().map(Into::into),
76                    )
77                }))
78            })
79        })
80        .collect())
81}