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

Function OffsetsFromLengthsArray

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

Source from the content-addressed store, hash-verified

655
656template <typename OffsetArrayType>
657std::shared_ptr<Array> OffsetsFromLengthsArray(OffsetArrayType* lengths,
658 bool force_empty_nulls, int64_t alignment,
659 MemoryPool* memory_pool) {
660 // Need N + 1 offsets for N items
661 int64_t size = lengths->length() + 1;
662 BufferVector buffers{2};
663
664 int64_t null_count = 0;
665
666 buffers[0] = *AllocateEmptyBitmap(size, alignment, memory_pool);
667 uint8_t* null_bitmap = buffers[0]->mutable_data();
668 // Make sure the first and last entry are non-null
669 arrow::bit_util::SetBit(null_bitmap, 0);
670 arrow::bit_util::SetBit(null_bitmap, size - 1);
671
672 buffers[1] = *AllocateBuffer(sizeof(typename OffsetArrayType::value_type) * size,
673 alignment, memory_pool);
674 auto data =
675 reinterpret_cast<typename OffsetArrayType::value_type*>(buffers[1]->mutable_data());
676 data[0] = 0;
677 int index = 1;
678 for (const auto& length : *lengths) {
679 if (length.has_value()) {
680 arrow::bit_util::SetBit(null_bitmap, index);
681 data[index] = data[index - 1] + *length;
682 DCHECK_GE(*length, 0);
683 } else if (index == size - 1) {
684 // Last list offset is non-null (see above)
685 data[index] = data[index - 1];
686 } else {
687 data[index] = data[index - 1];
688 null_count++;
689 }
690 index++;
691 }
692
693 if (force_empty_nulls) {
694 arrow::internal::BitmapReader reader(null_bitmap, 0, size - 1);
695 for (int64_t i = 0; i < size - 1; ++i) {
696 if (reader.IsNotSet()) {
697 // Ensure a null entry corresponds to a 0-sized list extent
698 // (note this can be neither the first nor the last list entry, see above)
699 data[i + 1] = data[i];
700 }
701 reader.Next();
702 }
703 }
704
705 auto array_data = ArrayData::Make(
706 std::make_shared<typename OffsetArrayType::TypeClass>(), size, buffers, null_count);
707 return std::make_shared<OffsetArrayType>(array_data);
708}
709
710// Helper for RandomArrayGenerator::ArrayOf: extract some C value from
711// a given metadata key.

Callers

nothing calls this directly

Calls 8

AllocateEmptyBitmapFunction · 0.85
SetBitFunction · 0.85
AllocateBufferFunction · 0.50
MakeFunction · 0.50
lengthMethod · 0.45
mutable_dataMethod · 0.45
IsNotSetMethod · 0.45
NextMethod · 0.45

Tested by

no test coverage detected