risingwave_meta_model/
function.rs1use 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 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 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}