Skip to content

[C++] SelectKUnstable can crash on empty inputs #51210

Closed

Description

@taepper

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<std::shared_ptr<Array>> 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<Array> Datum::make_array() const {                                                                                                                                                  
    DCHECK_EQ(Datum::ARRAY, this->kind());
    return MakeArray(std::get<std::shared_ptr<ArrayData>>(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++

Activity

  1. changed the title [-]`SelectKUnstable` can crash on empty inputs[/-] [+][C++] `SelectKUnstable` can crash on empty inputs[/+] on Sep 7, 2026
  2. Adarsh-Me commented on Sep 7, 2026

    @Adarsh-Me
    Contributor

    Hi, I'd like to work on this issue. Is it okay if I take this up?

  3. taepper commented on Sep 7, 2026

    @taepper
    ContributorAuthor

    take

  4. added a commit that references this issue on Sep 14, 2026
  5. added this to the 26.0.0 milestone on Sep 14, 2026
  6. pitrou commented on Sep 14, 2026

    @pitrou
    Member

    Issue resolved by pull request 51212
    #51212

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions