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

Tables interface #12

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
36 changes: 36 additions & 0 deletions src/tables.jl
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions src/tuplevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down