Coverage Report

Created: 2026-08-19 13:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/udf/python/python_client.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <arrow/status.h>
21
22
#include "arrow/flight/client.h"
23
#include "common/status.h"
24
#include "format/arrow/arrow_utils.h"
25
#include "storage/olap_define.h"
26
#include "udf/python/python_udf_meta.h"
27
#include "udf/python/python_udf_runtime.h"
28
29
namespace doris {
30
31
/**
32
 * Base class for Python UDF/UDAF/UDTF clients
33
 * 
34
 * Provides common functionality for communicating with Python server via Arrow Flight:
35
 * - Connection management
36
 * - Stream initialization
37
 * - Error handling
38
 * - Process lifecycle management
39
 */
40
class PythonClient {
41
public:
42
    using FlightDescriptor = arrow::flight::FlightDescriptor;
43
    using FlightClient = arrow::flight::FlightClient;
44
    using FlightStreamWriter = arrow::flight::FlightStreamWriter;
45
    using FlightStreamReader = arrow::flight::FlightStreamReader;
46
47
0
    PythonClient() = default;
48
0
    virtual ~PythonClient() = default;
49
50
    /**
51
     * Initialize connection to Python server
52
     * @param func_meta Function metadata (contains client_type for operation name)
53
     * @param process Python process handle
54
     * @return Status
55
     */
56
    Status init(const PythonUDFMeta& func_meta, ProcessPtr process);
57
58
    /**
59
     * Close connection and cleanup resources
60
     * @return Status
61
     */
62
    Status close();
63
64
    /**
65
     * Handle Arrow Flight error
66
     * @param status Arrow status
67
     * @return Doris Status with formatted error message
68
     */
69
    Status handle_error(arrow::Status status);
70
71
    /**
72
     * Get process information for debugging
73
     * @return Process string representation
74
     */
75
0
    std::string print_process() const { return _process ? _process->to_string() : "null"; }
76
77
    /**
78
     * Get the underlying Python process
79
     * @return Process pointer
80
     */
81
0
    ProcessPtr get_process() const { return _process; }
82
83
protected:
84
    /**
85
     * Begin Flight stream with schema (called only once per stream)
86
     * @param schema Input schema
87
     * @return Status
88
     */
89
    Status begin_stream(const std::shared_ptr<arrow::Schema>& schema);
90
91
    /**
92
     * Write RecordBatch to server
93
     * @param input Input RecordBatch
94
     * @return Status
95
     */
96
    Status write_batch(const arrow::RecordBatch& input);
97
98
    /**
99
     * Read RecordBatch from server
100
     * @param output Output RecordBatch
101
     * @return Status
102
     */
103
    Status read_batch(std::shared_ptr<arrow::RecordBatch>* output);
104
105
    // Common state
106
    bool _inited = false;
107
    bool _begin = false;         // Track if Begin() has been called
108
    std::string _operation_name; // Operation name for error messages
109
    std::unique_ptr<FlightClient> _arrow_client;
110
    std::unique_ptr<FlightStreamWriter> _writer;
111
    std::unique_ptr<FlightStreamReader> _reader;
112
    ProcessPtr _process;
113
114
private:
115
    DISALLOW_COPY_AND_ASSIGN(PythonClient);
116
};
117
118
} // namespace doris