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