Skip to content

Commit

Permalink
Get rid of code duplication at DecimalScalar
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Chigarev <[email protected]>
  • Loading branch information
dchigarev committed Oct 12, 2020
1 parent 6d13ab0 commit 17b0e31
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
25 changes: 12 additions & 13 deletions cpp/src/arrow/scalar.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "arrow/type_traits.h"
#include "arrow/util/compare.h"
#include "arrow/util/decimal.h"
#include "arrow/util/decimal_type_traits.h"
#include "arrow/util/string_view.h"
#include "arrow/util/visibility.h"

Expand Down Expand Up @@ -336,26 +337,24 @@ struct ARROW_EXPORT DurationScalar : public TemporalScalar<DurationType> {
using TemporalScalar<DurationType>::TemporalScalar;
};

struct ARROW_EXPORT Decimal128Scalar : public Scalar {
template<uint32_t width>
struct BaseDecimalScalar : public Scalar {
using Scalar::Scalar;
using TypeClass = Decimal128Type;
using ValueType = Decimal128;
using TypeClass = typename DecimalTypeTraits<width>::TypeClass;
using ValueType = typename DecimalTypeTraits<width>::ValueType;

Decimal128Scalar(Decimal128 value, std::shared_ptr<DataType> type)
BaseDecimalScalar(ValueType value, std::shared_ptr<DataType> type)
: Scalar(std::move(type), true), value(value) {}

Decimal128 value;
ValueType value;
};

struct ARROW_EXPORT Decimal256Scalar : public Scalar {
using Scalar::Scalar;
using TypeClass = Decimal256Type;
using ValueType = Decimal256;

Decimal256Scalar(Decimal256 value, std::shared_ptr<DataType> type)
: Scalar(std::move(type), true), value(value) {}
struct ARROW_EXPORT Decimal128Scalar : public BaseDecimalScalar<128> {
using BaseDecimalScalar<128>::BaseDecimalScalar;
};

Decimal256 value;
struct ARROW_EXPORT Decimal256Scalar : public BaseDecimalScalar<256> {
using BaseDecimalScalar<256>::BaseDecimalScalar;
};

struct ARROW_EXPORT BaseListScalar : public Scalar {
Expand Down
12 changes: 5 additions & 7 deletions cpp/src/arrow/type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2149,15 +2149,13 @@ std::shared_ptr<DataType> decimal(int32_t precision, int32_t scale) {
: decimal256(precision, scale);
}

#define DECIMAL_BUILDER_DECL(width) \
std::shared_ptr<DataType> decimal##width(int32_t precision, int32_t scale) { \
return std::make_shared<Decimal##width##Type>(precision, scale); \
std::shared_ptr<DataType> decimal128(int32_t precision, int32_t scale) {
return std::make_shared<Decimal128Type>(precision, scale);
}

DECIMAL_BUILDER_DECL(128)
DECIMAL_BUILDER_DECL(256)

#undef DECIMAL_BUILDER_DECL
std::shared_ptr<DataType> decimal256(int32_t precision, int32_t scale) {
return std::make_shared<Decimal256Type>(precision, scale);
}

template<uint32_t width>
std::string BaseDecimalType<width>::ToString() const {
Expand Down

0 comments on commit 17b0e31

Please sign in to comment.