risingwave_frontend/catalog/system_catalog/rw_catalog/
rw_views.rs

1// Copyright 2023 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::id::{SchemaId, UserId, ViewId};
16use risingwave_common::types::{Fields, Timestamptz};
17use risingwave_frontend_macro::system_catalog;
18
19use crate::catalog::system_catalog::{SysCatalogReaderImpl, get_acl_items};
20use crate::error::Result;
21
22#[derive(Fields)]
23struct RwView {
24    #[primary_key]
25    id: ViewId,
26    name: String,
27    schema_id: SchemaId,
28    owner: UserId,
29    definition: String,
30    acl: Vec<String>,
31    created_at: Option<Timestamptz>,
32    created_at_cluster_version: Option<String>,
33}
34
35#[system_catalog(table, "rw_catalog.rw_views")]
36fn read_rw_view_info(reader: &SysCatalogReaderImpl) -> Result<Vec<RwView>> {
37    let catalog_reader = reader.catalog_reader.read_guard();
38    let schemas = catalog_reader.iter_schemas(&reader.auth_context.database)?;
39    let user_reader = reader.user_info_reader.read_guard();
40    let current_user = user_reader
41        .get_user_by_name(&reader.auth_context.user_name)
42        .expect("user not found");
43    let users = user_reader.get_all_users();
44    let username_map = user_reader.get_user_name_map();
45
46    Ok(schemas
47        .flat_map(|schema| {
48            schema.iter_view_with_acl(current_user).map(|view| RwView {
49                id: view.id,
50                name: view.name().to_owned(),
51                schema_id: schema.id(),
52                owner: view.owner,
53                definition: view.create_sql(schema.name()),
54                acl: get_acl_items(view.id, false, &users, username_map),
55                created_at: view.created_at_epoch.map(|e| e.as_timestamptz()),
56                created_at_cluster_version: view.created_at_cluster_version.clone(),
57            })
58        })
59        .collect())
60}