-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6aa7665
commit e5100d5
Showing
23 changed files
with
722 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include "vec/exprs/table_function/udf_table_function.h" | ||
|
||
#include <glog/logging.h> | ||
|
||
#include <algorithm> | ||
#include <iterator> | ||
#include <memory> | ||
#include <ostream> | ||
#include <sstream> | ||
|
||
#include "common/status.h" | ||
#include "runtime/user_function_cache.h" | ||
#include "vec/columns/column.h" | ||
#include "vec/columns/column_array.h" | ||
#include "vec/columns/column_nullable.h" | ||
#include "vec/columns/column_string.h" | ||
#include "vec/common/assert_cast.h" | ||
#include "vec/core/block.h" | ||
#include "vec/core/column_with_type_and_name.h" | ||
#include "vec/data_types/data_type_array.h" | ||
#include "vec/data_types/data_type_factory.hpp" | ||
#include "vec/exec/jni_connector.h" | ||
#include "vec/exprs/vexpr.h" | ||
#include "vec/exprs/vexpr_context.h" | ||
|
||
namespace doris::vectorized { | ||
const char* EXECUTOR_CLASS = "org/apache/doris/udf/UdfExecutor"; | ||
const char* EXECUTOR_CTOR_SIGNATURE = "([B)V"; | ||
const char* EXECUTOR_EVALUATE_SIGNATURE = "(Ljava/util/Map;Ljava/util/Map;)J"; | ||
const char* EXECUTOR_CLOSE_SIGNATURE = "()V"; | ||
UDFTableFunction::UDFTableFunction(const TFunction& t_fn) : TableFunction(), _t_fn(t_fn) { | ||
_fn_name = _t_fn.name.function_name; | ||
_return_type = DataTypeFactory::instance().create_data_type( | ||
TypeDescriptor::from_thrift(t_fn.ret_type)); | ||
_return_type = std::make_shared<DataTypeArray>(make_nullable(_return_type)); | ||
} | ||
|
||
Status UDFTableFunction::open() { | ||
JNIEnv* env = nullptr; | ||
RETURN_IF_ERROR(JniUtil::GetJNIEnv(&env)); | ||
if (env == nullptr) { | ||
return Status::InternalError("Failed to get/create JVM"); | ||
} | ||
_jni_ctx = std::make_shared<JniContext>(); | ||
// Add a scoped cleanup jni reference object. This cleans up local refs made below. | ||
JniLocalFrame jni_frame; | ||
{ | ||
std::string local_location; | ||
auto* function_cache = UserFunctionCache::instance(); | ||
RETURN_IF_ERROR(function_cache->get_jarpath(_t_fn.id, _t_fn.hdfs_location, _t_fn.checksum, | ||
&local_location)); | ||
TJavaUdfExecutorCtorParams ctor_params; | ||
ctor_params.__set_fn(_t_fn); | ||
ctor_params.__set_location(local_location); | ||
jbyteArray ctor_params_bytes; | ||
// Pushed frame will be popped when jni_frame goes out-of-scope. | ||
RETURN_IF_ERROR(jni_frame.push(env)); | ||
RETURN_IF_ERROR(SerializeThriftMsg(env, &ctor_params, &ctor_params_bytes)); | ||
RETURN_IF_ERROR(JniUtil::GetGlobalClassRef(env, EXECUTOR_CLASS, &_jni_ctx->executor_cl)); | ||
_jni_ctx->executor_ctor_id = | ||
env->GetMethodID(_jni_ctx->executor_cl, "<init>", EXECUTOR_CTOR_SIGNATURE); | ||
_jni_ctx->executor_evaluate_id = | ||
env->GetMethodID(_jni_ctx->executor_cl, "evaluate", EXECUTOR_EVALUATE_SIGNATURE); | ||
_jni_ctx->executor_close_id = | ||
env->GetMethodID(_jni_ctx->executor_cl, "close", EXECUTOR_CLOSE_SIGNATURE); | ||
_jni_ctx->executor = env->NewObject(_jni_ctx->executor_cl, _jni_ctx->executor_ctor_id, | ||
ctor_params_bytes); | ||
jbyte* pBytes = env->GetByteArrayElements(ctor_params_bytes, nullptr); | ||
env->ReleaseByteArrayElements(ctor_params_bytes, pBytes, JNI_ABORT); | ||
env->DeleteLocalRef(ctor_params_bytes); | ||
} | ||
RETURN_ERROR_IF_EXC(env); | ||
RETURN_IF_ERROR(JniUtil::LocalToGlobalRef(env, _jni_ctx->executor, &_jni_ctx->executor)); | ||
_jni_ctx->open_successes = true; | ||
return Status::OK(); | ||
} | ||
|
||
Status UDFTableFunction::process_init(Block* block, RuntimeState* state) { | ||
auto child_size = _expr_context->root()->children().size(); | ||
std::vector<size_t> child_column_idxs; | ||
child_column_idxs.resize(child_size); | ||
for (int i = 0; i < child_size; ++i) { | ||
int result_id = -1; | ||
RETURN_IF_ERROR(_expr_context->root()->children()[i]->execute(_expr_context.get(), block, | ||
&result_id)); | ||
DCHECK_NE(result_id, -1); | ||
child_column_idxs[i] = result_id; | ||
} | ||
JNIEnv* env = nullptr; | ||
RETURN_IF_ERROR(JniUtil::GetJNIEnv(&env)); | ||
std::unique_ptr<long[]> input_table; | ||
RETURN_IF_ERROR( | ||
JniConnector::to_java_table(block, block->rows(), child_column_idxs, input_table)); | ||
auto input_table_schema = JniConnector::parse_table_schema(block, child_column_idxs, true); | ||
std::map<String, String> input_params = { | ||
{"meta_address", std::to_string((long)input_table.get())}, | ||
{"required_fields", input_table_schema.first}, | ||
{"columns_types", input_table_schema.second}}; | ||
|
||
jobject input_map = JniUtil::convert_to_java_map(env, input_params); | ||
_array_result_column = _return_type->create_column(); | ||
_result_column_idx = block->columns(); | ||
block->insert({_array_result_column, _return_type, "res"}); | ||
auto output_table_schema = JniConnector::parse_table_schema(block, {_result_column_idx}, true); | ||
std::string output_nullable = _return_type->is_nullable() ? "true" : "false"; | ||
std::map<String, String> output_params = {{"is_nullable", output_nullable}, | ||
{"required_fields", output_table_schema.first}, | ||
{"columns_types", output_table_schema.second}}; | ||
|
||
jobject output_map = JniUtil::convert_to_java_map(env, output_params); | ||
DCHECK(_jni_ctx != nullptr); | ||
DCHECK(_jni_ctx->executor != nullptr); | ||
long output_address = env->CallLongMethod(_jni_ctx->executor, _jni_ctx->executor_evaluate_id, | ||
input_map, output_map); | ||
RETURN_IF_ERROR(JniUtil::GetJniExceptionMsg(env)); | ||
env->DeleteLocalRef(input_map); | ||
env->DeleteLocalRef(output_map); | ||
RETURN_IF_ERROR(JniConnector::fill_block(block, {_result_column_idx}, output_address)); | ||
block->erase(_result_column_idx); | ||
if (!extract_column_array_info(*_array_result_column, _array_column_detail)) { | ||
return Status::NotSupported("column type {} not supported now", | ||
block->get_by_position(_result_column_idx).column->get_name()); | ||
} | ||
return Status::OK(); | ||
} | ||
|
||
void UDFTableFunction::process_row(size_t row_idx) { | ||
TableFunction::process_row(row_idx); | ||
if (!_array_column_detail.array_nullmap_data || | ||
!_array_column_detail.array_nullmap_data[row_idx]) { | ||
_array_offset = (*_array_column_detail.offsets_ptr)[row_idx - 1]; | ||
_cur_size = (*_array_column_detail.offsets_ptr)[row_idx] - _array_offset; | ||
} | ||
} | ||
|
||
void UDFTableFunction::process_close() { | ||
_array_result_column = nullptr; | ||
_array_column_detail.reset(); | ||
_array_offset = 0; | ||
} | ||
|
||
void UDFTableFunction::get_value(MutableColumnPtr& column) { | ||
size_t pos = _array_offset + _cur_offset; | ||
if (current_empty() || (_array_column_detail.nested_nullmap_data && | ||
_array_column_detail.nested_nullmap_data[pos])) { | ||
column->insert_default(); | ||
} else { | ||
if (_is_nullable) { | ||
auto* nullable_column = assert_cast<ColumnNullable*>(column.get()); | ||
auto nested_column = nullable_column->get_nested_column_ptr(); | ||
auto nullmap_column = nullable_column->get_null_map_column_ptr(); | ||
nested_column->insert_from(*_array_column_detail.nested_col, pos); | ||
assert_cast<ColumnUInt8*>(nullmap_column.get())->insert_default(); | ||
} else { | ||
column->insert_from(*_array_column_detail.nested_col, pos); | ||
} | ||
} | ||
} | ||
} // namespace doris::vectorized |
Oops, something went wrong.