Skip to content

Commit

Permalink
rename parameter grouping_sets -> sets
Browse files Browse the repository at this point in the history
  • Loading branch information
xitology committed Mar 8, 2024
1 parent e73490d commit 109c22d
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 57 deletions.
16 changes: 8 additions & 8 deletions docs/src/test/clauses.md
Original file line number Diff line number Diff line change
Expand Up @@ -1133,8 +1133,8 @@ rendered.

`GROUP` can accept the grouping mode or a vector of grouping sets.

c = FROM(:person) |> GROUP(:year_of_birth, grouping_sets = :ROLLUP)
#-> (…) |> GROUP(…, grouping_sets = :ROLLUP)
c = FROM(:person) |> GROUP(:year_of_birth, sets = :ROLLUP)
#-> (…) |> GROUP(…, sets = :ROLLUP)

print(render(c |> SELECT(:year_of_birth, AGG(:count))))
#=>
Expand All @@ -1145,8 +1145,8 @@ rendered.
GROUP BY ROLLUP("year_of_birth")
=#

c = FROM(:person) |> GROUP(:year_of_birth, grouping_sets = :CUBE)
#-> (…) |> GROUP(…, grouping_sets = :CUBE)
c = FROM(:person) |> GROUP(:year_of_birth, sets = :CUBE)
#-> (…) |> GROUP(…, sets = :CUBE)

print(render(c |> SELECT(:year_of_birth, AGG(:count))))
#=>
Expand All @@ -1157,8 +1157,8 @@ rendered.
GROUP BY CUBE("year_of_birth")
=#

c = FROM(:person) |> GROUP(:year_of_birth, grouping_sets = [[1], Int[]])
#-> (…) |> GROUP(…, grouping_sets = [[1], Int64[]])
c = FROM(:person) |> GROUP(:year_of_birth, sets = [[1], Int[]])
#-> (…) |> GROUP(…, sets = [[1], Int64[]])

print(render(c |> SELECT(:year_of_birth, AGG(:count))))
#=>
Expand All @@ -1171,10 +1171,10 @@ rendered.

`GROUP` raises an error when the vector of grouping sets is out of bounds.

FROM(:person) |> GROUP(:year_of_birth, grouping_sets = [[1, 2], [1], Int[]])
FROM(:person) |> GROUP(:year_of_birth, sets = [[1, 2], [1], Int[]])
#=>
ERROR: DomainError with [[1, 2], [1], Int64[]]:
grouping_sets is out of bounds
sets are out of bounds
=#


Expand Down
12 changes: 6 additions & 6 deletions docs/src/test/nodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2505,14 +2505,14 @@ downstream.
`Group` allows specifying the grouping sets.

q = From(person) |>
Group(Get.year_of_birth, grouping_sets = :cube)
Group(Get.year_of_birth, sets = :cube)
Define(Agg.count())

display(q)
#=>
let person = SQLTable(:person, …),
q1 = From(person),
q2 = q1 |> Group(Get.year_of_birth, grouping_sets = :CUBE)
q2 = q1 |> Group(Get.year_of_birth, sets = :CUBE)
q2
end
=#
Expand All @@ -2525,14 +2525,14 @@ downstream.
=#

q = From(person) |>
Group(Get.year_of_birth, grouping_sets = [[1], Int[]])
Group(Get.year_of_birth, sets = [[1], Int[]])
Define(Agg.count())

display(q)
#=>
let person = SQLTable(:person, …),
q1 = From(person),
q2 = q1 |> Group(Get.year_of_birth, grouping_sets = [[1], []])
q2 = q1 |> Group(Get.year_of_birth, sets = [[1], []])
q2
end
=#
Expand All @@ -2547,10 +2547,10 @@ downstream.
`Group` complains about out-of-bound grouping sets.

From(person) |>
Group(Get.year_of_birth, grouping_sets = [[1, 2], [1], Int[]])
Group(Get.year_of_birth, sets = [[1, 2], [1], Int[]])
#=>
ERROR: DomainError with [[1, 2], [1], Int64[]]:
grouping_sets is out of bounds
sets are out of bounds
=#

`Group` allows specifying the name of a group field.
Expand Down
28 changes: 14 additions & 14 deletions src/clauses/group.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ import .GROUPING_MODE.GroupingMode
mutable struct GroupClause <: AbstractSQLClause
over::Union{SQLClause, Nothing}
by::Vector{SQLClause}
grouping_sets::Union{Vector{Vector{Int}}, GroupingMode, Nothing}
sets::Union{Vector{Vector{Int}}, GroupingMode, Nothing}

function GroupClause(;
over = nothing,
by = SQLClause[],
grouping_sets = nothing)
c = new(over, by, grouping_sets isa Symbol ? convert(GroupingMode, grouping_sets) : grouping_sets)
gs = c.grouping_sets
if gs isa Vector{Vector{Int}} && !checkbounds(Bool, c.by, gs)
throw(DomainError(gs, "grouping_sets is out of bounds"))
sets = nothing)
c = new(over, by, sets isa Symbol ? convert(GroupingMode, sets) : sets)
s = c.sets
if s isa Vector{Vector{Int}} && !checkbounds(Bool, c.by, s)
throw(DomainError(s, "sets are out of bounds"))
end
c
end
end

GroupClause(by...; over = nothing, grouping_sets = nothing) =
GroupClause(over = over, by = SQLClause[by...], grouping_sets = grouping_sets)
GroupClause(by...; over = nothing, sets = nothing) =
GroupClause(over = over, by = SQLClause[by...], sets = sets)

"""
GROUP(; over = nothing, by = [], grouping_sets = nothing)
GROUP(by...; over = nothing, grouping_sets = nothing)
GROUP(; over = nothing, by = [], sets = nothing)
GROUP(by...; over = nothing, sets = nothing)
A `GROUP BY` clause.
Expand Down Expand Up @@ -69,9 +69,9 @@ dissect(scr::Symbol, ::typeof(GROUP), pats::Vector{Any}) =
function PrettyPrinting.quoteof(c::GroupClause, ctx::QuoteContext)
ex = Expr(:call, nameof(GROUP))
append!(ex.args, quoteof(c.by, ctx))
gs = c.grouping_sets
if gs !== nothing
push!(ex.args, Expr(:kw, :grouping_sets, gs isa GroupingMode ? QuoteNode(Symbol(gs)) : gs))
s = c.sets
if s !== nothing
push!(ex.args, Expr(:kw, :sets, s isa GroupingMode ? QuoteNode(Symbol(s)) : s))
end
if c.over !== nothing
ex = Expr(:call, :|>, quoteof(c.over, ctx), ex)
Expand All @@ -80,5 +80,5 @@ function PrettyPrinting.quoteof(c::GroupClause, ctx::QuoteContext)
end

rebase(c::GroupClause, c′) =
GroupClause(over = rebase(c.over, c′), by = c.by, grouping_sets = c.grouping_sets)
GroupClause(over = rebase(c.over, c′), by = c.by, sets = c.sets)

6 changes: 3 additions & 3 deletions src/link.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ end
function dismantle(n::GroupNode, ctx)
over′ = dismantle(n.over, ctx)
by′ = dismantle_scalar(n.by, ctx)
Group(over = over′, by = by′, grouping_sets = n.grouping_sets, name = n.name, label_map = n.label_map)
Group(over = over′, by = by′, sets = n.sets, name = n.name, label_map = n.label_map)
end

function dismantle(n::IterateNode, ctx)
Expand Down Expand Up @@ -290,7 +290,7 @@ function link(n::GroupNode, ctx)
# To avoid duplicate SQL, they must be evaluated in a nested subquery.
refs = SQLNode[]
append!(refs, n.by)
if n.grouping_sets !== nothing
if n.sets !== nothing
# Force evaluation in a nested subquery.
append!(refs, n.by)
end
Expand All @@ -315,7 +315,7 @@ function link(n::GroupNode, ctx)
over = Padding(over = over)
end
over′ = Linked(refs, 0, over = link(over, ctx, refs))
Group(over = over′, by = n.by, grouping_sets = n.grouping_sets, name = n.name, label_map = n.label_map)
Group(over = over′, by = n.by, sets = n.sets, name = n.name, label_map = n.label_map)
end

function link(n::IterateNode, ctx)
Expand Down
30 changes: 15 additions & 15 deletions src/nodes/group.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
mutable struct GroupNode <: TabularNode
over::Union{SQLNode, Nothing}
by::Vector{SQLNode}
grouping_sets::Union{Vector{Vector{Int}}, GroupingMode, Nothing}
sets::Union{Vector{Vector{Int}}, GroupingMode, Nothing}
name::Union{Symbol, Nothing}
label_map::OrderedDict{Symbol, Int}

function GroupNode(;
over = nothing,
by = SQLNode[],
grouping_sets = nothing,
sets = nothing,
name::Union{Symbol, AbstractString, Nothing} = nothing,
label_map = nothing)
n = new(
over,
by,
grouping_sets isa Symbol ? convert(GroupingMode, grouping_sets) : grouping_sets,
sets isa Symbol ? convert(GroupingMode, sets) : sets,
name !== nothing ? Symbol(name) : nothing,
label_map !== nothing ? label_map : OrderedDict{Symbol, Int}())
gs = n.grouping_sets
if gs isa Vector{Vector{Int}} && !checkbounds(Bool, n.by, gs)
throw(DomainError(gs, "grouping_sets is out of bounds"))
s = n.sets
if s isa Vector{Vector{Int}} && !checkbounds(Bool, n.by, s)
throw(DomainError(s, "sets are out of bounds"))
end
if label_map === nothing
populate_label_map!(n, n.by, n.label_map, n.name)
Expand All @@ -30,19 +30,19 @@ mutable struct GroupNode <: TabularNode
end
end

GroupNode(by...; over = nothing, grouping_sets = nothing, name = nothing) =
GroupNode(over = over, by = SQLNode[by...], grouping_sets = grouping_sets, name = name)
GroupNode(by...; over = nothing, sets = nothing, name = nothing) =
GroupNode(over = over, by = SQLNode[by...], sets = sets, name = name)

"""
Group(; over, by = [], grouping_sets = grouping_sets, name = nothing)
Group(by...; over, grouping_sets = grouping_sets, name = nothing)
Group(; over, by = [], sets = sets, name = nothing)
Group(by...; over, sets = sets, name = nothing)
The `Group` node summarizes the input dataset.
Specifically, `Group` outputs all unique values of the given grouping key.
This key partitions the input rows into disjoint groups that are summarized
by aggregate functions [`Agg`](@ref) applied to the output of `Group`. The
parameter `grouping_sets` customizes the grouping sets. An optional parameter
parameter `sets` customizes the grouping sets. An optional parameter
`name` specifies the field to hold the group.
The `Group` node is translated to a SQL query with a `GROUP BY` clause:
Expand Down Expand Up @@ -108,7 +108,7 @@ GROUP BY "person_1"."year_of_birth"
julia> person = SQLTable(:person, columns = [:person_id, :year_of_birth]);
julia> q = From(:person) |>
Group(Get.year_of_birth, grouping_sets = :cube) |>
Group(Get.year_of_birth, sets = :cube) |>
Select(Get.year_of_birth, Agg.count());
julia> print(render(q, tables = [person]))
Expand Down Expand Up @@ -142,9 +142,9 @@ dissect(scr::Symbol, ::typeof(Group), pats::Vector{Any}) =

function PrettyPrinting.quoteof(n::GroupNode, ctx::QuoteContext)
ex = Expr(:call, nameof(Group), quoteof(n.by, ctx)...)
gs = n.grouping_sets
if gs !== nothing
push!(ex.args, Expr(:kw, :grouping_sets, gs isa GroupingMode ? QuoteNode(Symbol(gs)) : gs))
s = n.sets
if s !== nothing
push!(ex.args, Expr(:kw, :sets, s isa GroupingMode ? QuoteNode(Symbol(s)) : s))
end
if n.name !== nothing
push!(ex.args, Expr(:kw, :name, QuoteNode(n.name)))
Expand Down
2 changes: 1 addition & 1 deletion src/resolve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ function resolve(n::GroupNode, ctx)
fields[n.name] = RowType(FieldTypeMap(), group)
group = EmptyType()
end
n′ = Group(over = over′, by = by′, grouping_sets = n.grouping_sets, label_map = n.label_map)
n′ = Group(over = over′, by = by′, sets = n.sets, label_map = n.label_map)
Resolved(RowType(fields, group), over = n′)
end

Expand Down
14 changes: 7 additions & 7 deletions src/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -538,22 +538,22 @@ function serialize!(c::GroupClause, ctx)
!isempty(c.by) || return
newline(ctx)
print(ctx, "GROUP BY")
grouping_sets = c.grouping_sets
if grouping_sets !== nothing
if grouping_sets isa GroupingMode
if grouping_sets == GROUPING_MODE.ROLLUP
sets = c.sets
if sets !== nothing
if sets isa GroupingMode
if sets == GROUPING_MODE.ROLLUP
print(ctx, " ROLLUP(")
elseif grouping_sets == GROUPING_MODE.CUBE
elseif sets == GROUPING_MODE.CUBE
print(ctx, " CUBE(")
else
throw(DomainError(grouping_sets))
throw(DomainError(sets))
end
serialize!(c.by, ctx)
print(ctx, ')')
else
print(ctx, " GROUPING SETS(")
first = true
for set in grouping_sets
for set in sets
if !first
print(ctx, ", ")
else
Expand Down
6 changes: 3 additions & 3 deletions src/translate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -618,15 +618,15 @@ function assemble(n::GroupNode, ctx)
push!(trns, ref => translate(over, ctx, subs))
end
end
if !has_aggregates && n.grouping_sets === nothing
if !has_aggregates && n.sets === nothing
for name in keys(n.label_map)
push!(trns, Get(name = name) => by[n.label_map[name]])
end
end
repl, cols = make_repl_cols(trns)
@assert !isempty(cols)
if has_aggregates || n.grouping_sets !== nothing
c = GROUP(over = tail, by = by, grouping_sets = n.grouping_sets)
if has_aggregates || n.sets !== nothing
c = GROUP(over = tail, by = by, sets = n.sets)
else
args = complete(cols)
c = SELECT(over = tail, distinct = true, args = args)
Expand Down

0 comments on commit 109c22d

Please sign in to comment.