AxisKeys.jl icon indicating copy to clipboard operation
AxisKeys.jl copied to clipboard

stackoverflow in zero-dimensional indexing

Open aplavin opened this issue 4 years ago • 1 comments

Zero-dimensional KeyedArray works mostly fine:

julia> using AxisKeys
julia> ka0 = KeyedArray(fill(123));
julia> ka0[1]
123
julia> size(ka0)
()
julia> ndims(ka0)
0
julia> axiskeys(ka0)
()

However, the corresponding zero-dimensional indexing fails with a StackOverflowError:

julia> ka0[]
ERROR: StackOverflowError:
Stacktrace:
     [1] getindex(A::KeyedArray{Int64, 0, NamedDimsArray{(), Int64, 0, Array{Int64, 0}}, Tuple{}})
       @ AxisKeys ~/.julia/dev/AxisKeys/src/names.jl:83
     [2] getindex(A::KeyedArray{Int64, 0, NamedDimsArray{(), Int64, 0, Array{Int64, 0}}, Tuple{}}; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
       @ AxisKeys ~/.julia/dev/AxisKeys/src/names.jl:85
--- the last 2 lines are repeated 39990 more times ---
 [79983] getindex(A::KeyedArray{Int64, 0, NamedDimsArray{(), Int64, 0, Array{Int64, 0}}, Tuple{}})
       @ AxisKeys ~/.julia/dev/AxisKeys/src/names.jl:83

I'm not familiar with this kind of internals, but looks like a wrong getindex method gets called somehow.

For comparison, 0-d arrays in base julia:

julia> a0 = fill(123)
0-dimensional Array{Int64, 0}:
123
julia> a0[]
123
julia> a0[1]
123

aplavin avatar May 06 '21 14:05 aplavin

Sorry, there might be bugs. It's easy to introduce them when you slurp/splat all arguments, and don't think about the empty case. Indexing with a trailing 1 does seem to work, as a possible work-around:

julia> KeyedArray(fill(pi), ())  # should this work?
ERROR: must have names!

julia> wrapdims(fill(pi))[1,1,1]  # trailing 1s always allowed
Ï€ = 3.1415926535897...

julia> NamedDimsArray(fill(pi), ())[] # no problem
Ï€ = 3.1415926535897...

mcabbott avatar May 06 '21 15:05 mcabbott