risingwave_frontend/catalog/system_catalog/rw_catalog/
rw_tables.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 risingwave_common::types::{Fields, Timestamptz};
16use risingwave_frontend_macro::system_catalog;
17use risingwave_pb::user::grant_privilege::Object as GrantObject;
18
19use crate::catalog::system_catalog::{SysCatalogReaderImpl, get_acl_items};
20use crate::error::Result;
21
22#[derive(Fields)]
23struct RwTable {
24    #[primary_key]
25    id: i32,
26    name: String,
27    schema_id: i32,
28    owner: i32,
29    definition: String,
30    append_only: bool,
31    refreshable: bool,
32    acl: Vec<String>,
33    initialized_at: Option<Timestamptz>,
34    created_at: Option<Timestamptz>,
35    initialized_at_cluster_version: Option<String>,
36    created_at_cluster_version: Option<String>,
37}
38
39#[system_catalog(table, "rw_catalog.rw_tables")]
40fn read_rw_table_info(reader: &SysCatalogReaderImpl) -> Result<Vec<RwTable>> {
41    let catalog_reader = reader.catalog_reader.read_guard();
42    let schemas = catalog_reader.iter_schemas(&reader.auth_context.database)?;
43    let user_reader = reader.user_info_reader.read_guard();
44    let users = user_reader.get_all_users();
45    let current_user = user_reader
46        .get_user_by_name(&reader.auth_context.user_name)
47        .expect("user not found");
48    let username_map = user_reader.get_user_name_map();
49
50    Ok(schemas
51        .flat_map(|schema| {
52            schema
53                .iter_user_table_with_acl(current_user)
54                .map(|table| RwTable {
55                    id: table.id.table_id as i32,
56                    name: table.name().to_owned(),
57                    schema_id: schema.id() as i32,
58                    owner: table.owner as i32,
59                    definition: table.create_sql_purified(),
60                    append_only: table.append_only,
61                    refreshable: table.refreshable,
62                    acl: get_acl_items(
63                        &GrantObject::TableId(table.id.table_id),
64                        true,
65                        &users,
66                        username_map,
67                    ),
68                    initialized_at: table.initialized_at_epoch.map(|e| e.as_timestamptz()),
69                    created_at: table.created_at_epoch.map(|e| e.as_timestamptz()),
70                    initialized_at_cluster_version: table.initialized_at_cluster_version.clone(),
71                    created_at_cluster_version: table.created_at_cluster_version.clone(),
72                })
73        })
74        .collect())
75}