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

Function OffsetsFromLengthsArray

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

Source from the content-addressed store, hash-verified

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