sjyango commented on code in PR #57329:
URL: https://github.com/apache/doris/pull/57329#discussion_r2473430168


##########
be/src/udf/python/python_udf_client.cpp:
##########
@@ -0,0 +1,125 @@
+// 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 "udf/python/python_udf_client.h"
+
+#include <utility>
+
+#include "arrow/flight/client.h"
+#include "arrow/flight/server.h"
+#include "common/status.h"
+#include "udf/python/python_udf_meta.h"
+#include "udf/python/python_udf_runtime.h"
+#include "util/arrow/utils.h"
+
+namespace doris {
+
+Status PythonUDFClient::create(const PythonUDFMeta& func_meta, ProcessPtr 
process,
+                               PythonUDFClientPtr* client) {
+    PythonUDFClientPtr python_udf_client = std::make_shared<PythonUDFClient>();
+    RETURN_IF_ERROR(python_udf_client->init(func_meta, std::move(process)));
+    *client = std::move(python_udf_client);
+    return Status::OK();
+}
+
+Status PythonUDFClient::init(const PythonUDFMeta& func_meta, ProcessPtr 
process) {
+    if (_inited) {
+        return Status::InternalError("PythonUDFClient has already been 
initialized");
+    }
+    arrow::flight::Location location;
+    RETURN_DORIS_STATUS_IF_RESULT_ERROR(location,
+                                        
arrow::flight::Location::Parse(process->get_uri()));
+    RETURN_DORIS_STATUS_IF_RESULT_ERROR(_arrow_client, 
FlightClient::Connect(location));
+    std::string command;
+    RETURN_IF_ERROR(func_meta.serialize_to_json(&command));
+    FlightDescriptor descriptor = FlightDescriptor::Command(command);
+    arrow::flight::FlightClient::DoExchangeResult exchange_res;
+    RETURN_DORIS_STATUS_IF_RESULT_ERROR(exchange_res, 
_arrow_client->DoExchange(descriptor));
+    _reader = std::move(exchange_res.reader);
+    _writer = std::move(exchange_res.writer);
+    _process = std::move(process);
+    _inited = true;
+    return Status::OK();
+}
+
+Status PythonUDFClient::evaluate(const arrow::RecordBatch& input,
+                                 std::shared_ptr<arrow::RecordBatch>* output) {
+    if (!_process->is_alive()) {
+        return Status::RuntimeError("Python UDF process is not alive");
+    }
+
+    // Step 1: Begin exchange with schema (only once)
+    if (UNLIKELY(!_begin)) {
+        auto begin_res = _writer->Begin(input.schema());
+        if (!begin_res.ok()) {
+            return handle_error(begin_res);
+        }
+        _begin = true;
+    }
+
+    // Step 2: Write the record batch to server
+    auto write_res = _writer->WriteRecordBatch(input);
+    if (!write_res.ok()) {
+        return handle_error(write_res);
+    }
+
+    // Step 3: Read response from server
+    auto read_res = _reader->Next();
+    if (!read_res.ok()) {
+        return handle_error(read_res.status());
+    }
+
+    arrow::flight::FlightStreamChunk chunk = std::move(*read_res);
+    if (!chunk.data) {
+        _process->shutdown();
+        return Status::InternalError("Received empty RecordBatch from Python 
UDF server");

Review Comment:
   If input is empty, the chunk.data(aka std::shared_ptr<RecordBatch>) is 
empty(means empty RecordBatch), but not nullptr. If it is nullptr, throw 
Exception(Unexpected behavior).



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to