be/src/exprs/function/function_python_udf.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/function/function_python_udf.h" |
19 | | |
20 | | #include <arrow/record_batch.h> |
21 | | #include <arrow/type_fwd.h> |
22 | | #include <fmt/core.h> |
23 | | #include <glog/logging.h> |
24 | | |
25 | | #include <cstddef> |
26 | | #include <cstdint> |
27 | | #include <ctime> |
28 | | #include <memory> |
29 | | |
30 | | #include "common/status.h" |
31 | | #include "core/block/block.h" |
32 | | #include "format/arrow/arrow_block_convertor.h" |
33 | | #include "runtime/user_function_cache.h" |
34 | | #include "udf/python/python_server.h" |
35 | | #include "udf/python/python_udf_client.h" |
36 | | #include "udf/python/python_udf_meta.h" |
37 | | #include "util/timezone_utils.h" |
38 | | |
39 | | namespace doris { |
40 | | |
41 | | PythonFunctionCall::PythonFunctionCall(const TFunction& fn, const DataTypes& argument_types, |
42 | | const DataTypePtr& return_type) |
43 | 0 | : _fn(fn), _argument_types(argument_types), _return_type(return_type) {} |
44 | | |
45 | | Status PythonFunctionCall::open(FunctionContext* context, |
46 | 0 | FunctionContext::FunctionStateScope scope) { |
47 | 0 | if (scope == FunctionContext::FunctionStateScope::FRAGMENT_LOCAL) { |
48 | 0 | LOG(INFO) << "Open python UDF fragment local"; |
49 | 0 | return Status::OK(); |
50 | 0 | } |
51 | | |
52 | 0 | PythonVersion version; |
53 | 0 | PythonUDFMeta func_meta; |
54 | 0 | func_meta.id = _fn.id; |
55 | 0 | func_meta.name = _fn.name.function_name; |
56 | 0 | func_meta.symbol = _fn.scalar_fn.symbol; |
57 | 0 | if (!_fn.function_code.empty()) { |
58 | 0 | func_meta.type = PythonUDFLoadType::INLINE; |
59 | 0 | func_meta.location = "inline"; |
60 | 0 | func_meta.inline_code = _fn.function_code; |
61 | 0 | } else if (!_fn.hdfs_location.empty()) { |
62 | 0 | func_meta.type = PythonUDFLoadType::MODULE; |
63 | 0 | func_meta.location = _fn.hdfs_location; |
64 | 0 | func_meta.checksum = _fn.checksum; |
65 | 0 | } else { |
66 | 0 | func_meta.type = PythonUDFLoadType::UNKNOWN; |
67 | 0 | func_meta.location = "unknown"; |
68 | 0 | } |
69 | |
|
70 | 0 | func_meta.input_types = _argument_types; |
71 | 0 | func_meta.return_type = _return_type; |
72 | 0 | func_meta.client_type = PythonClientType::UDF; |
73 | |
|
74 | 0 | if (_fn.__isset.runtime_version && !_fn.runtime_version.empty()) { |
75 | 0 | RETURN_IF_ERROR( |
76 | 0 | PythonVersionManager::instance().get_version(_fn.runtime_version, &version)); |
77 | 0 | } else { |
78 | 0 | return Status::InvalidArgument("Python UDF runtime version is not set"); |
79 | 0 | } |
80 | | |
81 | 0 | func_meta.runtime_version = version.full_version; |
82 | 0 | RETURN_IF_ERROR(func_meta.check()); |
83 | 0 | func_meta.always_nullable = _return_type->is_nullable(); |
84 | 0 | LOG(INFO) << fmt::format("runtime_version: {}, func_meta: {}", version.to_string(), |
85 | 0 | func_meta.to_string()); |
86 | |
|
87 | 0 | if (func_meta.type == PythonUDFLoadType::MODULE) { |
88 | 0 | RETURN_IF_ERROR(UserFunctionCache::instance()->get_pypath( |
89 | 0 | func_meta.id, func_meta.location, func_meta.checksum, &func_meta.location)); |
90 | 0 | } |
91 | | |
92 | 0 | PythonUDFClientPtr client = nullptr; |
93 | 0 | RETURN_IF_ERROR(PythonServerManager::instance().get_client(func_meta, version, &client)); |
94 | | |
95 | 0 | if (!client) { |
96 | 0 | return Status::InternalError("Python UDF client is null"); |
97 | 0 | } |
98 | | |
99 | 0 | context->set_function_state(FunctionContext::THREAD_LOCAL, client); |
100 | 0 | LOG(INFO) << fmt::format("Successfully get python UDF client, process: {}", |
101 | 0 | client->print_process()); |
102 | 0 | return Status::OK(); |
103 | 0 | } |
104 | | |
105 | | Status PythonFunctionCall::execute_impl(FunctionContext* context, Block& block, |
106 | | const ColumnNumbers& arguments, uint32_t result, |
107 | 0 | size_t num_rows) const { |
108 | 0 | auto client = reinterpret_cast<PythonUDFClient*>( |
109 | 0 | context->get_function_state(FunctionContext::THREAD_LOCAL)); |
110 | 0 | if (!client) { |
111 | 0 | LOG(WARNING) << "Python UDF client is null"; |
112 | 0 | return Status::InternalError("Python UDF client is null"); |
113 | 0 | } |
114 | | |
115 | 0 | int64_t input_rows = block.rows(); |
116 | 0 | uint32_t input_columns = block.columns(); |
117 | 0 | DCHECK(input_columns > 0 && result < input_columns && |
118 | 0 | _argument_types.size() == arguments.size()); |
119 | 0 | Block input_block; |
120 | 0 | Block output_block; |
121 | |
|
122 | 0 | if (!_return_type->equals(*block.get_by_position(result).type)) { |
123 | 0 | return Status::InternalError(fmt::format("Python UDF output type {} not equal to {}", |
124 | 0 | block.get_by_position(result).type->get_name(), |
125 | 0 | _return_type->get_name())); |
126 | 0 | } |
127 | | |
128 | 0 | for (uint32_t i = 0; i < arguments.size(); ++i) { |
129 | 0 | if (!_argument_types[i]->equals(*block.get_by_position(arguments[i]).type)) { |
130 | 0 | return Status::InternalError( |
131 | 0 | fmt::format("Python UDF input type {} not equal to {}", |
132 | 0 | block.get_by_position(arguments[i]).type->get_name(), |
133 | 0 | _argument_types[i]->get_name())); |
134 | 0 | } |
135 | 0 | input_block.insert(block.get_by_position(arguments[i])); |
136 | 0 | } |
137 | | |
138 | 0 | std::shared_ptr<arrow::Schema> schema; |
139 | 0 | RETURN_IF_ERROR( |
140 | 0 | get_arrow_schema_from_block(input_block, &schema, TimezoneUtils::default_time_zone)); |
141 | 0 | std::shared_ptr<arrow::RecordBatch> input_batch; |
142 | 0 | std::shared_ptr<arrow::RecordBatch> output_batch; |
143 | 0 | cctz::time_zone _timezone_obj; // default UTC |
144 | 0 | RETURN_IF_ERROR(convert_to_arrow_batch(input_block, schema, arrow::default_memory_pool(), |
145 | 0 | &input_batch, _timezone_obj)); |
146 | 0 | RETURN_IF_ERROR(client->evaluate(*input_batch, &output_batch)); |
147 | 0 | int64_t output_rows = output_batch->num_rows(); |
148 | |
|
149 | 0 | if (output_batch->num_columns() != 1) { |
150 | 0 | return Status::InternalError(fmt::format("Python UDF output columns {} not equal to 1", |
151 | 0 | output_batch->num_columns())); |
152 | 0 | } |
153 | | |
154 | 0 | if (input_rows != output_rows) { |
155 | 0 | return Status::InternalError(fmt::format( |
156 | 0 | "Python UDF output rows {} not equal to input rows {}", output_rows, input_rows)); |
157 | 0 | } |
158 | | |
159 | 0 | RETURN_IF_ERROR( |
160 | 0 | convert_from_arrow_batch(output_batch, {_return_type}, &output_block, _timezone_obj)); |
161 | 0 | DCHECK_EQ(output_block.columns(), 1); |
162 | 0 | block.replace_by_position(result, std::move(output_block.get_by_position(0).column)); |
163 | 0 | return Status::OK(); |
164 | 0 | } |
165 | | |
166 | | Status PythonFunctionCall::close(FunctionContext* context, |
167 | 0 | FunctionContext::FunctionStateScope scope) { |
168 | 0 | auto client = reinterpret_cast<PythonUDFClient*>( |
169 | 0 | context->get_function_state(FunctionContext::THREAD_LOCAL)); |
170 | 0 | if (!client) { |
171 | 0 | LOG(WARNING) << "Python UDF client is null"; |
172 | 0 | return Status::InternalError("Python UDF client is null"); |
173 | 0 | } |
174 | 0 | RETURN_IF_ERROR(client->close()); |
175 | 0 | return Status::OK(); |
176 | 0 | } |
177 | | |
178 | | } // namespace doris |