Skip to content

Commit

Permalink
implement isless for ColumnsRow (#51)
Browse files Browse the repository at this point in the history
* implement isless for ColumnsRow

* add isequal

* fix tail

* use generated functions instead

* Add tests for isless and isequal
  • Loading branch information
Pietro Vertechi authored and quinnj committed Dec 11, 2018
1 parent 61dae02 commit 0d0ca87
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/fallbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,33 @@ Base.getproperty(c::ColumnsRow, ::Type{T}, col::Int, nm::Symbol) where {T} = get
Base.getproperty(c::ColumnsRow, nm::Symbol) = getproperty(getfield(c, 1), nm)[getfield(c, 2)]
Base.propertynames(c::ColumnsRow) = propertynames(getfield(c, 1))

@generated function Base.isless(c::ColumnsRow{T}, d::ColumnsRow{T}) where {T <: NamedTuple{names}} where names
exprs = Expr[]
for n in names
var1 = Expr(:., :c, QuoteNode(n))
var2 = Expr(:., :d, QuoteNode(n))
bl = quote
a, b = $var1, $var2
isless(a, b) && return true
isequal(a, b) || return false
end
push!(exprs, bl)
end
push!(exprs, :(return false))
Expr(:block, exprs...)
end

@generated function Base.isequal(c::ColumnsRow{T}, d::ColumnsRow{T}) where {T <: NamedTuple{names}} where names
exprs = Expr[]
for n in names
var1 = Expr(:., :c, QuoteNode(n))
var2 = Expr(:., :d, QuoteNode(n))
push!(exprs, :(isequal($var1, $var2) || return false))
end
push!(exprs, :(return true))
Expr(:block, exprs...)
end

struct RowIterator{T}
columns::T
len::Int
Expand Down Expand Up @@ -41,7 +68,7 @@ haslength(L) = L isa Union{Base.HasShape, Base.HasLength}

"""
Tables.allocatecolumn(::Type{T}, len) => returns a column type (usually AbstractVector) w/ size to hold `len` elements
Custom column types can override with an appropriate "scalar" element type that should dispatch to their column allocator.
"""
allocatecolumn(T, len) = Vector{T}(undef, len)
Expand Down
11 changes: 11 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ end
@test gr == (gr |> genericrowtable)
end

@testset "isless" begin
t = (x = [1, 1, 0, 2], y = [-1, 1, 3, 2])
a,b,c,d = Tables.rows(t)
@test isless(a, b)
@test isless(c, d)
@test !isless(d, a)
@test !isequal(a, b)
@test isequal(a, a)
@test sortperm([a, b, c, d]) == [3, 1, 2, 4]
end

@static if :Query in Symbol.(Base.loaded_modules_array())
rt = (a = Real[1, 2.0, 3], b = Union{Missing, Float64}[4.0, missing, 6.0], c = ["7", "8", "9"])

Expand Down

0 comments on commit 0d0ca87

Please sign in to comment.