risingwave_frontend/catalog/
function_catalog.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2024 RisingWave Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use enum_as_inner::EnumAsInner;
use parse_display::Display;
use risingwave_common::catalog::FunctionId;
use risingwave_common::types::DataType;
use risingwave_pb::catalog::function::PbKind;
use risingwave_pb::catalog::PbFunction;
use risingwave_pb::expr::PbUserDefinedFunctionMetadata;

use crate::catalog::OwnedByUserCatalog;

#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct FunctionCatalog {
    pub id: FunctionId,
    pub name: String,
    pub owner: u32,
    pub kind: FunctionKind,
    pub arg_names: Vec<String>,
    pub arg_types: Vec<DataType>,
    pub return_type: DataType,
    pub language: String,
    pub runtime: Option<String>,
    pub identifier: Option<String>,
    pub body: Option<String>,
    pub link: Option<String>,
    pub compressed_binary: Option<Vec<u8>>,
    pub always_retry_on_network_error: bool,
}

#[derive(Clone, Display, PartialEq, Eq, Hash, Debug, EnumAsInner)]
#[display(style = "UPPERCASE")]
pub enum FunctionKind {
    Scalar,
    Table,
    Aggregate,
}

impl From<&PbKind> for FunctionKind {
    fn from(prost: &PbKind) -> Self {
        use risingwave_pb::catalog::function::*;
        match prost {
            Kind::Scalar(ScalarFunction {}) => Self::Scalar,
            Kind::Table(TableFunction {}) => Self::Table,
            Kind::Aggregate(AggregateFunction {}) => Self::Aggregate,
        }
    }
}

impl From<&PbFunction> for FunctionCatalog {
    fn from(prost: &PbFunction) -> Self {
        FunctionCatalog {
            id: prost.id.into(),
            name: prost.name.clone(),
            owner: prost.owner,
            kind: prost.kind.as_ref().unwrap().into(),
            arg_names: prost.arg_names.clone(),
            arg_types: prost.arg_types.iter().map(|arg| arg.into()).collect(),
            return_type: prost.return_type.as_ref().expect("no return type").into(),
            language: prost.language.clone(),
            runtime: prost.runtime.clone(),
            identifier: prost.identifier.clone(),
            body: prost.body.clone(),
            link: prost.link.clone(),
            compressed_binary: prost.compressed_binary.clone(),
            always_retry_on_network_error: prost.always_retry_on_network_error,
        }
    }
}

impl From<&FunctionCatalog> for PbUserDefinedFunctionMetadata {
    fn from(c: &FunctionCatalog) -> Self {
        PbUserDefinedFunctionMetadata {
            arg_names: c.arg_names.clone(),
            arg_types: c.arg_types.iter().map(|t| t.to_protobuf()).collect(),
            return_type: Some(c.return_type.to_protobuf()),
            language: c.language.clone(),
            runtime: c.runtime.clone(),
            link: c.link.clone(),
            identifier: c.identifier.clone(),
            body: c.body.clone(),
            compressed_binary: c.compressed_binary.clone(),
        }
    }
}

impl OwnedByUserCatalog for FunctionCatalog {
    fn owner(&self) -> u32 {
        self.owner
    }
}