Coverage Report

Created: 2025-11-20 04:52

/root/doris/be/src/common/cast_set.h
Line
Count
Source (jump to first uncovered line)
1
2
// Licensed to the Apache Software Foundation (ASF) under one
3
// or more contributor license agreements.  See the NOTICE file
4
// distributed with this work for additional information
5
// regarding copyright ownership.  The ASF licenses this file
6
// to you under the Apache License, Version 2.0 (the
7
// "License"); you may not use this file except in compliance
8
// with the License.  You may obtain a copy of the License at
9
//
10
//   http://www.apache.org/licenses/LICENSE-2.0
11
//
12
// Unless required by applicable law or agreed to in writing,
13
// software distributed under the License is distributed on an
14
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
// KIND, either express or implied.  See the License for the
16
// specific language governing permissions and limitations
17
// under the License.
18
19
#pragma once
20
21
#include <limits>
22
#include <type_traits>
23
24
#include "common/exception.h"
25
#include "common/status.h"
26
27
namespace doris {
28
29
template <typename T, typename U>
30
32
void check_cast_value(U b) {
31
32
    if constexpr (std::is_unsigned_v<U>) {
  Branch (31:19): [Folded - Ignored]
  Branch (31:19): [Folded - Ignored]
  Branch (31:19): [Folded - Ignored]
  Branch (31:19): [Folded - Ignored]
  Branch (31:19): [Folded - Ignored]
  Branch (31:19): [Folded - Ignored]
  Branch (31:19): [Folded - Ignored]
  Branch (31:19): [Folded - Ignored]
  Branch (31:19): [Folded - Ignored]
32
32
        if (b > std::numeric_limits<T>::max()) {
  Branch (32:13): [True: 0, False: 32]
  Branch (32:13): [True: 0, False: 0]
33
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
34
0
                                   "value {} cast  to type {} out of range [{},{}]", b,
35
0
                                   typeid(T).name(), std::numeric_limits<T>::min(),
36
0
                                   std::numeric_limits<T>::max());
37
0
        }
38
32
    } else if constexpr (std::is_unsigned_v<T>) {
  Branch (38:26): [Folded - Ignored]
  Branch (38:26): [Folded - Ignored]
  Branch (38:26): [Folded - Ignored]
  Branch (38:26): [Folded - Ignored]
  Branch (38:26): [Folded - Ignored]
  Branch (38:26): [Folded - Ignored]
  Branch (38:26): [Folded - Ignored]
39
0
        if (b < 0 || b > std::numeric_limits<T>::max()) {
  Branch (39:13): [True: 0, False: 0]
  Branch (39:22): [True: 0, False: 0]
40
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
41
0
                                   "value {} cast  to type {} out of range [{},{}]", b,
42
0
                                   typeid(T).name(), std::numeric_limits<T>::min(),
43
0
                                   std::numeric_limits<T>::max());
44
0
        }
45
0
    } else {
46
0
        if (b < std::numeric_limits<T>::min() || b > std::numeric_limits<T>::max()) {
  Branch (46:13): [True: 0, False: 0]
  Branch (46:50): [True: 0, False: 0]
  Branch (46:13): [True: 0, False: 0]
  Branch (46:50): [True: 0, False: 0]
  Branch (46:13): [True: 0, False: 0]
  Branch (46:50): [True: 0, False: 0]
  Branch (46:13): [True: 0, False: 0]
  Branch (46:50): [True: 0, False: 0]
  Branch (46:13): [True: 0, False: 0]
  Branch (46:50): [True: 0, False: 0]
  Branch (46:13): [True: 0, False: 0]
  Branch (46:50): [True: 0, False: 0]
47
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
48
0
                                   "value {} cast  to type {} out of range [{},{}]", b,
49
0
                                   typeid(T).name(), std::numeric_limits<T>::min(),
50
0
                                   std::numeric_limits<T>::max());
51
0
        }
52
0
    }
53
32
}
_ZN5doris16check_cast_valueIimEEvT0_
Line
Count
Source
30
32
void check_cast_value(U b) {
31
32
    if constexpr (std::is_unsigned_v<U>) {
  Branch (31:19): [Folded - Ignored]
32
32
        if (b > std::numeric_limits<T>::max()) {
  Branch (32:13): [True: 0, False: 32]
33
0
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
34
0
                                   "value {} cast  to type {} out of range [{},{}]", b,
35
0
                                   typeid(T).name(), std::numeric_limits<T>::min(),
36
0
                                   std::numeric_limits<T>::max());
37
0
        }
38
32
    } else if constexpr (std::is_unsigned_v<T>) {
39
32
        if (b < 0 || b > std::numeric_limits<T>::max()) {
40
32
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
41
32
                                   "value {} cast  to type {} out of range [{},{}]", b,
42
32
                                   typeid(T).name(), std::numeric_limits<T>::min(),
43
32
                                   std::numeric_limits<T>::max());
44
32
        }
45
32
    } else {
46
32
        if (b < std::numeric_limits<T>::min() || b > std::numeric_limits<T>::max()) {
47
32
            throw doris::Exception(ErrorCode::INVALID_ARGUMENT,
48
32
                                   "value {} cast  to type {} out of range [{},{}]", b,
49
32
                                   typeid(T).name(), std::numeric_limits<T>::min(),
50
32
                                   std::numeric_limits<T>::max());
51
32
        }
52
32
    }
53
32
}
Unexecuted instantiation: _ZN5doris16check_cast_valueIjmEEvT0_
Unexecuted instantiation: _ZN5doris16check_cast_valueIaiEEvT0_
Unexecuted instantiation: _ZN5doris16check_cast_valueIsiEEvT0_
Unexecuted instantiation: _ZN5doris16check_cast_valueIliEEvT0_
Unexecuted instantiation: _ZN5doris16check_cast_valueIllEEvT0_
Unexecuted instantiation: _ZN5doris16check_cast_valueIlnEEvT0_
Unexecuted instantiation: _ZN5doris16check_cast_valueIilEEvT0_
Unexecuted instantiation: _ZN5doris16check_cast_valueIjlEEvT0_
54
55
template <typename T, typename U, bool need_check_value = true>
56
    requires std::is_integral_v<T> && std::is_integral_v<U>
57
void cast_set(T& a, U b) {
58
    if constexpr (need_check_value) {
59
        check_cast_value<T>(b);
60
    }
61
    a = static_cast<T>(b);
62
}
63
64
template <typename T, typename U>
65
    requires std::is_floating_point_v<T> and std::is_integral_v<U>
66
void cast_set(T& a, U b) {
67
    a = static_cast<T>(b);
68
}
69
70
template <typename T, typename U, bool need_check_value = true>
71
    requires std::is_integral_v<T> && std::is_integral_v<U>
72
32
T cast_set(U b) {
73
32
    if constexpr (need_check_value) {
  Branch (73:19): [Folded - Ignored]
  Branch (73:19): [Folded - Ignored]
  Branch (73:19): [Folded - Ignored]
  Branch (73:19): [Folded - Ignored]
  Branch (73:19): [Folded - Ignored]
  Branch (73:19): [Folded - Ignored]
  Branch (73:19): [Folded - Ignored]
  Branch (73:19): [Folded - Ignored]
  Branch (73:19): [Folded - Ignored]
74
32
        check_cast_value<T>(b);
75
32
    }
76
32
    return static_cast<T>(b);
77
32
}
_ZN5doris8cast_setIimLb1EEET_T0_
Line
Count
Source
72
32
T cast_set(U b) {
73
32
    if constexpr (need_check_value) {
  Branch (73:19): [Folded - Ignored]
74
32
        check_cast_value<T>(b);
75
32
    }
76
32
    return static_cast<T>(b);
77
32
}
Unexecuted instantiation: _ZN5doris8cast_setIjmLb1EEET_T0_
Unexecuted instantiation: _ZN5doris8cast_setIaiLb1EEET_T0_
Unexecuted instantiation: _ZN5doris8cast_setIsiLb1EEET_T0_
Unexecuted instantiation: _ZN5doris8cast_setIliLb1EEET_T0_
Unexecuted instantiation: _ZN5doris8cast_setIllLb1EEET_T0_
Unexecuted instantiation: _ZN5doris8cast_setIlnLb1EEET_T0_
Unexecuted instantiation: _ZN5doris8cast_setIilLb1EEET_T0_
Unexecuted instantiation: _ZN5doris8cast_setIjlLb1EEET_T0_
78
79
template <typename T, typename U>
80
    requires std::is_floating_point_v<T> and std::is_integral_v<U>
81
T cast_set(U b) {
82
    return static_cast<T>(b);
83
}
84
85
} // namespace doris