1 | //===----------------------------------------------------------------------===// |
2 | // DuckDB |
3 | // |
4 | // duckdb/common/operator/subtract.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/common/types.hpp" |
12 | #include "duckdb/common/exception.hpp" |
13 | #include "duckdb/common/type_util.hpp" |
14 | |
15 | namespace duckdb { |
16 | |
17 | struct interval_t; |
18 | struct date_t; |
19 | struct timestamp_t; |
20 | struct dtime_t; |
21 | |
22 | struct SubtractOperator { |
23 | template <class TA, class TB, class TR> |
24 | static inline TR Operation(TA left, TB right) { |
25 | return left - right; |
26 | } |
27 | }; |
28 | |
29 | template <> |
30 | float SubtractOperator::Operation(float left, float right); |
31 | template <> |
32 | double SubtractOperator::Operation(double left, double right); |
33 | template <> |
34 | interval_t SubtractOperator::Operation(interval_t left, interval_t right); |
35 | template <> |
36 | int64_t SubtractOperator::Operation(date_t left, date_t right); |
37 | template <> |
38 | date_t SubtractOperator::Operation(date_t left, int32_t right); |
39 | template <> |
40 | date_t SubtractOperator::Operation(date_t left, interval_t right); |
41 | template <> |
42 | timestamp_t SubtractOperator::Operation(timestamp_t left, interval_t right); |
43 | template <> |
44 | interval_t SubtractOperator::Operation(timestamp_t left, timestamp_t right); |
45 | |
46 | struct TrySubtractOperator { |
47 | template <class TA, class TB, class TR> |
48 | static inline bool Operation(TA left, TB right, TR &result) { |
49 | throw InternalException("Unimplemented type for TrySubtractOperator" ); |
50 | } |
51 | }; |
52 | |
53 | template <> |
54 | bool TrySubtractOperator::Operation(uint8_t left, uint8_t right, uint8_t &result); |
55 | template <> |
56 | bool TrySubtractOperator::Operation(uint16_t left, uint16_t right, uint16_t &result); |
57 | template <> |
58 | bool TrySubtractOperator::Operation(uint32_t left, uint32_t right, uint32_t &result); |
59 | template <> |
60 | bool TrySubtractOperator::Operation(uint64_t left, uint64_t right, uint64_t &result); |
61 | |
62 | template <> |
63 | bool TrySubtractOperator::Operation(int8_t left, int8_t right, int8_t &result); |
64 | template <> |
65 | bool TrySubtractOperator::Operation(int16_t left, int16_t right, int16_t &result); |
66 | template <> |
67 | bool TrySubtractOperator::Operation(int32_t left, int32_t right, int32_t &result); |
68 | template <> |
69 | bool TrySubtractOperator::Operation(int64_t left, int64_t right, int64_t &result); |
70 | template <> |
71 | bool TrySubtractOperator::Operation(hugeint_t left, hugeint_t right, hugeint_t &result); |
72 | |
73 | struct SubtractOperatorOverflowCheck { |
74 | template <class TA, class TB, class TR> |
75 | static inline TR Operation(TA left, TB right) { |
76 | TR result; |
77 | if (!TrySubtractOperator::Operation(left, right, result)) { |
78 | throw OutOfRangeException("Overflow in subtraction of %s (%d - %d)!" , TypeIdToString(GetTypeId<TA>()), left, |
79 | right); |
80 | } |
81 | return result; |
82 | } |
83 | }; |
84 | |
85 | struct TryDecimalSubtract { |
86 | template <class TA, class TB, class TR> |
87 | static inline bool Operation(TA left, TB right, TR &result) { |
88 | throw InternalException("Unimplemented type for TryDecimalSubtract" ); |
89 | } |
90 | }; |
91 | |
92 | template <> |
93 | bool TryDecimalSubtract::Operation(int16_t left, int16_t right, int16_t &result); |
94 | template <> |
95 | bool TryDecimalSubtract::Operation(int32_t left, int32_t right, int32_t &result); |
96 | template <> |
97 | bool TryDecimalSubtract::Operation(int64_t left, int64_t right, int64_t &result); |
98 | template <> |
99 | bool TryDecimalSubtract::Operation(hugeint_t left, hugeint_t right, hugeint_t &result); |
100 | |
101 | struct DecimalSubtractOverflowCheck { |
102 | template <class TA, class TB, class TR> |
103 | static inline TR Operation(TA left, TB right) { |
104 | TR result; |
105 | if (!TryDecimalSubtract::Operation<TA, TB, TR>(left, right, result)) { |
106 | throw OutOfRangeException("Overflow in subtract of DECIMAL(18) (%d - %d). You might want to add an " |
107 | "explicit cast to a bigger decimal." , |
108 | left, right); |
109 | } |
110 | return result; |
111 | } |
112 | }; |
113 | |
114 | template <> |
115 | hugeint_t DecimalSubtractOverflowCheck::Operation(hugeint_t left, hugeint_t right); |
116 | |
117 | struct SubtractTimeOperator { |
118 | template <class TA, class TB, class TR> |
119 | static TR Operation(TA left, TB right); |
120 | }; |
121 | |
122 | template <> |
123 | dtime_t SubtractTimeOperator::Operation(dtime_t left, interval_t right); |
124 | |
125 | } // namespace duckdb |
126 | |