JuliaData / JuliaData/DelimitedFiles.jl

Custom iterator breaks on last row of a readdlm-loaded array

Open
#3 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
20
Forks
3
PR merge metrics
No merged PRs in 30d

Description

When using a custom iterator over an array created with readdlm, the last row is broken in Julia 1.0. Yes, I did test this with no warnings in 0.7. This behavior looks like a bug to me and possibly related to JuliaLang/julia#28763

using DelimitedFiles # for readdlm

mutable struct mytbl # a custom object
    source
    function mytbl(csvdata::Base.GenericIOBuffer)
        source = readdlm(csvdata, ',')
        source = convert(Array, source[2:end,:]) # clear the headers
        new(source)
    end
end

Base.length(it::mytbl) = size(it.source, 1)

function Base.iterate(it::mytbl, (el, i)=(it.source[1,:], 1))
   return i >= length(it) ? nothing : (el, (it.source[i + 1,:], i + 1))
end

# some sample data
TABLE_CAST = """id,height,age,name,occupation
1,10.0,1,string1,2012-06-15 00:00:00
2,10.1,2,string2,2013-06-15 01:00:00
"""

table =  mytbl(IOBuffer(TABLE_CAST))

[ row for row in table ]
# Returns: last element is #undef
# Expected: the last element is the second row

[ row[1] for row in table ]
# Returns:
[1, 239486056]
# Expected: [1, 2]

[ row[2] for row in table ]
# Returns:
[10.0, 1.18e-315]
# Expected: [10.0, 10.1]

Note that I've tested this with more elements, the last row is always..well, broken.

Further discussion and a workaround in https://discourse.julialang.org/t/last-row-is-broken-when-iterating-an-array/15696

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided Julia reproducer with DelimitedFiles.readdlm and the custom Base.iterate implementation. Compare the iterator output with the expected final row, then inspect the linked Julia issue and Discourse discussion. Done means determining whether the bug belongs in DelimitedFiles.jl and documenting or testing the correct behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.