Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ end
# IMPLEMENTATIONS
# ----------------

include("transforms/colspec.jl")
include("transforms/select.jl")
include("transforms/filter.jl")
include("transforms/rename.jl")
Expand Down
37 changes: 37 additions & 0 deletions src/transforms/colspec.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------

# types used to select a column
const ColSelector = Union{Symbol,Integer,AbstractString}

# filter table columns using colspec
function _filter(colspec::Vector{Symbol}, cols)
# validate columns
@assert !isempty(colspec) "Invalid column selection."
@assert colspec ⊆ cols "Invalid column selection."
return colspec
end

_filter(colspec::Vector{<:AbstractString}, cols) =
_filter(Symbol.(colspec), cols)

_filter(colspec::Vector{<:Integer}, cols::Vector) =
_filter(cols[colspec], cols)

_filter(colspec::Vector{<:Integer}, cols::Tuple) =
_filter(colspec, collect(cols))

_filter(colspec::NTuple{N,<:ColSelector}, cols) where {N} =
_filter(collect(colspec), cols)

function _filter(colspec::Regex, cols::Vector)
fcols = filter(col -> occursin(colspec, String(col)), cols)
_filter(fcols, cols)
end

_filter(colspec::Regex, cols::Tuple) =
_filter(colspec, collect(cols))

_filter(::Colon, cols::Vector) = cols
_filter(::Colon, cols::Tuple) = collect(cols)
1 change: 0 additions & 1 deletion src/transforms/filter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ end
# DropMissing

const VecOrTuple{T} = Union{Vector{T}, NTuple{N, T}} where {T, N}
const ColSelector = Union{Symbol, Integer, AbstractString}

"""
DropMissing()
Expand Down
86 changes: 86 additions & 0 deletions test/transforms.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,92 @@
@testset "Transforms" begin
# using MersenneTwister for compatibility between Julia versions
rng = MersenneTwister(42)
@testset "ColSpec" begin
veccols = [:a, :b, :c, :d, :e, :f]
tupcols = (:a, :b, :c, :d, :e, :f)

# vector of symbols
colspec = [:a, :c, :e]
cols = TableTransforms._filter(colspec, veccols)
@test cols == [:a, :c, :e]
cols = TableTransforms._filter(colspec, tupcols)
@test cols == [:a, :c, :e]

# tuple of symbols
colspec = (:a, :c, :e)
cols = TableTransforms._filter(colspec, veccols)
@test cols == [:a, :c, :e]
cols = TableTransforms._filter(colspec, tupcols)
@test cols == [:a, :c, :e]

# vector of strings
colspec = ["a", "c", "e"]
cols = TableTransforms._filter(colspec, veccols)
@test cols == [:a, :c, :e]
cols = TableTransforms._filter(colspec, tupcols)
@test cols == [:a, :c, :e]

# tuple of strings
colspec = ("a", "c", "e")
cols = TableTransforms._filter(colspec, veccols)
@test cols == [:a, :c, :e]
cols = TableTransforms._filter(colspec, tupcols)
@test cols == [:a, :c, :e]

# vector of integers
colspec = [1, 3, 5]
cols = TableTransforms._filter(colspec, veccols)
@test cols == [:a, :c, :e]
cols = TableTransforms._filter(colspec, tupcols)
@test cols == [:a, :c, :e]

# tuple of integers
colspec = (1, 3, 5)
cols = TableTransforms._filter(colspec, veccols)
@test cols == [:a, :c, :e]
cols = TableTransforms._filter(colspec, tupcols)
@test cols == [:a, :c, :e]

# regex
colspec = r"[ace]"
cols = TableTransforms._filter(colspec, veccols)
@test cols == [:a, :c, :e]
cols = TableTransforms._filter(colspec, tupcols)
@test cols == [:a, :c, :e]

# colon
cols = TableTransforms._filter(:, veccols)
@test cols == [:a, :b, :c, :d, :e, :f]
cols = TableTransforms._filter(:, tupcols)
@test cols == [:a, :b, :c, :d, :e, :f]

# throws
@test_throws AssertionError TableTransforms._filter(r"x", veccols)
@test_throws AssertionError TableTransforms._filter(r"x", tupcols)
@test_throws AssertionError TableTransforms._filter(String[], veccols)
@test_throws AssertionError TableTransforms._filter(String[], tupcols)
@test_throws AssertionError TableTransforms._filter(Symbol[], veccols)
@test_throws AssertionError TableTransforms._filter(Symbol[], tupcols)

# type stability
@inferred TableTransforms._filter([:a, :b], veccols)
@inferred TableTransforms._filter([:a, :b], tupcols)
@inferred TableTransforms._filter((:a, :b), veccols)
@inferred TableTransforms._filter((:a, :b), tupcols)
@inferred TableTransforms._filter(["a", "b"], veccols)
@inferred TableTransforms._filter(["a", "b"], tupcols)
@inferred TableTransforms._filter(("a", "b"), veccols)
@inferred TableTransforms._filter(("a", "b"), tupcols)
@inferred TableTransforms._filter([1, 2], veccols)
@inferred TableTransforms._filter([1, 2], tupcols)
@inferred TableTransforms._filter((1, 2), veccols)
@inferred TableTransforms._filter((1, 2), tupcols)
@inferred TableTransforms._filter(r"[ab]", veccols)
@inferred TableTransforms._filter(r"[ab]", tupcols)
@inferred TableTransforms._filter(:, veccols)
@inferred TableTransforms._filter(:, tupcols)
end

@testset "Select" begin
a = rand(4000)
b = rand(4000)
Expand Down