Coverage Report

Created: 2026-03-14 20:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/function_python_udf.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 <gen_cpp/Types_types.h>
21
22
#include <functional>
23
#include <memory>
24
25
#include "common/status.h"
26
#include "core/block/block.h"
27
#include "core/block/column_numbers.h"
28
#include "core/block/columns_with_type_and_name.h"
29
#include "core/data_type/data_type.h"
30
#include "core/types.h"
31
#include "exprs/function/function.h"
32
33
namespace doris {
34
35
class PythonUDFPreparedFunction : public PreparedFunctionImpl {
36
public:
37
    using execute_call_back = std::function<Status(FunctionContext* context, Block& block,
38
                                                   const ColumnNumbers& arguments, uint32_t result,
39
                                                   size_t input_rows_count)>;
40
41
    explicit PythonUDFPreparedFunction(const execute_call_back& func, const std::string& name)
42
0
            : callback_function(func), name(name) {}
43
44
0
    String get_name() const override { return name; }
45
46
protected:
47
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
48
0
                        uint32_t result, size_t input_rows_count) const override {
49
0
        return callback_function(context, block, arguments, result, input_rows_count);
50
0
    }
51
52
0
    bool use_default_implementation_for_nulls() const override { return false; }
53
54
private:
55
    execute_call_back callback_function;
56
    std::string name;
57
};
58
59
class PythonFunctionCall : public IFunctionBase {
60
public:
61
    PythonFunctionCall(const TFunction& fn, const DataTypes& argument_types,
62
                       const DataTypePtr& return_type);
63
64
    static FunctionBasePtr create(const TFunction& fn, const ColumnsWithTypeAndName& argument_types,
65
0
                                  const DataTypePtr& return_type) {
66
0
        DataTypes data_types(argument_types.size());
67
0
        for (size_t i = 0; i < argument_types.size(); ++i) {
68
0
            data_types[i] = argument_types[i].type;
69
0
        }
70
0
        return std::make_shared<PythonFunctionCall>(fn, data_types, return_type);
71
0
    }
72
73
    /// Get the main function name.
74
0
    String get_name() const override { return _fn.name.function_name; }
75
76
0
    const DataTypes& get_argument_types() const override { return _argument_types; }
77
0
    const DataTypePtr& get_return_type() const override { return _return_type; }
78
79
    PreparedFunctionPtr prepare(FunctionContext* context, const Block& sample_block,
80
0
                                const ColumnNumbers& arguments, uint32_t result) const override {
81
0
        return std::make_shared<PythonUDFPreparedFunction>(
82
0
                [this](auto&& PH1, auto&& PH2, auto&& PH3, auto&& PH4, auto&& PH5) {
83
0
                    return PythonFunctionCall::execute_impl(
84
0
                            std::forward<decltype(PH1)>(PH1), std::forward<decltype(PH2)>(PH2),
85
0
                            std::forward<decltype(PH3)>(PH3), std::forward<decltype(PH4)>(PH4),
86
0
                            std::forward<decltype(PH5)>(PH5));
87
0
                },
88
0
                _fn.name.function_name);
89
0
    }
90
91
    Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override;
92
93
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
94
                        uint32_t result, size_t input_rows_count) const;
95
96
    Status close(FunctionContext* context, FunctionContext::FunctionStateScope scope) override;
97
98
0
    bool is_use_default_implementation_for_constants() const override { return true; }
99
100
0
    bool is_udf_function() const override { return true; }
101
102
private:
103
    const TFunction& _fn;
104
    const DataTypes _argument_types;
105
    const DataTypePtr _return_type {nullptr};
106
};
107
108
} // namespace doris