tadeja commented on code in PR #55759:
URL: https://github.com/apache/arrow/pull/55759#discussion_r4799167857


##########
python/pyarrow/compute.py:
##########
@@ -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)):

Review Comment:
   Array-like inputs like lists silently accept `sort_keys` but checking for 
table-like inputs instead would fix this:
   ```suggestion
       if not isinstance(values, (pa.Table, pa.RecordBatch)):
   ```
   Without the fix such example incorrectly succeeds:
   ```python
   import pyarrow.compute as pc
   pc.top_k_unstable([1, 2, None, 0], 2, sort_keys=["a"])
   ```
   
   And this example fails but shouldn't:
   ```python
   pc.top_k_unstable([1, 2, None, 0], k=2)
   # pyarrow.lib.ArrowInvalid: select_k_unstable requires a non-empty 
`sort_keys`
   ```



##########
python/pyarrow/tests/test_compute.py:
##########
@@ -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_():

Review Comment:
   ```suggestion
   def test_top_k_bottom_k():
   ```
   Typo?



##########
python/pyarrow/compute.py:
##########
@@ -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:

Review Comment:
   One more edge case which produces `TypeError` instead of `ValueError`
   ```python
   import pyarrow as pa
   import pyarrow.compute as pc
   pc.top_k_unstable(pa.array([1, 2, None, 0]), k=2, sort_keys="a")
   ```
   `TypeError: sort_keys and null_placements must be list-like, not str`
   
   This can be fixed by reordering these checks like so:
   ```diff
   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 `ValueError: sort_keys is only supported for 
table-like input`
   
   Plus extra check on (str, bytes) prevents second edge case `b"a"` from 
becoming column indices (`[97]`).



##########
python/pyarrow/compute.py:
##########
@@ -584,9 +613,11 @@ def top_k_unstable(
         The number of `k` elements to keep.
     sort_keys : List-like

Review Comment:
   `sort_keys` here and in `bottom_k_unstable` could be indicated as optional 
and `field references` mentioned for clarity.
   ```
       sort_keys : List-like, optional
           Column names or field references to order by when input is table-like
           data. Required for table-like input. Not supported for array-like 
input.
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to