MCPcopy Create free account
hub / github.com/apache/arrow / MakeRandomArray

Method MakeRandomArray

cpp/src/arrow/testing/random.cc:335–391  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

333 }
334
335 std::shared_ptr<Array> MakeRandomArray(int64_t size, double null_probability,
336 int64_t alignment, MemoryPool* memory_pool) {
337 // 10**19 fits in a 64-bit signed integer
338 static constexpr int32_t kMaxDigitsInInteger = 19;
339 static constexpr int kNumIntegers = DecimalType::kByteWidth / 8;
340
341 static_assert(
342 kNumIntegers == (DecimalType::kMaxPrecision + kMaxDigitsInInteger - 1) /
343 (kMaxDigitsInInteger + 1),
344 "inconsistent decimal metadata: kMaxPrecision doesn't match kByteWidth");
345
346 // First generate separate random values for individual components:
347 // boolean sign (including null-ness), and uint64 "digits" in big endian order.
348 const auto& decimal_type = checked_cast<const DecimalType&>(*type_);
349
350 const auto sign_array = checked_pointer_cast<BooleanArray>(rng_->Boolean(
351 size, /*true_probability=*/0.5, null_probability, alignment, memory_pool));
352 std::array<std::shared_ptr<UInt64Array>, kNumIntegers> digit_arrays;
353
354 auto remaining_digits = decimal_type.precision();
355 for (int i = kNumIntegers - 1; i >= 0; --i) {
356 const auto digits = std::min(kMaxDigitsInInteger, remaining_digits);
357 digit_arrays[i] = checked_pointer_cast<UInt64Array>(
358 rng_->UInt64(size, 0, MaxDecimalInteger(digits), /*null_probability=*/0,
359 alignment, memory_pool));
360 DCHECK_EQ(digit_arrays[i]->null_count(), 0);
361 remaining_digits -= digits;
362 }
363
364 // Second compute decimal values from the individual components,
365 // building up a decimal array.
366 DecimalBuilderType builder(type_, memory_pool, alignment);
367 ABORT_NOT_OK(builder.Reserve(size));
368
369 const DecimalValue kDigitsMultiplier =
370 DecimalValue::GetScaleMultiplier(kMaxDigitsInInteger);
371
372 for (int64_t i = 0; i < size; ++i) {
373 if (sign_array->IsValid(i)) {
374 DecimalValue dec_value{0};
375 for (int j = 0; j < kNumIntegers; ++j) {
376 dec_value =
377 dec_value * kDigitsMultiplier + DecimalValue(digit_arrays[j]->Value(i));
378 }
379 if (sign_array->Value(i)) {
380 builder.UnsafeAppend(dec_value.Negate());
381 } else {
382 builder.UnsafeAppend(dec_value);
383 }
384 } else {
385 builder.UnsafeAppendNull();
386 }
387 }
388 std::shared_ptr<Array> array;
389 ABORT_NOT_OK(builder.Finish(&array));
390 return array;
391 }
392};

Callers 4

Decimal32Method · 0.45
Decimal64Method · 0.45
Decimal128Method · 0.45
Decimal256Method · 0.45

Calls 12

GetScaleMultiplierFunction · 0.85
UInt64Method · 0.80
NegateMethod · 0.80
BooleanMethod · 0.45
precisionMethod · 0.45
null_countMethod · 0.45
ReserveMethod · 0.45
IsValidMethod · 0.45
ValueMethod · 0.45
UnsafeAppendMethod · 0.45
UnsafeAppendNullMethod · 0.45
FinishMethod · 0.45

Tested by

no test coverage detected