Coverage Report

Created: 2026-10-24 06:52

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