Coverage Report

Created: 2025-07-27 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/udf/udf.cpp
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
// 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
259k
        const std::vector<vectorized::DataTypePtr>& arg_types) {
43
259k
    auto ctx = std::unique_ptr<doris::FunctionContext>(new doris::FunctionContext());
44
259k
    ctx->_state = state;
45
259k
    ctx->_return_type = return_type;
46
259k
    ctx->_arg_types = arg_types;
47
259k
    ctx->_num_warnings = 0;
48
259k
    ctx->_thread_local_fn_state = nullptr;
49
259k
    ctx->_fragment_local_fn_state = nullptr;
50
259k
    return ctx;
51
259k
}
52
53
void FunctionContext::set_constant_cols(
54
259k
        const std::vector<std::shared_ptr<doris::ColumnPtrWrapper>>& constant_cols) {
55
259k
    _constant_cols = constant_cols;
56
259k
}
57
58
16
std::unique_ptr<FunctionContext> FunctionContext::clone() {
59
16
    auto new_context = create_context(_state, _return_type, _arg_types);
60
16
    new_context->_constant_cols = _constant_cols;
61
16
    new_context->_fragment_local_fn_state = _fragment_local_fn_state;
62
16
    new_context->_check_overflow_for_decimal = _check_overflow_for_decimal;
63
16
    new_context->_enable_strict_mode = _enable_strict_mode;
64
16
    new_context->_string_as_jsonb_string = _string_as_jsonb_string;
65
16
    new_context->_jsonb_string_as_string = _jsonb_string_as_string;
66
16
    return new_context;
67
16
}
68
69
624
void FunctionContext::set_function_state(FunctionStateScope scope, std::shared_ptr<void> ptr) {
70
624
    switch (scope) {
71
209
    case THREAD_LOCAL:
72
209
        _thread_local_fn_state = std::move(ptr);
73
209
        break;
74
415
    case FRAGMENT_LOCAL:
75
415
        _fragment_local_fn_state = std::move(ptr);
76
415
        break;
77
0
    default:
78
0
        std::stringstream ss;
79
0
        ss << "Unknown FunctionStateScope: " << scope;
80
0
        set_error(ss.str().c_str());
81
624
    }
82
624
}
83
84
0
void FunctionContext::set_error(const char* error_msg) {
85
0
    if (_error_msg.empty()) {
86
0
        _error_msg = error_msg;
87
0
        std::stringstream ss;
88
0
        ss << "UDF ERROR: " << error_msg;
89
90
0
        if (_state != nullptr) {
91
0
            _state->cancel(Status::InternalError(ss.str()));
92
0
        }
93
0
    }
94
0
}
95
96
0
bool FunctionContext::add_warning(const char* warning_msg) {
97
0
    if (_num_warnings++ >= MAX_WARNINGS) {
98
0
        return false;
99
0
    }
100
101
0
    std::stringstream ss;
102
0
    ss << "UDF WARNING: " << warning_msg;
103
104
0
    if (_state != nullptr) {
105
0
        return _state->log_error(ss.str());
106
0
    } else {
107
0
        std::cerr << ss.str() << std::endl;
108
0
        return true;
109
0
    }
110
0
}
111
112
17
const vectorized::DataTypePtr FunctionContext::get_arg_type(int arg_idx) const {
113
17
    if (arg_idx < 0 || arg_idx >= _arg_types.size()) {
114
0
        return nullptr;
115
0
    }
116
17
    return _arg_types[arg_idx];
117
17
}
118
119
353
bool FunctionContext::is_col_constant(int i) const {
120
353
    if (i < 0 || i >= _constant_cols.size()) {
121
136
        return false;
122
136
    }
123
217
    return _constant_cols[i] != nullptr;
124
353
}
125
126
601
doris::ColumnPtrWrapper* FunctionContext::get_constant_col(int i) const {
127
601
    if (i < 0 || i >= _constant_cols.size()) {
128
0
        return nullptr;
129
0
    }
130
601
    return _constant_cols[i].get();
131
601
}
132
133
676
int FunctionContext::get_num_args() const {
134
676
    return cast_set<int>(_arg_types.size());
135
676
}
136
137
0
const vectorized::DataTypePtr FunctionContext::get_return_type() const {
138
0
    return _return_type;
139
0
}
140
141
567
void* FunctionContext::get_function_state(FunctionStateScope scope) const {
142
567
    switch (scope) {
143
242
    case THREAD_LOCAL:
144
242
        return _thread_local_fn_state.get();
145
325
    case FRAGMENT_LOCAL:
146
325
        return _fragment_local_fn_state.get();
147
0
    default:
148
        // TODO: signal error somehow
149
0
        return nullptr;
150
567
    }
151
567
}
152
153
63
StringRef FunctionContext::create_temp_string_val(int64_t len) {
154
63
    _string_result.resize(len);
155
63
    return StringRef((uint8_t*)_string_result.c_str(), len);
156
63
}
157
158
#include "common/compile_check_end.h"
159
} // namespace doris