From 7db9a6e48b3e50a47e58e851797016217105d749 Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Thu, 23 Nov 2017 17:43:35 -0800 Subject: [PATCH 1/2] Add support for get_columns_copy interface --- src/TableTraitsUtils.jl | 47 +++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/TableTraitsUtils.jl b/src/TableTraitsUtils.jl index db59651..1459480 100644 --- a/src/TableTraitsUtils.jl +++ b/src/TableTraitsUtils.jl @@ -108,38 +108,45 @@ function _default_array_factory(t,rows) end function create_columns_from_iterabletable(source, sel_cols = :all; array_factory::Function=_default_array_factory) - iter = getiterator(source) + if 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 = [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 From 77e9b65198713111c537a0e389123cb16396abef Mon Sep 17 00:00:00 2001 From: David Anthoff Date: Mon, 22 Jan 2018 21:31:06 -0800 Subject: [PATCH 2/2] Add array_conversion argument --- src/TableTraitsUtils.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/TableTraitsUtils.jl b/src/TableTraitsUtils.jl index 192b25b..17ae866 100644 --- a/src/TableTraitsUtils.jl +++ b/src/TableTraitsUtils.jl @@ -110,11 +110,11 @@ function _default_array_factory(t,rows) end end -function create_columns_from_iterabletable(source, sel_cols = :all; array_factory::Function=_default_array_factory) - if supports_get_columns_copy(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) - columns = [data[i] for i in 1:length(data)] + columns = [array_conversion(data[i]) for i in 1:length(data)] column_names = fieldnames(data) else iter = getiterator(source)