Skip to main content

risingwave_meta_model/
function.rs

1// Copyright 2024 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 std::collections::BTreeMap;
16
17use risingwave_pb::catalog::PbFunction;
18use risingwave_pb::catalog::function::Kind;
19use sea_orm::ActiveValue::Set;
20use sea_orm::entity::prelude::*;
21use serde::{Deserialize, Serialize};
22
23use crate::{DataType, DataTypeArray, FunctionId, Property};
24
25#[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
26#[sea_orm(rs_type = "String", db_type = "string(None)")]
27pub enum FunctionKind {
28    #[sea_orm(string_value = "Scalar")]
29    Scalar,
30    #[sea_orm(string_value = "Table")]
31    Table,
32    #[sea_orm(string_value = "Aggregate")]
33    Aggregate,
34}
35
36#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
37#[sea_orm(table_name = "function")]
38pub struct Model {
39    #[sea_orm(primary_key, auto_increment = false)]
40    pub function_id: FunctionId,
41    pub name: String,
42    // encode Vec<String> as comma separated string
43    pub arg_names: String,
44    pub arg_types: DataTypeArray,
45    pub return_type: DataType,
46    pub language: String,
47    pub runtime: Option<String>,
48    pub link: Option<String>,
49    pub name_in_runtime: Option<String>,
50    pub body: Option<String>,
51    pub compressed_binary: Option<Vec<u8>>,
52    pub kind: FunctionKind,
53    // Keep these as dedicated columns rather than entries in `options`.
54    pub always_retry_on_network_error: bool,
55    pub unsafe_skip_materializing_exprs: bool,
56    pub is_immutable: bool,
57    pub options: Option<Property>,
58}
59
60#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
61pub enum Relation {
62    #[sea_orm(
63        belongs_to = "super::object::Entity",
64        from = "Column::FunctionId",
65        to = "super::object::Column::Oid",
66        on_update = "NoAction",
67        on_delete = "Cascade"
68    )]
69    Object,
70}
71
72impl Related<super::object::Entity> for Entity {
73    fn to() -> RelationDef {
74        Relation::Object.def()
75    }
76}
77
78impl ActiveModelBehavior for ActiveModel {}
79
80impl From<Kind> for FunctionKind {
81    fn from(kind: Kind) -> Self {
82        match kind {
83            Kind::Scalar(_) => Self::Scalar,
84            Kind::Table(_) => Self::Table,
85            Kind::Aggregate(_) => Self::Aggregate,
86        }
87    }
88}
89
90impl From<FunctionKind> for Kind {
91    fn from(value: FunctionKind) -> Self {
92        match value {
93            FunctionKind::Scalar => Self::Scalar(Default::default()),
94            FunctionKind::Table => Self::Table(Default::default()),
95            FunctionKind::Aggregate => Self::Aggregate(Default::default()),
96        }
97    }
98}
99
100impl From<PbFunction> for ActiveModel {
101    fn from(function: PbFunction) -> Self {
102        let mut options = BTreeMap::new();
103        if let Some(b) = function.is_batched {
104            options.insert("batch".to_string(), b.to_string());
105        }
106        if let Some(b) = function.is_async {
107            options.insert("async".to_string(), b.to_string());
108        }
109        Self {
110            function_id: Set(function.id),
111            name: Set(function.name),
112            arg_names: Set(function.arg_names.join(",")),
113            arg_types: Set(DataTypeArray::from(function.arg_types)),
114            return_type: Set(DataType::from(&function.return_type.unwrap())),
115            language: Set(function.language),
116            runtime: Set(function.runtime),
117            link: Set(function.link),
118            name_in_runtime: Set(function.name_in_runtime),
119            body: Set(function.body),
120            compressed_binary: Set(function.compressed_binary),
121            kind: Set(function.kind.unwrap().into()),
122            always_retry_on_network_error: Set(function.always_retry_on_network_error),
123            unsafe_skip_materializing_exprs: Set(function.unsafe_skip_materializing_exprs),
124            is_immutable: Set(function.is_immutable),
125            options: Set(Some(options.into())),
126        }
127    }
128}