Skip to content

Commit

Permalink
apacheGH-37650: [Python] Check filter inputs in FilterMetaFunction (a…
Browse files Browse the repository at this point in the history
…pache#38075)

### Rationale for this change
Prevent a segfault upon passing a non-(chunked_)array object into `Table.filter`. See apache#37650.

### What changes are included in this PR?
1. Check filter Datum kind to make sure that it is an array or a chunked array
2. test that attempting to filter a table with another table raises a not implemented error

### Are these changes tested?
In PyArrow, yes

### Are there any user-facing changes?
Raises an error if a non-array or non-chunked_array object is passed into `Table.filter`

* Closes: apache#37650

Lead-authored-by: Patrick Clarke <[email protected]>
Co-authored-by: Antoine Pitrou <[email protected]>
Signed-off-by: Antoine Pitrou <[email protected]>
  • Loading branch information
2 people authored and loicalleyne committed Nov 13, 2023
1 parent 8148a27 commit 8ff1dbd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ Result<std::shared_ptr<Table>> FilterTable(const Table& table, const Datum& filt
inputs.back() = filter.chunked_array()->chunks();
break;
default:
return Status::NotImplemented("Filter should be array-like");
return Status::TypeError("Filter should be array-like");
}

// Rechunk inputs to allow consistent iteration over their respective chunks
Expand Down Expand Up @@ -998,6 +998,10 @@ class FilterMetaFunction : public MetaFunction {
Result<Datum> ExecuteImpl(const std::vector<Datum>& args,
const FunctionOptions* options,
ExecContext* ctx) const override {
if (args[1].kind() != Datum::ARRAY && args[1].kind() != Datum::CHUNKED_ARRAY) {
return Status::TypeError("Filter should be array-like");
}

const auto& filter_type = *args[1].type();
const bool filter_is_plain_bool = filter_type.id() == Type::BOOL;
const bool filter_is_ree_bool =
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 @@ -1378,6 +1378,11 @@ def test_filter_errors():
match="must all be the same length"):
obj.filter(mask)

scalar = pa.scalar(True)
for filt in [batch, table, scalar]:
with pytest.raises(TypeError):
table.filter(filt)


def test_filter_null_type():
# ARROW-10027
Expand Down

0 comments on commit 8ff1dbd

Please sign in to comment.