1919
2020#include < string_view>
2121
22+ #include " exprs/anyval_util.h"
2223#include " fmt/format.h"
2324#include " fmt/ranges.h"
25+ #include " udf/udf_internal.h"
2426#include " vec/data_types/data_type_nullable.h"
2527#include " vec/data_types/data_type_number.h"
2628#include " vec/functions/simple_function_factory.h"
@@ -47,30 +49,64 @@ doris::Status VectorizedFnCall::prepare(doris::RuntimeState* state,
4749 fmt::format (" Function {} is not implemented" , _fn.name .function_name ));
4850 }
4951 _expr_name = fmt::format (" {}({})" , _fn.name .function_name , child_expr_name);
52+
53+ FunctionContext::TypeDesc return_type = AnyValUtil::column_type_to_type_desc (_type);
54+ std::vector<FunctionContext::TypeDesc> arg_types;
55+ for (int i = 0 ; i < _children.size (); ++i) {
56+ arg_types.push_back (AnyValUtil::column_type_to_type_desc (_children[i]->type ()));
57+ }
58+
59+ _fn_context_index = context->register_func (state, return_type, arg_types, 0 );
5060 return Status::OK ();
5161}
52- doris::Status VectorizedFnCall::open (doris::RuntimeState* state, VExprContext* context) {
53- RETURN_IF_ERROR (VExpr::open (state, context));
62+
63+ doris::Status VectorizedFnCall::open (doris::RuntimeState* state, VExprContext* context,
64+ FunctionContext::FunctionStateScope scope) {
65+ RETURN_IF_ERROR (VExpr::open (state, context, scope));
66+
67+ FunctionContext* fn_ctx = context->fn_context (_fn_context_index);
68+ if (scope == FunctionContext::FRAGMENT_LOCAL ) {
69+ std::vector<ColumnPtrWrapper*> constant_cols;
70+ for (int i = 0 ; i < _children.size (); ++i) {
71+ constant_cols.push_back (_children[i]->get_const_col (context));
72+ }
73+ fn_ctx->impl ()->set_constant_cols (constant_cols);
74+ }
75+
76+ if (scope == FunctionContext::FRAGMENT_LOCAL ) {
77+ RETURN_IF_ERROR (_function->prepare (fn_ctx, FunctionContext::FRAGMENT_LOCAL ));
78+ }
79+ RETURN_IF_ERROR (_function->prepare (fn_ctx, FunctionContext::THREAD_LOCAL ));
5480 return Status::OK ();
5581}
56- void VectorizedFnCall::close (doris::RuntimeState* state, VExprContext* context) {
57- VExpr::close (state, context);
82+
83+ void VectorizedFnCall::close (doris::RuntimeState* state, VExprContext* context,
84+ FunctionContext::FunctionStateScope scope) {
85+ if (_fn_context_index != -1 ) {
86+ FunctionContext* fn_ctx = context->fn_context (_fn_context_index);
87+ _function->close (fn_ctx, FunctionContext::THREAD_LOCAL );
88+ if (scope == FunctionContext::FRAGMENT_LOCAL ) {
89+ _function->close (fn_ctx, FunctionContext::FRAGMENT_LOCAL );
90+ }
91+ }
92+ VExpr::close (state, context, scope);
5893}
5994
60- doris::Status VectorizedFnCall::execute (doris::vectorized::Block* block, int * result_column_id) {
95+ doris::Status VectorizedFnCall::execute (VExprContext* context, doris::vectorized::Block* block,
96+ int * result_column_id) {
6197 // for each child call execute
6298 doris::vectorized::ColumnNumbers arguments (_children.size ());
6399 for (int i = 0 ; i < _children.size (); ++i) {
64100 int column_id = -1 ;
65- _children[i]->execute (block, &column_id);
101+ _children[i]->execute (context, block, &column_id);
66102 arguments[i] = column_id;
67103 }
68104 // call function
69105 size_t num_columns_without_result = block->columns ();
70106 // prepare a column to save result
71107 block->insert ({nullptr , _data_type, _expr_name});
72- RETURN_IF_ERROR (_function->execute (*block, arguments, num_columns_without_result, block-> rows () ,
73- false ));
108+ RETURN_IF_ERROR (_function->execute (context-> fn_context (_fn_context_index), *block, arguments,
109+ num_columns_without_result, block-> rows (), false ));
74110 *result_column_id = num_columns_without_result;
75111 return Status::OK ();
76112}
0 commit comments