Skip to content

Parts of sorting broken due to scratch kwarg #53113

@palday

Description

@palday

I noticed that sorting in AxisKeys.jl is broken. I'm posting here because it seems related to the changes in sorting that happened in Julia 1.9 and the relevant piece of the error (keyword argument scratch, see below) does not occur in AxisKeys.jl, but rather from Base.Sort.__sort!.

MWE:

using Pkg
Pkg.activate(; temp=true)
@info "Installing packages"
Pkg.add(["AxisKeys"]; io=devnull)
using AxisKeys

data = rand(2, 10, 3)
A = KeyedArray(data; channel=[:left, :right], time=range(13, step=2.5, length=10), iter=31:33)
sort!(A; dims=1)

Error:

julia> sort!(A; dims=1)
ERROR: TypeError: in keyword argument scratch, expected Union{Nothing, Vector{<:Integer}}, got a value of type Vector{Float64}
Stacktrace:
 [1] sort!(A::KeyedArray{…}; kw::@Kwargs{})
   @ AxisKeys ~/.julia/packages/AxisKeys/QsNqy/src/functions.jl:274
 [2] __sort!(A::KeyedArray{…}, ::Val{…}, alg::Base.Sort.MissingOptimization{…}, order::Base.Order.ForwardOrdering, scratch::Vector{…})
   @ Base.Sort ./sort.jl:1879
 [3] sort!(A::KeyedArray{…}; dims::Int64, alg::Base.Sort.MissingOptimization{…}, lt::Function, by::Function, rev::Nothing, order::Base.Order.ForwardOrdering, scratch::Vector{…})
   @ Base.Sort ./sort.jl:1866
 [4] top-level scope
   @ REPL[8]:1
Some type information was truncated. Use `show(err)` to see complete types.

Notably, the error does not happen if the eltype of the array is Int.

I bisected to this:

commit cee0a0494c70208b6cd5a32ccdf75d954a429870
Author: Lilith Orion Hafner <lilithhafner@gmail.com>
Date:   Sat Dec 3 06:19:20 2022 +0600

    Refactor and document sorting dispatch (#47383)
    
    * create an internal `_sort!` function and use it (rename the existing `_sort!` to `__sort!`)
    
    * test for several of bugs that slipped through test suite
    
    * Give each sorting pass and DEFAULT_STABLE a docstring
    
    * add pretty printing for the new algorithms that are much more flexible than the old ones
    
    * fix unexpected allocations in Radix Sort
    
    fixes #47474
    in this PR rather than separate to avoid dealing with the merge
    
    * support and test backwards compatibility with packages that depend in sorting internals
    
    * support 3-, 5-, and 6-argument sort! for backwards compatibility
    
    * overhall scratch space handling
    
    make _sort! return scratch space rather than sorted vector
    so that things like IEEEFloatOptimization can re-use the
    scratch space allocated on their first recursive call
    
    * test handling -0.0 in IEEEFloatOptimization
    
    * fix and test bug where countsort's correct overflow behavior triggers error due to unexpected promotion to UInt

 base/sort.jl    | 1287 +++++++++++++++++++++++++++++++++++--------------------
 test/sorting.jl |  171 +++++++-
 2 files changed, 968 insertions(+), 490 deletions(-)

cc @LilithHafner @mcabbott

Metadata

Metadata

Assignees

No one assigned

    Labels

    sortingPut things in order

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions