Coverage Report

Created: 2026-09-15 15:40

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_evaluate_id;
58
        Jni::MethodId executor_close_id;
59
        Jni::GlobalObject executor;
60
        bool is_closed = false;
61
        bool open_successes = false;
62
63
0
        JniContext() = default;
64
65
0
        Status close() {
66
0
            if (is_closed) {
67
0
                return Status::OK();
68
0
            }
69
            // Not gated on open_successes: _open_udf() creates the Java executor before it
70
            // resolves any method id, so a failure in between leaves an executor that only this
71
            // can close. What has to be bound is the close id itself, which _open_udf() resolves
72
            // first for exactly this reason.
73
0
            if (executor.uninitialized() || executor_cl.uninitialized() ||
74
0
                executor_close_id.uninitialized()) {
75
0
                if (!open_successes) {
76
0
                    LOG_WARNING("maybe open failed, need check the reason");
77
0
                }
78
0
                return Status::OK();
79
0
            }
80
0
            JNIEnv* env = nullptr;
81
0
            Status status = Jni::Env::Get(&env);
82
0
            if (!status.ok() || env == nullptr) {
83
0
                LOG(WARNING) << "errors while get jni env " << status;
84
0
                return status;
85
0
            }
86
            // Before the call, not after: a close that threw halfway is still a close, and the
87
            // executor it was closing must not be handed to a second one. Same order as the
88
            // scalar path in function_java_udf.h and the UDAF path.
89
0
            is_closed = true;
90
0
            return executor.call_nonvirtual_void_method(env, executor_cl, executor_close_id).call();
91
0
        }
92
    };
93
94
    const TFunction& _t_fn;
95
    std::shared_ptr<JniContext> _jni_ctx = nullptr;
96
    DataTypePtr _return_type = nullptr;
97
    ColumnPtr _array_result_column = nullptr;
98
    ColumnArrayExecutionData _array_column_detail;
99
    uint32_t _result_column_idx = 0; // _array_result_column pos in block
100
    size_t _array_offset = 0;        // start offset of array[row_idx]
101
};
102
103
} // namespace doris