Coverage Report

Created: 2025-04-28 15:34

/root/doris/be/src/udf/udf.cpp
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.cpp
19
// and modified by Doris
20
21
#include "udf/udf.h"
22
23
#include <iostream>
24
#include <utility>
25
26
// Be careful what this includes since this needs to be linked into the UDF's
27
// binary. For example, it would be unfortunate if they had a random dependency
28
// on libhdfs.
29
#include "common/cast_set.h"
30
#include "runtime/runtime_state.h"
31
#include "runtime/types.h"
32
#include "vec/common/string_ref.h"
33
#include "vec/data_types/data_type.h"
34
35
namespace doris {
36
#include "common/compile_check_begin.h"
37
38
static const int MAX_WARNINGS = 1000;
39
40
std::unique_ptr<doris::FunctionContext> FunctionContext::create_context(
41
        RuntimeState* state, const vectorized::DataTypePtr& return_type,
42
4.01M
        const std::vector<vectorized::DataTypePtr>& arg_types) {
43
4.01M
    auto ctx = std::unique_ptr<doris::FunctionContext>(new doris::FunctionContext());
44
4.01M
    ctx->_state = state;
45
4.01M
    ctx->_return_type = return_type;
46
4.01M
    ctx->_arg_types = arg_types;
47
4.01M
    ctx->_num_warnings = 0;
48
4.01M
    ctx->_thread_local_fn_state = nullptr;
49
4.01M
    ctx->_fragment_local_fn_state = nullptr;
50
4.01M
    return ctx;
51
4.01M
}
52
53
void FunctionContext::set_constant_cols(
54
1.00M
        const std::vector<std::shared_ptr<doris::ColumnPtrWrapper>>& constant_cols) {
55
1.00M
    _constant_cols = constant_cols;
56
1.00M
}
57
58
3.00M
std::unique_ptr<FunctionContext> FunctionContext::clone() {
59
3.00M
    auto new_context = create_context(_state, _return_type, _arg_types);
60
3.00M
    new_context->_constant_cols = _constant_cols;
61
3.00M
    new_context->_fragment_local_fn_state = _fragment_local_fn_state;
62
3.00M
    new_context->_check_overflow_for_decimal = _check_overflow_for_decimal;
63
3.00M
    new_context->_string_as_jsonb_string = _string_as_jsonb_string;
64
3.00M
    new_context->_jsonb_string_as_string = _jsonb_string_as_string;
65
3.00M
    return new_context;
66
3.00M
}
67
68
319k
void FunctionContext::set_function_state(FunctionStateScope scope, std::shared_ptr<void> ptr) {
69
319k
    switch (scope) {
70
97.9k
    case THREAD_LOCAL:
71
97.9k
        _thread_local_fn_state = std::move(ptr);
72
97.9k
        break;
73
222k
    case FRAGMENT_LOCAL:
74
222k
        _fragment_local_fn_state = std::move(ptr);
75
222k
        break;
76
0
    default:
77
0
        std::stringstream ss;
78
0
        ss << "Unknown FunctionStateScope: " << scope;
79
0
        set_error(ss.str().c_str());
80
319k
    }
81
319k
}
82
83
0
void FunctionContext::set_error(const char* error_msg) {
84
0
    if (_error_msg.empty()) {
85
0
        _error_msg = error_msg;
86
0
        std::stringstream ss;
87
0
        ss << "UDF ERROR: " << error_msg;
88
89
0
        if (_state != nullptr) {
90
0
            _state->cancel(Status::InternalError(ss.str()));
91
0
        }
92
0
    }
93
0
}
94
95
0
bool FunctionContext::add_warning(const char* warning_msg) {
96
0
    if (_num_warnings++ >= MAX_WARNINGS) {
97
0
        return false;
98
0
    }
99
100
0
    std::stringstream ss;
101
0
    ss << "UDF WARNING: " << warning_msg;
102
103
0
    if (_state != nullptr) {
104
0
        return _state->log_error(ss.str());
105
0
    } else {
106
0
        std::cerr << ss.str() << std::endl;
107
0
        return true;
108
0
    }
109
0
}
110
111
193k
const vectorized::DataTypePtr FunctionContext::get_arg_type(int arg_idx) const {
112
193k
    if (arg_idx < 0 || arg_idx >= _arg_types.size()) {
113
0
        return nullptr;
114
0
    }
115
193k
    return _arg_types[arg_idx];
116
193k
}
117
118
63.0k
bool FunctionContext::is_col_constant(int i) const {
119
63.0k
    if (i < 0 || i >= _constant_cols.size()) {
120
0
        return false;
121
0
    }
122
63.0k
    return _constant_cols[i] != nullptr;
123
63.0k
}
124
125
585k
doris::ColumnPtrWrapper* FunctionContext::get_constant_col(int i) const {
126
586k
    if (i < 0 || i >= _constant_cols.size()) {
127
0
        return nullptr;
128
0
    }
129
585k
    return _constant_cols[i].get();
130
585k
}
131
132
657k
int FunctionContext::get_num_args() const {
133
657k
    return cast_set<int>(_arg_types.size());
134
657k
}
135
136
85.8k
const vectorized::DataTypePtr FunctionContext::get_return_type() const {
137
85.8k
    return _return_type;
138
85.8k
}
139
140
369k
void* FunctionContext::get_function_state(FunctionStateScope scope) const {
141
369k
    switch (scope) {
142
27.9k
    case THREAD_LOCAL:
143
27.9k
        return _thread_local_fn_state.get();
144
341k
    case FRAGMENT_LOCAL:
145
341k
        return _fragment_local_fn_state.get();
146
0
    default:
147
        // TODO: signal error somehow
148
0
        return nullptr;
149
369k
    }
150
369k
}
151
152
1.52k
StringRef FunctionContext::create_temp_string_val(int64_t len) {
153
1.52k
    _string_result.resize(len);
154
1.52k
    return StringRef((uint8_t*)_string_result.c_str(), len);
155
1.52k
}
156
157
#include "common/compile_check_end.h"
158
} // namespace doris