Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
decimal.h
Go to the documentation of this file.
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 #ifndef ARROW_DECIMAL_H
19 #define ARROW_DECIMAL_H
20 
21 #include <array>
22 #include <cstdint>
23 #include <string>
24 #include <type_traits>
25 
26 #include "arrow/status.h"
27 #include "arrow/util/macros.h"
28 #include "arrow/util/visibility.h"
29 
30 namespace arrow {
31 
39 class ARROW_EXPORT Decimal128 {
40  public:
42  constexpr Decimal128(int64_t high, uint64_t low) noexcept
43  : high_bits_(high), low_bits_(low) {}
44 
46  constexpr Decimal128() noexcept : Decimal128(0, 0) {}
47 
49  template <typename T,
50  typename = typename std::enable_if<std::is_integral<T>::value, T>::type>
51  constexpr Decimal128(T value) noexcept
52  : Decimal128(static_cast<int64_t>(value) >= 0 ? 0 : -1,
53  static_cast<uint64_t>(value)) {}
54 
56  explicit Decimal128(const std::string& value);
57 
60  explicit Decimal128(const uint8_t* bytes);
61 
63  Decimal128& Negate();
64 
66  Decimal128& Abs();
67 
69  Decimal128& operator+=(const Decimal128& right);
70 
72  Decimal128& operator-=(const Decimal128& right);
73 
75  Decimal128& operator*=(const Decimal128& right);
76 
86  Status Divide(const Decimal128& divisor, Decimal128* result,
87  Decimal128* remainder) const;
88 
90  Decimal128& operator/=(const Decimal128& right);
91 
93  Decimal128& operator|=(const Decimal128& right);
94 
96  Decimal128& operator&=(const Decimal128& right);
97 
99  Decimal128& operator<<=(uint32_t bits);
100 
102  Decimal128& operator>>=(uint32_t bits);
103 
105  inline int64_t high_bits() const { return high_bits_; }
106 
108  inline uint64_t low_bits() const { return low_bits_; }
109 
111  std::array<uint8_t, 16> ToBytes() const;
112  void ToBytes(uint8_t* out) const;
113 
116  std::string ToString(int32_t scale) const;
117 
119  std::string ToIntegerString() const;
120 
122  explicit operator int64_t() const;
123 
126  static Status FromString(const std::string& s, Decimal128* out,
127  int* precision = NULLPTR, int* scale = NULLPTR);
128 
130  Status Rescale(int32_t original_scale, int32_t new_scale, Decimal128* out) const;
131 
132  private:
133  int64_t high_bits_;
134  uint64_t low_bits_;
135 };
136 
137 ARROW_EXPORT bool operator==(const Decimal128& left, const Decimal128& right);
138 ARROW_EXPORT bool operator!=(const Decimal128& left, const Decimal128& right);
139 ARROW_EXPORT bool operator<(const Decimal128& left, const Decimal128& right);
140 ARROW_EXPORT bool operator<=(const Decimal128& left, const Decimal128& right);
141 ARROW_EXPORT bool operator>(const Decimal128& left, const Decimal128& right);
142 ARROW_EXPORT bool operator>=(const Decimal128& left, const Decimal128& right);
143 
144 ARROW_EXPORT Decimal128 operator-(const Decimal128& operand);
145 ARROW_EXPORT Decimal128 operator~(const Decimal128& operand);
146 ARROW_EXPORT Decimal128 operator+(const Decimal128& left, const Decimal128& right);
147 ARROW_EXPORT Decimal128 operator-(const Decimal128& left, const Decimal128& right);
148 ARROW_EXPORT Decimal128 operator*(const Decimal128& left, const Decimal128& right);
149 ARROW_EXPORT Decimal128 operator/(const Decimal128& left, const Decimal128& right);
150 ARROW_EXPORT Decimal128 operator%(const Decimal128& left, const Decimal128& right);
151 
152 } // namespace arrow
153 
154 #endif // ARROW_DECIMAL_H
Decimal128 operator*(const Decimal128 &left, const Decimal128 &right)
constexpr Decimal128(int64_t high, uint64_t low) noexcept
Create an Decimal128 from the two&#39;s complement representation.
Definition: decimal.h:42
int64_t high_bits() const
Get the high bits of the two&#39;s complement representation of the number.
Definition: decimal.h:105
#define NULLPTR
Definition: macros.h:69
Decimal128 operator%(const Decimal128 &left, const Decimal128 &right)
bool operator<(const Decimal128 &left, const Decimal128 &right)
bool operator<=(const Decimal128 &left, const Decimal128 &right)
Decimal128 operator-(const Decimal128 &operand)
Definition: status.h:106
constexpr Decimal128(T value) noexcept
Convert any integer value into an Decimal128.
Definition: decimal.h:51
Decimal128 operator/(const Decimal128 &left, const Decimal128 &right)
bool operator!=(const stl_allocator< T1 > &lhs, const stl_allocator< T2 > &rhs) noexcept
Definition: allocator.h:94
Decimal128 operator+(const Decimal128 &left, const Decimal128 &right)
Top-level namespace for Apache Arrow C++ API.
Definition: adapter.h:32
bool operator==(const stl_allocator< T1 > &lhs, const stl_allocator< T2 > &rhs) noexcept
Definition: allocator.h:89
constexpr Decimal128() noexcept
Empty constructor creates an Decimal128 with a value of 0.
Definition: decimal.h:46
bool operator>=(const Decimal128 &left, const Decimal128 &right)
bool operator>(const Decimal128 &left, const Decimal128 &right)
Decimal128 operator~(const Decimal128 &operand)
Represents a signed 128-bit integer in two&#39;s complement.
Definition: decimal.h:39
uint64_t low_bits() const
Get the low bits of the two&#39;s complement representation of the number.
Definition: decimal.h:108