Coverage Report

Created: 2026-03-31 03:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/cast/cast_to_timestamptz.h
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
#pragma once
19
20
#include "core/assert_cast.h"
21
#include "core/data_type/data_type_date_or_datetime_v2.h"
22
#include "core/data_type/data_type_timestamptz.h"
23
#include "core/data_type/primitive_type.h"
24
#include "core/types.h"
25
#include "core/value/timestamptz_value.h"
26
#include "core/value/vdatetime_value.h"
27
#include "exprs/function/cast/cast_base.h"
28
#include "exprs/function/cast/cast_to_datetimev2_impl.hpp"
29
#include "exprs/function/cast/cast_to_timestamptz_impl.hpp"
30
#include "util/io_helper.h"
31
32
namespace doris {
33
34
template <CastModeType Mode>
35
class CastToImpl<Mode, DataTypeString, DataTypeTimeStampTz> : public CastToBase {
36
public:
37
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
38
                        uint32_t result, size_t input_rows_count,
39
4
                        const NullMap::value_type* null_map = nullptr) const override {
40
4
        const auto& col_from = assert_cast<const DataTypeString::ColumnType&>(
41
4
                *block.get_by_position(arguments[0]).column);
42
43
4
        auto to_type = block.get_by_position(result).type;
44
4
        auto serde = remove_nullable(to_type)->get_serde();
45
4
        MutableColumnPtr column_to;
46
47
4
        DataTypeSerDe::FormatOptions options;
48
4
        options.timezone = &context->state()->timezone_obj();
49
50
4
        if constexpr (Mode == CastModeType::NonStrictMode) {
51
2
            auto to_nullable_type = make_nullable(to_type);
52
2
            column_to = to_nullable_type->create_column();
53
2
            auto& nullable_col_to = assert_cast<ColumnNullable&>(*column_to);
54
2
            RETURN_IF_ERROR(serde->from_string_batch(col_from, nullable_col_to, options));
55
2
        } else if constexpr (Mode == CastModeType::StrictMode) {
56
2
            column_to = to_type->create_column();
57
2
            RETURN_IF_ERROR(
58
2
                    serde->from_string_strict_mode_batch(col_from, *column_to, options, null_map));
59
2
        }
60
61
3
        block.get_by_position(result).column = std::move(column_to);
62
4
        return Status::OK();
63
4
    }
_ZNK5doris10CastToImplILNS_12CastModeTypeE0ENS_14DataTypeStringENS_19DataTypeTimeStampTzEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh
Line
Count
Source
39
2
                        const NullMap::value_type* null_map = nullptr) const override {
40
2
        const auto& col_from = assert_cast<const DataTypeString::ColumnType&>(
41
2
                *block.get_by_position(arguments[0]).column);
42
43
2
        auto to_type = block.get_by_position(result).type;
44
2
        auto serde = remove_nullable(to_type)->get_serde();
45
2
        MutableColumnPtr column_to;
46
47
2
        DataTypeSerDe::FormatOptions options;
48
2
        options.timezone = &context->state()->timezone_obj();
49
50
        if constexpr (Mode == CastModeType::NonStrictMode) {
51
            auto to_nullable_type = make_nullable(to_type);
52
            column_to = to_nullable_type->create_column();
53
            auto& nullable_col_to = assert_cast<ColumnNullable&>(*column_to);
54
            RETURN_IF_ERROR(serde->from_string_batch(col_from, nullable_col_to, options));
55
2
        } else if constexpr (Mode == CastModeType::StrictMode) {
56
2
            column_to = to_type->create_column();
57
2
            RETURN_IF_ERROR(
58
2
                    serde->from_string_strict_mode_batch(col_from, *column_to, options, null_map));
59
2
        }
60
61
1
        block.get_by_position(result).column = std::move(column_to);
62
2
        return Status::OK();
63
2
    }
_ZNK5doris10CastToImplILNS_12CastModeTypeE1ENS_14DataTypeStringENS_19DataTypeTimeStampTzEE12execute_implEPNS_15FunctionContextERNS_5BlockERKSt6vectorIjSaIjEEjmPKh
Line
Count
Source
39
2
                        const NullMap::value_type* null_map = nullptr) const override {
40
2
        const auto& col_from = assert_cast<const DataTypeString::ColumnType&>(
41
2
                *block.get_by_position(arguments[0]).column);
42
43
2
        auto to_type = block.get_by_position(result).type;
44
2
        auto serde = remove_nullable(to_type)->get_serde();
45
2
        MutableColumnPtr column_to;
46
47
2
        DataTypeSerDe::FormatOptions options;
48
2
        options.timezone = &context->state()->timezone_obj();
49
50
2
        if constexpr (Mode == CastModeType::NonStrictMode) {
51
2
            auto to_nullable_type = make_nullable(to_type);
52
2
            column_to = to_nullable_type->create_column();
53
2
            auto& nullable_col_to = assert_cast<ColumnNullable&>(*column_to);
54
2
            RETURN_IF_ERROR(serde->from_string_batch(col_from, nullable_col_to, options));
55
        } else if constexpr (Mode == CastModeType::StrictMode) {
56
            column_to = to_type->create_column();
57
            RETURN_IF_ERROR(
58
                    serde->from_string_strict_mode_batch(col_from, *column_to, options, null_map));
59
        }
60
61
2
        block.get_by_position(result).column = std::move(column_to);
62
2
        return Status::OK();
63
2
    }
64
};
65
66
template <>
67
class CastToImpl<CastModeType::StrictMode, DataTypeDateTimeV2, DataTypeTimeStampTz>
68
        : public CastToBase {
69
public:
70
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
71
                        uint32_t result, size_t input_rows_count,
72
2
                        const NullMap::value_type* null_map = nullptr) const override {
73
2
        const auto& col_from =
74
2
                assert_cast<const ColumnDateTimeV2&>(*block.get_by_position(arguments[0]).column)
75
2
                        .get_data();
76
77
2
        auto col_to = ColumnTimeStampTz::create(input_rows_count);
78
2
        auto& col_to_data = col_to->get_data();
79
2
        const auto& local_time_zone = context->state()->timezone_obj();
80
81
2
        const auto dt_scale = block.get_by_position(arguments[0]).type->get_scale();
82
2
        const auto tz_scale = block.get_by_position(result).type->get_scale();
83
84
6
        for (int i = 0; i < input_rows_count; ++i) {
85
5
            if (null_map && null_map[i]) {
86
0
                continue;
87
0
            }
88
89
5
            DateV2Value<DateTimeV2ValueType> from_dt {col_from[i]};
90
5
            TimestampTzValue tz_value;
91
92
5
            if (!tz_value.from_datetime(from_dt, local_time_zone, dt_scale, tz_scale)) {
93
1
                return Status::InternalError(
94
1
                        "can not cast from  datetime : {} to timestamptz in timezone : {}",
95
1
                        from_dt.to_string(), context->state()->timezone());
96
1
            }
97
98
4
            col_to_data[i] = tz_value;
99
4
        }
100
1
        block.get_by_position(result).column = std::move(col_to);
101
1
        return Status::OK();
102
2
    }
103
};
104
105
template <>
106
class CastToImpl<CastModeType::NonStrictMode, DataTypeDateTimeV2, DataTypeTimeStampTz>
107
        : public CastToBase {
108
public:
109
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
110
                        uint32_t result, size_t input_rows_count,
111
2
                        const NullMap::value_type*) const override {
112
2
        const auto& col_from =
113
2
                assert_cast<const ColumnDateTimeV2&>(*block.get_by_position(arguments[0]).column)
114
2
                        .get_data();
115
116
2
        auto col_to = ColumnTimeStampTz::create(input_rows_count);
117
2
        auto& col_to_data = col_to->get_data();
118
2
        auto col_null = ColumnBool::create(input_rows_count, 0);
119
2
        auto& col_null_map = col_null->get_data();
120
2
        const auto& local_time_zone = context->state()->timezone_obj();
121
122
2
        const auto dt_scale = block.get_by_position(arguments[0]).type->get_scale();
123
2
        const auto tz_scale = block.get_by_position(result).type->get_scale();
124
125
10
        for (int i = 0; i < input_rows_count; ++i) {
126
8
            DateV2Value<DateTimeV2ValueType> from_dt {col_from[i]};
127
8
            TimestampTzValue tz_value {};
128
129
8
            if (tz_value.from_datetime(from_dt, local_time_zone, dt_scale, tz_scale)) {
130
7
                col_to_data[i] = tz_value;
131
7
            } else {
132
1
                col_null_map[i] = true;
133
1
            }
134
8
        }
135
136
2
        block.get_by_position(result).column =
137
2
                ColumnNullable::create(std::move(col_to), std::move(col_null));
138
2
        return Status::OK();
139
2
    }
140
};
141
142
template <>
143
class CastToImpl<CastModeType::StrictMode, DataTypeTimeStampTz, DataTypeTimeStampTz>
144
        : public CastToBase {
145
public:
146
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
147
                        uint32_t result, size_t input_rows_count,
148
0
                        const NullMap::value_type* null_map = nullptr) const override {
149
0
        const auto& col_from =
150
0
                assert_cast<const ColumnTimeStampTz&>(*block.get_by_position(arguments[0]).column)
151
0
                        .get_data();
152
153
0
        auto col_to = ColumnTimeStampTz::create(input_rows_count);
154
0
        auto& col_to_data = col_to->get_data();
155
0
        const auto& local_time_zone = context->state()->timezone_obj();
156
157
0
        const auto from_scale = block.get_by_position(arguments[0]).type->get_scale();
158
0
        const auto to_scale = block.get_by_position(result).type->get_scale();
159
160
0
        for (int i = 0; i < input_rows_count; ++i) {
161
0
            if (null_map && null_map[i]) {
162
0
                continue;
163
0
            }
164
165
0
            const auto& from_tz = col_from[i];
166
0
            auto& to_tz = col_to_data[i];
167
168
0
            if (!transform_date_scale(to_scale, from_scale, to_tz, from_tz)) {
169
0
                return Status::InternalError(
170
0
                        "can not cast from  timestamptz : {} to timestamptz in timezone : {}",
171
0
                        TimestampTzValue {from_tz}.to_string(local_time_zone, from_scale),
172
0
                        context->state()->timezone());
173
0
            }
174
0
        }
175
0
        block.get_by_position(result).column = std::move(col_to);
176
0
        return Status::OK();
177
0
    }
178
};
179
180
template <>
181
class CastToImpl<CastModeType::NonStrictMode, DataTypeTimeStampTz, DataTypeTimeStampTz>
182
        : public CastToBase {
183
public:
184
    Status execute_impl(FunctionContext* context, Block& block, const ColumnNumbers& arguments,
185
                        uint32_t result, size_t input_rows_count,
186
0
                        const NullMap::value_type*) const override {
187
0
        const auto& col_from =
188
0
                assert_cast<const ColumnTimeStampTz&>(*block.get_by_position(arguments[0]).column)
189
0
                        .get_data();
190
191
0
        auto col_to = ColumnTimeStampTz::create(input_rows_count);
192
0
        auto& col_to_data = col_to->get_data();
193
0
        auto col_null = ColumnBool::create(input_rows_count, 0);
194
0
        auto& col_null_map = col_null->get_data();
195
196
0
        const auto from_scale = block.get_by_position(arguments[0]).type->get_scale();
197
0
        const auto to_scale = block.get_by_position(result).type->get_scale();
198
199
0
        for (int i = 0; i < input_rows_count; ++i) {
200
0
            const auto& from_tz = col_from[i];
201
0
            auto& to_tz = col_to_data[i];
202
203
0
            if (!transform_date_scale(to_scale, from_scale, to_tz, from_tz)) {
204
0
                col_null_map[i] = true;
205
0
                to_tz = TimestampTzValue(MIN_DATETIME_V2);
206
0
            }
207
0
        }
208
209
0
        block.get_by_position(result).column =
210
0
                ColumnNullable::create(std::move(col_to), std::move(col_null));
211
0
        return Status::OK();
212
0
    }
213
};
214
215
namespace CastWrapper {
216
inline WrapperType create_timestamptz_wrapper(FunctionContext* context,
217
0
                                              const DataTypePtr& from_type) {
218
0
    std::shared_ptr<CastToBase> cast_to_timestamptz;
219
220
0
    auto make_timestamptz_wrapper = [&](const auto& types) -> bool {
221
0
        using Types = std::decay_t<decltype(types)>;
222
0
        using FromDataType = typename Types::LeftType;
223
        if constexpr (CastUtil::IsBaseCastFromType<FromDataType> ||
224
0
                      IsTimeStampTzType<FromDataType>) {
225
0
            if (context->enable_strict_mode()) {
226
0
                cast_to_timestamptz = std::make_shared<
227
0
                        CastToImpl<CastModeType::StrictMode, FromDataType, DataTypeTimeStampTz>>();
228
0
            } else {
229
0
                cast_to_timestamptz =
230
0
                        std::make_shared<CastToImpl<CastModeType::NonStrictMode, FromDataType,
231
0
                                                    DataTypeTimeStampTz>>();
232
0
            }
233
0
            return true;
234
        } else {
235
            return false;
236
        }
237
0
    };
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_14DataTypeNumberILNS_13PrimitiveTypeE2EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_14DataTypeNumberILNS_13PrimitiveTypeE3EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_14DataTypeNumberILNS_13PrimitiveTypeE4EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_14DataTypeNumberILNS_13PrimitiveTypeE5EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_14DataTypeNumberILNS_13PrimitiveTypeE6EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_14DataTypeNumberILNS_13PrimitiveTypeE7EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_14DataTypeNumberILNS_13PrimitiveTypeE8EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_14DataTypeNumberILNS_13PrimitiveTypeE9EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_15DataTypeDecimalILNS_13PrimitiveTypeE28EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_15DataTypeDecimalILNS_13PrimitiveTypeE29EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_15DataTypeDecimalILNS_13PrimitiveTypeE20EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_15DataTypeDecimalILNS_13PrimitiveTypeE30EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_15DataTypeDecimalILNS_13PrimitiveTypeE35EEEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_12DataTypeDateEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_14DataTypeDateV2EvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_18DataTypeDateTimeV2EvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_16DataTypeDateTimeEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_14DataTypeTimeV2EvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_19DataTypeTimeStampTzEvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_12DataTypeIPv4EvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_12DataTypeIPv6EvEEEEbSB_
Unexecuted instantiation: _ZZN5doris11CastWrapper26create_timestamptz_wrapperEPNS_15FunctionContextERKSt10shared_ptrIKNS_9IDataTypeEEENKUlRKT_E_clINS_8TypePairINS_14DataTypeStringEvEEEEbSB_
238
239
0
    if (!call_on_index_and_data_type<void>(from_type->get_primitive_type(),
240
0
                                           make_timestamptz_wrapper)) {
241
0
        return create_unsupport_wrapper(
242
0
                fmt::format("CAST AS timestamptz not supported {}", from_type->get_name()));
243
0
    }
244
245
0
    return [cast_to_timestamptz](FunctionContext* context, Block& block,
246
0
                                 const ColumnNumbers& arguments, uint32_t result,
247
0
                                 size_t input_rows_count,
248
0
                                 const NullMap::value_type* null_map = nullptr) {
249
0
        return cast_to_timestamptz->execute_impl(context, block, arguments, result,
250
0
                                                 input_rows_count, null_map);
251
0
    };
252
0
}
253
254
}; // namespace CastWrapper
255
256
} // namespace doris