Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions cpp/src/arrow/array/array_list_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,43 @@ class TestListConversions : public ::testing::Test {
*checked_pointer_cast<SrcListViewArrayClass>(list_view_wo_nulls), pool_));
ASSERT_OK(result->ValidateFull());
AssertArraysEqual(*expected_list_wo_nulls, *result, /*verbose=*/true);

// The views are contiguous, so the values are kept as they are (GH-51612).
ASSERT_EQ(result->values()->data()->buffers[1].get(),
checked_pointer_cast<SrcListViewArrayClass>(list_view_wo_nulls)
->values()
->data()
->buffers[1]
.get());
}

template <typename DestListType, typename SrcListViewType>
void DoTestListFromSlicedListView() {
using SrcListViewArrayClass = typename TypeTraits<SrcListViewType>::ArrayType;
using DestListArrayClass = typename TypeTraits<DestListType>::ArrayType;
auto list_view_type = std::make_shared<SrcListViewType>(int32());
auto list_type = std::make_shared<DestListType>(int32());

auto list_view =
ArrayFromJSON(list_view_type, "[[1, 2], [3], null, [], [4], [5, 6], null, [7]]");

auto check = [&](const Array& sliced, const std::shared_ptr<Array>& expected) {
ASSERT_OK_AND_ASSIGN(
auto result, DestListArrayClass::FromListView(
checked_cast<const SrcListViewArrayClass&>(sliced), pool_));
ASSERT_OK(result->ValidateFull());
AssertArraysEqual(*expected, *result, /*verbose=*/true);
};

// A slice whose element offset is not a multiple of 8, so the bitmap's bit
// offset and byte offset differ (GH-51613).
check(*list_view->Slice(1, 6),
ArrayFromJSON(list_type, "[[3], null, [], [4], [5, 6], null]"));
// A slice starting on a byte boundary, so that the bit offset is zero.
check(*list_view->Slice(2, 4), ArrayFromJSON(list_type, "[null, [], [4], [5, 6]]"));
// A slice whose only null is the last slot.
check(*list_view->Slice(0, 8),
ArrayFromJSON(list_type, "[[1, 2], [3], null, [], [4], [5, 6], null, [7]]"));
}
};

Expand All @@ -1061,6 +1098,11 @@ TEST_F(TestListConversions, ListFromListView) {
this->DoTestListFromListView<LargeListType, LargeListViewType>();
}

TEST_F(TestListConversions, ListFromSlicedListView) {
this->DoTestListFromSlicedListView<ListType, ListViewType>();
this->DoTestListFromSlicedListView<LargeListType, LargeListViewType>();
}

// ----------------------------------------------------------------------
// Map tests

Expand Down
63 changes: 33 additions & 30 deletions cpp/src/arrow/array/array_nested.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include "arrow/util/bitmap_ops.h"
#include "arrow/util/checked_cast.h"
#include "arrow/util/int_util_overflow.h"
#include "arrow/util/list_util.h"
#include "arrow/util/logging_internal.h"
#include "arrow/util/unreachable.h"

Expand Down Expand Up @@ -279,9 +278,11 @@ Result<std::shared_ptr<Array>> FlattenListViewArray(const ListViewArrayT& list_v
}

// If the list array is *all* nulls, then just return an empty array.
// Slicing (rather than MakeEmptyArray) also works for value types that have
// no builder, such as extension types nested in a struct.
if constexpr (HasNulls) {
if (list_view_array.null_count() == list_view_array.length()) {
return MakeEmptyArray(value_array->type(), memory_pool);
return SliceArrayWithOffsets(*value_array, 0, 0);
}
}

Expand All @@ -305,7 +306,7 @@ Result<std::shared_ptr<Array>> FlattenListViewArray(const ListViewArrayT& list_v
}
// If all list-views are empty, return an empty array.
if (first_i == list_view_array_length) {
return MakeEmptyArray(value_array->type(), memory_pool);
return SliceArrayWithOffsets(*value_array, 0, 0);
}

std::vector<std::shared_ptr<Array>> slices;
Expand Down Expand Up @@ -416,36 +417,38 @@ Result<std::shared_ptr<ArrayData>> ListFromListViewImpl(
typename DestListType::offset_type>::value,
"Offset types between list type and list-view type are expected to match");
using offset_type = typename DestListType::offset_type;
using ListBuilderType = typename TypeTraits<DestListType>::BuilderType;
using ListViewArrayType = typename TypeTraits<SrcListViewType>::ArrayType;

const ListViewArrayType list_view_array(list_view_data);
const int64_t length = list_view_array.length();

// The views may be out of order, overlap or leave gaps, so the values they
// reference are packed in list order. Contiguous views are sliced, not copied.
ARROW_ASSIGN_OR_RAISE(auto values, list_view_array.Flatten(pool));

ARROW_ASSIGN_OR_RAISE(auto offsets_buffer,
AllocateBuffer(sizeof(offset_type) * (length + 1), pool));
auto* offsets = offsets_buffer->mutable_data_as<offset_type>();
offset_type offset = 0;
for (int64_t i = 0; i < length; ++i) {
offsets[i] = offset;
if (list_view_array.IsValid(i)) {
offset += list_view_array.value_length(i);
}
}
offsets[length] = offset;

std::shared_ptr<Buffer> null_bitmap = list_view_data->buffers[0];
if (null_bitmap && list_view_data->offset != 0) {
ARROW_ASSIGN_OR_RAISE(null_bitmap, CopyBitmap(pool, null_bitmap->data(),
list_view_data->offset, length));
}

const auto& list_view_type =
checked_cast<const SrcListViewType&>(*list_view_data->type);
const auto& value_type = list_view_type.value_type();
const auto list_type = std::make_shared<DestListType>(value_type);

ARROW_ASSIGN_OR_RAISE(auto sum_of_list_view_sizes,
list_util::internal::SumOfLogicalListSizes(*list_view_data));
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<ArrayBuilder> value_builder,
MakeBuilder(value_type, pool));
RETURN_NOT_OK(value_builder->Reserve(sum_of_list_view_sizes));
auto list_builder = std::make_shared<ListBuilderType>(pool, value_builder, list_type);
RETURN_NOT_OK(list_builder->Reserve(list_view_data->length));

ArraySpan values{*list_view_data->child_data[0]};
const auto* in_validity_bitmap = list_view_data->GetValues<uint8_t>(0);
const auto* in_offsets = list_view_data->GetValues<offset_type>(1);
const auto* in_sizes = list_view_data->GetValues<offset_type>(2);
for (int64_t i = 0; i < list_view_data->length; ++i) {
const bool is_valid =
!in_validity_bitmap ||
bit_util::GetBit(in_validity_bitmap, list_view_data->offset + i);
const int64_t size = is_valid ? in_sizes[i] : 0;
RETURN_NOT_OK(list_builder->Append(is_valid, size));
RETURN_NOT_OK(value_builder->AppendArraySlice(values, in_offsets[i], size));
}
std::shared_ptr<ArrayData> list_array_data;
RETURN_NOT_OK(list_builder->FinishInternal(&list_array_data));
return list_array_data;
return ArrayData::Make(std::make_shared<DestListType>(list_view_type.value_type()),
length, {std::move(null_bitmap), std::move(offsets_buffer)},
{values->data()}, list_view_array.null_count());
}

} // namespace
Expand Down
50 changes: 46 additions & 4 deletions cpp/src/arrow/compute/kernels/scalar_cast_nested.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,44 @@ struct CastList {
return Status::OK();
}

/// \brief Pack the values referenced by list-views in sequence
///
/// The views may be out of order, overlap or leave gaps between them, so
/// their offsets cannot be reused as list offsets (GH-51612). The values
/// referenced by the valid views are packed in sequence instead (contiguous
/// views are sliced rather than copied), and the list offsets are computed
/// from the views' sizes.
static Status HandleListViewOffsets(KernelContext* ctx, const ArraySpan& in_array,
ArrayData* out_array,
std::shared_ptr<ArrayData>* values) {
using ListViewArrayType = typename TypeTraits<SrcType>::ArrayType;
const ListViewArrayType list_view_array(in_array.ToArrayData());

ARROW_ASSIGN_OR_RAISE(auto packed_values,
list_view_array.Flatten(ctx->memory_pool()));
if constexpr (is_downcast) {
if (packed_values->length() > std::numeric_limits<dest_offset_type>::max()) {
return Status::Invalid("Array of type ", in_array.type->ToString(),
" too large to convert to ", out_array->type->ToString());
}
}

ARROW_ASSIGN_OR_RAISE(out_array->buffers[1], ctx->Allocate(sizeof(dest_offset_type) *
(in_array.length + 1)));
auto* out_offsets = out_array->GetMutableValues<dest_offset_type>(1);
dest_offset_type offset = 0;
for (int64_t i = 0; i < in_array.length; ++i) {
out_offsets[i] = offset;
if (list_view_array.IsValid(i)) {
offset += static_cast<dest_offset_type>(list_view_array.value_length(i));
}
}
out_offsets[in_array.length] = offset;

*values = packed_values->data();
return Status::OK();
}

static Status Exec(KernelContext* ctx, const ExecSpan& batch, ExecResult* out) {
const CastOptions& options = CastState::Get(ctx);

Expand All @@ -115,11 +153,15 @@ struct CastList {
ArrayData* out_array = out->array_data().get();
ARROW_ASSIGN_OR_RAISE(out_array->buffers[0],
GetOrCopyNullBitmapBuffer(in_array, ctx->memory_pool()));
out_array->buffers[1] = in_array.GetBuffer(1);

std::shared_ptr<ArrayData> values = in_array.child_data[0].ToArrayData();

RETURN_NOT_OK(HandleOffsets(ctx, in_array, out_array, &values));
std::shared_ptr<ArrayData> values;
if constexpr (is_list_view(SrcType::type_id)) {
RETURN_NOT_OK(HandleListViewOffsets(ctx, in_array, out_array, &values));
} else {
out_array->buffers[1] = in_array.GetBuffer(1);
values = in_array.child_data[0].ToArrayData();
RETURN_NOT_OK(HandleOffsets(ctx, in_array, out_array, &values));
}

// Handle values
ARROW_ASSIGN_OR_RAISE(Datum cast_values,
Expand Down
Loading