Skip to content

Commit

Permalink
Get rid of declaring API via '#define'
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 f2b25ce commit 6d13ab0
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 87 deletions.
6 changes: 3 additions & 3 deletions cpp/src/arrow/array/array_decimal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ using internal::checked_cast;
template<uint32_t width>
BaseDecimalArray<width>::BaseDecimalArray(const std::shared_ptr<ArrayData>& data)
: FixedSizeBinaryArray(data) {
ARROW_CHECK_EQ(data->type->id(), DecimalArrayHelper<width>::id);
ARROW_CHECK_EQ(data->type->id(), DecimalTypeTraits<width>::Id);
}

template<uint32_t width>
std::string BaseDecimalArray<width>::FormatValue(int64_t i) const {
const auto& type_ = checked_cast<const typename DecimalArrayHelper<width>::type&>(*type());
const typename DecimalArrayHelper<width>::value_type value(GetValue(i));
const auto& type_ = checked_cast<const typename DecimalTypeTraits<width>::TypeClass&>(*type());
const typename DecimalTypeTraits<width>::ValueType value(GetValue(i));
return value.ToString(type_.scale());
}

Expand Down
34 changes: 9 additions & 25 deletions cpp/src/arrow/array/array_decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,18 @@
#include <string>

#include "arrow/array/array_binary.h"
#include "arrow/util/decimal_type_traits.h"
#include "arrow/array/data.h"
#include "arrow/type.h"
#include "arrow/util/visibility.h"

namespace arrow {


template<uint32_t width>
struct DecimalArrayHelper;

#define DECL_DECIMAL_TYPES_HELPER(width) \
template<> \
struct DecimalArrayHelper<width> { \
static constexpr Type::type id = Type::DECIMAL##width; \
using type = Decimal##width##Type; \
using value_type = Decimal##width; \
};

DECL_DECIMAL_TYPES_HELPER(128)
DECL_DECIMAL_TYPES_HELPER(256)

#undef DECL_DECIMAL_TYPES_HELPER

/// Template Array class for decimal data
template<uint32_t width>
class BaseDecimalArray : public FixedSizeBinaryArray {
public:
using TypeClass = typename DecimalArrayHelper<width>::type;
using TypeClass = typename DecimalTypeTraits<width>::TypeClass;

using FixedSizeBinaryArray::FixedSizeBinaryArray;

Expand All @@ -59,15 +43,15 @@ class BaseDecimalArray : public FixedSizeBinaryArray {
std::string FormatValue(int64_t i) const;
};

#define DECIMAL_ARRAY_DECL(width) \
class ARROW_EXPORT Decimal##width##Array : public BaseDecimalArray<width> { \
using BaseDecimalArray<width>::BaseDecimalArray; \
/// Array class for decimal 128-bit data
class ARROW_EXPORT Decimal128Array : public BaseDecimalArray<128> {
using BaseDecimalArray<128>::BaseDecimalArray;
};

DECIMAL_ARRAY_DECL(128)
DECIMAL_ARRAY_DECL(256)

#undef DECIMAL_ARRAY_DECL
/// Array class for decimal 256-bit data
class ARROW_EXPORT Decimal256Array : public BaseDecimalArray<256> {
using BaseDecimalArray<256>::BaseDecimalArray;
};

// Backward compatibility
using DecimalArray = Decimal128Array;
Expand Down
6 changes: 3 additions & 3 deletions cpp/src/arrow/array/builder_decimal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ template<uint32_t width>
BaseDecimalBuilder<width>::BaseDecimalBuilder(const std::shared_ptr<DataType>& type,
MemoryPool* pool)
: FixedSizeBinaryBuilder(type, pool),
decimal_type_(internal::checked_pointer_cast<typename DecimalBuilderHelper<width>::type>(type)) {}
decimal_type_(internal::checked_pointer_cast<typename DecimalTypeTraits<width>::TypeClass>(type)) {}

template<uint32_t width>
Status BaseDecimalBuilder<width>::Append(typename DecimalBuilderHelper<width>::value_type value) {
Status BaseDecimalBuilder<width>::Append(typename DecimalTypeTraits<width>::ValueType value) {
RETURN_NOT_OK(FixedSizeBinaryBuilder::Reserve(1));
UnsafeAppend(value);
return Status::OK();
}

template<uint32_t width>
void BaseDecimalBuilder<width>::UnsafeAppend(typename DecimalBuilderHelper<width>::value_type value) {
void BaseDecimalBuilder<width>::UnsafeAppend(typename DecimalTypeTraits<width>::ValueType value) {
value.ToBytes(GetMutableValue(length()));
byte_builder_.UnsafeAdvance((width >> 3));
UnsafeAppendToBitmap(true);
Expand Down
42 changes: 14 additions & 28 deletions cpp/src/arrow/array/builder_decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,17 @@
#include "arrow/array/builder_base.h"
#include "arrow/array/builder_binary.h"
#include "arrow/array/data.h"
#include "arrow/util/decimal_type_traits.h"
#include "arrow/status.h"
#include "arrow/type.h"
#include "arrow/util/visibility.h"

namespace arrow {

template<uint32_t width>
struct DecimalBuilderHelper;

#define DECL_DECIMAL_TYPES_HELPER(width) \
template<> \
struct DecimalBuilderHelper<width> { \
using type = Decimal##width##Type; \
using array_type = Decimal##width##Array; \
using value_type = Decimal##width; \
};

DECL_DECIMAL_TYPES_HELPER(128)
DECL_DECIMAL_TYPES_HELPER(256)

#undef DECL_DECIMAL_TYPES_HELPER

template<uint32_t width>
class BaseDecimalBuilder : public FixedSizeBinaryBuilder {
public:
using TypeClass = typename DecimalBuilderHelper<width>::type;
using TypeClass = typename DecimalTypeTraits<width>::TypeClass;

explicit BaseDecimalBuilder(const std::shared_ptr<DataType>& type,
MemoryPool* pool = default_memory_pool());
Expand All @@ -57,8 +42,8 @@ class BaseDecimalBuilder : public FixedSizeBinaryBuilder {
using FixedSizeBinaryBuilder::AppendValues;
using FixedSizeBinaryBuilder::Reset;

Status Append(typename DecimalBuilderHelper<width>::value_type val);
void UnsafeAppend(typename DecimalBuilderHelper<width>::value_type val);
Status Append(typename DecimalTypeTraits<width>::ValueType val);
void UnsafeAppend(typename DecimalTypeTraits<width>::ValueType val);
void UnsafeAppend(util::string_view val);

Status FinishInternal(std::shared_ptr<ArrayData>* out) override;
Expand All @@ -67,24 +52,25 @@ class BaseDecimalBuilder : public FixedSizeBinaryBuilder {
using ArrayBuilder::Finish;
/// \endcond

Status Finish(std::shared_ptr<typename DecimalBuilderHelper<width>::array_type>* out) { return FinishTyped(out); }
Status Finish(std::shared_ptr<typename DecimalTypeTraits<width>::ArrayType>* out) { return FinishTyped(out); }

std::shared_ptr<DataType> type() const override { return decimal_type_; }

protected:
std::shared_ptr<typename DecimalBuilderHelper<width>::type> decimal_type_;
std::shared_ptr<typename DecimalTypeTraits<width>::TypeClass> decimal_type_;
};

#define DECIMAL_BUILDER_DECL(width) \
class ARROW_EXPORT Decimal##width##Builder : public BaseDecimalBuilder<width> { \
using BaseDecimalBuilder<width>::BaseDecimalBuilder; \
/// Builder class for decimal 128-bit
class ARROW_EXPORT Decimal128Builder : public BaseDecimalBuilder<128> {
using BaseDecimalBuilder<128>::BaseDecimalBuilder;
};

DECIMAL_BUILDER_DECL(128)
DECIMAL_BUILDER_DECL(256)

#undef DECIMAL_BUILDER_DECL
/// Builder class for decimal 128-bit
class ARROW_EXPORT Decimal256Builder : public BaseDecimalBuilder<256> {
using BaseDecimalBuilder<256>::BaseDecimalBuilder;
};

// Backward compatibility
using DecimalBuilder = Decimal128Builder;

} // namespace arrow
20 changes: 3 additions & 17 deletions cpp/src/arrow/type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "arrow/util/make_unique.h"
#include "arrow/util/range.h"
#include "arrow/util/vector.h"
#include "arrow/util/decimal_type_traits.h"
#include "arrow/visitor_inline.h"

namespace arrow {
Expand Down Expand Up @@ -750,25 +751,10 @@ std::vector<std::shared_ptr<Field>> StructType::GetAllFieldsByName(
// ----------------------------------------------------------------------
// Decimal type

template<uint32_t width>
struct DecimalTypeHelper;

#define DECIMAL_TYPE_HELPER_DECL(width) \
template<> \
struct DecimalTypeHelper<width> { \
static constexpr Type::type id = Type::DECIMAL##width; \
using type = Decimal##width##Type; \
};

DECIMAL_TYPE_HELPER_DECL(128)
DECIMAL_TYPE_HELPER_DECL(256)

#undef DECIMAL_TYPE_HELPER_DECL


template<uint32_t width>
BaseDecimalType<width>::BaseDecimalType(int32_t precision, int32_t scale)
: DecimalType(DecimalTypeHelper<width>::id, (width >> 3), precision, scale) {
: DecimalType(DecimalTypeTraits<width>::Id, (width >> 3), precision, scale) {
ARROW_CHECK_GE(precision, kMinPrecision);
ARROW_CHECK_LE(precision, kMaxPrecision);
}
Expand All @@ -778,7 +764,7 @@ Result<std::shared_ptr<DataType>> BaseDecimalType<width>::Make(int32_t precision
if (precision < kMinPrecision || precision > kMaxPrecision) {
return Status::Invalid("Decimal precision out of range: ", precision);
}
return std::make_shared<typename DecimalTypeHelper<width>::type>(precision, scale);
return std::make_shared<typename DecimalTypeTraits<width>::TypeClass>(precision, scale);
}

// ----------------------------------------------------------------------
Expand Down
20 changes: 11 additions & 9 deletions cpp/src/arrow/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,6 @@ class ARROW_EXPORT DecimalType : public FixedSizeBinaryType {
int32_t scale_;
};


/// \brief Template type class for decimal data
template<uint32_t width>
class BaseDecimalType : public DecimalType {
Expand All @@ -896,16 +895,19 @@ class BaseDecimalType : public DecimalType {
static constexpr int32_t kMaxPrecision = DecimalMeta<width>::max_precision;
};

#define DECIMAL_TYPE_DECL(width) \
class ARROW_EXPORT Decimal##width##Type : public BaseDecimalType<width> { \
public: static constexpr Type::type type_id = Type::DECIMAL##width; \
using BaseDecimalType<width>::BaseDecimalType; \
/// \brief Concrete type class for decimal 128-bit data
class ARROW_EXPORT Decimal128Type : public BaseDecimalType<128> {
public:
static constexpr Type::type type_id = Type::DECIMAL128;
using BaseDecimalType<128>::BaseDecimalType;
};

DECIMAL_TYPE_DECL(128)
DECIMAL_TYPE_DECL(256)

#undef DECIMAL_TYPE_DECL
/// \brief Concrete type class for decimal 256-bit data
class ARROW_EXPORT Decimal256Type : public BaseDecimalType<256> {
public:
static constexpr Type::type type_id = Type::DECIMAL256;
using BaseDecimalType<256>::BaseDecimalType;
};

/// \brief Concrete type class for union data
class ARROW_EXPORT UnionType : public NestedType {
Expand Down
2 changes: 0 additions & 2 deletions cpp/src/arrow/util/basic_decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <string>
#include <type_traits>

#include "arrow/util/decimal_meta.h"
#include "arrow/util/macros.h"
#include "arrow/util/type_traits.h"
#include "arrow/util/visibility.h"
Expand All @@ -37,7 +36,6 @@ enum class DecimalStatus {
kRescaleDataLoss,
};


/// Represents a signed 128-bit integer in two's complement.
///
/// This class is also compiled into LLVM IR - so, it should not have cpp references like
Expand Down
4 changes: 4 additions & 0 deletions cpp/src/arrow/util/decimal_meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#pragma once

namespace arrow {

template<uint32_t width>
struct DecimalMeta;

Expand All @@ -31,3 +33,5 @@ struct DecimalMeta<256> {
static constexpr const char* name = "decimal256";
static constexpr int32_t max_precision = 76;
};

} // namespace arrow
41 changes: 41 additions & 0 deletions cpp/src/arrow/util/decimal_type_traits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

#include "arrow/type_fwd.h"

namespace arrow {

template<uint32_t width>
struct DecimalTypeTraits;

#define DECIMAL_TYPE_TRAITS_DECL(width) \
template<> \
struct DecimalTypeTraits<width> { \
static constexpr Type::type Id = Type::DECIMAL##width; \
using ArrayType = Decimal##width##Array; \
using BuilderType = Decimal##width##Builder; \
using ScalarType = Decimal##width##Scalar; \
using TypeClass = Decimal##width##Type; \
using ValueType = Decimal##width; \
};

DECIMAL_TYPE_TRAITS_DECL(128)
DECIMAL_TYPE_TRAITS_DECL(256)

} // namespace arrow

0 comments on commit 6d13ab0

Please sign in to comment.