Skip to content

Commit 0d0ca87

Browse files
Pietro Vertechiquinnj
authored andcommitted
implement isless for ColumnsRow (#51)
* implement isless for ColumnsRow * add isequal * fix tail * use generated functions instead * Add tests for isless and isequal
1 parent 61dae02 commit 0d0ca87

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/fallbacks.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,33 @@ Base.getproperty(c::ColumnsRow, ::Type{T}, col::Int, nm::Symbol) where {T} = get
1414
Base.getproperty(c::ColumnsRow, nm::Symbol) = getproperty(getfield(c, 1), nm)[getfield(c, 2)]
1515
Base.propertynames(c::ColumnsRow) = propertynames(getfield(c, 1))
1616

17+
@generated function Base.isless(c::ColumnsRow{T}, d::ColumnsRow{T}) where {T <: NamedTuple{names}} where names
18+
exprs = Expr[]
19+
for n in names
20+
var1 = Expr(:., :c, QuoteNode(n))
21+
var2 = Expr(:., :d, QuoteNode(n))
22+
bl = quote
23+
a, b = $var1, $var2
24+
isless(a, b) && return true
25+
isequal(a, b) || return false
26+
end
27+
push!(exprs, bl)
28+
end
29+
push!(exprs, :(return false))
30+
Expr(:block, exprs...)
31+
end
32+
33+
@generated function Base.isequal(c::ColumnsRow{T}, d::ColumnsRow{T}) where {T <: NamedTuple{names}} where names
34+
exprs = Expr[]
35+
for n in names
36+
var1 = Expr(:., :c, QuoteNode(n))
37+
var2 = Expr(:., :d, QuoteNode(n))
38+
push!(exprs, :(isequal($var1, $var2) || return false))
39+
end
40+
push!(exprs, :(return true))
41+
Expr(:block, exprs...)
42+
end
43+
1744
struct RowIterator{T}
1845
columns::T
1946
len::Int
@@ -41,7 +68,7 @@ haslength(L) = L isa Union{Base.HasShape, Base.HasLength}
4168

4269
"""
4370
Tables.allocatecolumn(::Type{T}, len) => returns a column type (usually AbstractVector) w/ size to hold `len` elements
44-
71+
4572
Custom column types can override with an appropriate "scalar" element type that should dispatch to their column allocator.
4673
"""
4774
allocatecolumn(T, len) = Vector{T}(undef, len)

test/runtests.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,17 @@ end
212212
@test gr == (gr |> genericrowtable)
213213
end
214214

215+
@testset "isless" begin
216+
t = (x = [1, 1, 0, 2], y = [-1, 1, 3, 2])
217+
a,b,c,d = Tables.rows(t)
218+
@test isless(a, b)
219+
@test isless(c, d)
220+
@test !isless(d, a)
221+
@test !isequal(a, b)
222+
@test isequal(a, a)
223+
@test sortperm([a, b, c, d]) == [3, 1, 2, 4]
224+
end
225+
215226
@static if :Query in Symbol.(Base.loaded_modules_array())
216227
rt = (a = Real[1, 2.0, 3], b = Union{Missing, Float64}[4.0, missing, 6.0], c = ["7", "8", "9"])
217228

0 commit comments

Comments
 (0)