Skip to content
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

Start an idea of what an "in memory" requirement would look like #278

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,15 @@ See also [`rowtable`](@ref) and [`namedtupleiterator`](@ref).
"""
function rows end

"""
Tables.indexablerows(x) => Row indexable

Similar to `Tables.rows`, but instead of only returning an iterator, returns an indexable object.
This allows random-access to the input table rows, which can be useful in contexts when
a known subset of the input is needed.
"""
function indexablerows end

# Schema implementation
"""
Tables.Schema(names, types)
Expand Down
18 changes: 17 additions & 1 deletion src/fallbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ end
end

# RowIterator wraps an AbstractColumns object and provides row iteration via lazy row views
struct RowIterator{T}
struct RowIterator{T} <: AbstractVector{ColumnsRow{T}}
columns::T
len::Int
end

Base.eltype(x::RowIterator{T}) where {T} = ColumnsRow{T}
Base.length(x::RowIterator) = getfield(x, :len)
Base.size(x::RowIterator) = (length(x),)
Base.getproperty(x::RowIterator, nm::Symbol) = getcolumn(x, nm)
Base.getproperty(x::RowIterator, i::Int) = getcolumn(x, i)
Base.propertynames(x::RowIterator) = columnnames(x)
Expand All @@ -74,6 +75,11 @@ getcolumn(x::RowIterator, i::Int) = getcolumn(columns(x), i)
materializer(x::RowIterator) = materializer(columns(x))
schema(x::RowIterator) = schema(columns(x))

Base.@propagate_inbounds function Base.getindex(x::RowIterator, i::Int)
@boundscheck checkbounds(x, i)
return ColumnsRow(columns(x), i)
end

@inline function Base.iterate(rows::RowIterator, st=1)
st > length(rows) && return nothing
return ColumnsRow(columns(rows), st), st + 1
Expand All @@ -96,6 +102,16 @@ function rows(x::T) where {T}
throw(ArgumentError("no default `Tables.rows` implementation for type: $T"))
end

# fallback for indexablerows if not overloaded explicitly
function indexablerows(x::T) where {T}
y = rows(x)
if y isa AbstractArray
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why AbstractArray here not AbstractVector?

return y
else
throw(ArgumentError("no default `Tables.indexablerows` implementation for type: $T"))
end
end

# for AbstractRow iterators, we define a "collect"-like routine to build up columns from iterated rows

"""
Expand Down