Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gather macro #213

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/Query.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using QueryOperators
export @from, @query, @count, Grouping, key

export @map, @filter, @groupby, @orderby, @orderby_descending, @unique,
@thenby, @thenby_descending, @groupjoin, @join, @mapmany, @take, @drop
@thenby, @thenby_descending, @groupjoin, @join, @mapmany, @take, @drop, @gather

export @select, @rename, @mutate

Expand Down
13 changes: 13 additions & 0 deletions src/standalone_query_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ macro map(f)
helper_namedtuples_replacement
end

macro gather(args...)
parsedArgs = ()
for arg in args
if typeof(arg) == Expr
m1 = match(r"^-:(.+)", string(arg))
parsedArgs = (parsedArgs..., :(QueryOperators.Not($(QuoteNode(Symbol(m1[1]))))))
else
parsedArgs = (parsedArgs..., arg)
end
end
:( i -> QueryOperators.gather(QueryOperators.query(i), $(parsedArgs...)))
end

macro mapmany(source, collectionSelector,resultSelector)
collectionSelector_as_anonym_func = helper_replace_anon_func_syntax(collectionSelector)
resultSelector_as_anonym_func = helper_replace_anon_func_syntax(resultSelector)
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ q = collect(@map(source_df, i->i.children))

include("test_dplyr-syntax.jl")
include("test_pipesyntax.jl")
include("test_standalone.jl")
include("test_macros.jl")

end
18 changes: 18 additions & 0 deletions test/test_standalone.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Query
using QueryOperators
using DataFrames
using Test

Expand Down Expand Up @@ -44,6 +45,23 @@ end
@test df2[:c] == ["b","c"]
end

@testset "@gather operator" begin
source_gather = DataFrame(Year=[2017,2018,2019], US=[1,2,3], EU=[4,5,6], CN=[7,8,9])
@test source_gather |> @gather(:US, :EU, :CN) |> collect ==
[
(Year = 2017, key = :US, value = 1),
(Year = 2017, key = :EU, value = 4),
(Year = 2017, key = :CN, value = 7),
(Year = 2018, key = :US, value = 2),
(Year = 2018, key = :EU, value = 5),
(Year = 2018, key = :CN, value = 8),
(Year = 2019, key = :US, value = 3),
(Year = 2019, key = :EU, value = 6),
(Year = 2019, key = :CN, value = 9)
]
@test eltype(source_gather |> @gather(:US, :EU, :CN)) == NamedTuple{(:Year, :key, :value),Tuple{Int, Symbol, Int}}
end

@testset "@unique operator" begin
df = DataFrame(a=[1,2,1], b=[3.,3.,3.])

Expand Down