Skip to content

Commit

Permalink
feat(udf): add body field for udf body definition (#14300)
Browse files Browse the repository at this point in the history
  • Loading branch information
xzhseh authored Jan 2, 2024
1 parent 53e53ff commit d6c3d0d
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions proto/catalog.proto
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ message Function {
string language = 7;
string link = 8;
string identifier = 10;
optional string body = 14;

oneof kind {
ScalarFunction scalar = 11;
Expand Down
11 changes: 9 additions & 2 deletions src/frontend/src/binder/expr/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,16 @@ impl Binder {
{
use crate::catalog::function_catalog::FunctionKind::*;
if func.language == "sql" {
if func.body.is_none() {
return Err(ErrorCode::InvalidInputSyntax(
"`body` must exist for sql udf".to_string(),
)
.into());
}
// This represents the current user defined function is `language sql`
let parse_result =
risingwave_sqlparser::parser::Parser::parse_sql(func.identifier.as_str());
let parse_result = risingwave_sqlparser::parser::Parser::parse_sql(
func.body.as_ref().unwrap().as_str(),
);
if let Err(ParserError::ParserError(err)) | Err(ParserError::TokenizerError(err)) =
parse_result
{
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/catalog/function_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct FunctionCatalog {
pub return_type: DataType,
pub language: String,
pub identifier: String,
pub body: Option<String>,
pub link: String,
}

Expand Down Expand Up @@ -63,6 +64,7 @@ impl From<&PbFunction> for FunctionCatalog {
return_type: prost.return_type.as_ref().expect("no return type").into(),
language: prost.language.clone(),
identifier: prost.identifier.clone(),
body: prost.body.clone(),
link: prost.link.clone(),
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/frontend/src/expr/user_defined_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ impl UserDefinedFunction {
return_type,
language: udf.get_language().clone(),
identifier: udf.get_identifier().clone(),
// TODO: Ensure if we need `body` here
body: None,
link: udf.get_link().clone(),
};

Expand Down
1 change: 1 addition & 0 deletions src/frontend/src/handler/create_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ pub async fn handle_create_function(
return_type: Some(return_type.into()),
language,
identifier,
body: None,
link,
owner: session.user_id(),
};
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/src/handler/create_sql_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ pub async fn handle_create_sql_function(
arg_types: arg_types.into_iter().map(|t| t.into()).collect(),
return_type: Some(return_type.into()),
language,
identifier: body,
identifier: "".to_string(),
body: Some(body),
link: "".to_string(),
owner: session.user_id(),
};
Expand Down
2 changes: 2 additions & 0 deletions src/meta/model_v2/migration/src/m20230908_072257_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ impl MigrationTrait for Migration {
.col(ColumnDef::new(Function::Language).string().not_null())
.col(ColumnDef::new(Function::Link).string().not_null())
.col(ColumnDef::new(Function::Identifier).string().not_null())
.col(ColumnDef::new(Function::Body).string())
.col(ColumnDef::new(Function::Kind).string().not_null())
.foreign_key(
&mut ForeignKey::create()
Expand Down Expand Up @@ -1099,6 +1100,7 @@ enum Function {
Language,
Link,
Identifier,
Body,
Kind,
}

Expand Down
2 changes: 2 additions & 0 deletions src/meta/model_v2/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct Model {
pub language: String,
pub link: String,
pub identifier: String,
pub body: Option<String>,
pub kind: FunctionKind,
}

Expand Down Expand Up @@ -94,6 +95,7 @@ impl From<PbFunction> for ActiveModel {
language: Set(function.language),
link: Set(function.link),
identifier: Set(function.identifier),
body: Set(function.body),
kind: Set(function.kind.unwrap().into()),
}
}
Expand Down
1 change: 1 addition & 0 deletions src/meta/src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ impl From<ObjectModel<function::Model>> for PbFunction {
language: value.0.language,
link: value.0.link,
identifier: value.0.identifier,
body: value.0.body,
kind: Some(value.0.kind.into()),
}
}
Expand Down

0 comments on commit d6c3d0d

Please sign in to comment.