risingwave_frontend/catalog/system_catalog/rw_catalog/
rw_functions.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;
18
19use crate::catalog::system_catalog::{SysCatalogReaderImpl, get_acl_items};
20use crate::error::Result;
21
22#[derive(Fields)]
23struct RwFunction {
24    #[primary_key]
25    id: i32,
26    name: String,
27    schema_id: i32,
28    owner: i32,
29    r#type: String,
30    arg_type_ids: Vec<i32>,
31    return_type_id: i32,
32    language: String,
33    link: Option<String>,
34    acl: Vec<String>,
35    always_retry_on_network_error: bool,
36    created_at: Option<Timestamptz>,
37    created_at_cluster_version: Option<String>,
38}
39
40#[system_catalog(table, "rw_catalog.rw_functions")]
41fn read(reader: &SysCatalogReaderImpl) -> Result<Vec<RwFunction>> {
42    let catalog_reader = reader.catalog_reader.read_guard();
43    let schemas = catalog_reader.iter_schemas(&reader.auth_context.database)?;
44    let user_reader = reader.user_info_reader.read_guard();
45    let users = user_reader.get_all_users();
46    let current_user = user_reader
47        .get_user_by_name(&reader.auth_context.user_name)
48        .expect("user not found");
49    let username_map = user_reader.get_user_name_map();
50
51    Ok(schemas
52        .flat_map(|schema| {
53            schema
54                .iter_function_with_acl(current_user)
55                .map(|function| RwFunction {
56                    id: function.id.function_id() as i32,
57                    name: function.name.clone(),
58                    schema_id: schema.id() as i32,
59                    owner: function.owner as i32,
60                    r#type: function.kind.to_string(),
61                    arg_type_ids: function.arg_types.iter().map(|t| t.to_oid()).collect(),
62                    return_type_id: function.return_type.to_oid(),
63                    language: function.language.clone(),
64                    link: function.link.clone(),
65                    acl: get_acl_items(
66                        &Object::FunctionId(function.id.function_id()),
67                        false,
68                        &users,
69                        username_map,
70                    ),
71                    always_retry_on_network_error: function.always_retry_on_network_error,
72                    created_at: function.created_at_epoch.map(|e| e.as_timestamptz()),
73                    created_at_cluster_version: function.created_at_cluster_version.clone(),
74                })
75        })
76        .collect())
77}