/
aggregate.jl
72 lines (56 loc) · 2.04 KB
/
aggregate.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Aggregate functions.
mutable struct AggregateClause <: AbstractSQLClause
name::Symbol
args::Vector{SQLClause}
filter::Union{SQLClause, Nothing}
over::Union{SQLClause, Nothing}
AggregateClause(;
name::Union{Symbol, AbstractString},
args = SQLClause[],
filter = nothing,
over = nothing) =
new(Symbol(name), args, filter, over)
end
AggregateClause(name; args = SQLClause[], filter = nothing, over = nothing) =
AggregateClause(name = name, args = args, filter = filter, over = over)
AggregateClause(name, args...; filter = nothing, over = nothing) =
AggregateClause(name, args = SQLClause[args...], filter = filter, over = over)
"""
AGG(; name, args = [], filter = nothing, over = nothing)
AGG(name; args = [], filter = nothing, over = nothing)
AGG(name, args...; filter = nothing, over = nothing)
An application of an aggregate function.
# Examples
```jldoctest
julia> c = AGG(:max, :year_of_birth);
julia> print(render(c))
max("year_of_birth")
```
```jldoctest
julia> c = AGG(:count, filter = FUN(">", :year_of_birth, 1970));
julia> print(render(c))
(count(*) FILTER (WHERE ("year_of_birth" > 1970)))
```
```jldoctest
julia> c = AGG(:row_number, over = PARTITION(:year_of_birth));
julia> print(render(c))
(row_number() OVER (PARTITION BY "year_of_birth"))
```
"""
AGG(args...; kws...) =
AggregateClause(args...; kws...) |> SQLClause
dissect(scr::Symbol, ::typeof(AGG), pats::Vector{Any}) =
dissect(scr, AggregateClause, pats)
function PrettyPrinting.quoteof(c::AggregateClause, ctx::QuoteContext)
ex = Expr(:call, nameof(AGG), string(c.name))
append!(ex.args, quoteof(c.args, ctx))
if c.filter !== nothing
push!(ex.args, Expr(:kw, :filter, quoteof(c.filter, ctx)))
end
if c.over !== nothing
push!(ex.args, Expr(:kw, :over, quoteof(c.over, ctx)))
end
ex
end
rebase(c::AggregateClause, c′) =
AggregateClause(name = c.name, args = c.args, filter = c.filter, over = rebase(c.over, c′))