Coverage Report

Created: 2026-04-15 12:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/table_function/udf_table_function.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "exprs/table_function/udf_table_function.h"
19
20
#include <glog/logging.h>
21
22
#include "core/assert_cast.h"
23
#include "core/block/block.h"
24
#include "core/block/column_numbers.h"
25
#include "core/column/column_array.h"
26
#include "core/column/column_nullable.h"
27
#include "core/data_type/data_type_array.h"
28
#include "core/data_type/data_type_factory.hpp"
29
#include "core/types.h"
30
#include "exprs/vexpr.h"
31
#include "exprs/vexpr_context.h"
32
#include "format/jni/jni_data_bridge.h"
33
#include "runtime/user_function_cache.h"
34
35
namespace doris {
36
37
const char* EXECUTOR_CLASS = "org/apache/doris/udf/UdfExecutor";
38
const char* EXECUTOR_CTOR_SIGNATURE = "([B)V";
39
const char* EXECUTOR_EVALUATE_SIGNATURE = "(Ljava/util/Map;Ljava/util/Map;)J";
40
const char* EXECUTOR_CLOSE_SIGNATURE = "()V";
41
0
UDFTableFunction::UDFTableFunction(const TFunction& t_fn) : TableFunction(), _t_fn(t_fn) {
42
0
    _fn_name = _t_fn.name.function_name;
43
0
    _return_type = DataTypeFactory::instance().create_data_type(t_fn.ret_type);
44
    // as the java-utdf function in java code is eg: ArrayList<String>
45
    // so we need a array column to save the execute result, and make_nullable could help deal with nullmap
46
0
    _return_type = make_nullable(std::make_shared<DataTypeArray>(make_nullable(_return_type)));
47
0
}
48
49
0
Status UDFTableFunction::open() {
50
0
    JNIEnv* env = nullptr;
51
0
    RETURN_IF_ERROR(Jni::Env::Get(&env));
52
0
    _jni_ctx = std::make_shared<JniContext>();
53
0
    std::string local_location;
54
0
    auto* function_cache = UserFunctionCache::instance();
55
0
    TJavaUdfExecutorCtorParams ctor_params;
56
0
    ctor_params.__set_fn(_t_fn);
57
0
    if (!_t_fn.hdfs_location.empty() && !_t_fn.checksum.empty()) {
58
        // get jar path if both file path location and checksum are null
59
0
        RETURN_IF_ERROR(function_cache->get_jarpath(_t_fn.id, _t_fn.hdfs_location, _t_fn.checksum,
60
0
                                                    &local_location));
61
0
        ctor_params.__set_location(local_location);
62
0
    }
63
0
    RETURN_IF_ERROR(Jni::Util::find_class(env, EXECUTOR_CLASS, &_jni_ctx->executor_cl));
64
65
0
    Jni::LocalArray ctor_params_bytes;
66
0
    RETURN_IF_ERROR(Jni::Util::SerializeThriftMsg(env, &ctor_params, &ctor_params_bytes));
67
68
0
    RETURN_IF_ERROR(_jni_ctx->executor_cl.get_method(env, "<init>", EXECUTOR_CTOR_SIGNATURE,
69
0
                                                     &_jni_ctx->executor_ctor_id));
70
71
0
    RETURN_IF_ERROR(_jni_ctx->executor_cl.get_method(env, "evaluate", EXECUTOR_EVALUATE_SIGNATURE,
72
0
                                                     &_jni_ctx->executor_evaluate_id));
73
74
0
    RETURN_IF_ERROR(_jni_ctx->executor_cl.get_method(env, "close", EXECUTOR_CLOSE_SIGNATURE,
75
0
                                                     &_jni_ctx->executor_close_id));
76
77
0
    RETURN_IF_ERROR(_jni_ctx->executor_cl.new_object(env, _jni_ctx->executor_ctor_id)
78
0
                            .with_arg(ctor_params_bytes)
79
0
                            .call(&_jni_ctx->executor));
80
81
0
    _jni_ctx->open_successes = true;
82
0
    return Status::OK();
83
0
}
84
85
0
Status UDFTableFunction::process_init(Block* block, RuntimeState* state) {
86
0
    auto child_size = _expr_context->root()->children().size();
87
0
    ColumnNumbers child_column_idxs;
88
0
    child_column_idxs.resize(child_size);
89
0
    for (int i = 0; i < child_size; ++i) {
90
0
        int result_id = -1;
91
0
        RETURN_IF_ERROR(_expr_context->root()->children()[i]->execute(_expr_context.get(), block,
92
0
                                                                      &result_id));
93
0
        DCHECK_NE(result_id, -1);
94
0
        child_column_idxs[i] = result_id;
95
0
    }
96
0
    JNIEnv* env = nullptr;
97
0
    RETURN_IF_ERROR(Jni::Env::Get(&env));
98
0
    std::unique_ptr<long[]> input_table;
99
0
    RETURN_IF_ERROR(
100
0
            JniDataBridge::to_java_table(block, block->rows(), child_column_idxs, input_table));
101
0
    auto input_table_schema = JniDataBridge::parse_table_schema(block, child_column_idxs, true);
102
0
    std::map<String, String> input_params = {
103
0
            {"meta_address", std::to_string((long)input_table.get())},
104
0
            {"required_fields", input_table_schema.first},
105
0
            {"columns_types", input_table_schema.second}};
106
107
0
    Jni::LocalObject input_map;
108
0
    RETURN_IF_ERROR(Jni::Util::convert_to_java_map(env, input_params, &input_map));
109
0
    _array_result_column = _return_type->create_column();
110
0
    _result_column_idx = block->columns();
111
0
    block->insert({_array_result_column, _return_type, "res"});
112
0
    auto output_table_schema = JniDataBridge::parse_table_schema(block, {_result_column_idx}, true);
113
0
    std::string output_nullable = _return_type->is_nullable() ? "true" : "false";
114
0
    std::map<String, String> output_params = {{"is_nullable", output_nullable},
115
0
                                              {"required_fields", output_table_schema.first},
116
0
                                              {"columns_types", output_table_schema.second}};
117
118
0
    Jni::LocalObject output_map;
119
0
    RETURN_IF_ERROR(Jni::Util::convert_to_java_map(env, output_params, &output_map));
120
0
    long output_address;
121
0
    RETURN_IF_ERROR(_jni_ctx->executor.call_long_method(env, _jni_ctx->executor_evaluate_id)
122
0
                            .with_arg(input_map)
123
0
                            .with_arg(output_map)
124
0
                            .call(&output_address));
125
0
    RETURN_IF_ERROR(JniDataBridge::fill_block(block, {_result_column_idx}, output_address));
126
0
    block->erase(_result_column_idx);
127
0
    if (!extract_column_array_info(*_array_result_column, _array_column_detail)) {
128
0
        return Status::NotSupported("column type {} not supported now",
129
0
                                    block->get_by_position(_result_column_idx).column->get_name());
130
0
    }
131
0
    return Status::OK();
132
0
}
133
134
0
void UDFTableFunction::process_row(size_t row_idx) {
135
0
    TableFunction::process_row(row_idx);
136
0
    if (!_array_column_detail.array_nullmap_data ||
137
0
        !_array_column_detail.array_nullmap_data[row_idx]) {
138
0
        _array_offset = (*_array_column_detail.offsets_ptr)[row_idx - 1];
139
0
        _cur_size = (*_array_column_detail.offsets_ptr)[row_idx] - _array_offset;
140
0
    }
141
    // so when it's NULL of row_idx, will not update _cur_size
142
    // it's will be _cur_size == 0, and means current_empty.
143
    // if the fn is outer, will be continue insert_default
144
    // if the fn is not outer function, will be not insert any value.
145
0
}
146
147
0
void UDFTableFunction::process_close() {
148
0
    _array_result_column = nullptr;
149
0
    _array_column_detail.reset();
150
0
    _array_offset = 0;
151
0
}
152
153
0
void UDFTableFunction::get_same_many_values(MutableColumnPtr& column, int length) {
154
0
    size_t pos = _array_offset + _cur_offset;
155
0
    if (current_empty() || (_array_column_detail.nested_nullmap_data &&
156
0
                            _array_column_detail.nested_nullmap_data[pos])) {
157
0
        column->insert_many_defaults(length);
158
0
    } else {
159
0
        if (_is_nullable) {
160
0
            auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
161
0
            auto nested_column = nullable_column->get_nested_column_ptr();
162
0
            auto nullmap_column = nullable_column->get_null_map_column_ptr();
163
0
            nested_column->insert_many_from(*_array_column_detail.nested_col, pos, length);
164
0
            assert_cast<ColumnUInt8*>(nullmap_column.get())->insert_many_defaults(length);
165
0
        } else {
166
0
            column->insert_many_from(*_array_column_detail.nested_col, pos, length);
167
0
        }
168
0
    }
169
0
}
170
171
0
int UDFTableFunction::get_value(MutableColumnPtr& column, int max_step) {
172
0
    max_step = std::min(max_step, (int)(_cur_size - _cur_offset));
173
0
    size_t pos = _array_offset + _cur_offset;
174
0
    if (current_empty()) {
175
0
        column->insert_default();
176
0
        max_step = 1;
177
0
    } else {
178
0
        if (_is_nullable) {
179
0
            auto* nullable_column = assert_cast<ColumnNullable*>(column.get());
180
0
            auto nested_column = nullable_column->get_nested_column_ptr();
181
0
            auto* nullmap_column =
182
0
                    assert_cast<ColumnUInt8*>(nullable_column->get_null_map_column_ptr().get());
183
0
            nested_column->insert_range_from(*_array_column_detail.nested_col, pos, max_step);
184
0
            size_t old_size = nullmap_column->size();
185
0
            nullmap_column->resize(old_size + max_step);
186
0
            memcpy(nullmap_column->get_data().data() + old_size,
187
0
                   _array_column_detail.nested_nullmap_data + pos * sizeof(UInt8),
188
0
                   max_step * sizeof(UInt8));
189
0
        } else {
190
0
            column->insert_range_from(*_array_column_detail.nested_col, pos, max_step);
191
0
        }
192
0
    }
193
0
    forward(max_step);
194
0
    return max_step;
195
0
}
196
197
} // namespace doris