Coverage Report

Created: 2025-04-25 14:29

/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
11.4k
        const std::vector<vectorized::DataTypePtr>& arg_types) {
43
11.4k
    auto ctx = std::unique_ptr<doris::FunctionContext>(new doris::FunctionContext());
44
11.4k
    ctx->_state = state;
45
11.4k
    ctx->_return_type = return_type;
46
11.4k
    ctx->_arg_types = arg_types;
47
11.4k
    ctx->_num_warnings = 0;
48
11.4k
    ctx->_thread_local_fn_state = nullptr;
49
11.4k
    ctx->_fragment_local_fn_state = nullptr;
50
11.4k
    return ctx;
51
11.4k
}
52
53
void FunctionContext::set_constant_cols(
54
11.4k
        const std::vector<std::shared_ptr<doris::ColumnPtrWrapper>>& constant_cols) {
55
11.4k
    _constant_cols = constant_cols;
56
11.4k
}
57
58
11
std::unique_ptr<FunctionContext> FunctionContext::clone() {
59
11
    auto new_context = create_context(_state, _return_type, _arg_types);
60
11
    new_context->_constant_cols = _constant_cols;
61
11
    new_context->_fragment_local_fn_state = _fragment_local_fn_state;
62
11
    new_context->_check_overflow_for_decimal = _check_overflow_for_decimal;
63
11
    new_context->_string_as_jsonb_string = _string_as_jsonb_string;
64
11
    new_context->_jsonb_string_as_string = _jsonb_string_as_string;
65
11
    return new_context;
66
11
}
67
68
532
void FunctionContext::set_function_state(FunctionStateScope scope, std::shared_ptr<void> ptr) {
69
532
    switch (scope) {
70
74
    case THREAD_LOCAL:
71
74
        _thread_local_fn_state = std::move(ptr);
72
74
        break;
73
458
    case FRAGMENT_LOCAL:
74
458
        _fragment_local_fn_state = std::move(ptr);
75
458
        break;
76
0
    default:
77
0
        std::stringstream ss;
78
0
        ss << "Unknown FunctionStateScope: " << scope;
79
0
        set_error(ss.str().c_str());
80
532
    }
81
532
}
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
71
const vectorized::DataTypePtr FunctionContext::get_arg_type(int arg_idx) const {
112
71
    if (arg_idx < 0 || arg_idx >= _arg_types.size()) {
113
0
        return nullptr;
114
0
    }
115
71
    return _arg_types[arg_idx];
116
71
}
117
118
87
bool FunctionContext::is_col_constant(int i) const {
119
87
    if (i < 0 || i >= _constant_cols.size()) {
120
0
        return false;
121
0
    }
122
87
    return _constant_cols[i] != nullptr;
123
87
}
124
125
509
doris::ColumnPtrWrapper* FunctionContext::get_constant_col(int i) const {
126
509
    if (i < 0 || i >= _constant_cols.size()) {
127
0
        return nullptr;
128
0
    }
129
509
    return _constant_cols[i].get();
130
509
}
131
132
667
int FunctionContext::get_num_args() const {
133
667
    return cast_set<int>(_arg_types.size());
134
667
}
135
136
27
const vectorized::DataTypePtr FunctionContext::get_return_type() const {
137
27
    return _return_type;
138
27
}
139
140
478
void* FunctionContext::get_function_state(FunctionStateScope scope) const {
141
478
    switch (scope) {
142
113
    case THREAD_LOCAL:
143
113
        return _thread_local_fn_state.get();
144
365
    case FRAGMENT_LOCAL:
145
365
        return _fragment_local_fn_state.get();
146
0
    default:
147
        // TODO: signal error somehow
148
0
        return nullptr;
149
478
    }
150
478
}
151
152
67
StringRef FunctionContext::create_temp_string_val(int64_t len) {
153
67
    _string_result.resize(len);
154
67
    return StringRef((uint8_t*)_string_result.c_str(), len);
155
67
}
156
157
#include "common/compile_check_end.h"
158
} // namespace doris