Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for array arguments and return types in UDFs #454

Merged
merged 18 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ pub struct FunctionCallBuilder {

pub enum FuncCallReturnTypeBuilder {
Scalar,
List,
Node(NodeBuilder),
Connection(ConnectionBuilder),
}
Expand Down Expand Up @@ -600,6 +601,7 @@ where

let return_type_builder = match func_call_resp_type.return_type.deref() {
__Type::Scalar(_) => FuncCallReturnTypeBuilder::Scalar,
__Type::List(_) => FuncCallReturnTypeBuilder::List,
__Type::Node(_) => {
let node_builder = to_node_builder(
field,
Expand Down Expand Up @@ -628,7 +630,7 @@ where
.unmodified_type()
.name()
.ok_or("Encountered type without name in function call builder")?
))
));
}
};

Expand Down Expand Up @@ -2193,7 +2195,12 @@ impl __Schema {
None => __TypeField::PossibleTypes(None),
},
"ofType" => {
let unwrapped_type_builder = match type_ {
let field_type = if let __Type::FuncCallResponse(func_call_resp_type) = type_ {
func_call_resp_type.return_type.deref()
} else {
type_
};
let unwrapped_type_builder = match field_type {
__Type::List(list_type) => {
let inner_type: __Type = (*(list_type.type_)).clone();
Some(self.to_type_builder_from_type(
Expand Down
2 changes: 1 addition & 1 deletion src/sql_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl Function {
self.args().all(|(arg_type, _, _, _)| {
if let Some(return_type) = types.get(&arg_type) {
return_type.category == TypeCategory::Other
|| return_type.category == TypeCategory::Array
} else {
false
}
Expand All @@ -137,7 +138,6 @@ impl Function {
&& return_type.name != "record"
&& return_type.name != "trigger"
&& return_type.name != "event_trigger"
&& !self.type_name.ends_with("[]")
} else {
false
}
Expand Down
5 changes: 4 additions & 1 deletion src/transpile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ impl FunctionCallBuilder {
let func_name = quote_ident(&self.function.name);

let query = match &self.return_type_builder {
FuncCallReturnTypeBuilder::Scalar => {
FuncCallReturnTypeBuilder::Scalar | FuncCallReturnTypeBuilder::List => {
let type_adjustment_clause = apply_suffix_casts(self.function.type_oid);
format!("select to_jsonb({func_schema}.{func_name}{args_clause}{type_adjustment_clause}) {block_name};")
}
Expand Down Expand Up @@ -1353,6 +1353,9 @@ fn apply_suffix_casts(type_oid: u32) -> String {
20 => "::text", // bigints as text
114 | 3802 => "#>> '{}'", // json/b as stringified
1700 => "::text", // numeric as text
1016 => "::text[]", // bigint arrays as array of text
199 | 3807 => "::text[]", // json/b array as array of text
1231 => "::text[]", // numeric array as array of text
_ => "",
}
.to_string()
Expand Down
Loading
Loading