Skip to content

Commit 9bc355c

Browse files
add getrows
1 parent 60c080c commit 9bc355c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

docs/src/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,11 @@ the table-specific use-case, knowing that it will Just Work™️.
197197
198198
Before moving on to _implementing_ the Tables.jl interfaces, we take a quick
199199
break to highlight some useful utility functions provided by Tables.jl:
200+
200201
```@docs
201202
Tables.Schema
202203
Tables.schema
204+
Tables.getrows
203205
Tables.partitions
204206
Tables.partitioner
205207
Tables.rowtable
@@ -239,6 +241,7 @@ For a type `MyTable`, the interface to becoming a proper table is straightforwar
239241
| **Optional methods** | | |
240242
| `Tables.schema(x::MyTable)` | `Tables.schema(x) = nothing` | Return a [`Tables.Schema`](@ref) object from your `Tables.AbstractRow` iterator or `Tables.AbstractColumns` object; or `nothing` for unknown schema |
241243
| `Tables.materializer(::Type{MyTable})` | `Tables.columntable` | Declare a "materializer" sink function for your table type that can construct an instance of your type from any Tables.jl input |
244+
| `Tables.getrows(x::MyTable, inds; view)` | | Return a row or a sub-table of the original table
242245
243246
Based on whether your table type has defined `Tables.rows` or `Tables.columns`, you then ensure that the `Tables.AbstractRow` iterator
244247
or `Tables.AbstractColumns` object satisfies the respective interface.

src/Tables.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,28 @@ struct Partitioner{T}
565565
x::T
566566
end
567567

568+
"""
569+
getrows(x, inds; view=nothing)
570+
571+
Return one or more rows from table `x` according to the position(s) specified by `inds`.
572+
573+
- If `inds` is a single integer return a row object.
574+
- If `inds` is a collection of integers, return a table object.
575+
In this case,t he returned type is not necessarily the same as the original table type.
576+
577+
The `view` argument influences whether the returned object is a view of the original table
578+
or an independent copy:
579+
- If `view=nothing` (the default) then the implementation for a specific table type
580+
is free to decide whether to return a copy or a view.
581+
- If `view=true` then a view is returned and if `view=false` a copy is returned.
582+
This applies both to returning a row or a table.
583+
584+
Any specialized implementation of `getrows` must support the `view=nothing` argument.
585+
Support for `view=true` or `view=false` instead can be an opt-in
586+
(i.e. implementations might error on them if they are not supported).
587+
"""
588+
function getrows end
589+
568590
"""
569591
Tables.partitioner(f, itr)
570592
Tables.partitioner(x)

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,4 +798,8 @@ Tables.columnnames(::WideTable2) = [Symbol("x", i) for i = 1:1000]
798798
@test nm isa Symbol
799799
@test col isa Vector{Float64}
800800
end
801+
802+
@testset "getrows" begin
803+
Tables.getrows isa Function
804+
end
801805
end

0 commit comments

Comments
 (0)