From 0c300ecf2b9e5cbfdf4f6a226e31cfc33ef44581 Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Sun, 9 Apr 2017 19:48:06 -0400 Subject: [PATCH 1/5] ARROW-800: [C++] Boost headers being transitively included in pyarrow --- cpp/src/arrow/array.cc | 44 +++++++++++++++++----------- cpp/src/arrow/array.h | 3 +- python/pyarrow/includes/common.pxd | 5 ---- python/pyarrow/includes/libarrow.pxd | 7 +---- python/pyarrow/scalar.pyx | 9 +----- 5 files changed, 30 insertions(+), 38 deletions(-) diff --git a/cpp/src/arrow/array.cc b/cpp/src/arrow/array.cc index 4e73e7176fa9..77751d52060c 100644 --- a/cpp/src/arrow/array.cc +++ b/cpp/src/arrow/array.cc @@ -310,25 +310,35 @@ bool DecimalArray::IsNegative(int64_t i) const { return sign_bitmap_data_ != nullptr ? BitUtil::GetBit(sign_bitmap_data_, i) : false; } -template -ARROW_EXPORT Decimal DecimalArray::Value(int64_t i) const { - Decimal 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::Value(int64_t i) const { + auto type_ = std::dynamic_pointer_cast(type()); + int precision = type_->precision; + int scale = type_->scale; + const int byte_width = byte_width_; + const uint8_t* bytes = GetValue(i); + switch (byte_width) { + case 4: { + Decimal32 value; + FromBytes(bytes, &value); + return ToString(value, precision, scale); + } + case 8: { + Decimal64 value; + FromBytes(bytes, &value); + return ToString(value, precision, scale); + } + case 16: { + Decimal128 value; + FromBytes(bytes, IsNegative(i), &value); + return 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 DecimalArray::Slice(int64_t offset, int64_t length) const { ConformSliceParams(offset_, length_, &offset, &length); return std::make_shared( diff --git a/cpp/src/arrow/array.h b/cpp/src/arrow/array.h index a4117facdefd..d341e9d6a369 100644 --- a/cpp/src/arrow/array.h +++ b/cpp/src/arrow/array.h @@ -384,8 +384,7 @@ class ARROW_EXPORT DecimalArray : public FixedSizeBinaryArray { bool IsNegative(int64_t i) const; - template - ARROW_EXPORT Decimal Value(int64_t i) const; + ARROW_EXPORT std::string Value(int64_t i) const; std::shared_ptr Slice(int64_t offset, int64_t length) const override; diff --git a/python/pyarrow/includes/common.pxd b/python/pyarrow/includes/common.pxd index 4860334a9213..ab38ff3084f0 100644 --- a/python/pyarrow/includes/common.pxd +++ b/python/pyarrow/includes/common.pxd @@ -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 = o diff --git a/python/pyarrow/includes/libarrow.pxd b/python/pyarrow/includes/libarrow.pxd index 73d96b25f521..be9fb52479c8 100644 --- a/python/pyarrow/includes/libarrow.pxd +++ b/python/pyarrow/includes/libarrow.pxd @@ -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 @@ -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 Value(int i) cdef cppclass CListArray" arrow::ListArray"(CArray): const int32_t* raw_value_offsets() diff --git a/python/pyarrow/scalar.pyx b/python/pyarrow/scalar.pyx index 196deedefa95..9814ad375c05 100644 --- a/python/pyarrow/scalar.pyx +++ b/python/pyarrow/scalar.pyx @@ -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 @@ -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.Value(self.index) return decimal.Decimal(s.decode('utf8')) From 900143289ca1f2e4c1ba537f901a86dd4a611e22 Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Sun, 9 Apr 2017 19:53:47 -0400 Subject: [PATCH 2/5] Remove ARROW_EXPORT of method inside ARROW_EXPORTed class --- cpp/src/arrow/array.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/src/arrow/array.h b/cpp/src/arrow/array.h index d341e9d6a369..2ace8451407a 100644 --- a/cpp/src/arrow/array.h +++ b/cpp/src/arrow/array.h @@ -384,7 +384,7 @@ class ARROW_EXPORT DecimalArray : public FixedSizeBinaryArray { bool IsNegative(int64_t i) const; - ARROW_EXPORT std::string Value(int64_t i) const; + std::string Value(int64_t i) const; std::shared_ptr Slice(int64_t offset, int64_t length) const override; From f326f3ac712d8ff9385a79872ed80579f6e73bf3 Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Sun, 9 Apr 2017 19:55:12 -0400 Subject: [PATCH 3/5] Const things --- cpp/src/arrow/array.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cpp/src/arrow/array.cc b/cpp/src/arrow/array.cc index 77751d52060c..7d4a3d48a139 100644 --- a/cpp/src/arrow/array.cc +++ b/cpp/src/arrow/array.cc @@ -311,9 +311,9 @@ bool DecimalArray::IsNegative(int64_t i) const { } std::string DecimalArray::Value(int64_t i) const { - auto type_ = std::dynamic_pointer_cast(type()); - int precision = type_->precision; - int scale = type_->scale; + const auto type_ = std::dynamic_pointer_cast(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) { From ba46502a4b36af933c2b9f147e8fbceb788a9749 Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Sun, 9 Apr 2017 20:28:53 -0400 Subject: [PATCH 4/5] decimal namespace and change to FormatValue --- cpp/src/arrow/array-decimal-test.cc | 2 + cpp/src/arrow/array.cc | 20 +++++----- cpp/src/arrow/array.h | 2 +- cpp/src/arrow/builder.cc | 12 +++--- cpp/src/arrow/builder.h | 6 ++- cpp/src/arrow/python/builtin_convert.cc | 2 +- cpp/src/arrow/python/helpers.cc | 10 ++--- cpp/src/arrow/python/helpers.h | 6 ++- cpp/src/arrow/python/pandas_convert.cc | 10 ++--- cpp/src/arrow/python/python-test.cc | 4 +- cpp/src/arrow/type_fwd.h | 2 +- cpp/src/arrow/util/CMakeLists.txt | 1 - cpp/src/arrow/util/decimal-test.cc | 2 + cpp/src/arrow/util/decimal.cc | 2 + cpp/src/arrow/util/decimal.h | 52 +++++++++++++------------ python/pyarrow/includes/libarrow.pxd | 2 +- python/pyarrow/scalar.pyx | 2 +- 17 files changed, 76 insertions(+), 61 deletions(-) diff --git a/cpp/src/arrow/array-decimal-test.cc b/cpp/src/arrow/array-decimal-test.cc index 4c01f928a6f2..8353acc454f4 100644 --- a/cpp/src/arrow/array-decimal-test.cc +++ b/cpp/src/arrow/array-decimal-test.cc @@ -23,6 +23,7 @@ #include "arrow/util/decimal.h" namespace arrow { +namespace decimal { TEST(TypesTest, TestDecimal32Type) { DecimalType t1(8, 4); @@ -221,4 +222,5 @@ INSTANTIATE_TEST_CASE_P(Decimal128BuilderTest, Decimal128BuilderTest, ::testing::Range( DecimalPrecision::minimum, DecimalPrecision::maximum)); +} // namespace decimal } // namespace arrow diff --git a/cpp/src/arrow/array.cc b/cpp/src/arrow/array.cc index 7d4a3d48a139..c4a78f3b2e40 100644 --- a/cpp/src/arrow/array.cc +++ b/cpp/src/arrow/array.cc @@ -310,7 +310,7 @@ bool DecimalArray::IsNegative(int64_t i) const { return sign_bitmap_data_ != nullptr ? BitUtil::GetBit(sign_bitmap_data_, i) : false; } -std::string DecimalArray::Value(int64_t i) const { +std::string DecimalArray::FormatValue(int64_t i) const { const auto type_ = std::dynamic_pointer_cast(type()); const int precision = type_->precision; const int scale = type_->scale; @@ -318,19 +318,19 @@ std::string DecimalArray::Value(int64_t i) const { const uint8_t* bytes = GetValue(i); switch (byte_width) { case 4: { - Decimal32 value; - FromBytes(bytes, &value); - return ToString(value, precision, scale); + decimal::Decimal32 value; + decimal::FromBytes(bytes, &value); + return decimal::ToString(value, precision, scale); } case 8: { - Decimal64 value; - FromBytes(bytes, &value); - return ToString(value, precision, scale); + decimal::Decimal64 value; + decimal::FromBytes(bytes, &value); + return decimal::ToString(value, precision, scale); } case 16: { - Decimal128 value; - FromBytes(bytes, IsNegative(i), &value); - return ToString(value, precision, scale); + decimal::Decimal128 value; + decimal::FromBytes(bytes, IsNegative(i), &value); + return decimal::ToString(value, precision, scale); } default: { DCHECK(false) << "Invalid byte width: " << byte_width; diff --git a/cpp/src/arrow/array.h b/cpp/src/arrow/array.h index 2ace8451407a..4f8b22e31b4e 100644 --- a/cpp/src/arrow/array.h +++ b/cpp/src/arrow/array.h @@ -384,7 +384,7 @@ class ARROW_EXPORT DecimalArray : public FixedSizeBinaryArray { bool IsNegative(int64_t i) const; - std::string Value(int64_t i) const; + std::string FormatValue(int64_t i) const; std::shared_ptr Slice(int64_t offset, int64_t length) const override; diff --git a/cpp/src/arrow/builder.cc b/cpp/src/arrow/builder.cc index a3677eff6866..4281a61474cc 100644 --- a/cpp/src/arrow/builder.cc +++ b/cpp/src/arrow/builder.cc @@ -332,7 +332,7 @@ DecimalBuilder::DecimalBuilder(MemoryPool* pool, const std::shared_ptr sign_bitmap_data_(nullptr) {} template -ARROW_EXPORT Status DecimalBuilder::Append(const Decimal& val) { +ARROW_EXPORT Status DecimalBuilder::Append(const decimal::Decimal& val) { DCHECK_EQ(sign_bitmap_, nullptr) << "sign_bitmap_ is not null"; DCHECK_EQ(sign_bitmap_data_, nullptr) << "sign_bitmap_data_ is not null"; @@ -340,11 +340,11 @@ ARROW_EXPORT Status DecimalBuilder::Append(const Decimal& val) { return FixedSizeBinaryBuilder::Append(reinterpret_cast(&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"; @@ -352,7 +352,7 @@ ARROW_EXPORT Status DecimalBuilder::Append(const Decimal128& value) { 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 @@ -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)); diff --git a/cpp/src/arrow/builder.h b/cpp/src/arrow/builder.h index d42ab5b01d1b..68769165b02c 100644 --- a/cpp/src/arrow/builder.h +++ b/cpp/src/arrow/builder.h @@ -37,9 +37,13 @@ namespace arrow { class Array; +namespace decimal { + template struct Decimal; +} // namespace decimal + static constexpr int64_t kMinBuilderCapacity = 1 << 5; /// Base class for all data array builders. @@ -421,7 +425,7 @@ class ARROW_EXPORT DecimalBuilder : public FixedSizeBinaryBuilder { explicit DecimalBuilder(MemoryPool* pool, const std::shared_ptr& type); template - ARROW_EXPORT Status Append(const Decimal& val); + ARROW_EXPORT Status Append(const decimal::Decimal& val); Status Init(int64_t capacity) override; Status Resize(int64_t capacity) override; diff --git a/cpp/src/arrow/python/builtin_convert.cc b/cpp/src/arrow/python/builtin_convert.cc index a064a3daf970..565d55311a6a 100644 --- a/cpp/src/arrow/python/builtin_convert.cc +++ b/cpp/src/arrow/python/builtin_convert.cc @@ -523,7 +523,7 @@ class ListConverter : public TypedConverter { #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; \ diff --git a/cpp/src/arrow/python/helpers.cc b/cpp/src/arrow/python/helpers.cc index ffba7bbc21c1..4c84e849b4ff 100644 --- a/cpp/src/arrow/python/helpers.cc +++ b/cpp/src/arrow/python/helpers.cc @@ -74,7 +74,7 @@ Status ImportFromModule(const OwnedRef& module, const std::string& name, OwnedRe } template -Status PythonDecimalToArrowDecimal(PyObject* python_decimal, Decimal* arrow_decimal) { +Status PythonDecimalToArrowDecimal(PyObject* python_decimal, decimal::Decimal* arrow_decimal) { // Call Python's str(decimal_object) OwnedRef str_obj(PyObject_Str(python_decimal)); RETURN_IF_PYERROR(); @@ -92,11 +92,11 @@ Status PythonDecimalToArrowDecimal(PyObject* python_decimal, Decimal* 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) { @@ -111,7 +111,7 @@ Status InferDecimalPrecisionAndScale( auto size = str.size; std::string c_string(bytes, size); - return FromString(c_string, static_cast(nullptr), precision, scale); + return FromString(c_string, static_cast(nullptr), precision, scale); } Status DecimalFromString( diff --git a/cpp/src/arrow/python/helpers.h b/cpp/src/arrow/python/helpers.h index a19b25f7db80..a0e26c5fd804 100644 --- a/cpp/src/arrow/python/helpers.h +++ b/cpp/src/arrow/python/helpers.h @@ -29,9 +29,13 @@ namespace arrow { +namespace decimal { + template struct Decimal; +} // namespace decimal + namespace py { class OwnedRef; @@ -43,7 +47,7 @@ Status ImportFromModule( const OwnedRef& module, const std::string& module_name, OwnedRef* ref); template -Status PythonDecimalToArrowDecimal(PyObject* python_decimal, Decimal* arrow_decimal); +Status PythonDecimalToArrowDecimal(PyObject* python_decimal, decimal::Decimal* arrow_decimal); Status InferDecimalPrecisionAndScale( PyObject* python_decimal, int* precision = nullptr, int* scale = nullptr); diff --git a/cpp/src/arrow/python/pandas_convert.cc b/cpp/src/arrow/python/pandas_convert.cc index 5bb8e45e191a..413009f9dbcb 100644 --- a/cpp/src/arrow/python/pandas_convert.cc +++ b/cpp/src/arrow/python/pandas_convert.cc @@ -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; \ @@ -620,7 +620,7 @@ Status PandasConverter::ConvertObjectFixedWidthBytes( template Status validate_precision(int precision) { - constexpr static const int maximum_precision = DecimalPrecision::maximum; + constexpr static const int maximum_precision = decimal::DecimalPrecision::maximum; if (!(precision > 0 && precision <= maximum_precision)) { std::stringstream ss; ss << "Invalid precision: " << precision << ". Minimum is 1, maximum is " @@ -636,7 +636,7 @@ Status RawDecimalToString( DCHECK_NE(bytes, nullptr); DCHECK_NE(result, nullptr); RETURN_NOT_OK(validate_precision(precision)); - Decimal decimal; + decimal::Decimal decimal; FromBytes(bytes, &decimal); *result = ToString(decimal, precision, scale); return Status::OK(); @@ -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(precision)); - Decimal128 decimal; + RETURN_NOT_OK(validate_precision(precision)); + decimal::Decimal128 decimal; FromBytes(bytes, is_negative, &decimal); *result = ToString(decimal, precision, scale); return Status::OK(); diff --git a/cpp/src/arrow/python/python-test.cc b/cpp/src/arrow/python/python-test.cc index b63d2ffb1cd2..a4a11c039b60 100644 --- a/cpp/src/arrow/python/python-test.cc +++ b/cpp/src/arrow/python/python-test.cc @@ -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); diff --git a/cpp/src/arrow/type_fwd.h b/cpp/src/arrow/type_fwd.h index acf12c3d9d18..2bb05f853a09 100644 --- a/cpp/src/arrow/type_fwd.h +++ b/cpp/src/arrow/type_fwd.h @@ -147,7 +147,7 @@ std::shared_ptr ARROW_EXPORT binary(); std::shared_ptr ARROW_EXPORT date32(); std::shared_ptr ARROW_EXPORT date64(); -std::shared_ptr ARROW_EXPORT decimal(int precision, int scale); +std::shared_ptr ARROW_EXPORT decimal_type(int precision, int scale); } // namespace arrow diff --git a/cpp/src/arrow/util/CMakeLists.txt b/cpp/src/arrow/util/CMakeLists.txt index 054f11055b60..9aa8bae273fb 100644 --- a/cpp/src/arrow/util/CMakeLists.txt +++ b/cpp/src/arrow/util/CMakeLists.txt @@ -22,7 +22,6 @@ # Headers: top level install(FILES bit-util.h - decimal.h logging.h macros.h random.h diff --git a/cpp/src/arrow/util/decimal-test.cc b/cpp/src/arrow/util/decimal-test.cc index 1e22643962d5..dcaa9afd8724 100644 --- a/cpp/src/arrow/util/decimal-test.cc +++ b/cpp/src/arrow/util/decimal-test.cc @@ -23,6 +23,7 @@ #include "arrow/test-util.h" namespace arrow { +namespace decimal { template class DecimalTest : public ::testing::Test { @@ -158,4 +159,5 @@ TEST(DecimalTest, TestDecimal128StringAndBytesRoundTrip) { ASSERT_EQ(expected.value, result.value); } +} // namespace decimal } // namespace arrow diff --git a/cpp/src/arrow/util/decimal.cc b/cpp/src/arrow/util/decimal.cc index 1ac347180fec..3b8a3ff0398b 100644 --- a/cpp/src/arrow/util/decimal.cc +++ b/cpp/src/arrow/util/decimal.cc @@ -20,6 +20,7 @@ #include namespace arrow { +namespace decimal { static const boost::regex DECIMAL_PATTERN("(\\+?|-?)((0*)(\\d*))(\\.(\\d+))?"); @@ -138,4 +139,5 @@ void ToBytes(const Decimal128& decimal, uint8_t** bytes, bool* is_negative) { *is_negative = backend.isneg(); } +} // namespace decimal } // namespace arrow diff --git a/cpp/src/arrow/util/decimal.h b/cpp/src/arrow/util/decimal.h index 46883e3de93c..916df503ef23 100644 --- a/cpp/src/arrow/util/decimal.h +++ b/cpp/src/arrow/util/decimal.h @@ -31,29 +31,30 @@ #include namespace arrow { +namespace decimal { using boost::multiprecision::int128_t; -template +template struct ARROW_EXPORT Decimal; ARROW_EXPORT void StringToInteger( - const std::string& whole, const std::string& fractional, int8_t sign, int32_t* out); + const std::string &whole, const std::string &fractional, int8_t sign, int32_t *out); ARROW_EXPORT void StringToInteger( - const std::string& whole, const std::string& fractional, int8_t sign, int64_t* out); + const std::string &whole, const std::string &fractional, int8_t sign, int64_t *out); ARROW_EXPORT void StringToInteger( - const std::string& whole, const std::string& fractional, int8_t sign, int128_t* out); + const std::string &whole, const std::string &fractional, int8_t sign, int128_t *out); -template -ARROW_EXPORT Status FromString(const std::string& s, Decimal* out, - int* precision = nullptr, int* scale = nullptr); +template +ARROW_EXPORT Status FromString(const std::string &s, Decimal *out, + int *precision = nullptr, int *scale = nullptr); -template +template struct ARROW_EXPORT Decimal { Decimal() : value() {} - explicit Decimal(const std::string& s) : value() { FromString(s, this); } - explicit Decimal(const char* s) : Decimal(std::string(s)) {} - explicit Decimal(const T& value) : value(value) {} + explicit Decimal(const std::string &s) : value() { FromString(s, this); } + explicit Decimal(const char *s) : Decimal(std::string(s)) {} + explicit Decimal(const T &value) : value(value) {} using value_type = T; value_type value; @@ -63,38 +64,38 @@ using Decimal32 = Decimal; using Decimal64 = Decimal; using Decimal128 = Decimal; -template +template struct ARROW_EXPORT DecimalPrecision {}; -template <> +template<> struct ARROW_EXPORT DecimalPrecision { constexpr static const int minimum = 1; constexpr static const int maximum = 9; }; -template <> +template<> struct ARROW_EXPORT DecimalPrecision { constexpr static const int minimum = 10; constexpr static const int maximum = 18; }; -template <> +template<> struct ARROW_EXPORT DecimalPrecision { constexpr static const int minimum = 19; constexpr static const int maximum = 38; }; -template +template ARROW_EXPORT std::string ToString( - const Decimal& decimal_value, int precision, int scale) { + const Decimal &decimal_value, int precision, int scale) { T value = decimal_value.value; // Decimal values are sent to clients as strings so in the interest of // speed the string will be created without the using stringstream with the // whole/fractional_part(). size_t last_char_idx = precision + (scale > 0) // Add a space for decimal place - + (scale == precision) // Add a space for leading 0 - + (value < 0); // Add a space for negative sign + + (scale == precision) // Add a space for leading 0 + + (value < 0); // Add a space for negative sign std::string str = std::string(last_char_idx, '0'); // Start filling in the values in reverse order by taking the last digit // of the value. Use a positive value and worry about the sign later. At this @@ -131,14 +132,15 @@ ARROW_EXPORT std::string ToString( } /// Conversion from raw bytes to a Decimal value -ARROW_EXPORT void FromBytes(const uint8_t* bytes, Decimal32* value); -ARROW_EXPORT void FromBytes(const uint8_t* bytes, Decimal64* value); -ARROW_EXPORT void FromBytes(const uint8_t* bytes, bool is_negative, Decimal128* decimal); +ARROW_EXPORT void FromBytes(const uint8_t *bytes, Decimal32 *value); +ARROW_EXPORT void FromBytes(const uint8_t *bytes, Decimal64 *value); +ARROW_EXPORT void FromBytes(const uint8_t *bytes, bool is_negative, Decimal128 *decimal); /// Conversion from a Decimal value to raw bytes -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); +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 diff --git a/python/pyarrow/includes/libarrow.pxd b/python/pyarrow/includes/libarrow.pxd index be9fb52479c8..e719e185b7b1 100644 --- a/python/pyarrow/includes/libarrow.pxd +++ b/python/pyarrow/includes/libarrow.pxd @@ -221,7 +221,7 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil: const uint8_t* GetValue(int i) cdef cppclass CDecimalArray" arrow::DecimalArray"(CFixedSizeBinaryArray): - c_string Value(int i) + c_string FormatValue(int i) cdef cppclass CListArray" arrow::ListArray"(CArray): const int32_t* raw_value_offsets() diff --git a/python/pyarrow/scalar.pyx b/python/pyarrow/scalar.pyx index 9814ad375c05..7591ae880da3 100644 --- a/python/pyarrow/scalar.pyx +++ b/python/pyarrow/scalar.pyx @@ -212,7 +212,7 @@ cdef class DecimalValue(ArrayValue): int bit_width = t.bit_width() int precision = t.precision int scale = t.scale - c_string s = ap.Value(self.index) + c_string s = ap.FormatValue(self.index) return decimal.Decimal(s.decode('utf8')) From a983841b19bf420cbf58559af2d47065d841ca0e Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Sun, 9 Apr 2017 20:30:15 -0400 Subject: [PATCH 5/5] Formatting --- cpp/src/arrow/python/builtin_convert.cc | 2 +- cpp/src/arrow/python/helpers.cc | 6 ++- cpp/src/arrow/python/helpers.h | 3 +- cpp/src/arrow/python/pandas_convert.cc | 2 +- cpp/src/arrow/util/decimal.h | 50 ++++++++++++------------- 5 files changed, 33 insertions(+), 30 deletions(-) diff --git a/cpp/src/arrow/python/builtin_convert.cc b/cpp/src/arrow/python/builtin_convert.cc index 565d55311a6a..1ae13f3db061 100644 --- a/cpp/src/arrow/python/builtin_convert.cc +++ b/cpp/src/arrow/python/builtin_convert.cc @@ -523,7 +523,7 @@ class ListConverter : public TypedConverter { #define DECIMAL_CONVERT_CASE(bit_width, item, builder) \ case bit_width: { \ - arrow::decimal::Decimal##bit_width out; \ + arrow::decimal::Decimal##bit_width out; \ RETURN_NOT_OK(PythonDecimalToArrowDecimal((item), &out)); \ RETURN_NOT_OK((builder)->Append(out)); \ break; \ diff --git a/cpp/src/arrow/python/helpers.cc b/cpp/src/arrow/python/helpers.cc index 4c84e849b4ff..3d3d07a51583 100644 --- a/cpp/src/arrow/python/helpers.cc +++ b/cpp/src/arrow/python/helpers.cc @@ -74,7 +74,8 @@ Status ImportFromModule(const OwnedRef& module, const std::string& name, OwnedRe } template -Status PythonDecimalToArrowDecimal(PyObject* python_decimal, decimal::Decimal* arrow_decimal) { +Status PythonDecimalToArrowDecimal( + PyObject* python_decimal, decimal::Decimal* arrow_decimal) { // Call Python's str(decimal_object) OwnedRef str_obj(PyObject_Str(python_decimal)); RETURN_IF_PYERROR(); @@ -111,7 +112,8 @@ Status InferDecimalPrecisionAndScale( auto size = str.size; std::string c_string(bytes, size); - return FromString(c_string, static_cast(nullptr), precision, scale); + return FromString( + c_string, static_cast(nullptr), precision, scale); } Status DecimalFromString( diff --git a/cpp/src/arrow/python/helpers.h b/cpp/src/arrow/python/helpers.h index a0e26c5fd804..77fde263de7e 100644 --- a/cpp/src/arrow/python/helpers.h +++ b/cpp/src/arrow/python/helpers.h @@ -47,7 +47,8 @@ Status ImportFromModule( const OwnedRef& module, const std::string& module_name, OwnedRef* ref); template -Status PythonDecimalToArrowDecimal(PyObject* python_decimal, decimal::Decimal* arrow_decimal); +Status PythonDecimalToArrowDecimal( + PyObject* python_decimal, decimal::Decimal* arrow_decimal); Status InferDecimalPrecisionAndScale( PyObject* python_decimal, int* precision = nullptr, int* scale = nullptr); diff --git a/cpp/src/arrow/python/pandas_convert.cc b/cpp/src/arrow/python/pandas_convert.cc index 413009f9dbcb..1a250e83c509 100644 --- a/cpp/src/arrow/python/pandas_convert.cc +++ b/cpp/src/arrow/python/pandas_convert.cc @@ -530,7 +530,7 @@ Status PandasConverter::ConvertDates() { #define CONVERT_DECIMAL_CASE(bit_width, builder, object) \ case bit_width: { \ - decimal::Decimal##bit_width d; \ + decimal::Decimal##bit_width d; \ RETURN_NOT_OK(PythonDecimalToArrowDecimal((object), &d)); \ RETURN_NOT_OK((builder).Append(d)); \ break; \ diff --git a/cpp/src/arrow/util/decimal.h b/cpp/src/arrow/util/decimal.h index 916df503ef23..c73bae1b4c99 100644 --- a/cpp/src/arrow/util/decimal.h +++ b/cpp/src/arrow/util/decimal.h @@ -35,26 +35,26 @@ namespace decimal { using boost::multiprecision::int128_t; -template +template struct ARROW_EXPORT Decimal; ARROW_EXPORT void StringToInteger( - const std::string &whole, const std::string &fractional, int8_t sign, int32_t *out); + const std::string& whole, const std::string& fractional, int8_t sign, int32_t* out); ARROW_EXPORT void StringToInteger( - const std::string &whole, const std::string &fractional, int8_t sign, int64_t *out); + const std::string& whole, const std::string& fractional, int8_t sign, int64_t* out); ARROW_EXPORT void StringToInteger( - const std::string &whole, const std::string &fractional, int8_t sign, int128_t *out); + const std::string& whole, const std::string& fractional, int8_t sign, int128_t* out); -template -ARROW_EXPORT Status FromString(const std::string &s, Decimal *out, - int *precision = nullptr, int *scale = nullptr); +template +ARROW_EXPORT Status FromString(const std::string& s, Decimal* out, + int* precision = nullptr, int* scale = nullptr); -template +template struct ARROW_EXPORT Decimal { Decimal() : value() {} - explicit Decimal(const std::string &s) : value() { FromString(s, this); } - explicit Decimal(const char *s) : Decimal(std::string(s)) {} - explicit Decimal(const T &value) : value(value) {} + explicit Decimal(const std::string& s) : value() { FromString(s, this); } + explicit Decimal(const char* s) : Decimal(std::string(s)) {} + explicit Decimal(const T& value) : value(value) {} using value_type = T; value_type value; @@ -64,38 +64,38 @@ using Decimal32 = Decimal; using Decimal64 = Decimal; using Decimal128 = Decimal; -template +template struct ARROW_EXPORT DecimalPrecision {}; -template<> +template <> struct ARROW_EXPORT DecimalPrecision { constexpr static const int minimum = 1; constexpr static const int maximum = 9; }; -template<> +template <> struct ARROW_EXPORT DecimalPrecision { constexpr static const int minimum = 10; constexpr static const int maximum = 18; }; -template<> +template <> struct ARROW_EXPORT DecimalPrecision { constexpr static const int minimum = 19; constexpr static const int maximum = 38; }; -template +template ARROW_EXPORT std::string ToString( - const Decimal &decimal_value, int precision, int scale) { + const Decimal& decimal_value, int precision, int scale) { T value = decimal_value.value; // Decimal values are sent to clients as strings so in the interest of // speed the string will be created without the using stringstream with the // whole/fractional_part(). size_t last_char_idx = precision + (scale > 0) // Add a space for decimal place - + (scale == precision) // Add a space for leading 0 - + (value < 0); // Add a space for negative sign + + (scale == precision) // Add a space for leading 0 + + (value < 0); // Add a space for negative sign std::string str = std::string(last_char_idx, '0'); // Start filling in the values in reverse order by taking the last digit // of the value. Use a positive value and worry about the sign later. At this @@ -132,14 +132,14 @@ ARROW_EXPORT std::string ToString( } /// Conversion from raw bytes to a Decimal value -ARROW_EXPORT void FromBytes(const uint8_t *bytes, Decimal32 *value); -ARROW_EXPORT void FromBytes(const uint8_t *bytes, Decimal64 *value); -ARROW_EXPORT void FromBytes(const uint8_t *bytes, bool is_negative, Decimal128 *decimal); +ARROW_EXPORT void FromBytes(const uint8_t* bytes, Decimal32* value); +ARROW_EXPORT void FromBytes(const uint8_t* bytes, Decimal64* value); +ARROW_EXPORT void FromBytes(const uint8_t* bytes, bool is_negative, Decimal128* decimal); /// Conversion from a Decimal value to raw bytes -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); +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