risingwave_frontend/handler/
create_aggregate.rs1use anyhow::Context;
16use either::Either;
17use risingwave_common::catalog::FunctionId;
18use risingwave_expr::sig::{CreateOptions, UdfKind};
19use risingwave_pb::catalog::Function;
20use risingwave_pb::catalog::function::{AggregateFunction, Kind};
21use risingwave_sqlparser::ast::DataType as AstDataType;
22
23use super::*;
24use crate::handler::create_function::{
25 reject_variant_in_udf_signature, should_reject_function_behavior,
26};
27use crate::{Binder, bind_data_type};
28
29pub async fn handle_create_aggregate(
30 handler_args: HandlerArgs,
31 or_replace: bool,
32 if_not_exists: bool,
33 name: ObjectName,
34 args: Vec<OperateFunctionArg>,
35 returns: AstDataType,
36 params: CreateFunctionBody,
37) -> Result<RwPgResponse> {
38 if or_replace {
39 bail_not_implemented!("CREATE OR REPLACE AGGREGATE");
40 }
41
42 let udf_config = handler_args.session.env().udf_config();
43
44 let language = match params.language {
46 Some(lang) => {
47 let lang = lang.real_value().to_lowercase();
48 match &*lang {
49 "python" if udf_config.enable_embedded_python_udf => lang,
50 "javascript" if udf_config.enable_embedded_javascript_udf => lang,
51 "python" | "javascript" => {
52 return Err(ErrorCode::InvalidParameterValue(format!(
53 "{} UDF is not enabled in configuration",
54 lang
55 ))
56 .into());
57 }
58 _ => {
59 return Err(ErrorCode::InvalidParameterValue(format!(
60 "language {} is not supported",
61 lang
62 ))
63 .into());
64 }
65 }
66 }
67 None => return Err(ErrorCode::InvalidParameterValue("no language".into()).into()),
68 };
69
70 let runtime = match params.runtime {
71 Some(_) => {
72 return Err(ErrorCode::InvalidParameterValue(
73 "runtime selection is currently not supported".to_owned(),
74 )
75 .into());
76 }
77 None => None,
78 };
79
80 if let Some(behavior) = ¶ms.behavior
81 && should_reject_function_behavior(behavior, UdfKind::Aggregate)
82 {
83 return Err(ErrorCode::InvalidParameterValue(format!(
84 "`{behavior}` is not supported for aggregate functions"
85 ))
86 .into());
87 }
88
89 let return_type = bind_data_type(&returns)?;
90
91 let mut arg_names = vec![];
92 let mut arg_types = vec![];
93 for arg in args {
94 arg_names.push(arg.name.map_or("".to_owned(), |n| n.real_value()));
95 arg_types.push(bind_data_type(&arg.data_type)?);
96 }
97
98 reject_variant_in_udf_signature(&return_type, &arg_types, "aggregate function")?;
99
100 let session = &handler_args.session;
102 let db_name = &session.database();
103 let (schema_name, function_name) = Binder::resolve_schema_qualified_name(db_name, &name)?;
104 let (database_id, schema_id) = session.get_database_and_schema_id_for_create(schema_name)?;
105
106 if let Either::Right(resp) = session.check_function_name_duplicated(
108 StatementType::CREATE_FUNCTION,
109 name,
110 &arg_types,
111 if_not_exists,
112 )? {
113 return Ok(resp);
114 }
115
116 let link = match ¶ms.using {
117 Some(CreateFunctionUsing::Link(l)) => Some(l.as_str()),
118 _ => None,
119 };
120 let base64_decoded = match ¶ms.using {
121 Some(CreateFunctionUsing::Base64(encoded)) => {
122 use base64::prelude::{BASE64_STANDARD, Engine};
123 let bytes = BASE64_STANDARD
124 .decode(encoded)
125 .context("invalid base64 encoding")?;
126 Some(bytes)
127 }
128 _ => None,
129 };
130
131 let create_fn = risingwave_expr::sig::find_udf_impl(&language, None, link)?.create_fn;
132 let output = create_fn(CreateOptions {
133 kind: UdfKind::Aggregate,
134 name: &function_name,
135 arg_names: &arg_names,
136 arg_types: &arg_types,
137 return_type: &return_type,
138 as_: params.as_.as_ref().map(|s| s.as_str()),
139 using_link: link,
140 using_base64_decoded: base64_decoded.as_deref(),
141 })?;
142
143 let function = Function {
144 id: FunctionId::placeholder(),
145 schema_id,
146 database_id,
147 name: function_name,
148 kind: Some(Kind::Aggregate(AggregateFunction {})),
149 arg_names,
150 arg_types: arg_types.into_iter().map(|t| t.into()).collect(),
151 return_type: Some(return_type.into()),
152 language,
153 runtime,
154 name_in_runtime: Some(output.name_in_runtime),
155 link: link.map(|s| s.to_owned()),
156 body: output.body,
157 compressed_binary: output.compressed_binary,
158 owner: session.user_id(),
159 always_retry_on_network_error: false,
160 unsafe_skip_materializing_exprs: false,
161 is_immutable: false,
162 is_async: None,
163 is_batched: None,
164 created_at_epoch: None,
165 created_at_cluster_version: None,
166 };
167
168 let catalog_writer = session.catalog_writer()?;
169 catalog_writer.create_function(function).await?;
170
171 Ok(PgResponse::empty_result(StatementType::CREATE_AGGREGATE))
172}