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