bkietz commented on code in PR #43957:
URL: https://github.com/apache/arrow/pull/43957#discussion_r1752668339
##########
cpp/src/arrow/array/builder_dict.h:
##########
@@ -298,20 +298,12 @@ class DictionaryBuilderBase : public ArrayBuilder {
return Append(std::string_view(value, length));
}
- /// \brief Append a decimal (only for Decimal128Type)
- template <typename T1 = T>
- enable_if_decimal128<T1, Status> Append(const Decimal128& value) {
- uint8_t data[16];
- value.ToBytes(data);
- return Append(data, 16);
- }
-
- /// \brief Append a decimal (only for Decimal128Type)
- template <typename T1 = T>
- enable_if_decimal256<T1, Status> Append(const Decimal256& value) {
- uint8_t data[32];
+ /// \brief Append a decimal (only for Decimal32/64/128/256 Type)
+ template <typename T1 = T, typename CType = typename TypeTraits<T1>::CType>
+ enable_if_decimal<T1, Status> Append(const CType& value) {
+ uint8_t data[T1::kByteWidth];
value.ToBytes(data);
- return Append(data, 32);
+ return Append(data, T1::kByteWidth);
Review Comment:
Should be equivalent, little bit nicer:
```suggestion
auto bytes = value.ToBytes();
return Append(bytes.data(), bytes.size());
```
##########
cpp/src/arrow/util/decimal.h:
##########
@@ -31,6 +31,263 @@
namespace arrow {
+class Decimal64;
+
+/// Represents a signed 32-bit decimal value in two's complement.
+/// Calulations wrap around and overflow is ignored.
+/// The max decimal precision that can be safely represented is
+/// 9 significant digits.
+///
+/// The implementation is split into two parts :
+///
+/// 1. BasicDecimal32
+/// - can be safely compiled to IR without references to libstdc++
+/// 2. Decimal32
+/// - has additional functionality on top of BasicDecimal32 to deal with
+/// strings and streams
+class ARROW_EXPORT Decimal32 : public BasicDecimal32 {
+ public:
+ /// \cond FALSE
+ // (need to avoid a duplicate definition in sphinx)
+ using BasicDecimal32::BasicDecimal32;
+ /// \endcond
+
+ /// \brief constructor creates a Decimal32 from a BasicDecimal32
+ constexpr Decimal32(const BasicDecimal32& value) noexcept // NOLINT
runtime/explicit
+ : BasicDecimal32(value) {}
+
+ /// \brief Parse the number from a base 10 string representation
+ explicit Decimal32(const std::string& value);
+
+ /// \brief Empty constructor creates a Decimal32 with a value of 0
+ /// this is required for some older compilers
+ constexpr Decimal32() noexcept : BasicDecimal32() {}
+
+ /// \brief Divide this number by right and return the result.
+ ///
+ /// This operation is not destructive.
+ /// The answer rounds to zero. Signs work like:
+ /// 21 / 5 -> 4, 1
+ /// -21 / 5 -> -4, -1
+ /// 21 / -5 -> -4, 1
+ /// -21 / -5 -> 4, -1
+ /// \param[in] divisor the number to divide by
+ /// \return the pair of the quotient and the remainder
+ Result<std::pair<Decimal32, Decimal32>> Divide(const Decimal32& divisor)
const {
+ std::pair<Decimal32, Decimal32> result;
+ auto dstatus = BasicDecimal32::Divide(divisor, &result.first,
&result.second);
+ ARROW_RETURN_NOT_OK(ToArrowStatus(dstatus));
+ return result;
+ }
+
+ /// \brief Convert the Decimal32 value to a base 10 decimal string with the
given scale
+ std::string ToString(int32_t scale) const;
+
+ /// \brief Convert the value to an integer string
+ std::string ToIntegerString() const;
+
+ /// \brief Cast this value to an int64_t
+ explicit operator int64_t() const;
+
+ explicit operator Decimal64() const;
+
+ /// \brief Convert a decimal string to a Decimal value, optionally including
+ /// precision and scale if they're passed in and not null.
+ static Status FromString(std::string_view s, Decimal32* out, int32_t*
precision,
+ int32_t* scale = NULLPTR);
+ static Status FromString(const std::string& s, Decimal32* out, int32_t*
precision,
+ int32_t* scale = NULLPTR);
+ static Status FromString(const char* s, Decimal32* out, int32_t* precision,
+ int32_t* scale = NULLPTR);
+ static Result<Decimal32> FromString(std::string_view s);
+ static Result<Decimal32> FromString(const std::string& s);
+ static Result<Decimal32> FromString(const char* s);
+
+ static Result<Decimal32> FromReal(double real, int32_t precision, int32_t
scale);
+ static Result<Decimal32> FromReal(float real, int32_t precision, int32_t
scale);
+
+ /// \brief Convert from a big-endian byte representation. The length must be
+ /// between 1 and 4
+ /// \return error statis if the length is an invalid value
+ static Result<Decimal32> FromBigEndian(const uint8_t* data, int32_t length);
+
+ static void SwapEndian(const uint32_t* old_data, uint32_t* const new_data,
+ const int64_t length);
Review Comment:
This makes the usage in util.cc a bit nicer, but as a method on Decimal
itself it might cause confusion. I was imagining
```suggestion
Decimal32 SwapEndian() const;
```
Then util.cc would look like
```c++
template <typename T>
enable_if_decimal<T, Status> Visit(const T& type) {
using value_type = typename TypeTraits<T>::CType;
auto data = data_->buffers[1]->span_as<value_type>();
auto new_data = new_buffer->mutable_data_as<value_type>();
for (value_type v : data) {
*new_data++ = v.SwapEndian();
}
```
... actually now that I think of it, it'd also be pretty straightforward to
just use ToBytes instead of adding SwapEndian:
```c++
template <typename T>
enable_if_decimal<T, Status> Visit(const T& type) {
using value_type = typename TypeTraits<T>::CType;
auto data = data_->buffers[1]->span_as<value_type>();
auto new_data = new_buffer->mutable_data_as<value_type>();
for (value_type v : data) {
auto bytes = v.ToBytes();
std::reverse(bytes.begin(), bytes.end());
memcpy(new_data++, &bytes, sizeof(bytes));
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]