Coverage Report

Created: 2026-06-05 19:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/function_convert_tz.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 <cctz/time_zone.h>
19
20
#include <cstddef>
21
#include <cstdint>
22
#include <memory>
23
#include <string>
24
#include <utility>
25
26
#include "common/status.h"
27
#include "core/assert_cast.h"
28
#include "core/binary_cast.hpp"
29
#include "core/block/block.h"
30
#include "core/block/column_numbers.h"
31
#include "core/block/column_with_type_and_name.h"
32
#include "core/column/column.h"
33
#include "core/column/column_const.h"
34
#include "core/column/column_nullable.h"
35
#include "core/column/column_string.h"
36
#include "core/column/column_vector.h"
37
#include "core/data_type/data_type.h"
38
#include "core/data_type/data_type_date.h"
39
#include "core/data_type/data_type_date_or_datetime_v2.h"
40
#include "core/data_type/data_type_date_time.h"
41
#include "core/data_type/data_type_nullable.h"
42
#include "core/data_type/data_type_string.h"
43
#include "core/data_type/define_primitive_type.h"
44
#include "core/data_type/primitive_type.h"
45
#include "core/string_ref.h"
46
#include "core/types.h"
47
#include "core/value/vdatetime_value.h"
48
#include "exec/common/util.hpp"
49
#include "exprs/aggregate/aggregate_function.h"
50
#include "exprs/function/datetime_errors.h"
51
#include "exprs/function/function.h"
52
#include "exprs/function/function_helpers.h"
53
#include "exprs/function/simple_function_factory.h"
54
#include "exprs/function_context.h"
55
#include "util/timezone_utils.h"
56
57
namespace doris {
58
59
struct ConvertTzState {
60
    bool use_state = false;
61
    bool is_valid = false;
62
    cctz::time_zone from_tz;
63
    cctz::time_zone to_tz;
64
};
65
66
class FunctionConvertTZ : public IFunction {
67
    constexpr static PrimitiveType PType = PrimitiveType::TYPE_DATETIMEV2;
68
    using DateValueType = PrimitiveTypeTraits<PType>::CppType;
69
    using ColumnType = PrimitiveTypeTraits<PType>::ColumnType;
70
71
public:
72
    static constexpr auto name = "convert_tz";
73
74
83
    static FunctionPtr create() { return std::make_shared<FunctionConvertTZ>(); }
75
76
1
    String get_name() const override { return name; }
77
78
75
    size_t get_number_of_arguments() const override { return 3; }
79
80
75
    DataTypePtr get_return_type_impl(const DataTypes& arguments) const override {
81
75
        return have_nullable(arguments) ? make_nullable(std::make_shared<DataTypeDateTimeV2>())
82
75
                                        : std::make_shared<DataTypeDateTimeV2>();
83
75
    }
84
85
    // default value of timezone is invalid, should skip to avoid wrong exception
86
165
    bool use_default_implementation_for_nulls() const override { return false; }
87
88
557
    Status open(FunctionContext* context, FunctionContext::FunctionStateScope scope) override {
89
557
        if (scope == FunctionContext::THREAD_LOCAL) {
90
482
            return Status::OK();
91
482
        }
92
75
        std::shared_ptr<ConvertTzState> state = std::make_shared<ConvertTzState>();
93
94
75
        context->set_function_state(scope, state);
95
75
        DCHECK_EQ(context->get_num_args(), 3);
96
75
        const auto* const_from_tz = context->get_constant_col(1);
97
75
        const auto* const_to_tz = context->get_constant_col(2);
98
99
        // ConvertTzState is used only when both the second and third parameters are constants
100
75
        if (const_from_tz != nullptr && const_to_tz != nullptr) {
101
50
            state->use_state = true;
102
50
            init_convert_tz_state(state, const_from_tz, const_to_tz);
103
50
        } else {
104
25
            state->use_state = false;
105
25
        }
106
107
75
        return IFunction::open(context, scope);
108
557
    }
109
110
    void init_convert_tz_state(std::shared_ptr<ConvertTzState> state,
111
                               const ColumnPtrWrapper* const_from_tz,
112
50
                               const ColumnPtrWrapper* const_to_tz) {
113
50
        auto const_data_from_tz = const_from_tz->column_ptr->get_data_at(0);
114
50
        auto const_data_to_tz = const_to_tz->column_ptr->get_data_at(0);
115
116
        // from_tz and to_tz must both be non-null.
117
50
        if (const_data_from_tz.data == nullptr || const_data_to_tz.data == nullptr) {
118
2
            state->is_valid = false;
119
2
            return;
120
2
        }
121
122
48
        auto from_tz_name = const_data_from_tz.to_string();
123
48
        auto to_tz_name = const_data_to_tz.to_string();
124
125
48
        if (!TimezoneUtils::find_cctz_time_zone(from_tz_name, state->from_tz)) [[unlikely]] {
126
2
            throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} invalid timezone: {}", name,
127
2
                            from_tz_name);
128
2
        }
129
46
        if (!TimezoneUtils::find_cctz_time_zone(to_tz_name, state->to_tz)) [[unlikely]] {
130
2
            throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} invalid timezone: {}", name,
131
2
                            to_tz_name);
132
2
        }
133
44
        state->is_valid = true;
134
44
    }
135
136
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
137
90
                        uint32_t result, size_t input_rows_count) const override {
138
90
        auto* convert_tz_state = reinterpret_cast<ConvertTzState*>(
139
90
                context->get_function_state(FunctionContext::FRAGMENT_LOCAL));
140
90
        if (!convert_tz_state) {
141
0
            return Status::RuntimeError(
142
0
                    "funciton context for function '{}' must have ConvertTzState;", get_name());
143
0
        }
144
145
90
        auto result_null_map_column = ColumnUInt8::create(input_rows_count, 0);
146
90
        NullMap& result_null_map = result_null_map_column->get_data();
147
148
90
        ColumnPtr argument_columns[3];
149
90
        bool col_const[3];
150
151
        // calculate result null map and col_const
152
360
        for (int i = 0; i < 3; ++i) {
153
270
            ColumnPtr& col = block.get_by_position(arguments[i]).column;
154
270
            col_const[i] = is_column_const(*col);
155
270
            const NullMap* null_map = VectorizedUtils::get_null_map(col);
156
270
            if (null_map) {
157
126
                VectorizedUtils::update_null_map(result_null_map, *null_map, col_const[i]);
158
126
            }
159
270
        }
160
161
        // Extract nested columns from const(nullable) wrappers
162
90
        argument_columns[0] = col_const[0] ? static_cast<const ColumnConst&>(
163
1
                                                     *block.get_by_position(arguments[0]).column)
164
1
                                                     .convert_to_full_column()
165
90
                                           : block.get_by_position(arguments[0]).column;
166
90
        argument_columns[0] = remove_nullable(argument_columns[0]);
167
90
        default_preprocess_parameter_columns(argument_columns, col_const, {1, 2}, block, arguments);
168
90
        argument_columns[1] = remove_nullable(argument_columns[1]);
169
90
        argument_columns[2] = remove_nullable(argument_columns[2]);
170
171
90
        auto result_column = ColumnType::create();
172
90
        if (convert_tz_state->use_state) {
173
            // ignore argument columns, use cached timezone input in state
174
56
            execute_tz_const_with_state(convert_tz_state,
175
56
                                        assert_cast<const ColumnType*>(argument_columns[0].get()),
176
56
                                        result_column.get(), result_null_map, input_rows_count);
177
56
        } else if (col_const[1] && col_const[2]) {
178
            // arguments are const
179
10
            execute_tz_const(context, assert_cast<const ColumnType*>(argument_columns[0].get()),
180
10
                             assert_cast<const ColumnString*>(argument_columns[1].get()),
181
10
                             assert_cast<const ColumnString*>(argument_columns[2].get()),
182
10
                             result_column.get(), result_null_map, input_rows_count);
183
24
        } else {
184
24
            _execute(context, assert_cast<const ColumnType*>(argument_columns[0].get()),
185
24
                     assert_cast<const ColumnString*>(argument_columns[1].get()),
186
24
                     assert_cast<const ColumnString*>(argument_columns[2].get()),
187
24
                     result_column.get(), result_null_map, input_rows_count);
188
24
        } //if const
189
190
90
        if (block.get_data_type(result)->is_nullable()) {
191
79
            block.get_by_position(result).column = ColumnNullable::create(
192
79
                    std::move(result_column), std::move(result_null_map_column));
193
79
        } else {
194
11
            block.get_by_position(result).column = std::move(result_column);
195
11
        }
196
90
        return Status::OK();
197
90
    }
198
199
private:
200
    static void _execute(FunctionContext* context, const ColumnType* date_column,
201
                         const ColumnString* from_tz_column, const ColumnString* to_tz_column,
202
                         ColumnType* result_column, NullMap& result_null_map,
203
24
                         size_t input_rows_count) {
204
106
        for (size_t i = 0; i < input_rows_count; i++) {
205
82
            if (result_null_map[i]) {
206
0
                result_column->insert_default();
207
0
                continue;
208
0
            }
209
82
            auto from_tz = from_tz_column->get_data_at(i).to_string();
210
82
            auto to_tz = to_tz_column->get_data_at(i).to_string();
211
82
            execute_inner_loop(date_column, from_tz, to_tz, result_column, result_null_map, i);
212
82
        }
213
24
    }
214
215
    static std::pair<int64_t, int64_t> unix_timestamp_for_convert_tz(
216
204
            const DateValueType& ts_value, const cctz::time_zone& from_tz) {
217
204
        cctz::civil_second civil_time(ts_value.year(), ts_value.month(), ts_value.day(),
218
204
                                      ts_value.hour(), ts_value.minute(), ts_value.second());
219
204
        const auto lookup = from_tz.lookup(civil_time);
220
204
        const bool skipped = lookup.kind == cctz::time_zone::civil_lookup::SKIPPED;
221
204
        const auto tp = skipped ? lookup.trans : lookup.pre;
222
223
        // Skipped civil times map to the transition instant. Do not keep the
224
        // input fractional part inside a local time interval that never existed.
225
204
        return {tp.time_since_epoch().count(), skipped ? 0 : ts_value.microsecond()};
226
204
    }
227
228
    static void execute_tz_const_with_state(ConvertTzState* convert_tz_state,
229
                                            const ColumnType* date_column,
230
                                            ColumnType* result_column, NullMap& result_null_map,
231
56
                                            size_t input_rows_count) {
232
56
        cctz::time_zone& from_tz = convert_tz_state->from_tz;
233
56
        cctz::time_zone& to_tz = convert_tz_state->to_tz;
234
56
        auto push_null = [&](size_t row) {
235
2
            result_null_map[row] = true;
236
2
            result_column->insert_default();
237
2
        };
238
        // state isn't valid means there's NULL in timezone input. so return null rather than exception
239
56
        if (!convert_tz_state->is_valid) [[unlikely]] {
240
            // If an invalid timezone is present, return null
241
4
            for (size_t i = 0; i < input_rows_count; i++) {
242
2
                push_null(i);
243
2
            }
244
2
            return;
245
2
        }
246
159
        for (size_t i = 0; i < input_rows_count; i++) {
247
105
            if (result_null_map[i]) {
248
3
                result_column->insert_default();
249
3
                continue;
250
3
            }
251
252
102
            DateValueType ts_value = date_column->get_element(i);
253
102
            DateValueType ts_value2;
254
255
102
            ts_value2.from_unixtime(unix_timestamp_for_convert_tz(ts_value, from_tz), to_tz);
256
257
102
            if (!ts_value2.is_valid_date()) [[unlikely]] {
258
1
                throw_out_of_bound_convert_tz<DateValueType>(date_column->get_element(i),
259
1
                                                             from_tz.name(), to_tz.name());
260
1
            }
261
262
102
            result_column->insert(Field::create_field<TYPE_DATETIMEV2>(ts_value2));
263
102
        }
264
54
    }
265
266
    static void execute_tz_const(FunctionContext* context, const ColumnType* date_column,
267
                                 const ColumnString* from_tz_column,
268
                                 const ColumnString* to_tz_column, ColumnType* result_column,
269
10
                                 NullMap& result_null_map, size_t input_rows_count) {
270
10
        auto from_tz = from_tz_column->get_data_at(0).to_string();
271
10
        auto to_tz = to_tz_column->get_data_at(0).to_string();
272
10
        cctz::time_zone from_zone, to_zone;
273
10
        if (!TimezoneUtils::find_cctz_time_zone(from_tz, from_zone)) [[unlikely]] {
274
0
            throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} invalid timezone: {}", name,
275
0
                            from_tz);
276
0
        }
277
10
        if (!TimezoneUtils::find_cctz_time_zone(to_tz, to_zone)) [[unlikely]] {
278
0
            throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} invalid timezone: {}", name,
279
0
                            to_tz);
280
0
        }
281
30
        for (size_t i = 0; i < input_rows_count; i++) {
282
20
            if (result_null_map[i]) {
283
0
                result_column->insert_default();
284
0
                continue;
285
0
            }
286
20
            execute_inner_loop(date_column, from_tz, to_tz, result_column, result_null_map, i);
287
20
        }
288
10
    }
289
290
    static void execute_inner_loop(const ColumnType* date_column, const std::string& from_tz_name,
291
                                   const std::string& to_tz_name, ColumnType* result_column,
292
102
                                   NullMap& result_null_map, const size_t index_now) {
293
102
        DateValueType ts_value = date_column->get_element(index_now);
294
102
        cctz::time_zone from_tz {}, to_tz {};
295
102
        DateValueType ts_value2;
296
297
102
        if (!TimezoneUtils::find_cctz_time_zone(from_tz_name, from_tz)) [[unlikely]] {
298
0
            throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} invalid timezone: {}", name,
299
0
                            from_tz_name);
300
0
        }
301
102
        if (!TimezoneUtils::find_cctz_time_zone(to_tz_name, to_tz)) [[unlikely]] {
302
0
            throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} invalid timezone: {}", name,
303
0
                            to_tz_name);
304
0
        }
305
306
102
        ts_value2.from_unixtime(unix_timestamp_for_convert_tz(ts_value, from_tz), to_tz);
307
308
102
        if (!ts_value2.is_valid_date()) [[unlikely]] {
309
0
            throw_out_of_bound_convert_tz<DateValueType>(date_column->get_element(index_now),
310
0
                                                         from_tz.name(), to_tz.name());
311
0
        }
312
313
102
        result_column->insert(Field::create_field<TYPE_DATETIMEV2>(ts_value2));
314
102
    }
315
};
316
317
7
void register_function_convert_tz(SimpleFunctionFactory& factory) {
318
7
    factory.register_function<FunctionConvertTZ>();
319
7
}
320
321
} // namespace doris