You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@doris.apache.org by GitBox <gi...@apache.org> on 2022/06/02 07:12:16 UTC

[GitHub] [incubator-doris] BiteTheDDDDt commented on a diff in pull request #9930: [Vectorized][UDF] support java-udaf

BiteTheDDDDt commented on code in PR #9930:
URL: https://github.com/apache/incubator-doris/pull/9930#discussion_r887636880


##########
be/src/vec/aggregate_functions/aggregate_function_java_udaf.h:
##########
@@ -0,0 +1,392 @@
+// 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.
+#pragma once
+
+#ifdef LIBJVM
+
+#include <jni.h>
+#include <unistd.h>
+
+#include <cstdint>
+#include <memory>
+
+#include "common/status.h"
+#include "gen_cpp/Exprs_types.h"
+#include "runtime/user_function_cache.h"
+#include "util/jni-util.h"
+#include "vec/aggregate_functions/aggregate_function.h"
+#include "vec/columns/column_string.h"
+#include "vec/common/exception.h"
+#include "vec/common/string_ref.h"
+#include "vec/core/block.h"
+#include "vec/core/column_numbers.h"
+#include "vec/core/field.h"
+#include "vec/core/types.h"
+#include "vec/data_types/data_type_string.h"
+#include "vec/io/io_helper.h"
+
+namespace doris::vectorized {
+
+const char* UDAF_EXECUTOR_CLASS = "org/apache/doris/udf/UdafExecutor";
+const char* UDAF_EXECUTOR_CTOR_SIGNATURE = "([B)V";
+const char* UDAF_EXECUTOR_CLOSE_SIGNATURE = "()V";
+const char* UDAF_EXECUTOR_CREATE_SIGNATURE = "()Ljava/lang/Object;";
+const char* UDAF_EXECUTOR_DESTORY_SIGNATURE = "()V";
+const char* UDAF_EXECUTOR_ADD_SIGNATURE = "(J)V";
+const char* UDAF_EXECUTOR_MERGE_SIGNATURE = "(Ljava/lang/Object;)V";
+const char* UDAF_EXECUTOR_SERIALIZE_SIGNATURE = "(Ljava/lang/Object;)V";
+const char* UDAF_EXECUTOR_DESERIALIZE_SIGNATURE = "(Ljava/lang/Object;)V";
+const char* UDAF_EXECUTOR_RESULT_SIGNATURE = "(J)Z";
+
+struct AggregateJavaUdafData {
+public:
+    AggregateJavaUdafData() = default;
+    AggregateJavaUdafData(int64_t num_args) {
+        argument_size = num_args;
+        first_init = true;
+        input_values_buffer_ptr.reset(new int64_t[num_args]);
+        input_nulls_buffer_ptr.reset(new int64_t[num_args]);
+        input_offsets_ptrs.reset(new int64_t[num_args]);
+        output_value_buffer.reset(new int64_t);
+        output_null_value.reset(new int64_t);
+        batch_size_ptr.reset(new int32_t);
+        output_offsets_ptr.reset(new int64_t);
+        output_intermediate_state_ptr.reset(new int64_t);
+    }
+
+    ~AggregateJavaUdafData() {
+        JNIEnv* env;
+        Status status;
+        RETURN_IF_STATUS_ERROR(status, JniUtil::GetJNIEnv(&env));
+        env->CallNonvirtualVoidMethod(executor_obj, executor_cl_, executor_close_id_);
+        Status s = JniUtil::GetJniExceptionMsg(env);
+        if (!s.ok()) {
+            LOG(WARNING) << "meet some error in destroy: " << s.get_error_msg();
+        }
+        env->DeleteGlobalRef(executor_obj);
+    }
+
+    Status init_udaf(const TFunction& fn) {
+        if (first_init) {
+            JNIEnv* env = nullptr;
+            RETURN_IF_ERROR(JniUtil::GetJNIEnv(&env));
+            if (env == nullptr) {
+                return Status::InternalError("Failed to get/create JVM");
+            }
+            RETURN_IF_ERROR(JniUtil::GetGlobalClassRef(env, UDAF_EXECUTOR_CLASS, &executor_cl_));
+
+            Status ret_code = register_func_id(env);
+            if (!ret_code.ok()) {
+                LOG(WARNING) << "register_func_id has error : " << ret_code.get_error_msg();
+            }
+
+            // 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(fn.id, fn.hdfs_location, fn.checksum,
+                                                            &local_location));
+                TJavaUdfExecutorCtorParams ctor_params;
+                ctor_params.__set_fn(fn);
+                ctor_params.__set_location(local_location);
+                ctor_params.__set_input_offsets_ptrs((int64_t)input_offsets_ptrs.get());
+                ctor_params.__set_input_buffer_ptrs((int64_t)input_values_buffer_ptr.get());
+                ctor_params.__set_input_nulls_ptrs((int64_t)input_nulls_buffer_ptr.get());
+                ctor_params.__set_output_buffer_ptr((int64_t)output_value_buffer.get());
+
+                ctor_params.__set_output_null_ptr((int64_t)output_null_value.get());
+                ctor_params.__set_output_offsets_ptr((int64_t)output_offsets_ptr.get());
+                ctor_params.__set_output_intermediate_state_ptr(
+                        (int64_t)output_intermediate_state_ptr.get());
+                ctor_params.__set_batch_size_ptr((int64_t)batch_size_ptr.get());
+
+                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));
+                executor_obj = env->NewObject(executor_cl_, executor_ctor_id_, ctor_params_bytes);
+            }
+            RETURN_ERROR_IF_EXC(env);
+            RETURN_IF_ERROR(JniUtil::LocalToGlobalRef(env, executor_obj, &executor_obj));
+            first_init = false;
+        }
+        return Status::OK();
+    }
+
+    void add(const IColumn** columns, size_t row_num, const DataTypes& argument_types) const {
+        JNIEnv* env = nullptr;

Review Comment:
   we can wrap a function `JNIEnv* get_env(string when)`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@doris.apache.org
For additional commands, e-mail: commits-help@doris.apache.org