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