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


##########
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;
+        Status ret_code = JniUtil::GetJNIEnv(&env);
+        if (env == nullptr || !ret_code.ok()) {
+            LOG(WARNING) << "Java-Udaf get error when add: " << 
ret_code.get_error_msg();
+        }
+        *batch_size_ptr = columns[0]->size();
+        for (int arg_idx = 0; arg_idx < argument_size; ++arg_idx) {
+            auto data_col = columns[arg_idx];
+            if (auto* nullable = check_and_get_column<const 
ColumnNullable>(*columns[arg_idx])) {
+                data_col = nullable->get_nested_column_ptr();
+                auto null_col = check_and_get_column<ColumnVector<UInt8>>(
+                        nullable->get_null_map_column_ptr());
+                input_nulls_buffer_ptr.get()[arg_idx] =
+                        reinterpret_cast<int64_t>(null_col->get_data().data());
+            } else {
+                input_nulls_buffer_ptr.get()[arg_idx] = -1;
+            }
+            if (data_col->is_column_string()) {
+                const ColumnString* str_col = 
check_and_get_column<ColumnString>(data_col);
+                input_values_buffer_ptr.get()[arg_idx] =
+                        reinterpret_cast<int64_t>(str_col->get_chars().data());
+                input_offsets_ptrs.get()[arg_idx] =
+                        
reinterpret_cast<int64_t>(str_col->get_offsets().data());
+            } else if (data_col->is_numeric() || 
data_col->is_column_decimal()) {
+                input_values_buffer_ptr.get()[arg_idx] =
+                        
reinterpret_cast<int64_t>(data_col->get_raw_data().data);
+            } else {
+                LOG(WARNING) << "Java UDAF doesn't support type: "
+                             << argument_types[arg_idx]->get_name() << " now 
!";
+            }
+        }
+        env->CallNonvirtualVoidMethod(executor_obj, executor_cl_, 
executor_add_id_, row_num);
+    }
+
+    void merge(const AggregateJavaUdafData& rhs) {
+        if (rhs.first_init) {
+            return;
+        }
+        JNIEnv* env = nullptr;
+        Status ret_code = JniUtil::GetJNIEnv(&env);
+        if (env == nullptr || !ret_code.ok()) {
+            LOG(WARNING) << "Java-Udaf get error when merge: " << 
ret_code.get_error_msg();
+        }
+        serialize_data = rhs.serialize_data;
+        long len = serialize_data.length();

Review Comment:
   Should `long` be used here? `long` has different meaning in `c++` and `java`.



-- 
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-unsubscr...@doris.apache.org

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


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

Reply via email to