Skip to main content

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