From 81e82a7a450d15d6b96508c381a1b2648ebd7525 Mon Sep 17 00:00:00 2001 From: Chad Scherrer Date: Sun, 25 Apr 2021 17:05:32 -0700 Subject: [PATCH] Tables interface --- Project.toml | 1 + src/tables.jl | 36 ++++++++++++++++++++++++++++++++++++ src/tuplevector.jl | 2 ++ 3 files changed, 39 insertions(+) create mode 100644 src/tables.jl diff --git a/Project.toml b/Project.toml index e3d5dd4..2aa4c76 100644 --- a/Project.toml +++ b/Project.toml @@ -12,6 +12,7 @@ NestedTuples = "a734d2a7-8d68-409b-9419-626914d4061d" Requires = "ae029012-a4dd-5104-9daa-d747884805df" StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a" +Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" [compat] ArraysOfArrays = "0.5" diff --git a/src/tables.jl b/src/tables.jl new file mode 100644 index 0000000..dad078c --- /dev/null +++ b/src/tables.jl @@ -0,0 +1,36 @@ +import Tables + +Tables.isrowtable(::Type{<:TupleVector}) = true + +Tables.columnaccess(::Type{<:TupleVectors}) = true +Tables.columns(tv::TupleVector) = unwrap(tv) + + + +Tables.schema(s::TupleVector{T,X}) where {T,X} = Tables.Schema(T) + + +# Adapted from StructVectors.jl +function try_compatible_columns(rows::R, s::TupleVector) where {R} + Tables.isrowtable(rows) && Tables.columnaccess(rows) || return nothing + T = eltype(rows) + hasfields(T) || return nothing + NT = StructArrays.staticschema(T) + StructArrays._schema(NT) == Tables.schema(rows) || return nothing + return Tables.columntable(rows) +end + +# Adapted from StructVectors.jl +function Base.append!(s::TupleVector, rows) + table = try_compatible_columns(rows, s) + if table !== nothing + # Input `rows` is a container of rows _and_ satisfies column + # table interface. Thus, we can add the input column-by-column. + foreachfield(append!, s, table) + return s + else + # Otherwise, fallback to a generic implementation expecting + # that `rows` is an iterator: + return foldl(push!, rows; init = s) + end +end diff --git a/src/tuplevector.jl b/src/tuplevector.jl index 64e3393..3932bf6 100644 --- a/src/tuplevector.jl +++ b/src/tuplevector.jl @@ -8,6 +8,8 @@ struct TupleVector{T,X} <: AbstractVector{T} data :: X end +Base.ismutable(::TupleVector) = true + NestedTuples.unwrap(tv::TupleVector) = getfield(tv, :data) function TupleVector(data::X) where {X <: NamedTuple}