Coverage Report

Created: 2026-04-14 20:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/table_function/udf_table_function.h
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
#pragma once
19
20
#include "common/status.h"
21
#include "core/column/column.h"
22
#include "core/data_type/data_type.h"
23
#include "exprs/function/array/function_array_utils.h"
24
#include "exprs/table_function/table_function.h"
25
#include "jni.h"
26
#include "util/jni-util.h"
27
28
namespace doris {
29
30
class UDFTableFunction final : public TableFunction {
31
    ENABLE_FACTORY_CREATOR(UDFTableFunction);
32
33
public:
34
    UDFTableFunction(const TFunction& t_fn);
35
0
    ~UDFTableFunction() override = default;
36
37
    Status open() override;
38
    Status process_init(Block* block, RuntimeState* state) override;
39
    void process_row(size_t row_idx) override;
40
    void process_close() override;
41
    void get_same_many_values(MutableColumnPtr& column, int length) override;
42
    int get_value(MutableColumnPtr& column, int max_step) override;
43
0
    Status close() override {
44
0
        if (_jni_ctx) {
45
0
            RETURN_IF_ERROR(_jni_ctx->close());
46
0
        }
47
0
        return TableFunction::close();
48
0
    }
49
50
private:
51
    struct JniContext {
52
        // Do not save parent directly, because parent is in VExpr, but jni context is in FunctionContext
53
        // The deconstruct sequence is not determined, it will core.
54
        // JniContext's lifecycle should same with function context, not related with expr
55
56
        Jni::GlobalClass executor_cl;
57
        Jni::MethodId executor_ctor_id;
58
        Jni::MethodId executor_evaluate_id;
59
        Jni::MethodId executor_close_id;
60
        Jni::GlobalObject executor;
61
        bool is_closed = false;
62
        bool open_successes = false;
63
64
0
        JniContext() = default;
65
66
0
        Status close() {
67
0
            if (!open_successes) {
68
0
                LOG_WARNING("maybe open failed, need check the reason");
69
0
                return Status::OK(); //maybe open failed, so can't call some jni
70
0
            }
71
0
            if (is_closed) {
72
0
                return Status::OK();
73
0
            }
74
0
            JNIEnv* env = nullptr;
75
0
            Status status = Jni::Env::Get(&env);
76
0
            if (!status.ok() || env == nullptr) {
77
0
                LOG(WARNING) << "errors while get jni env " << status;
78
0
                return status;
79
0
            }
80
0
            RETURN_IF_ERROR(
81
0
                    executor.call_nonvirtual_void_method(env, executor_cl, executor_close_id)
82
0
                            .call());
83
84
0
            is_closed = true;
85
0
            return Status::OK();
86
0
        }
87
    };
88
89
    const TFunction& _t_fn;
90
    std::shared_ptr<JniContext> _jni_ctx = nullptr;
91
    DataTypePtr _return_type = nullptr;
92
    ColumnPtr _array_result_column = nullptr;
93
    ColumnArrayExecutionData _array_column_detail;
94
    uint32_t _result_column_idx = 0; // _array_result_column pos in block
95
    size_t _array_offset = 0;        // start offset of array[row_idx]
96
};
97
98
} // namespace doris