Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-38770: [C++][Python] RecordBatch.filter() segfaults if passed a ChunkedArray #40971

Merged
merged 4 commits into from
May 8, 2024
Merged
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
26 changes: 20 additions & 6 deletions cpp/src/arrow/compute/kernels/vector_selection_filter_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <type_traits>
#include <vector>

#include "arrow/array/concatenate.h"
#include "arrow/array/data.h"
#include "arrow/buffer_builder.h"
#include "arrow/chunked_array.h"
Expand Down Expand Up @@ -924,12 +925,26 @@ Result<std::shared_ptr<RecordBatch>> FilterRecordBatch(const RecordBatch& batch,
return Status::Invalid("Filter inputs must all be the same length");
}

// Convert filter to selection vector/indices and use Take
// Fetch filter
const auto& filter_opts = *static_cast<const FilterOptions*>(options);
ARROW_ASSIGN_OR_RAISE(
std::shared_ptr<ArrayData> indices,
GetTakeIndices(*filter.array(), filter_opts.null_selection_behavior,
ctx->memory_pool()));
ArrayData filter_array;
switch (filter.kind()) {
case Datum::ARRAY:
filter_array = *filter.array();
break;
case Datum::CHUNKED_ARRAY: {
ARROW_ASSIGN_OR_RAISE(auto combined, Concatenate(filter.chunked_array()->chunks()));
filter_array = *combined->data();
break;
}
default:
return Status::TypeError("Filter should be array-like");
}
AlenkaF marked this conversation as resolved.
Show resolved Hide resolved

// Convert filter to selection vector/indices and use Take
ARROW_ASSIGN_OR_RAISE(std::shared_ptr<ArrayData> indices,
GetTakeIndices(filter_array, filter_opts.null_selection_behavior,
ctx->memory_pool()));
std::vector<std::shared_ptr<Array>> columns(batch.num_columns());
for (int i = 0; i < batch.num_columns(); ++i) {
ARROW_ASSIGN_OR_RAISE(Datum out, Take(batch.column(i)->data(), Datum(indices),
Expand Down Expand Up @@ -1038,7 +1053,6 @@ class FilterMetaFunction : public MetaFunction {
}

if (args[0].kind() == Datum::RECORD_BATCH) {
auto values_batch = args[0].record_batch();
ARROW_ASSIGN_OR_RAISE(
std::shared_ptr<RecordBatch> out_batch,
FilterRecordBatch(*args[0].record_batch(), args[1], options, ctx));
Expand Down
5 changes: 5 additions & 0 deletions python/pyarrow/tests/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,11 @@ def test_filter_record_batch():
expected = pa.record_batch([pa.array(["a", "e"])], names=["a'"])
assert result.equals(expected)

# GH-38770: mask is chunked array
chunked_mask = pa.chunked_array([[True, False], [None], [False, True]])
result = batch.filter(chunked_mask)
assert result.equals(expected)

result = batch.filter(mask, null_selection_behavior="emit_null")
expected = pa.record_batch([pa.array(["a", None, "e"])], names=["a'"])
assert result.equals(expected)
Expand Down
Loading