Skip to main content

risingwave_frontend/catalog/
function_catalog.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 enum_as_inner::EnumAsInner;
16use parse_display::Display;
17use risingwave_common::catalog::FunctionId;
18use risingwave_common::types::DataType;
19use risingwave_common::util::epoch::Epoch;
20use risingwave_pb::catalog::PbFunction;
21use risingwave_pb::catalog::function::PbKind;
22use risingwave_pb::expr::{PbUdfExprVersion, PbUserDefinedFunctionMetadata};
23use risingwave_pb::id::UserId;
24
25use crate::catalog::OwnedByUserCatalog;
26
27#[derive(Clone, PartialEq, Eq, Hash, Debug)]
28pub struct FunctionCatalog {
29    pub id: FunctionId,
30    pub name: String,
31    pub owner: UserId,
32    pub kind: FunctionKind,
33    pub arg_names: Vec<String>,
34    pub arg_types: Vec<DataType>,
35    pub return_type: DataType,
36    pub language: String,
37    pub runtime: Option<String>,
38    pub name_in_runtime: Option<String>,
39    pub body: Option<String>,
40    pub link: Option<String>,
41    pub compressed_binary: Option<Vec<u8>>,
42    pub always_retry_on_network_error: bool,
43    pub unsafe_skip_materializing_exprs: bool,
44    pub is_immutable: bool,
45    pub is_async: Option<bool>,
46    pub is_batched: Option<bool>,
47    pub created_at_epoch: Option<Epoch>,
48    pub created_at_cluster_version: Option<String>,
49}
50
51#[derive(Clone, Display, PartialEq, Eq, Hash, Debug, EnumAsInner)]
52#[display(style = "UPPERCASE")]
53pub enum FunctionKind {
54    Scalar,
55    Table,
56    Aggregate,
57}
58
59impl From<&PbKind> for FunctionKind {
60    fn from(prost: &PbKind) -> Self {
61        use risingwave_pb::catalog::function::*;
62        match prost {
63            Kind::Scalar(ScalarFunction {}) => Self::Scalar,
64            Kind::Table(TableFunction {}) => Self::Table,
65            Kind::Aggregate(AggregateFunction {}) => Self::Aggregate,
66        }
67    }
68}
69
70impl From<&PbFunction> for FunctionCatalog {
71    fn from(prost: &PbFunction) -> Self {
72        FunctionCatalog {
73            id: prost.id,
74            name: prost.name.clone(),
75            owner: prost.owner,
76            kind: prost.kind.as_ref().unwrap().into(),
77            arg_names: prost.arg_names.clone(),
78            arg_types: prost.arg_types.iter().map(|arg| arg.into()).collect(),
79            return_type: prost.return_type.as_ref().expect("no return type").into(),
80            language: prost.language.clone(),
81            runtime: prost.runtime.clone(),
82            name_in_runtime: prost.name_in_runtime.clone(),
83            body: prost.body.clone(),
84            link: prost.link.clone(),
85            compressed_binary: prost.compressed_binary.clone(),
86            always_retry_on_network_error: prost.always_retry_on_network_error,
87            unsafe_skip_materializing_exprs: prost.unsafe_skip_materializing_exprs,
88            is_immutable: prost.is_immutable,
89            is_async: prost.is_async,
90            is_batched: prost.is_batched,
91            created_at_epoch: prost.created_at_epoch.map(Epoch::from),
92            created_at_cluster_version: prost.created_at_cluster_version.clone(),
93        }
94    }
95}
96
97impl From<&FunctionCatalog> for PbUserDefinedFunctionMetadata {
98    fn from(c: &FunctionCatalog) -> Self {
99        PbUserDefinedFunctionMetadata {
100            arg_names: c.arg_names.clone(),
101            arg_types: c.arg_types.iter().map(|t| t.to_protobuf()).collect(),
102            return_type: Some(c.return_type.to_protobuf()),
103            language: c.language.clone(),
104            runtime: c.runtime.clone(),
105            link: c.link.clone(),
106            identifier: c.name_in_runtime.clone(),
107            body: c.body.clone(),
108            compressed_binary: c.compressed_binary.clone(),
109            version: PbUdfExprVersion::LATEST as _,
110            is_async: c.is_async,
111            is_batched: c.is_batched,
112        }
113    }
114}
115
116impl OwnedByUserCatalog for FunctionCatalog {
117    fn owner(&self) -> UserId {
118        self.owner
119    }
120}