Coverage Report

Created: 2026-03-15 20:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/datetime_errors.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 <string>
21
#include <string_view>
22
23
#include "common/exception.h"
24
#include "common/status.h"
25
#include "core/binary_cast.hpp"
26
#include "core/types.h"
27
#include "core/value/timestamptz_value.h"
28
29
namespace doris {
30
// Convert a native datelike value to printable string using DateValueType::to_string
31
// Note: DateValueType must support to_string(char*) -> char*
32
//       NativeT is the corresponding FieldType of the DataType
33
template <typename DateValueType>
34
0
inline std::string datelike_to_string(DateValueType value) {
35
0
    char buf[40];
36
0
    char* end = value.to_string(buf);
37
    // minus 1 to skip trailing '\0'
38
0
    return std::string(buf, end - 1);
39
0
}
Unexecuted instantiation: _ZN5doris18datelike_to_stringINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_
Unexecuted instantiation: _ZN5doris18datelike_to_stringINS_11DateV2ValueINS_15DateV2ValueTypeEEEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEET_
40
41
// Specialization for TimestampTzValue: output UTC time for error messages
42
template <>
43
0
inline std::string datelike_to_string<TimestampTzValue>(TimestampTzValue native) {
44
    // Use UTC timezone for error messages
45
0
    return native.to_string(cctz::utc_time_zone());
46
0
}
47
48
// Throw for operations with one datelike argument
49
template <typename DateValueType>
50
0
[[noreturn]] inline void throw_out_of_bound_one_date(const char* op, DateValueType arg0) {
51
0
    throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} of {} out of range", op,
52
0
                    datelike_to_string<DateValueType>(arg0));
53
0
}
Unexecuted instantiation: _ZN5doris27throw_out_of_bound_one_dateINS_11DateV2ValueINS_15DateV2ValueTypeEEEEEvPKcT_
Unexecuted instantiation: _ZN5doris27throw_out_of_bound_one_dateINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEEEEvPKcT_
Unexecuted instantiation: _ZN5doris27throw_out_of_bound_one_dateINS_16TimestampTzValueEEEvPKcT_
54
55
// Throw for operations with a datelike and an integer (e.g. period)
56
template <typename DateValueType>
57
[[noreturn]] inline void throw_out_of_bound_date_int(const char* op, DateValueType arg0,
58
0
                                                     Int32 delta) {
59
0
    throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} of {}, {} out of range", op,
60
0
                    datelike_to_string<DateValueType>(arg0), delta);
61
0
}
Unexecuted instantiation: _ZN5doris27throw_out_of_bound_date_intINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEEEEvPKcT_i
Unexecuted instantiation: _ZN5doris27throw_out_of_bound_date_intINS_11DateV2ValueINS_15DateV2ValueTypeEEEEEvPKcT_i
Unexecuted instantiation: _ZN5doris27throw_out_of_bound_date_intINS_16TimestampTzValueEEEvPKcT_i
62
63
// Throw for operations with a datelike and a string interval(e.g., date_add(date, interval '-1 2:3:4' day_second))
64
template <typename DateValueType>
65
[[noreturn]] inline void throw_out_of_bound_date_string(const char* op, DateValueType arg0,
66
0
                                                        std::string_view delta) {
67
0
    throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} of {}, {} out of range", op,
68
0
                    datelike_to_string<DateValueType>(arg0), delta);
69
0
}
Unexecuted instantiation: _ZN5doris30throw_out_of_bound_date_stringINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEEEEvPKcT_St17basic_string_viewIcSt11char_traitsIcEE
Unexecuted instantiation: _ZN5doris30throw_out_of_bound_date_stringINS_16TimestampTzValueEEEvPKcT_St17basic_string_viewIcSt11char_traitsIcEE
70
71
// Throw for operations with a single integer argument (e.g., from_days daynr)
72
0
[[noreturn]] inline void throw_out_of_bound_int(const char* op, int64_t value) {
73
0
    throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} of {} out of range", op, value);
74
0
}
75
76
// Throw for operations with two integer arguments (e.g., makedate(year, day))
77
0
[[noreturn]] inline void throw_out_of_bound_two_ints(const char* op, int64_t a, int64_t b) {
78
0
    throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} of {}, {} out of range", op, a, b);
79
0
}
80
81
// for convert_tz
82
template <typename DateValueType>
83
[[noreturn]] inline void throw_out_of_bound_convert_tz(DateValueType arg0,
84
                                                       std::string_view from_name,
85
0
                                                       std::string_view to_name) {
86
0
    throw Exception(ErrorCode::OUT_OF_BOUND, "Cannot convert {} from {} to {}",
87
0
                    datelike_to_string<DateValueType>(arg0), from_name, to_name);
88
0
}
89
90
// Throw for operations with a datelike, an integer and an origin datelike
91
// (e.g. time_round(datetime, period, origin))
92
template <typename DateValueType>
93
[[noreturn]] inline void throw_out_of_bound_int_date(const char* op, DateValueType arg0,
94
0
                                                     Int32 delta, DateValueType origin) {
95
0
    throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} of {}, {}, {} out of range", op,
96
0
                    datelike_to_string<DateValueType>(arg0), delta,
97
0
                    datelike_to_string<DateValueType>(origin));
98
0
}
Unexecuted instantiation: _ZN5doris27throw_out_of_bound_int_dateINS_11DateV2ValueINS_15DateV2ValueTypeEEEEEvPKcT_iS6_
Unexecuted instantiation: _ZN5doris27throw_out_of_bound_int_dateINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEEEEvPKcT_iS6_
Unexecuted instantiation: _ZN5doris27throw_out_of_bound_int_dateINS_16TimestampTzValueEEEvPKcT_iS4_
99
100
// Throw for operations with two datelike arguments
101
// (e.g. time_round(datetime, origin))
102
template <typename DateValueType>
103
[[noreturn]] inline void throw_out_of_bound_date_date(const char* op, DateValueType arg0,
104
0
                                                      DateValueType arg1) {
105
0
    throw Exception(ErrorCode::OUT_OF_BOUND, "Operation {} of {}, {} out of range", op,
106
0
                    datelike_to_string<DateValueType>(arg0),
107
0
                    datelike_to_string<DateValueType>(arg1));
108
0
}
Unexecuted instantiation: _ZN5doris28throw_out_of_bound_date_dateINS_11DateV2ValueINS_15DateV2ValueTypeEEEEEvPKcT_S6_
Unexecuted instantiation: _ZN5doris28throw_out_of_bound_date_dateINS_11DateV2ValueINS_19DateTimeV2ValueTypeEEEEEvPKcT_S6_
Unexecuted instantiation: _ZN5doris28throw_out_of_bound_date_dateINS_16TimestampTzValueEEEvPKcT_S4_
109
110
// Throw for operations with an invalid string argument (e.g., from_iso8601_date)
111
12
[[noreturn]] inline void throw_invalid_string(const char* op, std::string_view s) {
112
12
    throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} of {} is invalid", op, s);
113
12
}
114
115
// Throw for operations with two invalid string arguments (e.g., unix_timestamp with format)
116
[[noreturn]] inline void throw_invalid_strings(const char* op, std::string_view s0,
117
0
                                               std::string_view s1) {
118
0
    throw Exception(ErrorCode::INVALID_ARGUMENT, "Operation {} of {}, {} is invalid", op, s0, s1);
119
0
}
120
121
// Helper to get time unit name for error messages
122
12
constexpr const char* get_time_unit_name(TimeUnit unit) {
123
12
    switch (unit) {
124
0
    case TimeUnit::YEAR:
125
0
        return "year_add";
126
0
    case TimeUnit::QUARTER:
127
0
        return "quarter_add";
128
0
    case TimeUnit::MONTH:
129
0
        return "month_add";
130
0
    case TimeUnit::WEEK:
131
0
        return "week_add";
132
0
    case TimeUnit::DAY:
133
0
        return "day_add";
134
0
    case TimeUnit::HOUR:
135
0
        return "hour_add";
136
0
    case TimeUnit::MINUTE:
137
0
        return "minute_add";
138
0
    case TimeUnit::SECOND:
139
0
        return "second_add";
140
0
    case TimeUnit::MILLISECOND:
141
0
        return "millisecond_add";
142
0
    case TimeUnit::MICROSECOND:
143
0
        return "microsecond_add";
144
1
    case TimeUnit::YEAR_MONTH:
145
1
        return "year_month_add";
146
1
    case TimeUnit::DAY_HOUR:
147
1
        return "day_hour_add";
148
1
    case TimeUnit::DAY_MINUTE:
149
1
        return "day_minute_add";
150
2
    case TimeUnit::DAY_SECOND:
151
2
        return "day_second_add";
152
1
    case TimeUnit::DAY_MICROSECOND:
153
1
        return "day_microsecond_add";
154
1
    case TimeUnit::HOUR_MINUTE:
155
1
        return "hour_minute_add";
156
1
    case TimeUnit::HOUR_SECOND:
157
1
        return "hour_second_add";
158
1
    case TimeUnit::HOUR_MICROSECOND:
159
1
        return "hour_microsecond_add";
160
1
    case TimeUnit::MINUTE_SECOND:
161
1
        return "minute_second_add";
162
1
    case TimeUnit::MINUTE_MICROSECOND:
163
1
        return "minute_microsecond_add";
164
1
    case TimeUnit::SECOND_MICROSECOND:
165
1
        return "second_microsecond_add";
166
0
    default:
167
0
        return "date_add";
168
12
    }
169
12
}
170
} // namespace doris