Coverage Report

Created: 2026-04-10 16:11

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