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

Method MakeRandomArray

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

Source from the content-addressed store, hash-verified

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

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