Coverage Report

Created: 2026-03-13 03:47

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
#include "common/compile_check_begin.h"
33
34
static const int MAX_WARNINGS = 1000;
35
36
std::unique_ptr<doris::FunctionContext> FunctionContext::create_context(
37
        RuntimeState* state, const DataTypePtr& return_type,
38
1.96M
        const std::vector<DataTypePtr>& arg_types) {
39
1.96M
    auto ctx = std::unique_ptr<doris::FunctionContext>(new doris::FunctionContext());
40
1.96M
    ctx->_state = state;
41
1.96M
    ctx->_return_type = return_type;
42
1.96M
    ctx->_arg_types = arg_types;
43
1.96M
    ctx->_num_warnings = 0;
44
1.96M
    ctx->_thread_local_fn_state = nullptr;
45
1.96M
    ctx->_fragment_local_fn_state = nullptr;
46
1.96M
    return ctx;
47
1.96M
}
48
49
void FunctionContext::set_constant_cols(
50
683k
        const std::vector<std::shared_ptr<doris::ColumnPtrWrapper>>& constant_cols) {
51
683k
    _constant_cols = constant_cols;
52
683k
}
53
54
1.15M
std::unique_ptr<FunctionContext> FunctionContext::clone() {
55
1.15M
    auto new_context = create_context(_state, _return_type, _arg_types);
56
1.15M
    new_context->_constant_cols = _constant_cols;
57
1.15M
    new_context->_fragment_local_fn_state = _fragment_local_fn_state;
58
1.15M
    new_context->_check_overflow_for_decimal = _check_overflow_for_decimal;
59
1.15M
    new_context->_enable_strict_mode = _enable_strict_mode;
60
1.15M
    new_context->_string_as_jsonb_string = _string_as_jsonb_string;
61
1.15M
    new_context->_jsonb_string_as_string = _jsonb_string_as_string;
62
1.15M
    return new_context;
63
1.15M
}
64
65
46.9k
void FunctionContext::set_function_state(FunctionStateScope scope, std::shared_ptr<void> ptr) {
66
46.9k
    switch (scope) {
67
41.0k
    case THREAD_LOCAL:
68
41.0k
        _thread_local_fn_state = std::move(ptr);
69
41.0k
        break;
70
5.85k
    case FRAGMENT_LOCAL:
71
5.85k
        _fragment_local_fn_state = std::move(ptr);
72
5.85k
        break;
73
0
    default:
74
0
        std::stringstream ss;
75
0
        ss << "Unknown FunctionStateScope: " << scope;
76
0
        set_error(ss.str().c_str());
77
46.9k
    }
78
46.9k
}
79
80
3
void FunctionContext::set_error(const char* error_msg) {
81
3
    if (_error_msg.empty()) {
82
3
        _error_msg = error_msg;
83
3
        std::stringstream ss;
84
3
        ss << "UDF ERROR: " << error_msg;
85
86
3
        if (_state != nullptr) {
87
3
            _state->cancel(Status::InternalError(ss.str()));
88
3
        }
89
3
    }
90
3
}
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
7.79k
const DataTypePtr FunctionContext::get_arg_type(int arg_idx) const {
109
7.79k
    if (arg_idx < 0 || arg_idx >= _arg_types.size()) {
110
0
        return nullptr;
111
0
    }
112
7.79k
    return _arg_types[arg_idx];
113
7.79k
}
114
115
27.5k
bool FunctionContext::is_col_constant(int i) const {
116
27.5k
    if (i < 0 || i >= _constant_cols.size()) {
117
4.11k
        return false;
118
4.11k
    }
119
23.4k
    return _constant_cols[i] != nullptr;
120
27.5k
}
121
122
30.3k
doris::ColumnPtrWrapper* FunctionContext::get_constant_col(int i) const {
123
30.3k
    if (i < 0 || i >= _constant_cols.size()) {
124
0
        return nullptr;
125
0
    }
126
30.3k
    return _constant_cols[i].get();
127
30.3k
}
128
129
19.7k
int FunctionContext::get_num_args() const {
130
19.7k
    return cast_set<int>(_arg_types.size());
131
19.7k
}
132
133
0
const DataTypePtr FunctionContext::get_return_type() const {
134
0
    return _return_type;
135
0
}
136
137
22.8k
void* FunctionContext::get_function_state(FunctionStateScope scope) const {
138
22.8k
    switch (scope) {
139
15.5k
    case THREAD_LOCAL:
140
15.5k
        return _thread_local_fn_state.get();
141
7.25k
    case FRAGMENT_LOCAL:
142
7.25k
        return _fragment_local_fn_state.get();
143
0
    default:
144
        // TODO: signal error somehow
145
0
        return nullptr;
146
22.8k
    }
147
22.8k
}
148
149
431
StringRef FunctionContext::create_temp_string_val(int64_t len) {
150
431
    _string_result.resize(len);
151
431
    return StringRef((uint8_t*)_string_result.c_str(), len);
152
431
}
153
154
#include "common/compile_check_end.h"
155
} // namespace doris