Coverage Report

Created: 2026-03-16 13:13

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 "udf/python/python_udf_meta.h"
26
#include "udf/python/python_udf_runtime.h"
27
28
namespace doris {
29
30
/**
31
 * Base class for Python UDF/UDAF/UDTF clients
32
 * 
33
 * Provides common functionality for communicating with Python server via Arrow Flight:
34
 * - Connection management
35
 * - Stream initialization
36
 * - Error handling
37
 * - Process lifecycle management
38
 */
39
class PythonClient {
40
public:
41
    using FlightDescriptor = arrow::flight::FlightDescriptor;
42
    using FlightClient = arrow::flight::FlightClient;
43
    using FlightStreamWriter = arrow::flight::FlightStreamWriter;
44
    using FlightStreamReader = arrow::flight::FlightStreamReader;
45
46
6.95k
    PythonClient() = default;
47
6.94k
    virtual ~PythonClient() = default;
48
49
    /**
50
     * Initialize connection to Python server
51
     * @param func_meta Function metadata (contains client_type for operation name)
52
     * @param process Python process handle
53
     * @return Status
54
     */
55
    Status init(const PythonUDFMeta& func_meta, ProcessPtr process);
56
57
    /**
58
     * Close connection and cleanup resources
59
     * @return Status
60
     */
61
    Status close();
62
63
    /**
64
     * Handle Arrow Flight error
65
     * @param status Arrow status
66
     * @return Doris Status with formatted error message
67
     */
68
    Status handle_error(arrow::Status status);
69
70
    /**
71
     * Get process information for debugging
72
     * @return Process string representation
73
     */
74
1.40k
    std::string print_process() const { return _process ? _process->to_string() : "null"; }
75
76
    /**
77
     * Get the underlying Python process
78
     * @return Process pointer
79
     */
80
1.94k
    ProcessPtr get_process() const { return _process; }
81
82
protected:
83
    /**
84
     * Begin Flight stream with schema (called only once per stream)
85
     * @param schema Input schema
86
     * @return Status
87
     */
88
    Status begin_stream(const std::shared_ptr<arrow::Schema>& schema);
89
90
    /**
91
     * Write RecordBatch to server
92
     * @param input Input RecordBatch
93
     * @return Status
94
     */
95
    Status write_batch(const arrow::RecordBatch& input);
96
97
    /**
98
     * Read RecordBatch from server
99
     * @param output Output RecordBatch
100
     * @return Status
101
     */
102
    Status read_batch(std::shared_ptr<arrow::RecordBatch>* output);
103
104
    // Common state
105
    bool _inited = false;
106
    bool _begin = false;         // Track if Begin() has been called
107
    std::string _operation_name; // Operation name for error messages
108
    std::unique_ptr<FlightClient> _arrow_client;
109
    std::unique_ptr<FlightStreamWriter> _writer;
110
    std::unique_ptr<FlightStreamReader> _reader;
111
    ProcessPtr _process;
112
113
private:
114
    DISALLOW_COPY_AND_ASSIGN(PythonClient);
115
};
116
117
} // namespace doris