diff --git a/src/Query.jl b/src/Query.jl index 780e6c10..d6ed6e02 100644 --- a/src/Query.jl +++ b/src/Query.jl @@ -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 diff --git a/src/standalone_query_macros.jl b/src/standalone_query_macros.jl index 0655e8fe..0063b8db 100644 --- a/src/standalone_query_macros.jl +++ b/src/standalone_query_macros.jl @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index f1750225..69bf976c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 diff --git a/test/test_standalone.jl b/test/test_standalone.jl index 4ed987ac..56711f79 100644 --- a/test/test_standalone.jl +++ b/test/test_standalone.jl @@ -1,4 +1,5 @@ using Query +using QueryOperators using DataFrames using Test @@ -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.])