forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_context.cpp
More file actions
153 lines (131 loc) · 4.69 KB
/
Copy pathfunction_context.cpp
File metadata and controls
153 lines (131 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "exprs/function_context.h"
#include <iostream>
#include <utility>
// Be careful what this includes since this needs to be linked into UDF's
// binary. For example, it would be unfortunate if they had a random dependency
// on libhdfs.
#include "common/cast_set.h"
#include "core/data_type/data_type.h"
#include "core/string_ref.h"
#include "runtime/runtime_state.h"
namespace doris {
static const int MAX_WARNINGS = 1000;
std::unique_ptr<doris::FunctionContext> FunctionContext::create_context(
RuntimeState* state, const DataTypePtr& return_type,
const std::vector<DataTypePtr>& arg_types) {
auto ctx = std::unique_ptr<doris::FunctionContext>(new doris::FunctionContext());
ctx->_state = state;
ctx->_return_type = return_type;
ctx->_arg_types = arg_types;
ctx->_num_warnings = 0;
ctx->_thread_local_fn_state = nullptr;
ctx->_fragment_local_fn_state = nullptr;
return ctx;
}
void FunctionContext::set_constant_cols(
const std::vector<std::shared_ptr<doris::ColumnPtrWrapper>>& constant_cols) {
_constant_cols = constant_cols;
}
std::unique_ptr<FunctionContext> FunctionContext::clone() {
auto new_context = create_context(_state, _return_type, _arg_types);
new_context->_constant_cols = _constant_cols;
new_context->_fragment_local_fn_state = _fragment_local_fn_state;
new_context->_check_overflow_for_decimal = _check_overflow_for_decimal;
new_context->_enable_strict_mode = _enable_strict_mode;
new_context->_string_as_jsonb_string = _string_as_jsonb_string;
new_context->_jsonb_string_as_string = _jsonb_string_as_string;
return new_context;
}
void FunctionContext::set_function_state(FunctionStateScope scope, std::shared_ptr<void> ptr) {
switch (scope) {
case THREAD_LOCAL:
_thread_local_fn_state = std::move(ptr);
break;
case FRAGMENT_LOCAL:
_fragment_local_fn_state = std::move(ptr);
break;
default:
std::stringstream ss;
ss << "Unknown FunctionStateScope: " << scope;
set_error(ss.str().c_str());
}
}
void FunctionContext::set_error(const char* error_msg) {
if (_error_msg.empty()) {
_error_msg = error_msg;
std::stringstream ss;
ss << "UDF ERROR: " << error_msg;
if (_state != nullptr) {
_state->cancel(Status::InternalError(ss.str()));
}
}
}
bool FunctionContext::add_warning(const char* warning_msg) {
if (_num_warnings++ >= MAX_WARNINGS) {
return false;
}
std::stringstream ss;
ss << "UDF WARNING: " << warning_msg;
if (_state != nullptr) {
return _state->log_error(ss.str());
} else {
std::cerr << ss.str() << std::endl;
return true;
}
}
const DataTypePtr FunctionContext::get_arg_type(int arg_idx) const {
if (arg_idx < 0 || arg_idx >= _arg_types.size()) {
return nullptr;
}
return _arg_types[arg_idx];
}
bool FunctionContext::is_col_constant(int i) const {
if (i < 0 || i >= _constant_cols.size()) {
return false;
}
return _constant_cols[i] != nullptr;
}
doris::ColumnPtrWrapper* FunctionContext::get_constant_col(int i) const {
if (i < 0 || i >= _constant_cols.size()) {
return nullptr;
}
return _constant_cols[i].get();
}
int FunctionContext::get_num_args() const {
return cast_set<int>(_arg_types.size());
}
const DataTypePtr FunctionContext::get_return_type() const {
return _return_type;
}
void* FunctionContext::get_function_state(FunctionStateScope scope) const {
switch (scope) {
case THREAD_LOCAL:
return _thread_local_fn_state.get();
case FRAGMENT_LOCAL:
return _fragment_local_fn_state.get();
default:
// TODO: signal error somehow
return nullptr;
}
}
StringRef FunctionContext::create_temp_string_val(int64_t len) {
_string_result.resize(len);
return StringRef((uint8_t*)_string_result.c_str(), len);
}
} // namespace doris