Coverage Report

Created: 2024-11-20 15:52

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