Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cpp/src/arrow/array-decimal-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "arrow/util/decimal.h"

namespace arrow {
namespace decimal {

TEST(TypesTest, TestDecimal32Type) {
DecimalType t1(8, 4);
Expand Down Expand Up @@ -221,4 +222,5 @@ INSTANTIATE_TEST_CASE_P(Decimal128BuilderTest, Decimal128BuilderTest,
::testing::Range(
DecimalPrecision<int128_t>::minimum, DecimalPrecision<int128_t>::maximum));

} // namespace decimal
} // namespace arrow
44 changes: 27 additions & 17 deletions cpp/src/arrow/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,25 +310,35 @@ bool DecimalArray::IsNegative(int64_t i) const {
return sign_bitmap_data_ != nullptr ? BitUtil::GetBit(sign_bitmap_data_, i) : false;
}

template <typename T>
ARROW_EXPORT Decimal<T> DecimalArray::Value(int64_t i) const {
Decimal<T> result;
FromBytes(GetValue(i), &result);
return result;
}

template ARROW_EXPORT Decimal32 DecimalArray::Value(int64_t i) const;
template ARROW_EXPORT Decimal64 DecimalArray::Value(int64_t i) const;

template <>
ARROW_EXPORT Decimal128 DecimalArray::Value(int64_t i) const {
Decimal128 result;
FromBytes(GetValue(i), IsNegative(i), &result);
return result;
std::string DecimalArray::FormatValue(int64_t i) const {
const auto type_ = std::dynamic_pointer_cast<DecimalType>(type());
const int precision = type_->precision;
const int scale = type_->scale;
const int byte_width = byte_width_;
const uint8_t* bytes = GetValue(i);
switch (byte_width) {
case 4: {
decimal::Decimal32 value;
decimal::FromBytes(bytes, &value);
return decimal::ToString(value, precision, scale);
}
case 8: {
decimal::Decimal64 value;
decimal::FromBytes(bytes, &value);
return decimal::ToString(value, precision, scale);
}
case 16: {
decimal::Decimal128 value;
decimal::FromBytes(bytes, IsNegative(i), &value);
return decimal::ToString(value, precision, scale);
}
default: {
DCHECK(false) << "Invalid byte width: " << byte_width;
return "";
}
}
}

template ARROW_EXPORT Decimal128 DecimalArray::Value(int64_t i) const;

std::shared_ptr<Array> DecimalArray::Slice(int64_t offset, int64_t length) const {
ConformSliceParams(offset_, length_, &offset, &length);
return std::make_shared<DecimalArray>(
Expand Down
3 changes: 1 addition & 2 deletions cpp/src/arrow/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ class ARROW_EXPORT DecimalArray : public FixedSizeBinaryArray {

bool IsNegative(int64_t i) const;

template <typename T>
ARROW_EXPORT Decimal<T> Value(int64_t i) const;
std::string FormatValue(int64_t i) const;

std::shared_ptr<Array> Slice(int64_t offset, int64_t length) const override;

Expand Down
12 changes: 6 additions & 6 deletions cpp/src/arrow/builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -332,27 +332,27 @@ DecimalBuilder::DecimalBuilder(MemoryPool* pool, const std::shared_ptr<DataType>
sign_bitmap_data_(nullptr) {}

template <typename T>
ARROW_EXPORT Status DecimalBuilder::Append(const Decimal<T>& val) {
ARROW_EXPORT Status DecimalBuilder::Append(const decimal::Decimal<T>& val) {
DCHECK_EQ(sign_bitmap_, nullptr) << "sign_bitmap_ is not null";
DCHECK_EQ(sign_bitmap_data_, nullptr) << "sign_bitmap_data_ is not null";

RETURN_NOT_OK(FixedSizeBinaryBuilder::Reserve(1));
return FixedSizeBinaryBuilder::Append(reinterpret_cast<const uint8_t*>(&val.value));
}

template ARROW_EXPORT Status DecimalBuilder::Append(const Decimal32& val);
template ARROW_EXPORT Status DecimalBuilder::Append(const Decimal64& val);
template ARROW_EXPORT Status DecimalBuilder::Append(const decimal::Decimal32& val);
template ARROW_EXPORT Status DecimalBuilder::Append(const decimal::Decimal64& val);

template <>
ARROW_EXPORT Status DecimalBuilder::Append(const Decimal128& value) {
ARROW_EXPORT Status DecimalBuilder::Append(const decimal::Decimal128& value) {
DCHECK_NE(sign_bitmap_, nullptr) << "sign_bitmap_ is null";
DCHECK_NE(sign_bitmap_data_, nullptr) << "sign_bitmap_data_ is null";

RETURN_NOT_OK(FixedSizeBinaryBuilder::Reserve(1));
uint8_t stack_bytes[16] = {0};
uint8_t* bytes = stack_bytes;
bool is_negative;
ToBytes(value, &bytes, &is_negative);
decimal::ToBytes(value, &bytes, &is_negative);
RETURN_NOT_OK(FixedSizeBinaryBuilder::Append(bytes));

// TODO(phillipc): calculate the proper storage size here (do we have a function to do
Expand All @@ -363,7 +363,7 @@ ARROW_EXPORT Status DecimalBuilder::Append(const Decimal128& value) {
return Status::OK();
}

template ARROW_EXPORT Status DecimalBuilder::Append(const Decimal128& val);
template ARROW_EXPORT Status DecimalBuilder::Append(const decimal::Decimal128& val);

Status DecimalBuilder::Init(int64_t capacity) {
RETURN_NOT_OK(FixedSizeBinaryBuilder::Init(capacity));
Expand Down
6 changes: 5 additions & 1 deletion cpp/src/arrow/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ namespace arrow {

class Array;

namespace decimal {

template <typename T>
struct Decimal;

} // namespace decimal

static constexpr int64_t kMinBuilderCapacity = 1 << 5;

/// Base class for all data array builders.
Expand Down Expand Up @@ -421,7 +425,7 @@ class ARROW_EXPORT DecimalBuilder : public FixedSizeBinaryBuilder {
explicit DecimalBuilder(MemoryPool* pool, const std::shared_ptr<DataType>& type);

template <typename T>
ARROW_EXPORT Status Append(const Decimal<T>& val);
ARROW_EXPORT Status Append(const decimal::Decimal<T>& val);

Status Init(int64_t capacity) override;
Status Resize(int64_t capacity) override;
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/python/builtin_convert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ class ListConverter : public TypedConverter<ListBuilder> {

#define DECIMAL_CONVERT_CASE(bit_width, item, builder) \
case bit_width: { \
arrow::Decimal##bit_width out; \
arrow::decimal::Decimal##bit_width out; \
RETURN_NOT_OK(PythonDecimalToArrowDecimal((item), &out)); \
RETURN_NOT_OK((builder)->Append(out)); \
break; \
Expand Down
12 changes: 7 additions & 5 deletions cpp/src/arrow/python/helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ Status ImportFromModule(const OwnedRef& module, const std::string& name, OwnedRe
}

template <typename T>
Status PythonDecimalToArrowDecimal(PyObject* python_decimal, Decimal<T>* arrow_decimal) {
Status PythonDecimalToArrowDecimal(
PyObject* python_decimal, decimal::Decimal<T>* arrow_decimal) {
// Call Python's str(decimal_object)
OwnedRef str_obj(PyObject_Str(python_decimal));
RETURN_IF_PYERROR();
Expand All @@ -92,11 +93,11 @@ Status PythonDecimalToArrowDecimal(PyObject* python_decimal, Decimal<T>* arrow_d
}

template Status PythonDecimalToArrowDecimal(
PyObject* python_decimal, Decimal32* arrow_decimal);
PyObject* python_decimal, decimal::Decimal32* arrow_decimal);
template Status PythonDecimalToArrowDecimal(
PyObject* python_decimal, Decimal64* arrow_decimal);
PyObject* python_decimal, decimal::Decimal64* arrow_decimal);
template Status PythonDecimalToArrowDecimal(
PyObject* python_decimal, Decimal128* arrow_decimal);
PyObject* python_decimal, decimal::Decimal128* arrow_decimal);

Status InferDecimalPrecisionAndScale(
PyObject* python_decimal, int* precision, int* scale) {
Expand All @@ -111,7 +112,8 @@ Status InferDecimalPrecisionAndScale(
auto size = str.size;

std::string c_string(bytes, size);
return FromString(c_string, static_cast<Decimal32*>(nullptr), precision, scale);
return FromString(
c_string, static_cast<decimal::Decimal32*>(nullptr), precision, scale);
}

Status DecimalFromString(
Expand Down
7 changes: 6 additions & 1 deletion cpp/src/arrow/python/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@

namespace arrow {

namespace decimal {

template <typename T>
struct Decimal;

} // namespace decimal

namespace py {

class OwnedRef;
Expand All @@ -43,7 +47,8 @@ Status ImportFromModule(
const OwnedRef& module, const std::string& module_name, OwnedRef* ref);

template <typename T>
Status PythonDecimalToArrowDecimal(PyObject* python_decimal, Decimal<T>* arrow_decimal);
Status PythonDecimalToArrowDecimal(
PyObject* python_decimal, decimal::Decimal<T>* arrow_decimal);

Status InferDecimalPrecisionAndScale(
PyObject* python_decimal, int* precision = nullptr, int* scale = nullptr);
Expand Down
10 changes: 5 additions & 5 deletions cpp/src/arrow/python/pandas_convert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ Status PandasConverter::ConvertDates() {

#define CONVERT_DECIMAL_CASE(bit_width, builder, object) \
case bit_width: { \
Decimal##bit_width d; \
decimal::Decimal##bit_width d; \
RETURN_NOT_OK(PythonDecimalToArrowDecimal((object), &d)); \
RETURN_NOT_OK((builder).Append(d)); \
break; \
Expand Down Expand Up @@ -620,7 +620,7 @@ Status PandasConverter::ConvertObjectFixedWidthBytes(

template <typename T>
Status validate_precision(int precision) {
constexpr static const int maximum_precision = DecimalPrecision<T>::maximum;
constexpr static const int maximum_precision = decimal::DecimalPrecision<T>::maximum;
if (!(precision > 0 && precision <= maximum_precision)) {
std::stringstream ss;
ss << "Invalid precision: " << precision << ". Minimum is 1, maximum is "
Expand All @@ -636,7 +636,7 @@ Status RawDecimalToString(
DCHECK_NE(bytes, nullptr);
DCHECK_NE(result, nullptr);
RETURN_NOT_OK(validate_precision<T>(precision));
Decimal<T> decimal;
decimal::Decimal<T> decimal;
FromBytes(bytes, &decimal);
*result = ToString(decimal, precision, scale);
return Status::OK();
Expand All @@ -651,8 +651,8 @@ Status RawDecimalToString(const uint8_t* bytes, int precision, int scale,
bool is_negative, std::string* result) {
DCHECK_NE(bytes, nullptr);
DCHECK_NE(result, nullptr);
RETURN_NOT_OK(validate_precision<int128_t>(precision));
Decimal128 decimal;
RETURN_NOT_OK(validate_precision<boost::multiprecision::int128_t>(precision));
decimal::Decimal128 decimal;
FromBytes(bytes, is_negative, &decimal);
*result = ToString(decimal, precision, scale);
return Status::OK();
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/arrow/python/python-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ TEST(DecimalTest, TestPythonDecimalToArrowDecimal128) {
ASSERT_NE(pydecimal.obj(), nullptr);
ASSERT_EQ(PyErr_Occurred(), nullptr);

Decimal128 arrow_decimal;
int128_t boost_decimal(decimal_string);
decimal::Decimal128 arrow_decimal;
boost::multiprecision::int128_t boost_decimal(decimal_string);
PyObject* obj = pydecimal.obj();
ASSERT_OK(PythonDecimalToArrowDecimal(obj, &arrow_decimal));
ASSERT_EQ(boost_decimal, arrow_decimal.value);
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/type_fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ std::shared_ptr<DataType> ARROW_EXPORT binary();

std::shared_ptr<DataType> ARROW_EXPORT date32();
std::shared_ptr<DataType> ARROW_EXPORT date64();
std::shared_ptr<DataType> ARROW_EXPORT decimal(int precision, int scale);
std::shared_ptr<DataType> ARROW_EXPORT decimal_type(int precision, int scale);

} // namespace arrow

Expand Down
1 change: 0 additions & 1 deletion cpp/src/arrow/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
# Headers: top level
install(FILES
bit-util.h
decimal.h
logging.h
macros.h
random.h
Expand Down
2 changes: 2 additions & 0 deletions cpp/src/arrow/util/decimal-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "arrow/test-util.h"

namespace arrow {
namespace decimal {

template <typename T>
class DecimalTest : public ::testing::Test {
Expand Down Expand Up @@ -158,4 +159,5 @@ TEST(DecimalTest, TestDecimal128StringAndBytesRoundTrip) {

ASSERT_EQ(expected.value, result.value);
}
} // namespace decimal
} // namespace arrow
2 changes: 2 additions & 0 deletions cpp/src/arrow/util/decimal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <boost/regex.hpp>

namespace arrow {
namespace decimal {

static const boost::regex DECIMAL_PATTERN("(\\+?|-?)((0*)(\\d*))(\\.(\\d+))?");

Expand Down Expand Up @@ -138,4 +139,5 @@ void ToBytes(const Decimal128& decimal, uint8_t** bytes, bool* is_negative) {
*is_negative = backend.isneg();
}

} // namespace decimal
} // namespace arrow
2 changes: 2 additions & 0 deletions cpp/src/arrow/util/decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <boost/multiprecision/cpp_int.hpp>

namespace arrow {
namespace decimal {

using boost::multiprecision::int128_t;

Expand Down Expand Up @@ -140,5 +141,6 @@ ARROW_EXPORT void ToBytes(const Decimal32& value, uint8_t** bytes);
ARROW_EXPORT void ToBytes(const Decimal64& value, uint8_t** bytes);
ARROW_EXPORT void ToBytes(const Decimal128& decimal, uint8_t** bytes, bool* is_negative);

} // namespace decimal
} // namespace arrow
#endif // ARROW_DECIMAL_H
5 changes: 0 additions & 5 deletions python/pyarrow/includes/common.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,6 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
c_bool IsTypeError()


cdef extern from "arrow/util/decimal.h" namespace "arrow" nogil:
cdef cppclass int128_t:
pass


cdef inline object PyObject_to_object(PyObject* o):
# Cast to "object" increments reference count
cdef object result = <object> o
Expand Down
7 changes: 1 addition & 6 deletions python/pyarrow/includes/libarrow.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
TimeUnit_MICRO" arrow::TimeUnit::MICRO"
TimeUnit_NANO" arrow::TimeUnit::NANO"

cdef cppclass Decimal[T]:
Decimal(const T&)

cdef c_string ToString[T](const Decimal[T]&, int, int)

cdef cppclass CDataType" arrow::DataType":
Type type

Expand Down Expand Up @@ -226,7 +221,7 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
const uint8_t* GetValue(int i)

cdef cppclass CDecimalArray" arrow::DecimalArray"(CFixedSizeBinaryArray):
Decimal[T] Value[T](int i)
c_string FormatValue(int i)

cdef cppclass CListArray" arrow::ListArray"(CArray):
const int32_t* raw_value_offsets()
Expand Down
9 changes: 1 addition & 8 deletions python/pyarrow/scalar.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from pyarrow.schema cimport DataType, box_data_type

from pyarrow.includes.common cimport int128_t
from pyarrow.compat import frombytes
import pyarrow.schema as schema
import decimal
Expand Down Expand Up @@ -213,13 +212,7 @@ cdef class DecimalValue(ArrayValue):
int bit_width = t.bit_width()
int precision = t.precision
int scale = t.scale
c_string s
if bit_width == 32:
s = ToString[int32_t](ap.Value[int32_t](self.index), precision, scale)
elif bit_width == 64:
s = ToString[int64_t](ap.Value[int64_t](self.index), precision, scale)
elif bit_width == 128:
s = ToString[int128_t](ap.Value[int128_t](self.index), precision, scale)
c_string s = ap.FormatValue(self.index)
return decimal.Decimal(s.decode('utf8'))


Expand Down