-
Notifications
You must be signed in to change notification settings - Fork 44
RFC: Add append!(::StructVector, iterator::Any) using Tables.isrowtable #117
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,30 @@ Tables.columnaccess(::Type{<:StructVector}) = true | |
Tables.columns(s::StructVector) = fieldarrays(s) | ||
|
||
Tables.schema(s::StructVector) = Tables.Schema(staticschema(eltype(s))) | ||
|
||
function Base.append!(s::StructVector, rows) | ||
if Tables.isrowtable(rows) && Tables.columnaccess(rows) | ||
# Input `rows` is a container of rows _and_ satisfies column | ||
# table interface. Thus, we can add the input column-by-column. | ||
table = Tables.columns(rows) | ||
isempty(_setdiff(propertynames(s), Tables.columnnames(rows))) || | ||
_invalid_columns_error(s, rows) | ||
_foreach(propertynames(s)) do name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally for this StructArrays uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By looking at the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for merging this. Maybe you've already figured it out why, but the reason why I didn't use |
||
append!(getproperty(s, name), Tables.getcolumn(table, name)) | ||
end | ||
return s | ||
else | ||
# Otherwise, fallback to a generic implementation expecting | ||
# that `rows` is an iterator: | ||
return foldl(push!, rows; init = s) | ||
end | ||
end | ||
|
||
@noinline function _invalid_columns_error(s, rows) | ||
missingnames = setdiff!(collect(Tables.columnnames(rows)), propertynames(s)) | ||
throw(ArgumentError(string( | ||
"Cannot append rows from `$(typeof(rows))` to `$(typeof(s))` due to ", | ||
"missing column(s):\n", | ||
join(missingnames, ", "), | ||
))) | ||
end |
Uh oh!
There was an error while loading. Please reload this page.