forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_convert_tz.cpp
More file actions
321 lines (278 loc) · 14.4 KB
/
Copy pathfunction_convert_tz.cpp
File metadata and controls
321 lines (278 loc) · 14.4 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// 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 <cctz/time_zone.h>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include "common/status.h"
#include "core/assert_cast.h"
#include "core/binary_cast.hpp"
#include "core/block/block.h"
#include "core/block/column_numbers.h"
#include "core/block/column_with_type_and_name.h"
#include "core/column/column.h"
#include "core/column/column_const.h"
#include "core/column/column_nullable.h"
#include "core/column/column_string.h"
#include "core/column/column_vector.h"
#include "core/data_type/data_type.h"
#include "core/data_type/data_type_date.h"
#include "core/data_type/data_type_date_or_datetime_v2.h"
#include "core/data_type/data_type_date_time.h"
#include "core/data_type/data_type_nullable.h"
#include "core/data_type/data_type_string.h"
#include "core/data_type/define_primitive_type.h"
#include "core/data_type/primitive_type.h"
#include "core/string_ref.h"
#include "core/types.h"
#include "core/value/vdatetime_value.h"
#include "exec/common/util.hpp"
#include "exprs/aggregate/aggregate_function.h"
#include "exprs/function/datetime_errors.h"
#include "exprs/function/function.h"
#include "exprs/function/function_helpers.h"
#include "exprs/function/simple_function_factory.h"
#include "exprs/function_context.h"
#include "util/timezone_utils.h"
namespace doris {
struct ConvertTzState {
bool use_state = false;
bool is_valid = false;
cctz::time_zone from_tz;
cctz::time_zone to_tz;
};
class FunctionConvertTZ : public IFunction {
constexpr static PrimitiveType PType = PrimitiveType::TYPE_DATETIMEV2;
using DateValueType = PrimitiveTypeTraits<PType>::CppType;
using ColumnType = PrimitiveTypeTraits<PType>::ColumnType;
public:
static constexpr auto name = "convert_tz";
static FunctionPtr create() { return std::make_shared<FunctionConvertTZ>(); }
String get_name() const override { return name; }
size_t get_number_of_arguments() const override { return 3; }
DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
return have_nullable(arguments) ? make_nullable(std::make_shared<DataTypeDateTimeV2>())
: std::make_shared<DataTypeDateTimeV2>();
}
// default value of timezone is invalid, should skip to avoid wrong exception
bool use_default_implementation_for_nulls() const override { return false; }
Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
if (scope == FunctionContext::THREAD_LOCAL) {
return Status::OK();
}
std::shared_ptr<ConvertTzState> state = std::make_shared<ConvertTzState>();
context->set_function_state(scope, state);
DCHECK_EQ(context->get_num_args(), 3);
const auto* const_from_tz = context->get_constant_col(1);
const auto* const_to_tz = context->get_constant_col(2);
// ConvertTzState is used only when both the second and third parameters are constants
if (const_from_tz != nullptr && const_to_tz != nullptr) {
state->use_state = true;
init_convert_tz_state(state, const_from_tz, const_to_tz);
} else {
state->use_state = false;
}
return IFunction::open(context, scope);
}
void init_convert_tz_state(std::shared_ptr<ConvertTzState> state,
const ColumnPtrWrapper* const_from_tz,
const ColumnPtrWrapper* const_to_tz) {
auto const_data_from_tz = const_from_tz->column_ptr->get_data_at(0);
auto const_data_to_tz = const_to_tz->column_ptr->get_data_at(0);
// from_tz and to_tz must both be non-null.
if (const_data_from_tz.data == nullptr || const_data_to_tz.data == nullptr) {
state->is_valid = false;
return;
}
auto from_tz_name = const_data_from_tz.to_string();
auto to_tz_name = const_data_to_tz.to_string();
if (!TimezoneUtils::find_cctz_time_zone(from_tz_name, state->from_tz)) [[unlikely]] {
throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} invalid timezone: {}", name,
from_tz_name);
}
if (!TimezoneUtils::find_cctz_time_zone(to_tz_name, state->to_tz)) [[unlikely]] {
throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} invalid timezone: {}", name,
to_tz_name);
}
state->is_valid = true;
}
Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
uint32_t result, size_t input_rows_count) const override {
auto* convert_tz_state = reinterpret_cast<ConvertTzState*>(
context->get_function_state(FunctionContext::FRAGMENT_LOCAL));
if (!convert_tz_state) {
return Status::RuntimeError(
"funciton context for function '{}' must have ConvertTzState;", get_name());
}
auto result_null_map_column = ColumnUInt8::create(input_rows_count, 0);
NullMap& result_null_map = result_null_map_column->get_data();
ColumnPtr argument_columns[3];
bool col_const[3];
// calculate result null map and col_const
for (int i = 0; i < 3; ++i) {
ColumnPtr& col = block.get_by_position(arguments[i]).column;
col_const[i] = is_column_const(*col);
const NullMap* null_map = VectorizedUtils::get_null_map(col);
if (null_map) {
VectorizedUtils::update_null_map(result_null_map, *null_map, col_const[i]);
}
}
// Extract nested columns from const(nullable) wrappers
argument_columns[0] = col_const[0] ? static_cast<const ColumnConst&>(
*block.get_by_position(arguments[0]).column)
.convert_to_full_column()
: block.get_by_position(arguments[0]).column;
argument_columns[0] = remove_nullable(argument_columns[0]);
default_preprocess_parameter_columns(argument_columns, col_const, {1, 2}, block, arguments);
argument_columns[1] = remove_nullable(argument_columns[1]);
argument_columns[2] = remove_nullable(argument_columns[2]);
auto result_column = ColumnType::create();
if (convert_tz_state->use_state) {
// ignore argument columns, use cached timezone input in state
execute_tz_const_with_state(convert_tz_state,
assert_cast<const ColumnType*>(argument_columns[0].get()),
result_column.get(), result_null_map, input_rows_count);
} else if (col_const[1] && col_const[2]) {
// arguments are const
execute_tz_const(context, assert_cast<const ColumnType*>(argument_columns[0].get()),
assert_cast<const ColumnString*>(argument_columns[1].get()),
assert_cast<const ColumnString*>(argument_columns[2].get()),
result_column.get(), result_null_map, input_rows_count);
} else {
_execute(context, assert_cast<const ColumnType*>(argument_columns[0].get()),
assert_cast<const ColumnString*>(argument_columns[1].get()),
assert_cast<const ColumnString*>(argument_columns[2].get()),
result_column.get(), result_null_map, input_rows_count);
} //if const
if (block.get_data_type(result)->is_nullable()) {
block.get_by_position(result).column = ColumnNullable::create(
std::move(result_column), std::move(result_null_map_column));
} else {
block.get_by_position(result).column = std::move(result_column);
}
return Status::OK();
}
private:
static void _execute(FunctionContext* context, const ColumnType* date_column,
const ColumnString* from_tz_column, const ColumnString* to_tz_column,
ColumnType* result_column, NullMap& result_null_map,
size_t input_rows_count) {
for (size_t i = 0; i < input_rows_count; i++) {
if (result_null_map[i]) {
result_column->insert_default();
continue;
}
auto from_tz = from_tz_column->get_data_at(i).to_string();
auto to_tz = to_tz_column->get_data_at(i).to_string();
execute_inner_loop(date_column, from_tz, to_tz, result_column, result_null_map, i);
}
}
static std::pair<int64_t, int64_t> unix_timestamp_for_convert_tz(
const DateValueType& ts_value, const cctz::time_zone& from_tz) {
cctz::civil_second civil_time(ts_value.year(), ts_value.month(), ts_value.day(),
ts_value.hour(), ts_value.minute(), ts_value.second());
const auto lookup = from_tz.lookup(civil_time);
const bool skipped = lookup.kind == cctz::time_zone::civil_lookup::SKIPPED;
const auto tp = skipped ? lookup.trans : lookup.pre;
// Skipped civil times map to the transition instant. Do not keep the
// input fractional part inside a local time interval that never existed.
return {tp.time_since_epoch().count(), skipped ? 0 : ts_value.microsecond()};
}
static void execute_tz_const_with_state(ConvertTzState* convert_tz_state,
const ColumnType* date_column,
ColumnType* result_column, NullMap& result_null_map,
size_t input_rows_count) {
cctz::time_zone& from_tz = convert_tz_state->from_tz;
cctz::time_zone& to_tz = convert_tz_state->to_tz;
auto push_null = [&](size_t row) {
result_null_map[row] = true;
result_column->insert_default();
};
// state isn't valid means there's NULL in timezone input. so return null rather than exception
if (!convert_tz_state->is_valid) [[unlikely]] {
// If an invalid timezone is present, return null
for (size_t i = 0; i < input_rows_count; i++) {
push_null(i);
}
return;
}
for (size_t i = 0; i < input_rows_count; i++) {
if (result_null_map[i]) {
result_column->insert_default();
continue;
}
DateValueType ts_value = date_column->get_element(i);
DateValueType ts_value2;
ts_value2.from_unixtime(unix_timestamp_for_convert_tz(ts_value, from_tz), to_tz);
if (!ts_value2.is_valid_date()) [[unlikely]] {
throw_out_of_bound_convert_tz<DateValueType>(date_column->get_element(i),
from_tz.name(), to_tz.name());
}
result_column->insert(Field::create_field<TYPE_DATETIMEV2>(ts_value2));
}
}
static void execute_tz_const(FunctionContext* context, const ColumnType* date_column,
const ColumnString* from_tz_column,
const ColumnString* to_tz_column, ColumnType* result_column,
NullMap& result_null_map, size_t input_rows_count) {
auto from_tz = from_tz_column->get_data_at(0).to_string();
auto to_tz = to_tz_column->get_data_at(0).to_string();
cctz::time_zone from_zone, to_zone;
if (!TimezoneUtils::find_cctz_time_zone(from_tz, from_zone)) [[unlikely]] {
throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} invalid timezone: {}", name,
from_tz);
}
if (!TimezoneUtils::find_cctz_time_zone(to_tz, to_zone)) [[unlikely]] {
throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} invalid timezone: {}", name,
to_tz);
}
for (size_t i = 0; i < input_rows_count; i++) {
if (result_null_map[i]) {
result_column->insert_default();
continue;
}
execute_inner_loop(date_column, from_tz, to_tz, result_column, result_null_map, i);
}
}
static void execute_inner_loop(const ColumnType* date_column, const std::string& from_tz_name,
const std::string& to_tz_name, ColumnType* result_column,
NullMap& result_null_map, const size_t index_now) {
DateValueType ts_value = date_column->get_element(index_now);
cctz::time_zone from_tz {}, to_tz {};
DateValueType ts_value2;
if (!TimezoneUtils::find_cctz_time_zone(from_tz_name, from_tz)) [[unlikely]] {
throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} invalid timezone: {}", name,
from_tz_name);
}
if (!TimezoneUtils::find_cctz_time_zone(to_tz_name, to_tz)) [[unlikely]] {
throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} invalid timezone: {}", name,
to_tz_name);
}
ts_value2.from_unixtime(unix_timestamp_for_convert_tz(ts_value, from_tz), to_tz);
if (!ts_value2.is_valid_date()) [[unlikely]] {
throw_out_of_bound_convert_tz<DateValueType>(date_column->get_element(index_now),
from_tz.name(), to_tz.name());
}
result_column->insert(Field::create_field<TYPE_DATETIMEV2>(ts_value2));
}
};
void register_function_convert_tz(SimpleFunctionFactory& factory) {
factory.register_function<FunctionConvertTZ>();
}
} // namespace doris