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

WIP Add support for get_columns_copy interface #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
49 changes: 28 additions & 21 deletions src/TableTraitsUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,39 +110,46 @@ function _default_array_factory(t,rows)
end
end

function create_columns_from_iterabletable(source, sel_cols = :all; array_factory::Function=_default_array_factory)
iter = getiterator(source)
function create_columns_from_iterabletable(source, sel_cols = :all; array_factory::Function=_default_array_factory, array_conversion::Union{Function,Void}=nothing)
if array_conversion!=nothing && supports_get_columns_copy(source)
data = get_columns_copy(source)

T = eltype(iter)
if !(T<:NamedTuple)
error("Can only collect a NamedTuple iterator.")
end
columns = [array_conversion(data[i]) for i in 1:length(data)]
column_names = fieldnames(data)
else
iter = getiterator(source)

column_types = TableTraits.column_types(iter)
column_names = TableTraits.column_names(iter)
T = eltype(iter)
if !(T<:NamedTuple)
error("Can only collect a NamedTuple iterator.")
end

rows = Base.iteratorsize(typeof(iter))==Base.HasLength() ? length(iter) : 0
column_types = TableTraits.column_types(iter)
column_names = TableTraits.column_names(iter)

columns = []
for (i, t) in enumerate(column_types)
if sel_cols == :all || i in sel_cols
push!(columns, array_factory(t, rows))
else
push!(columns, nothing)
rows = Base.iteratorsize(typeof(iter))==Base.HasLength() ? length(iter) : 0

columns = []
for (i, t) in enumerate(column_types)
if sel_cols == :all || i in sel_cols
push!(columns, array_factory(t, rows))
else
push!(columns, nothing)
end
end
end

if Base.iteratorsize(typeof(iter))==Base.HasLength()
_fill_cols_with_length((columns...), iter)
else
_fill_cols_without_length((columns...), iter)
if Base.iteratorsize(typeof(iter))==Base.HasLength()
_fill_cols_with_length((columns...), iter)
else
_fill_cols_without_length((columns...), iter)
end
end

if sel_cols == :all
return columns, column_names
else
return columns[sel_cols], column_names[sel_cols]
end
end
end

end # module