Coverage Report

Created: 2025-04-30 18:30

/root/doris/be/src/udf/udf.h
Line
Count
Source (jump to first uncovered line)
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
// This file is copied from
18
// https://github.com/apache/impala/blob/branch-2.9.0/be/src/udf/udf.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <cstdint>
24
#include <memory>
25
#include <string>
26
#include <vector>
27
28
#include "runtime/types.h"
29
#include "util/runtime_profile.h"
30
#include "vec/common/arena.h"
31
32
namespace doris {
33
34
struct ColumnPtrWrapper;
35
struct StringRef;
36
class RuntimeState;
37
38
namespace vectorized {
39
class IDataType;
40
using DataTypePtr = std::shared_ptr<const IDataType>;
41
} // namespace vectorized
42
43
// The FunctionContext is passed to every UDF/UDA and is the interface for the UDF to the
44
// rest of the system. It contains APIs to examine the system state, report errors
45
// and manage memory.
46
class FunctionContext {
47
public:
48
    enum FunctionStateScope {
49
        /// Indicates that the function state for this FunctionContext's UDF is shared across
50
        /// the plan fragment (a query is divided into multiple plan fragments, each of which
51
        /// is responsible for a part of the query execution). Within the plan fragment, there
52
        /// may be multiple instances of the UDF executing concurrently with multiple
53
        /// FunctionContexts sharing this state, meaning that the state must be
54
        /// thread-safe. The Prepare() function for the UDF may be called with this scope
55
        /// concurrently on a single host if the UDF will be evaluated in multiple plan
56
        /// fragments on that host. In general, read-only state that doesn't need to be
57
        /// recomputed for every UDF call should be fragment-local.
58
        /// TODO: not yet implemented
59
        FRAGMENT_LOCAL,
60
61
        /// Indicates that the function state is local to the execution thread. This state
62
        /// does not need to be thread-safe. However, this state will be initialized (via the
63
        /// Prepare() function) once for every execution thread, so fragment-local state
64
        /// should be used when possible for better performance. In general, inexpensive
65
        /// shared state that is written to by the UDF (e.g. scratch space) should be
66
        /// thread-local.
67
        THREAD_LOCAL,
68
    };
69
70
    static std::unique_ptr<doris::FunctionContext> create_context(
71
            RuntimeState* state, const vectorized::DataTypePtr& return_type,
72
            const std::vector<vectorized::DataTypePtr>& arg_types);
73
74
    /// Returns a new FunctionContext with the same constant args, fragment-local state, and
75
    /// debug flag as this FunctionContext. The caller is responsible for calling delete on
76
    /// it.
77
    std::unique_ptr<doris::FunctionContext> clone();
78
79
    void set_constant_cols(const std::vector<std::shared_ptr<doris::ColumnPtrWrapper>>& cols);
80
81
56
    RuntimeState* state() { return _state; }
82
83
6.91k
    bool check_overflow_for_decimal() const { return _check_overflow_for_decimal; }
84
85
64
    bool set_check_overflow_for_decimal(bool check_overflow_for_decimal) {
86
64
        return _check_overflow_for_decimal = check_overflow_for_decimal;
87
64
    }
88
89
4
    void set_string_as_jsonb_string(bool string_as_jsonb_string) {
90
4
        _string_as_jsonb_string = string_as_jsonb_string;
91
4
    }
92
93
4
    void set_jsonb_string_as_string(bool jsonb_string_as_string) {
94
4
        _jsonb_string_as_string = jsonb_string_as_string;
95
4
    }
96
97
0
    void set_udf_execute_timer(RuntimeProfile::Counter* udf_execute_timer) {
98
0
        _udf_execute_timer = udf_execute_timer;
99
0
    }
100
101
0
    RuntimeProfile::Counter* get_udf_execute_timer() { return _udf_execute_timer; }
102
103
    // Cast flag, when enable string_as_jsonb_string, string casting to jsonb will not parse string
104
    // instead just insert a string literal
105
18
    bool string_as_jsonb_string() const { return _string_as_jsonb_string; }
106
107
    // Cast flag, when enable jsonb_string_as_string, jsonb string casting to string will not parse string
108
    // instead just insert a string literal
109
112
    bool jsonb_string_as_string() const { return _jsonb_string_as_string; }
110
111
    // Sets an error for this UDF. If this is called, this will trigger the
112
    // query to fail.
113
    // Note: when you set error for the UDFs used in Data Load, you should
114
    // ensure the function return value is null.
115
    void set_error(const char* error_msg);
116
117
    // Adds a warning that is returned to the user. This can include things like
118
    // overflow or other recoverable error conditions.
119
    // Warnings are capped at a maximum number. Returns true if the warning was
120
    // added and false if it was ignored due to the cap.
121
    bool add_warning(const char* warning_msg);
122
123
    /// Methods for maintaining state across UDF/UDA function calls. SetFunctionState() can
124
    /// be used to store a pointer that can then be retrieved via GetFunctionState(). If
125
    /// GetFunctionState() is called when no pointer is set, it will return
126
    /// nullptr. SetFunctionState() does not take ownership of 'ptr'; it is up to the UDF/UDA
127
    /// to clean up any function state if necessary.
128
    void set_function_state(FunctionStateScope scope, std::shared_ptr<void> ptr);
129
130
    void* get_function_state(FunctionStateScope scope) const;
131
132
    // Returns the return type information of this function. For UDAs, this is the final
133
    // return type of the UDA (e.g., the type returned by the finalize function).
134
    const vectorized::DataTypePtr get_return_type() const;
135
136
    // Returns the number of arguments to this function (not including the FunctionContext*
137
    // argument).
138
    int get_num_args() const;
139
140
    // Returns the type information for the arg_idx-th argument (0-indexed, not including
141
    // the FunctionContext* argument). Returns nullptr if arg_idx is invalid.
142
    const vectorized::DataTypePtr get_arg_type(int arg_idx) const;
143
144
    // Returns true if the arg_idx-th input argument (0 indexed, not including the
145
    // FunctionContext* argument) is a constant (e.g. 5, "string", 1 + 1).
146
    bool is_col_constant(int arg_idx) const;
147
148
    // Returns a pointer to the value of the arg_idx-th input argument (0 indexed, not
149
    // including the FunctionContext* argument). Returns nullptr if the argument is not
150
    // constant. This function can be used to obtain user-specified constants in a UDF's
151
    // Init() or Close() functions.
152
    doris::ColumnPtrWrapper* get_constant_col(int arg_idx) const;
153
154
    // Creates a StringRef, which memory is available when this function context is used next time
155
    StringRef create_temp_string_val(int64_t len);
156
157
11.5k
    ~FunctionContext() = default;
158
159
0
    vectorized::Arena& get_arena() { return arena; }
160
161
private:
162
11.5k
    FunctionContext() = default;
163
164
    // Disable copy ctor and assignment operator
165
    FunctionContext(const FunctionContext& other);
166
167
    FunctionContext& operator=(const FunctionContext& other);
168
169
    // We use the query's runtime state to report errors and warnings. nullptr for test
170
    // contexts.
171
    RuntimeState* _state = nullptr;
172
173
    // Empty if there's no error
174
    std::string _error_msg;
175
176
    // The number of warnings reported.
177
    int64_t _num_warnings;
178
179
    /// The function state accessed via FunctionContext::Get/SetFunctionState()
180
    std::shared_ptr<void> _thread_local_fn_state;
181
    std::shared_ptr<void> _fragment_local_fn_state;
182
183
    // Type descriptor for the return type of the function.
184
    vectorized::DataTypePtr _return_type;
185
186
    // Type descriptors for each argument of the function.
187
    std::vector<vectorized::DataTypePtr> _arg_types;
188
189
    std::vector<std::shared_ptr<doris::ColumnPtrWrapper>> _constant_cols;
190
191
    //udf execute timer
192
    RuntimeProfile::Counter* _udf_execute_timer = nullptr;
193
    bool _check_overflow_for_decimal = false;
194
195
    bool _string_as_jsonb_string = false;
196
    bool _jsonb_string_as_string = false;
197
198
    std::string _string_result;
199
200
    vectorized::Arena arena;
201
};
202
203
using doris::FunctionContext;
204
} // namespace doris