Repository navigation
GH-55758: [Python] Buggy top_k_unstable/bottom_k_unstable #55759
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -567,6 +567,35 @@ def fill_null(values, fill_value): | |
| return call_function("coalesce", [values, fill_value]) | ||
|
|
||
|
|
||
| def _select_k_sort_keys(values, sort_keys, null_placements, order): | ||
| if isinstance(sort_keys, str) or isinstance(null_placements, str): | ||
| raise TypeError( | ||
| "sort_keys and null_placements must be list-like, not str") | ||
|
|
||
| if isinstance(values, (pa.Array, pa.ChunkedArray)): | ||
| if sort_keys is not None: | ||
| raise ValueError( | ||
| "sort_keys is only supported for table-like input") | ||
| if null_placements is None: | ||
|
Comment on lines
+571
to
+579
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One more edge case which produces import pyarrow as pa
import pyarrow.compute as pc
pc.top_k_unstable(pa.array([1, 2, None, 0]), k=2, sort_keys="a")
This can be fixed by reordering these checks like so: diff --git a/python/pyarrow/compute.py b/python/pyarrow/compute.py
@@ -568,14 +568,18 @@ def fill_null(values, fill_value):
def _select_k_sort_keys(values, sort_keys, null_placements, order):
- if isinstance(sort_keys, str) or isinstance(null_placements, str):
+ is_array_like = not isinstance(values, (pa.Table, pa.RecordBatch))
+ if is_array_like and sort_keys is not None:
+ raise ValueError(
+ "sort_keys is only supported for table-like input")
+
+ if (isinstance(sort_keys, (str, bytes)) or
+ isinstance(null_placements, (str, bytes))):
raise TypeError(
- "sort_keys and null_placements must be list-like, not str")
+ "sort_keys and null_placements must be list-like, "
+ "not str or bytes")
- if isinstance(values, (pa.Array, pa.ChunkedArray)):
- if sort_keys is not None:
- raise ValueError(
- "sort_keys is only supported for table-like input")
+ if is_array_like:
if null_placements is None:
return [("dummy", order, "at_end")]... which results in correct Plus extra check on (str, bytes) prevents second edge case |
||
| return [("dummy", order, "at_end")] | ||
| else: | ||
| null_placements = list(null_placements) | ||
| if len(null_placements) != 1: | ||
| raise ValueError( | ||
| "null_placements must have one element for array input") | ||
| return [("dummy", order, null_placements[0])] | ||
|
|
||
| sort_keys = list(sort_keys) if sort_keys is not None else [] | ||
| if null_placements is None: | ||
| null_placements = ["at_end"] * len(sort_keys) | ||
| else: | ||
| null_placements = list(null_placements) | ||
| if len(null_placements) != len(sort_keys): | ||
| raise ValueError( | ||
| "sort_keys and null_placements must have the same length") | ||
| return [(key, order, null_placements[i]) for i, key in enumerate(sort_keys)] | ||
|
|
||
|
|
||
| def top_k_unstable( | ||
| values, k, sort_keys=None, null_placements=None, *, memory_pool=None): | ||
| """ | ||
|
|
@@ -584,9 +613,11 @@ def top_k_unstable( | |
| The number of `k` elements to keep. | ||
| sort_keys : List-like | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| Column key names to order by when input is table-like data. | ||
| null_placements : A list of "at_start" or "at_end" | ||
| Whether nulls and NaNs are placed at the start or at the end. | ||
| Accepted values are "at_end", "at_start". | ||
| Not supported for array-like input. | ||
| null_placements : List-like of "at_start" or "at_end", optional | ||
| Whether nulls and NaNs are placed at the start or at the end, | ||
| for each key in `sort_keys` or single element for array input. | ||
| If not passed, "at_end" is used for every key. | ||
| memory_pool : MemoryPool, optional | ||
| If not passed, will allocate memory from the default memory pool. | ||
|
|
||
|
|
@@ -608,13 +639,8 @@ def top_k_unstable( | |
| 2 | ||
| ] | ||
| """ | ||
| if sort_keys is None: | ||
| sort_keys = [] | ||
| if isinstance(values, (pa.Array, pa.ChunkedArray)): | ||
| sort_keys.append(("dummy", "descending", "at_end")) | ||
| else: | ||
| sort_keys = [(sort_key, "descending", null_placement) | ||
| for sort_key, null_placement in zip(sort_keys, null_placements)] | ||
| sort_keys = _select_k_sort_keys( | ||
| values, sort_keys, null_placements, "descending") | ||
| options = SelectKOptions(k, sort_keys) | ||
| return call_function("select_k_unstable", [values], options, memory_pool) | ||
|
|
||
|
|
@@ -636,9 +662,11 @@ def bottom_k_unstable( | |
| The number of `k` elements to keep. | ||
| sort_keys : List-like | ||
| Column key names to order by when input is table-like data. | ||
| null_placements : A list of "at_start" or "at_end" | ||
| Whether nulls and NaNs are placed at the start or at the end. | ||
| Accepted values are "at_end", "at_start". | ||
| Not supported for array-like input. | ||
| null_placements : List-like of "at_start" or "at_end", optional | ||
| Whether nulls and NaNs are placed at the start or at the end, | ||
| for each key in `sort_keys` or single element for array input. | ||
| If not passed, "at_end" is used for every key. | ||
| memory_pool : MemoryPool, optional | ||
| If not passed, will allocate memory from the default memory pool. | ||
|
|
||
|
|
@@ -660,14 +688,8 @@ def bottom_k_unstable( | |
| 2 | ||
| ] | ||
| """ | ||
| if sort_keys is None: | ||
| sort_keys = [] | ||
| if isinstance(values, (pa.Array, pa.ChunkedArray)): | ||
| sort_keys.append(("dummy", "ascending", "at_end")) | ||
| else: | ||
| sort_keys = [(sort_key, "ascending", null_placement) | ||
| for sort_key, null_placement in zip(sort_keys, null_placements)] | ||
|
|
||
| sort_keys = _select_k_sort_keys( | ||
| values, sort_keys, null_placements, "ascending") | ||
| options = SelectKOptions(k, sort_keys) | ||
| return call_function("select_k_unstable", [values], options, memory_pool) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3251,6 +3251,40 @@ def validate_select_k(select_k_indices, tbl, sort_keys, stable_sort=False): | |||||
| pc.select_k_unstable(table, k=k, sort_keys=[("unknown", "ascending", "at_end")]) | ||||||
|
|
||||||
|
|
||||||
| def test_top_k_bottom_k_(): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Typo? |
||||||
| arr = pa.array([1, 2, None, 0]) | ||||||
| table = pa.table({"a": [1, 2, None], "b": [None, 0, 1]}) | ||||||
|
|
||||||
| # null_placements defaults to "at_end" for every key | ||||||
| assert pc.top_k_unstable(table, k=2, sort_keys=["a"]).to_pylist() == [1, 0] | ||||||
|
tadeja marked this conversation as resolved.
Resolved
|
||||||
| assert pc.bottom_k_unstable( | ||||||
| table, k=2, sort_keys=("a", "b")).to_pylist() == [0, 1] | ||||||
|
|
||||||
| # null_placements "at_start" is not ignored for array input | ||||||
| assert pc.top_k_unstable(arr, k=2, null_placements=[ | ||||||
| "at_start"]).to_pylist() == [2, 1] | ||||||
| assert pc.bottom_k_unstable(arr, k=2, null_placements=[ | ||||||
| "at_start"]).to_pylist() == [2, 3] | ||||||
|
|
||||||
| for func in [pc.top_k_unstable, pc.bottom_k_unstable]: | ||||||
| sort_keys = ["a"] | ||||||
| with pytest.raises(ValueError, match="only supported for table-like"): | ||||||
| func(arr, k=2, sort_keys=sort_keys) | ||||||
| with pytest.raises(ValueError, match="must have one element"): | ||||||
| func(arr, k=2, null_placements=["at_start", "at_end"]) | ||||||
| with pytest.raises(TypeError, match="must be list-like, not str"): | ||||||
| func(arr, k=2, null_placements="at_start") | ||||||
| with pytest.raises(TypeError, match="'int' object is not iterable"): | ||||||
| func(arr, k=2, null_placements=1) | ||||||
| assert sort_keys == ["a"] # list is not mutated | ||||||
| with pytest.raises(TypeError, match="must be list-like"): | ||||||
| func(table, k=2, sort_keys="a") | ||||||
| with pytest.raises(TypeError, match="must be list-like"): | ||||||
| func(table, k=2, sort_keys=["a"], null_placements="at_end") | ||||||
| with pytest.raises(ValueError, match="same length"): | ||||||
| func(table, k=2, sort_keys=["a", "b"], null_placements=["at_end"]) | ||||||
|
|
||||||
|
|
||||||
| def test_array_sort_indices(): | ||||||
| arr = pa.array([1, 2, None, 0]) | ||||||
| result = pc.array_sort_indices(arr) | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array-like inputs like lists silently accept
sort_keysbut checking for table-like inputs instead would fix this:Without the fix such example incorrectly succeeds:
And this example fails but shouldn't: