Skip to content

[Python] Buggy top_k_unstable/bottom_k_unstable #55758

In review

Description

@AlenkaF

While working on compute module stubs in #49201 I came across a buggy behavior of the top_k_unstable/bottom_k_unstable functions. Both compute functions can be called with Array/ChunkedArray or with RecordBatch/Table with different handling of the options.

For Array and ChunkedArray sort_keys shouldn't be accepted and should return ValueError with correct message but currently fails due to issues when appending to sort_keys:

sort_keys.append(("dummy", "descending", "at_end"))

>>> pc.top_k_unstable(arr, k=3, sort_keys="a")
AttributeError: 'str' object has no attribute 'append'

>>> pc.top_k_unstable(arr, k=3, sort_keys=["a"])
ValueError: need more than 1 value to unpack

As for the Table and RecordBatch, the signature suggests null_placements is optional but that errors with:

>>> pc.top_k_unstable(table, k=2, sort_keys=["a"])
...
File ~/Repos/arrow/python/pyarrow/compute.py:617, in top_k_unstable(values, k, sort_keys, null_placements, memory_pool)
    614     sort_keys.append(("dummy", "descending", "at_end"))
    615 else:
    616     sort_keys = [(sort_key, "descending", null_placement)
--> 617                  for sort_key, null_placement in zip(sort_keys, null_placements)]
    618 options = SelectKOptions(k, sort_keys)
    619 return call_function("select_k_unstable", [values], options, memory_pool)

TypeError: 'NoneType' object is not iterable

because zip does not accept None. To match C++, I suggest that null_placements to default to "at_end". Also, sort_keys and null_placements should be checked for being list-like (at least not str) and they should have the same length.

(same for bottom_k_unstable)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions