Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Checkpoint [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Nov 19, 2017
1 parent 028fb03 commit 3d243d5
Showing 1 changed file with 64 additions and 7 deletions.
71 changes: 64 additions & 7 deletions src/parquet/arrow/test-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,67 @@ NonNullArray(size_t size, std::shared_ptr<Array>* out) {
return builder.Finish(out);
}

static void DecimalRange(int32_t precision, ::arrow::Decimal128* min_decimal,
::arrow::Decimal128* max_decimal) {
DCHECK_GE(precision, 1) << "decimal precision must be greater than or equal to 1, got "
<< precision;
DCHECK_LE(precision, 38) << "decimal precision must be less than or equal to 38, got "
<< precision;
DCHECK_NE(min_decimal, NULLPTR);
DCHECK_NE(max_decimal, NULLPTR);

*max_decimal = 1;
for (int32_t i = 0; i < precision; ++i) {
*max_decimal *= 10;
}
*max_decimal -= 1;
*min_decimal = -(*max_decimal);
}

class UniformDecimalDistribution {
public:
explicit UniformDecimalDistribution(int32_t precision) {
::arrow::Decimal128 min_decimal;
::arrow::Decimal128 max_decimal;

DecimalRange(precision, &min_decimal, &max_decimal);

const auto min_lower = static_cast<int64_t>(min_decimal.low_bits());
const auto max_lower = static_cast<int64_t>(max_decimal.low_bits());

lower_bits_ = std::uniform_int_distribution<int64_t>(min_lower, max_lower);
upper_bits_ = std::uniform_int_distribution<int64_t>(min_decimal.high_bits(),
max_decimal.high_bits());
}

template <typename Generator>
::arrow::Decimal128 operator()(Generator& gen) {
return ::arrow::Decimal128(upper_bits_(gen), static_cast<uint64_t>(lower_bits_(gen)));
}

private:
std::uniform_int_distribution<int64_t> lower_bits_;
std::uniform_int_distribution<int64_t> upper_bits_;
};

template <typename ArrowType>
typename std::enable_if<is_arrow_decimal<ArrowType>::value, Status>::type NonNullArray(
size_t size, std::shared_ptr<Array>* out) {
using BuilderType = typename ::arrow::TypeTraits<ArrowType>::BuilderType;
constexpr int32_t kDecimalPrecision = 4;
constexpr int32_t kDecimalScale = 2;

// todo: find a way to generate test data with more diversity.
BuilderType builder(::arrow::decimal(24, 7));
const auto type = ::arrow::decimal(kDecimalPrecision, kDecimalScale);

using BuilderType = typename ::arrow::TypeTraits<ArrowType>::BuilderType;
BuilderType builder(type);

constexpr int32_t seed = 0;
std::mt19937 gen(seed);
UniformDecimalDistribution decimal_dist(kDecimalPrecision);

for (size_t i = 0; i < size; i++) {
// XXX: Decimal128 value(-45047LL, 18388229154599321957ULL)
::arrow::Decimal128 value("-83095209205923957.2323995");
const ::arrow::Decimal128 value(decimal_dist(gen));
RETURN_NOT_OK(builder.Append(value));
}
return builder.Finish(out);
Expand Down Expand Up @@ -274,15 +325,21 @@ typename std::enable_if<is_arrow_decimal<ArrowType>::value, Status>::type Nullab
valid_bytes[i * 2] = 0;
}

constexpr int32_t kDecimalPrecision = 24;
constexpr int32_t kDecimalScale = 7;
const auto type = ::arrow::decimal(kDecimalPrecision, kDecimalScale);

using BuilderType = typename ::arrow::TypeTraits<ArrowType>::BuilderType;
BuilderType builder(::arrow::decimal(24, 7));
BuilderType builder(type);

std::mt19937 gen(seed);
UniformDecimalDistribution decimal_dist(kDecimalPrecision);

for (size_t i = 0; i < size; i++) {
if (!valid_bytes[i]) {
RETURN_NOT_OK(builder.AppendNull());
} else {
::arrow::Decimal128 value("-83095209205923957.2323995");
RETURN_NOT_OK(builder.Append(value));
RETURN_NOT_OK(builder.Append(decimal_dist(gen)));
}
}
return builder.Finish(out);
Expand Down

0 comments on commit 3d243d5

Please sign in to comment.