apache / apache/arrow

[C++] `SelectKUnstable` can crash on empty inputs

Closed
#51,210 2 comments 0 reactions 1 assignee Claimed by @taepper View on GitHub
Component: C++ Type: bug
Dominant language
C++
Stars
17.1k
Forks
4.3k
Avg merge
3d 13h
Merged PRs (30d)
88

Description

### Describe the bug, including details regarding any error messages, version, and platform.

If an input `Table` or `ChunkedArray` is empty, the `SelectKthInternal` functions in `TableSelector` and `ChunkedArraySelector` have the following early return:

```
if (num_rows == 0) {
return Status::OK();
}
```

Crucially, `output_` is never set in these cases. Therefore, `SelectKUnstable`'s `make_array()` call:
```
Result> SelectKUnstable(const Datum& datum, ...) {
ARROW_ASSIGN_OR_RAISE(Datum result,
CallFunction("select_k_unstable", {datum}, &options, ctx));
return result.make_array();
}
```

will fail this `DCHECK_EQ`:
```
std::shared_ptr Datum::make_array() const {
DCHECK_EQ(Datum::ARRAY, this->kind());
return MakeArray(std::get>(this->value));
}
```

In release builds. `std::get` will crash with a `std::bad_variant_access`

This can also be reproduced in python code by calling `select_k_unstable` with an empty `ChunkedArray` or `Table` respectively

### Component(s)

C++

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.