Coverage Report

Created: 2026-04-14 20:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/table_function/python_udtf_function.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/record_batch.h>
21
#include <cctz/time_zone.h>
22
23
#include "common/status.h"
24
#include "core/column/column.h"
25
#include "core/data_type/data_type.h"
26
#include "exprs/function/array/function_array_utils.h"
27
#include "exprs/table_function/table_function.h"
28
#include "udf/python/python_udtf_client.h"
29
30
namespace doris {
31
32
/**
33
 * PythonUDTFFunction - Python User-Defined Table Function
34
 * 
35
 * Execution Flow:
36
 * 1. open() - Create Python UDTF client and establish RPC connection
37
 * 2. process_init(block) - Batch evaluate all rows:
38
 *    - Convert input block to Arrow RecordBatch
39
 *    - Call Python UDTF server via RPC (evaluates all rows in one call)
40
 *    - Receive Arrow ListArray (one list per input row)
41
 *    - Convert to array<T> column using DataTypeArraySerDe
42
 * 3. process_row(row_idx) - Set array offset for current row
43
 * 4. get_value()/get_same_many_values() - Extract values from array column
44
 * 5. process_close() - Clean up batch state (array column, offsets)
45
 * 6. close() - Close Python UDTF client and RPC connection
46
 */
47
class PythonUDTFFunction final : public TableFunction {
48
    ENABLE_FACTORY_CREATOR(PythonUDTFFunction);
49
50
public:
51
    PythonUDTFFunction(const TFunction& t_fn);
52
0
    ~PythonUDTFFunction() override = default;
53
54
    Status open() override;
55
    Status process_init(Block* block, RuntimeState* state) override;
56
    void process_row(size_t row_idx) override;
57
    void process_close() override;
58
    void get_same_many_values(MutableColumnPtr& column, int length) override;
59
    int get_value(MutableColumnPtr& column, int max_step) override;
60
    Status close() override;
61
62
private:
63
    /**
64
     * Convert Python UDTF output (Arrow ListArray) to Doris array column
65
     * 
66
     * Input from Python server (via Arrow RPC):
67
     * - list_array: Arrow ListArray where each element corresponds to one input row's outputs
68
     *
69
     * Format:
70
     * - Single-column output: List<T> (e.g., List<int>, List<string>)
71
     * - Multi-column output: List<Struct<col1, col2, ...>>
72
     *
73
     * Example: 3 input rows producing variable output rows
74
     *   ListArray structure:
75
     *     [0]: [val1, val2, val3]       (3 elements)
76
     *     [1]: []                       (0 elements - empty array)
77
     *     [2]: [val4, val5, val6, val7] (4 elements)
78
     *
79
     * @param list_array Arrow ListArray containing UDTF output (length = num_input_rows)
80
     * @return Status indicating success or validation/conversion errors
81
     */
82
    Status _convert_list_array_to_array_column(const std::shared_ptr<arrow::ListArray>& list_array);
83
84
    const TFunction& _t_fn;
85
    DataTypePtr _return_type;
86
    PythonUDTFClientPtr _udtf_client;
87
    cctz::time_zone _timezone_obj;
88
89
    // Result storage (similar to Java UDTF)
90
    ColumnPtr _array_result_column;                // Array column storing all results
91
    ColumnArrayExecutionData _array_column_detail; // Array metadata for efficient access
92
    int64_t _array_offset = 0;                     // Offset into array for current row
93
};
94
95
} // namespace doris