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

Function RandomListView

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

Source from the content-addressed store, hash-verified

855
856template <typename ArrayType, typename RAG>
857Result<std::shared_ptr<Array>> RandomListView(RAG& self, const Array& values,
858 int64_t length, double null_probability,
859 bool force_empty_nulls, double coverage,
860 int64_t alignment,
861 MemoryPool* memory_pool) {
862 using TypeClass = typename ArrayType::TypeClass;
863 using offset_type = typename TypeClass::offset_type;
864 using OffsetArrayType = typename TypeTraits<TypeClass>::OffsetArrayType;
865 using OffsetArrowType = typename OffsetArrayType::TypeClass;
866
867 DCHECK_LE(values.length(), std::numeric_limits<offset_type>::max());
868 DCHECK_LE(length, std::numeric_limits<offset_type>::max());
869
870 auto offsets_array = GenerateOffsets<NumericArray<OffsetArrowType>>(
871 self.seed(), length + 1, 0, static_cast<int32_t>(values.length()), null_probability,
872 force_empty_nulls, alignment, memory_pool);
873 auto* offsets = offsets_array->data()->template GetValues<offset_type>(1);
874
875 // The buffers for the sizes array
876 BufferVector buffers{2};
877 buffers[0] = NULLPTR;
878 buffers[1] = *AllocateBuffer(sizeof(offset_type) * length, alignment, memory_pool);
879 auto sizes = buffers[1]->mutable_data_as<offset_type>();
880
881 // Derive sizes from offsets taking coverage into account
882 pcg32 rng(self.seed());
883 using NormalDist = std::normal_distribution<double>;
884 NormalDist size_dist;
885 for (int64_t i = 0; i < length; ++i) {
886 const double mean_size = coverage * (offsets[i + 1] - offsets[i]);
887 const double sampled_size =
888 std::max(0.0, size_dist(rng, NormalDist::param_type{mean_size}));
889 // This creates a higher probability of offset[i] + size[i] being closer or equal to
890 // values.length(), but that skew is acceptable for the purposes of testing.
891 const auto size = std::min(static_cast<offset_type>(std::llround(sampled_size)),
892 static_cast<offset_type>(values.length() - offsets[i]));
893 sizes[i] = offsets_array->IsNull(i) && force_empty_nulls ? 0 : size;
894 }
895
896 auto sizes_array_data = ArrayData::Make(TypeTraits<OffsetArrowType>::type_singleton(),
897 length, std::move(buffers), /*null_count=*/0);
898 auto sizes_array = std::make_shared<OffsetArrayType>(std::move(sizes_array_data));
899
900 ARROW_ASSIGN_OR_RAISE(
901 auto list_view_array,
902 ArrayType::FromArrays(*offsets_array, *sizes_array, values, memory_pool));
903 ShuffleListViewDataInPlace<TypeClass>(
904 self.seed(), const_cast<ArrayData*>(list_view_array->data().get()));
905 return list_view_array;
906}
907
908} // namespace
909

Callers

nothing calls this directly

Calls 7

AllocateBufferFunction · 0.50
MakeFunction · 0.50
type_singletonFunction · 0.50
lengthMethod · 0.45
dataMethod · 0.45
IsNullMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected