Skip to content

Commit 6d0b6d3

Browse files
authored
Change view to viewhint in Tables.subset (#304)
* Change view to viewhint in Tables.subset * fix * fix
1 parent 63067e3 commit 6d0b6d3

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

src/Tables.jl

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ struct Partitioner{T}
574574
end
575575

576576
"""
577-
Tables.subset(x, inds; view=nothing)
577+
Tables.subset(x, inds; viewhint=nothing)
578578
579579
Return one or more rows from table `x` according to the position(s) specified by `inds`:
580580
@@ -584,35 +584,39 @@ Return one or more rows from table `x` according to the position(s) specified by
584584
585585
If other types of `inds` are passed than specified above the behavior is undefined.
586586
587-
The `view` argument influences whether the returned object is a view of the original table
587+
The `viewhint` argument tries to influence whether the returned object is a view of the original table
588588
or an independent copy:
589589
590-
- If `view=nothing` (the default) then the implementation for a specific table type
590+
- If `viewhint=nothing` (the default) then the implementation for a specific table type
591591
is free to decide whether to return a copy or a view.
592-
- If `view=true` then a view is returned and if `view=false` a copy is returned.
592+
- If `viewhint=true` then a view is returned and if `viewhint=false` a copy is returned.
593593
This applies both to returning a row or a table.
594594
595-
Any specialized implementation of `subset` must support the `view=nothing` argument.
596-
Support for `view=true` or `view=false` is optional
597-
(i.e. implementations may ignore the keyword argument and return a view or a copy regardless of `view` value).
595+
Any specialized implementation of `subset` must support the `viewhint=nothing` argument.
596+
Support for `viewhint=true` or `viewhint=false` is optional
597+
(i.e. implementations may ignore the keyword argument and return a view or a copy regardless of `viewhint` value).
598598
"""
599-
function subset(x::T, inds; view::Union{Bool, Nothing}=nothing) where {T}
599+
function subset(x::T, inds; viewhint::Union{Bool, Nothing}=nothing, view::Union{Bool, Nothing}=nothing) where {T}
600+
if view !== nothing
601+
@warn "`view` keyword argument is deprecated for `Tables.subset`, use `viewhint` instead"
602+
viewhint = view
603+
end
600604
# because this method is being called, we know `x` didn't define it's own Tables.subset
601605
# first check if it supports column access, and if so, apply inds and wrap columns in a DictColumnTable
602606
if columnaccess(x)
603607
cols = columns(x)
604608
if inds isa Integer
605609
return ColumnsRow(cols, inds)
606610
else
607-
ret = view === true ? _map(c -> Base.view(c, inds), cols) : _map(c -> c[inds], cols)
611+
ret = viewhint === true ? _map(c -> Base.view(c, inds), cols) : _map(c -> c[inds], cols)
608612
return DictColumnTable(schema(cols), ret)
609613
end
610614
end
611615
# otherwise, let's get the rows and see if we can apply inds to them
612616
r = rows(x)
613617
if r isa AbstractVector
614618
inds isa Integer && return r[inds]
615-
ret = view === true ? Base.view(x, inds) : x[inds]
619+
ret = viewhint === true ? Base.view(x, inds) : x[inds]
616620
(ret isa AbstractVector) || throw(ArgumentError("`Tables.subset`: invalid `inds` argument, expected `AbstractVector` output, got $(typeof(ret))"))
617621
return ret
618622
end

src/dicts.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,12 @@ function Base.iterate(x::DictRowTable, st=1)
124124
return DictRow(x.names, x.values[st]), st + 1
125125
end
126126

127-
function subset(x::DictRowTable, inds; view::Union{Bool,Nothing} = nothing)
128-
values = view === true ? Base.view(getfield(x, :values), inds) : getfield(x, :values)[inds]
127+
function subset(x::DictRowTable, inds; viewhint::Union{Bool,Nothing}=nothing, view::Union{Bool,Nothing}=nothing)
128+
if view !== nothing
129+
@warn "`view` keyword argument is deprecated for `Tables.subset`, use `viewhint` instead"
130+
viewhint = view
131+
end
132+
values = viewhint === true ? Base.view(getfield(x, :values), inds) : getfield(x, :values)[inds]
129133
if inds isa Integer
130134
return DictRow(getfield(x, :names), values)
131135
else

src/namedtuples.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,15 @@ end
110110
const ColumnTable = NamedTuple{names, T} where {names, T <: NTuple{N, AbstractVector}} where {N}
111111
rowcount(c::ColumnTable) = length(c) == 0 ? 0 : length(c[1])
112112

113-
function subset(x::ColumnTable, inds; view::Union{Bool,Nothing}=nothing)
113+
function subset(x::ColumnTable, inds; viewhint::Union{Bool,Nothing}=nothing, view::Union{Bool,Nothing}=nothing)
114+
if view !== nothing
115+
@warn "`view` keyword argument is deprecated for `Tables.subset`, use `viewhint` instead"
116+
viewhint = view
117+
end
114118
if inds isa Integer
115119
return map(c -> c[inds], x)
116120
else
117-
return view === true ? map(c -> vectorcheck(Base.view(c, inds)), x) : map(c -> vectorcheck(c[inds]), x)
121+
return viewhint === true ? map(c -> vectorcheck(Base.view(c, inds)), x) : map(c -> vectorcheck(c[inds]), x)
118122
end
119123
end
120124

test/runtests.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,30 +147,30 @@ end
147147

148148
@testset "columntable subset" begin
149149
@test Tables.subset(nt, 1) == (a=1, b=4.0, c="7")
150-
@test Tables.subset(nt, 1, view=false) == (a=1, b=4.0, c="7")
151-
@test Tables.subset(nt, 1, view=nothing) == (a=1, b=4.0, c="7")
150+
@test Tables.subset(nt, 1, viewhint=false) == (a=1, b=4.0, c="7")
151+
@test Tables.subset(nt, 1, viewhint=nothing) == (a=1, b=4.0, c="7")
152152
@test Tables.subset(nt, 1:2) == (a=[1,2], b=[4.0, 5.0], c=["7","8"])
153-
@test Tables.subset(nt, 1:2, view=false) == (a=[1,2], b=[4.0, 5.0], c=["7","8"])
154-
@test Tables.subset(nt, 1:2, view=nothing) == (a=[1,2], b=[4.0, 5.0], c=["7","8"])
153+
@test Tables.subset(nt, 1:2, viewhint=false) == (a=[1,2], b=[4.0, 5.0], c=["7","8"])
154+
@test Tables.subset(nt, 1:2, viewhint=nothing) == (a=[1,2], b=[4.0, 5.0], c=["7","8"])
155155
@test_throws ArgumentError Tables.subset(nt, [1:2 1:2])
156156

157-
@test Tables.subset(nt, 1, view=true) == (a=1, b=4.0, c="7")
158-
rs = Tables.subset(nt, 1:2, view=true)
157+
@test Tables.subset(nt, 1, viewhint=true) == (a=1, b=4.0, c="7")
158+
rs = Tables.subset(nt, 1:2, viewhint=true)
159159
@test rs == (a=[1,2], b=[4.0, 5.0], c=["7","8"])
160160
@test rs.a.parent === nt.a
161161
end
162162

163163
@testset "rowtable subset" begin
164164
@test Tables.subset(rt, 1) == (a=1, b=4.0, c="7")
165-
@test Tables.subset(rt, 1, view=false) == (a=1, b=4.0, c="7")
166-
@test Tables.subset(rt, 1, view=nothing) == (a=1, b=4.0, c="7")
165+
@test Tables.subset(rt, 1, viewhint=false) == (a=1, b=4.0, c="7")
166+
@test Tables.subset(rt, 1, viewhint=nothing) == (a=1, b=4.0, c="7")
167167
@test Tables.subset(rt, 1:2) == [(a=1, b=4.0, c="7"), (a=2, b=5.0, c="8")]
168-
@test Tables.subset(rt, 1:2, view=false) == [(a=1, b=4.0, c="7"), (a=2, b=5.0, c="8")]
169-
@test Tables.subset(rt, 1:2, view=nothing) == [(a=1, b=4.0, c="7"), (a=2, b=5.0, c="8")]
168+
@test Tables.subset(rt, 1:2, viewhint=false) == [(a=1, b=4.0, c="7"), (a=2, b=5.0, c="8")]
169+
@test Tables.subset(rt, 1:2, viewhint=nothing) == [(a=1, b=4.0, c="7"), (a=2, b=5.0, c="8")]
170170
@test_throws ArgumentError Tables.subset(rt, [1:2 1:2])
171171

172-
@test Tables.subset(rt, 1, view=true) == (a=1, b=4.0, c="7")
173-
rs = Tables.subset(rt, 1:2, view=true)
172+
@test Tables.subset(rt, 1, viewhint=true) == (a=1, b=4.0, c="7")
173+
rs = Tables.subset(rt, 1:2, viewhint=true)
174174
@test rs == [(a=1, b=4.0, c="7"), (a=2, b=5.0, c="8")]
175175
@test rs.parent === rt
176176
end
@@ -794,7 +794,7 @@ end
794794
@test drow.a == 1 && drow.b == 2 && drow.c == 3
795795
drows = Tables.subset(drt, [1, 2])
796796
@test length(drows) == 2
797-
drowsv = Tables.subset(drt, [1, 2]; view=true)
797+
drowsv = Tables.subset(drt, [1, 2]; viewhint=true)
798798
@test length(drowsv) == 2
799799
end
800800

0 commit comments

Comments
 (0)