Coverage Report

Created: 2026-07-16 17:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/udf/python/python_udaf_client.cpp
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
#include "udf/python/python_udaf_client.h"
19
20
#include <arrow/array/builder_base.h>
21
#include <arrow/array/builder_binary.h>
22
#include <arrow/array/builder_primitive.h>
23
#include <arrow/flight/client.h>
24
#include <arrow/flight/server.h>
25
#include <arrow/io/memory.h>
26
#include <arrow/ipc/writer.h>
27
#include <arrow/record_batch.h>
28
#include <arrow/type.h>
29
30
#include "common/compiler_util.h"
31
#include "common/status.h"
32
#include "format/arrow/arrow_utils.h"
33
#include "runtime/exec_env.h"
34
#include "udf/python/python_udf_meta.h"
35
#include "udf/python/python_udf_runtime.h"
36
#include "util/unaligned.h"
37
38
namespace doris {
39
40
// Unified response structure for UDAF operations
41
// Arrow Schema: [success: bool, rows_processed: int64, data: binary]
42
// Different operations use different fields:
43
// - CREATE/MERGE/RESET/DESTROY: use success only
44
// - ACCUMULATE: use success + rows_processed (number of rows processed)
45
// - SERIALIZE: use success + data (serialized_state)
46
// - FINALIZE: use success + data (serialized result, may be null)
47
// - Any failed operation: use success=false + data (UTF-8 error message)
48
//
49
// This unified schema allows all operations to return consistent format,
50
// solving Arrow Flight's limitation that all responses must have the same schema.
51
static const std::shared_ptr<arrow::Schema> kUnifiedUDAFResponseSchema = arrow::schema({
52
        arrow::field("success", arrow::boolean()),
53
        arrow::field("rows_processed", arrow::int64()),
54
        arrow::field("serialized_data", arrow::binary()),
55
});
56
57
Status PythonUDAFClient::make_udaf_failure_status(
58
        const std::shared_ptr<arrow::RecordBatch>& response, const char* operation,
59
7
        int64_t place_id) {
60
7
    if (response == nullptr || response->num_rows() != 1 ||
61
7
        response->num_columns() != kUnifiedUDAFResponseSchema->num_fields()) [[unlikely]] {
62
3
        return Status::InternalError("Invalid {} failure response for place_id={}", operation,
63
3
                                     place_id);
64
3
    }
65
66
4
    auto data_array = std::static_pointer_cast<arrow::BinaryArray>(response->column(2));
67
4
    if (data_array->IsNull(0)) {
68
1
        return Status::InternalError("{} operation failed for place_id={}", operation, place_id);
69
1
    }
70
71
3
    const auto* offsets = data_array->raw_value_offsets();
72
3
    if (offsets == nullptr) [[unlikely]] {
73
0
        return Status::InternalError("Invalid {} failure response for place_id={}: null offsets",
74
0
                                     operation, place_id);
75
0
    }
76
    // Arrow Flight buffers may be unaligned after IPC deserialization
77
3
    int32_t offset_start = unaligned_load<int32_t>(offsets);
78
3
    int32_t offset_end = unaligned_load<int32_t>(offsets + 1);
79
80
3
    int32_t length = offset_end - offset_start;
81
3
    if (length <= 0) {
82
1
        return Status::InternalError("{} operation failed for place_id={}", operation, place_id);
83
1
    }
84
2
    const uint8_t* data = data_array->value_data()->data() + offset_start;
85
2
    std::string error_message(reinterpret_cast<const char*>(data), length);
86
2
    return Status::InternalError("{} operation failed for place_id={}: {}", operation, place_id,
87
2
                                 error_message);
88
3
}
89
90
#ifdef BE_TEST
91
Status PythonUDAFClient::make_udaf_failure_status_for_test(
92
        const std::shared_ptr<arrow::RecordBatch>& response, const char* operation,
93
        int64_t place_id) {
94
    return make_udaf_failure_status(response, operation, place_id);
95
}
96
#endif
97
98
Status PythonUDAFClient::create(const PythonUDFMeta& func_meta, ProcessPtr process,
99
                                const std::shared_ptr<arrow::Schema>& data_schema,
100
0
                                PythonUDAFClientPtr* client) {
101
0
    PythonUDAFClientPtr python_udaf_client = std::make_shared<PythonUDAFClient>();
102
0
    RETURN_IF_ERROR(python_udaf_client->init(func_meta, std::move(process), data_schema));
103
0
    *client = std::move(python_udaf_client);
104
0
    return Status::OK();
105
0
}
106
107
Status PythonUDAFClient::init(const PythonUDFMeta& func_meta, ProcessPtr process,
108
0
                              const std::shared_ptr<arrow::Schema>& data_schema) {
109
0
    _schema = data_schema;
110
0
    return PythonClient::init(func_meta, std::move(process));
111
0
}
112
113
0
Status PythonUDAFClient::create(int64_t place_id) {
114
0
    std::shared_ptr<arrow::RecordBatch> request_batch;
115
0
    RETURN_IF_ERROR(_get_empty_request_batch(&request_batch));
116
117
0
    UDAFMetadata metadata {
118
0
            .meta_version = UDAF_METADATA_VERSION,
119
0
            .operation = static_cast<uint8_t>(UDAFOperation::CREATE),
120
0
            .is_single_place = 0,
121
0
            .place_id = place_id,
122
0
            .row_start = 0,
123
0
            .row_end = 0,
124
0
    };
125
126
0
    std::shared_ptr<arrow::RecordBatch> response_batch;
127
0
    RETURN_IF_ERROR(_send_request(metadata, request_batch, &response_batch));
128
129
    // Parse unified response_batch: [success: bool, rows_processed: int64, serialized_data: binary]
130
0
    if (response_batch->num_rows() != 1) {
131
0
        return Status::InternalError("Invalid CREATE response_batch: expected 1 row");
132
0
    }
133
134
0
    auto success_array = std::static_pointer_cast<arrow::BooleanArray>(response_batch->column(0));
135
0
    if (!success_array->Value(0)) {
136
0
        return make_udaf_failure_status(response_batch, "CREATE", place_id);
137
0
    }
138
139
0
    _created_place_id = place_id;
140
0
    return Status::OK();
141
0
}
142
143
Status PythonUDAFClient::accumulate(int64_t place_id, bool is_single_place,
144
                                    const arrow::RecordBatch& input, int64_t row_start,
145
0
                                    int64_t row_end) {
146
    // Validate input parameters
147
0
    if (UNLIKELY(row_start < 0 || row_end < row_start || row_end > input.num_rows())) {
148
0
        return Status::InvalidArgument(
149
0
                "Invalid row range: row_start={}, row_end={}, input.num_rows={}", row_start,
150
0
                row_end, input.num_rows());
151
0
    }
152
153
    // In multi-place mode, input RecordBatch must contain "places" column as last column
154
0
    if (UNLIKELY(!is_single_place &&
155
0
                 (input.num_columns() == 0 ||
156
0
                  input.schema()->field(input.num_columns() - 1)->name() != "places"))) {
157
0
        return Status::InternalError(
158
0
                "In multi-place mode, input RecordBatch must contain 'places' column as the "
159
0
                "last column");
160
0
    }
161
162
    // Create request batch: input data + NULL binary_data column
163
0
    std::shared_ptr<arrow::RecordBatch> request_batch;
164
0
    RETURN_IF_ERROR(_create_data_request_batch(input, &request_batch));
165
166
    // Create metadata structure
167
0
    UDAFMetadata metadata {
168
0
            .meta_version = UDAF_METADATA_VERSION,
169
0
            .operation = static_cast<uint8_t>(UDAFOperation::ACCUMULATE),
170
0
            .is_single_place = static_cast<uint8_t>(is_single_place ? 1 : 0),
171
0
            .place_id = place_id,
172
0
            .row_start = row_start,
173
0
            .row_end = row_end,
174
0
    };
175
176
    // Send to server with metadata in app_metadata
177
0
    std::shared_ptr<arrow::RecordBatch> response;
178
0
    RETURN_IF_ERROR(_send_request(metadata, request_batch, &response));
179
180
    // Parse unified response: [success: bool, rows_processed: int64, serialized_data: binary]
181
0
    if (response->num_rows() != 1) {
182
0
        return Status::InternalError("Invalid ACCUMULATE response: expected 1 row");
183
0
    }
184
185
0
    auto success_array = std::static_pointer_cast<arrow::BooleanArray>(response->column(0));
186
0
    auto rows_processed_array = std::static_pointer_cast<arrow::Int64Array>(response->column(1));
187
188
0
    if (!success_array->Value(0)) {
189
0
        return make_udaf_failure_status(response, "ACCUMULATE", place_id);
190
0
    }
191
192
    // Arrow Flight buffers may be unaligned after IPC deserialization.
193
0
    const auto* raw_ptr = rows_processed_array->raw_values();
194
0
    if (raw_ptr == nullptr) {
195
0
        return Status::InternalError("ACCUMULATE response has null rows_processed array");
196
0
    }
197
0
    int64_t rows_processed = unaligned_load<int64_t>(raw_ptr);
198
199
0
    int64_t expected_rows = row_end - row_start;
200
201
0
    if (rows_processed < expected_rows) {
202
0
        return Status::InternalError(
203
0
                "ACCUMULATE operation only processed {} out of {} rows for place_id={}",
204
0
                rows_processed, expected_rows, place_id);
205
0
    }
206
0
    return Status::OK();
207
0
}
208
209
Status PythonUDAFClient::serialize(int64_t place_id,
210
0
                                   std::shared_ptr<arrow::Buffer>* serialized_state) {
211
0
    std::shared_ptr<arrow::RecordBatch> request_batch;
212
0
    RETURN_IF_ERROR(_get_empty_request_batch(&request_batch));
213
214
0
    UDAFMetadata metadata {
215
0
            .meta_version = UDAF_METADATA_VERSION,
216
0
            .operation = static_cast<uint8_t>(UDAFOperation::SERIALIZE),
217
0
            .is_single_place = 0,
218
0
            .place_id = place_id,
219
0
            .row_start = 0,
220
0
            .row_end = 0,
221
0
    };
222
223
0
    std::shared_ptr<arrow::RecordBatch> response;
224
0
    RETURN_IF_ERROR(_send_request(metadata, request_batch, &response));
225
226
    // Parse unified response: [success: bool, rows_processed: int64, serialized_data: binary]
227
0
    auto success_array = std::static_pointer_cast<arrow::BooleanArray>(response->column(0));
228
0
    auto data_array = std::static_pointer_cast<arrow::BinaryArray>(response->column(2));
229
230
0
    if (!success_array->Value(0)) {
231
0
        return make_udaf_failure_status(response, "SERIALIZE", place_id);
232
0
    }
233
234
    // Arrow Flight buffers may be unaligned after IPC deserialization.
235
0
    const auto* offsets = data_array->raw_value_offsets();
236
0
    if (offsets == nullptr) {
237
0
        return Status::InternalError("SERIALIZE response has null offsets");
238
0
    }
239
0
    int32_t offset_start = unaligned_load<int32_t>(offsets);
240
0
    int32_t offset_end = unaligned_load<int32_t>(offsets + 1);
241
242
0
    int32_t length = offset_end - offset_start;
243
244
0
    if (length == 0) {
245
0
        return Status::InternalError("SERIALIZE operation returned empty data for place_id={}",
246
0
                                     place_id);
247
0
    }
248
249
0
    const uint8_t* data = data_array->value_data()->data() + offset_start;
250
0
    *serialized_state = arrow::Buffer::Wrap(data, length);
251
0
    return Status::OK();
252
0
}
253
254
Status PythonUDAFClient::merge(int64_t place_id,
255
0
                               const std::shared_ptr<arrow::Buffer>& serialized_state) {
256
0
    std::shared_ptr<arrow::RecordBatch> request_batch;
257
0
    RETURN_IF_ERROR(_create_binary_request_batch(serialized_state, &request_batch));
258
259
0
    UDAFMetadata metadata {
260
0
            .meta_version = UDAF_METADATA_VERSION,
261
0
            .operation = static_cast<uint8_t>(UDAFOperation::MERGE),
262
0
            .is_single_place = 0,
263
0
            .place_id = place_id,
264
0
            .row_start = 0,
265
0
            .row_end = 0,
266
0
    };
267
268
0
    std::shared_ptr<arrow::RecordBatch> response;
269
0
    RETURN_IF_ERROR(_send_request(metadata, request_batch, &response));
270
271
    // Parse unified response: [success: bool, rows_processed: int64, serialized_data: binary]
272
0
    if (response->num_rows() != 1) {
273
0
        return Status::InternalError("Invalid MERGE response: expected 1 row");
274
0
    }
275
276
0
    auto success_array = std::static_pointer_cast<arrow::BooleanArray>(response->column(0));
277
0
    if (!success_array->Value(0)) {
278
0
        return make_udaf_failure_status(response, "MERGE", place_id);
279
0
    }
280
281
0
    return Status::OK();
282
0
}
283
284
0
Status PythonUDAFClient::finalize(int64_t place_id, std::shared_ptr<arrow::RecordBatch>* output) {
285
0
    std::shared_ptr<arrow::RecordBatch> request_batch;
286
0
    RETURN_IF_ERROR(_get_empty_request_batch(&request_batch));
287
288
0
    UDAFMetadata metadata {
289
0
            .meta_version = UDAF_METADATA_VERSION,
290
0
            .operation = static_cast<uint8_t>(UDAFOperation::FINALIZE),
291
0
            .is_single_place = 0,
292
0
            .place_id = place_id,
293
0
            .row_start = 0,
294
0
            .row_end = 0,
295
0
    };
296
297
0
    std::shared_ptr<arrow::RecordBatch> response_batch;
298
0
    RETURN_IF_ERROR(_send_request(metadata, request_batch, &response_batch));
299
300
    // Parse unified response_batch: [success: bool, rows_processed: int64, serialized_data: binary]
301
0
    auto success_array = std::static_pointer_cast<arrow::BooleanArray>(response_batch->column(0));
302
0
    auto data_array = std::static_pointer_cast<arrow::BinaryArray>(response_batch->column(2));
303
304
0
    if (!success_array->Value(0)) {
305
0
        return make_udaf_failure_status(response_batch, "FINALIZE", place_id);
306
0
    }
307
308
    // Arrow Flight buffers may be unaligned after IPC deserialization.
309
0
    const auto* offsets = data_array->raw_value_offsets();
310
0
    if (offsets == nullptr) {
311
0
        return Status::InternalError("FINALIZE response has null offsets");
312
0
    }
313
0
    int32_t offset_start = unaligned_load<int32_t>(offsets);
314
0
    int32_t offset_end = unaligned_load<int32_t>(offsets + 1);
315
316
0
    int32_t length = offset_end - offset_start;
317
318
0
    if (length == 0) {
319
0
        return Status::InternalError("FINALIZE operation returned empty data for place_id={}",
320
0
                                     place_id);
321
0
    }
322
323
0
    const uint8_t* data = data_array->value_data()->data() + offset_start;
324
0
    auto buffer = arrow::Buffer::Wrap(data, length);
325
0
    auto input_stream = std::make_shared<arrow::io::BufferReader>(buffer);
326
327
0
    auto reader_result = arrow::ipc::RecordBatchStreamReader::Open(input_stream);
328
0
    if (UNLIKELY(!reader_result.ok())) {
329
0
        return Status::InternalError("Failed to deserialize FINALIZE result: {}",
330
0
                                     reader_result.status().message());
331
0
    }
332
0
    auto reader = std::move(reader_result).ValueOrDie();
333
334
0
    auto batch_result = reader->Next();
335
0
    if (UNLIKELY(!batch_result.ok())) {
336
0
        return Status::InternalError("Failed to read FINALIZE result: {}",
337
0
                                     batch_result.status().message());
338
0
    }
339
340
0
    *output = std::move(batch_result).ValueOrDie();
341
342
0
    return Status::OK();
343
0
}
344
345
0
Status PythonUDAFClient::reset(int64_t place_id) {
346
0
    std::shared_ptr<arrow::RecordBatch> request_batch;
347
0
    RETURN_IF_ERROR(_get_empty_request_batch(&request_batch));
348
349
0
    UDAFMetadata metadata {
350
0
            .meta_version = UDAF_METADATA_VERSION,
351
0
            .operation = static_cast<uint8_t>(UDAFOperation::RESET),
352
0
            .is_single_place = 0,
353
0
            .place_id = place_id,
354
0
            .row_start = 0,
355
0
            .row_end = 0,
356
0
    };
357
358
0
    std::shared_ptr<arrow::RecordBatch> response;
359
0
    RETURN_IF_ERROR(_send_request(metadata, request_batch, &response));
360
361
    // Parse unified response: [success: bool, rows_processed: int64, serialized_data: binary]
362
0
    if (response->num_rows() != 1) {
363
0
        return Status::InternalError("Invalid RESET response: expected 1 row");
364
0
    }
365
366
0
    auto success_array = std::static_pointer_cast<arrow::BooleanArray>(response->column(0));
367
0
    if (!success_array->Value(0)) {
368
0
        return make_udaf_failure_status(response, "RESET", place_id);
369
0
    }
370
371
0
    return Status::OK();
372
0
}
373
374
0
Status PythonUDAFClient::destroy(int64_t place_id) {
375
0
    std::shared_ptr<arrow::RecordBatch> request_batch;
376
0
    RETURN_IF_ERROR(_get_empty_request_batch(&request_batch));
377
378
0
    UDAFMetadata metadata {
379
0
            .meta_version = UDAF_METADATA_VERSION,
380
0
            .operation = static_cast<uint8_t>(UDAFOperation::DESTROY),
381
0
            .is_single_place = 0,
382
0
            .place_id = place_id,
383
0
            .row_start = 0,
384
0
            .row_end = 0,
385
0
    };
386
387
0
    std::shared_ptr<arrow::RecordBatch> response;
388
0
    Status st = _send_request(metadata, request_batch, &response);
389
390
    // Always clear tracking, even if RPC failed
391
0
    _created_place_id.reset();
392
393
0
    if (!st.ok()) {
394
0
        LOG(WARNING) << "Failed to destroy place_id=" << place_id << ": " << st.to_string();
395
0
        return st;
396
0
    }
397
398
    // Parse unified response: [success: bool, rows_processed: int64, serialized_data: binary]
399
0
    if (response->num_rows() != 1) {
400
0
        return Status::InternalError("Invalid DESTROY response: expected 1 row");
401
0
    }
402
403
0
    auto success_array = std::static_pointer_cast<arrow::BooleanArray>(response->column(0));
404
405
0
    if (!success_array->Value(0)) {
406
0
        LOG(WARNING) << "DESTROY operation failed for place_id=" << place_id;
407
0
        return make_udaf_failure_status(response, "DESTROY", place_id);
408
0
    }
409
410
0
    return Status::OK();
411
0
}
412
413
0
Status PythonUDAFClient::close() {
414
0
    if (!_inited || !_writer) return Status::OK();
415
416
    // Destroy the place if it exists (cleanup on client destruction)
417
0
    if (_created_place_id.has_value()) {
418
0
        int64_t place_id = _created_place_id.value();
419
0
        Status st = destroy(place_id);
420
0
        if (!st.ok()) {
421
0
            LOG(WARNING) << "Failed to destroy place_id=" << place_id
422
0
                         << " during close: " << st.to_string();
423
            // Clear tracking even on failure to prevent issues in base class close
424
0
            _created_place_id.reset();
425
0
        }
426
0
    }
427
428
0
    return PythonClient::close();
429
0
}
430
431
Status PythonUDAFClient::_send_request(const UDAFMetadata& metadata,
432
                                       const std::shared_ptr<arrow::RecordBatch>& request_batch,
433
0
                                       std::shared_ptr<arrow::RecordBatch>* response_batch) {
434
0
    DCHECK(response_batch != nullptr);
435
436
    // Create app_metadata buffer from metadata struct
437
0
    auto app_metadata =
438
0
            arrow::Buffer::Wrap(reinterpret_cast<const uint8_t*>(&metadata), sizeof(metadata));
439
440
0
    std::lock_guard<std::mutex> lock(_operation_mutex);
441
442
    // Check if writer/reader are still valid (they could be reset by handle_error)
443
0
    if (UNLIKELY(!_writer || !_reader)) {
444
0
        return Status::InternalError("{} writer/reader have been closed due to previous error",
445
0
                                     _operation_name);
446
0
    }
447
448
    // Begin stream on first call (using data schema: argument_types + places + binary_data)
449
0
    if (UNLIKELY(!_begin)) {
450
0
        auto begin_res = _writer->Begin(_schema);
451
0
        if (!begin_res.ok()) {
452
0
            return handle_error(begin_res);
453
0
        }
454
0
        _begin = true;
455
0
    }
456
457
    // Write batch with metadata in app_metadata
458
0
    auto write_res = _writer->WriteWithMetadata(*request_batch, app_metadata);
459
0
    if (!write_res.ok()) {
460
0
        return handle_error(write_res);
461
0
    }
462
463
    // Read unified response: [success: bool, rows_processed: int64, serialized_data: binary]
464
0
    auto read_res = _reader->Next();
465
0
    if (!read_res.ok()) {
466
0
        return handle_error(read_res.status());
467
0
    }
468
469
0
    arrow::flight::FlightStreamChunk chunk = std::move(*read_res);
470
0
    if (!chunk.data) {
471
0
        return Status::InternalError("Received empty RecordBatch from {} server", _operation_name);
472
0
    }
473
474
    // Validate unified response schema
475
0
    if (!chunk.data->schema()->Equals(kUnifiedUDAFResponseSchema)) {
476
0
        return Status::InternalError(
477
0
                "Invalid response schema: expected [success: bool, rows_processed: int64, "
478
0
                "serialized_data: binary], got {}",
479
0
                chunk.data->schema()->ToString());
480
0
    }
481
482
0
    *response_batch = std::move(chunk.data);
483
0
    return Status::OK();
484
0
}
485
486
Status PythonUDAFClient::_create_data_request_batch(const arrow::RecordBatch& input_data,
487
0
                                                    std::shared_ptr<arrow::RecordBatch>* out) {
488
    // Determine if input has places column
489
0
    int num_input_columns = input_data.num_columns();
490
0
    bool has_places = false;
491
0
    if (num_input_columns > 0 &&
492
0
        input_data.schema()->field(num_input_columns - 1)->name() == "places") {
493
0
        has_places = true;
494
0
    }
495
496
    // Expected schema structure: [argument_types..., places, binary_data]
497
    // - Input in single-place mode: [argument_types...]
498
    // - Input in multi-place mode: [argument_types..., places]
499
0
    std::vector<std::shared_ptr<arrow::Array>> columns;
500
    // Copy argument_types columns
501
0
    int num_arg_columns = has_places ? (num_input_columns - 1) : num_input_columns;
502
503
0
    for (int i = 0; i < num_arg_columns; ++i) {
504
0
        columns.push_back(input_data.column(i));
505
0
    }
506
507
    // Add places column
508
0
    if (has_places) {
509
        // Use existing places column from input
510
0
        columns.push_back(input_data.column(num_input_columns - 1));
511
0
    } else {
512
        // Create NULL places column for single-place mode
513
0
        arrow::Int64Builder places_builder(ExecEnv::GetInstance()->arrow_memory_pool());
514
0
        std::shared_ptr<arrow::Array> places_array;
515
0
        RETURN_DORIS_STATUS_IF_ERROR(places_builder.AppendNulls(input_data.num_rows()));
516
0
        RETURN_DORIS_STATUS_IF_ERROR(places_builder.Finish(&places_array));
517
0
        columns.push_back(places_array);
518
0
    }
519
520
    // Add NULL binary_data column
521
0
    arrow::BinaryBuilder binary_builder(ExecEnv::GetInstance()->arrow_memory_pool());
522
0
    std::shared_ptr<arrow::Array> binary_array;
523
0
    RETURN_DORIS_STATUS_IF_ERROR(binary_builder.AppendNulls(input_data.num_rows()));
524
0
    RETURN_DORIS_STATUS_IF_ERROR(binary_builder.Finish(&binary_array));
525
0
    columns.push_back(binary_array);
526
527
0
    *out = arrow::RecordBatch::Make(_schema, input_data.num_rows(), columns);
528
0
    return Status::OK();
529
0
}
530
531
Status PythonUDAFClient::_create_binary_request_batch(
532
        const std::shared_ptr<arrow::Buffer>& binary_data,
533
0
        std::shared_ptr<arrow::RecordBatch>* out) {
534
0
    std::vector<std::shared_ptr<arrow::Array>> columns;
535
536
    // Create NULL arrays for data columns (all columns except the last binary_data column)
537
    // Schema: [argument_types..., places, binary_data]
538
0
    int num_data_columns = _schema->num_fields() - 1;
539
0
    for (int i = 0; i < num_data_columns; ++i) {
540
0
        std::unique_ptr<arrow::ArrayBuilder> builder;
541
0
        std::shared_ptr<arrow::Array> null_array;
542
0
        RETURN_DORIS_STATUS_IF_ERROR(arrow::MakeBuilder(ExecEnv::GetInstance()->arrow_memory_pool(),
543
0
                                                        _schema->field(i)->type(), &builder));
544
0
        RETURN_DORIS_STATUS_IF_ERROR(builder->AppendNull());
545
0
        RETURN_DORIS_STATUS_IF_ERROR(builder->Finish(&null_array));
546
0
        columns.push_back(null_array);
547
0
    }
548
549
    // Create binary_data column
550
0
    arrow::BinaryBuilder binary_builder(ExecEnv::GetInstance()->arrow_memory_pool());
551
0
    std::shared_ptr<arrow::Array> binary_array;
552
0
    RETURN_DORIS_STATUS_IF_ERROR(
553
0
            binary_builder.Append(binary_data->data(), static_cast<int32_t>(binary_data->size())));
554
0
    RETURN_DORIS_STATUS_IF_ERROR(binary_builder.Finish(&binary_array));
555
0
    columns.push_back(binary_array);
556
557
0
    *out = arrow::RecordBatch::Make(_schema, 1, columns);
558
0
    return Status::OK();
559
0
}
560
561
0
Status PythonUDAFClient::_get_empty_request_batch(std::shared_ptr<arrow::RecordBatch>* out) {
562
    // Return cached batch if already created
563
0
    if (_empty_request_batch) {
564
0
        *out = _empty_request_batch;
565
0
        return Status::OK();
566
0
    }
567
568
    // Create empty batch on first use (all columns NULL, 1 row)
569
0
    std::vector<std::shared_ptr<arrow::Array>> columns;
570
571
0
    for (int i = 0; i < _schema->num_fields(); ++i) {
572
0
        auto field = _schema->field(i);
573
0
        std::unique_ptr<arrow::ArrayBuilder> builder;
574
0
        std::shared_ptr<arrow::Array> null_array;
575
0
        RETURN_DORIS_STATUS_IF_ERROR(arrow::MakeBuilder(ExecEnv::GetInstance()->arrow_memory_pool(),
576
0
                                                        field->type(), &builder));
577
0
        RETURN_DORIS_STATUS_IF_ERROR(builder->AppendNull());
578
0
        RETURN_DORIS_STATUS_IF_ERROR(builder->Finish(&null_array));
579
0
        columns.push_back(null_array);
580
0
    }
581
582
0
    _empty_request_batch = arrow::RecordBatch::Make(_schema, 1, columns);
583
0
    *out = _empty_request_batch;
584
0
    return Status::OK();
585
0
}
586
587
} // namespace doris