yangzhg commented on code in PR #10685:
URL: https://github.com/apache/doris/pull/10685#discussion_r921741724


##########
be/src/vec/aggregate_functions/aggregate_function_rpc.h:
##########
@@ -0,0 +1,359 @@
+// 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.
+
+#pragma once
+
+#include <cstdint>
+#include <memory>
+
+#include "common/status.h"
+#include "exprs/rpc_fn_comm.h"
+#include "gen_cpp/Exprs_types.h"
+#include "json2pb/json_to_pb.h"
+#include "json2pb/pb_to_json.h"
+#include "runtime/exec_env.h"
+#include "runtime/user_function_cache.h"
+#include "util/brpc_client_cache.h"
+#include "util/jni-util.h"
+#include "vec/aggregate_functions/aggregate_function.h"
+#include "vec/columns/column_string.h"
+#include "vec/columns/column_vector.h"
+#include "vec/columns/columns_number.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 {
+
+constexpr int64_t max_buffered_rows = 4096;
+struct AggregateRpcUdafData {
+private:
+    std::string _update_fn;
+    std::string _merge_fn;
+    std::string _server_addr;
+    std::string _finalize_fn;
+    bool _saved_last_result;
+    std::shared_ptr<PFunctionService_Stub> _client;
+    PFunctionCallResponse _res;
+    std::vector<PFunctionCallRequest> _buffer_request;
+
+public:
+    AggregateRpcUdafData() = default;
+    AggregateRpcUdafData(int64_t num_args) { set_last_result(false); }
+
+    bool has_last_result() { return _saved_last_result == true; }
+
+    void set_last_result(bool flag) { _saved_last_result = flag; }
+
+    ~AggregateRpcUdafData() {}
+
+    Status merge(AggregateRpcUdafData& rhs) {
+        send_buffer_to_rpc_server();
+        if (has_last_result()) {
+            PFunctionCallRequest request;
+            PFunctionCallResponse response;
+            brpc::Controller cntl;
+            PFunctionCallResponse current_res = rhs.get_result();
+            request.set_function_name(_merge_fn);
+            //last result
+            PValues* arg = request.add_args();
+            arg->CopyFrom(_res.result(0));
+            arg = request.add_args();
+            //current result
+            arg->CopyFrom(current_res.result(0));
+            //send to rpc server  that impl the merge op, the will save the 
result
+            RETURN_IF_ERROR(send_rpc_request(cntl, request, response));
+            _res = response;
+        } else {
+            _res = rhs.get_result();
+            set_last_result(true);
+        }
+        return Status::OK();
+    }
+
+    Status init(const TFunction& fn) {
+        _update_fn = fn.aggregate_fn.update_fn_symbol;
+        _merge_fn = fn.aggregate_fn.merge_fn_symbol;
+        _server_addr = fn.hdfs_location;
+        _finalize_fn = fn.aggregate_fn.finalize_fn_symbol;
+        _client = 
ExecEnv::GetInstance()->brpc_function_client_cache()->get_client(_server_addr);
+        if (_client == nullptr) {
+            std::string err_msg = "init rpc error, addr:" + _server_addr;
+            LOG(ERROR) << err_msg;
+            return Status::InternalError(err_msg);
+        }
+        return Status::OK();
+    }
+
+    Status send_rpc_request(brpc::Controller& cntl, PFunctionCallRequest& 
request,
+                            PFunctionCallResponse& response) {
+        _client->fn_call(&cntl, &request, &response, nullptr);
+        if (cntl.Failed()) {
+            return Status::InternalError(fmt::format("call to rpc function {} 
failed: {}",
+                                                     request.function_name(), 
cntl.ErrorText())
+                                                 .c_str());
+        }
+        if (!response.has_status() || response.result_size() == 0) {
+            return Status::InternalError(
+                    fmt::format("call rpc function {} failed: status or result 
is not set.",
+                                request.function_name()));
+        }
+        if (response.status().status_code() != 0) {
+            return Status::InternalError(fmt::format("call to rpc function {} 
failed: {}",
+                                                     request.function_name(),
+                                                     
response.status().DebugString()));
+        }
+        return Status::OK();
+    }
+
+    Status gen_request_data(PFunctionCallRequest& request, const IColumn** 
columns, int start,
+                            int end, const DataTypes& argument_types) {
+        for (int i = 0; i < argument_types.size(); i++) {
+            PValues* arg = request.add_args();
+            if (auto* nullable = vectorized::check_and_get_column<const 
vectorized::ColumnNullable>(
+                        *columns[i])) {
+                auto data_col = nullable->get_nested_column_ptr();
+                auto& null_col = nullable->get_null_map_column();
+                auto data_type = std::reinterpret_pointer_cast<const 
vectorized::DataTypeNullable>(
+                        argument_types[i]);
+                data_col->get_data_at(0);
+                
convert_nullable_col_to_pvalue(data_col->convert_to_full_column_if_const(),
+                                               data_type->get_nested_type(), 
null_col, arg, start,
+                                               end);
+
+            } else {
+                
convert_col_to_pvalue<false>(columns[i]->convert_to_full_column_if_const(),
+                                             argument_types[i], arg, start, 
end);
+            }
+        }
+        return Status::OK();
+    }
+
+#ifndef RPC_ADD
+#define RPC_ADD

Review Comment:
   no use?



-- 
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