Coverage Report

Created: 2025-03-12 11:55

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