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 username_map = user_reader.get_user_name_map();
47
48    Ok(schemas
49        .flat_map(|schema| {
50            schema.iter_function().map(|function| RwFunction {
51                id: function.id.function_id() as i32,
52                name: function.name.clone(),
53                schema_id: schema.id() as i32,
54                owner: function.owner as i32,
55                r#type: function.kind.to_string(),
56                arg_type_ids: function.arg_types.iter().map(|t| t.to_oid()).collect(),
57                return_type_id: function.return_type.to_oid(),
58                language: function.language.clone(),
59                link: function.link.clone(),
60                acl: get_acl_items(
61                    &Object::FunctionId(function.id.function_id()),
62                    false,
63                    &users,
64                    username_map,
65                ),
66                always_retry_on_network_error: function.always_retry_on_network_error,
67                created_at: function.created_at_epoch.map(|e| e.as_timestamptz()),
68                created_at_cluster_version: function.created_at_cluster_version.clone(),
69            })
70        })
71        .collect())
72}