Interface Groups
- All Known Subinterfaces:
Groups.GroupingSetsBuilder
PipelineStage.aggregateByGroups(Function, Function)
A grouping is a set of columns to aggregate on. With several groupings, one aggregation stage computes the aggregates for each grouping, instead of one pipeline per grouping combined with a union.
Get an instance with QueryApi.groups(), that is api.queryApi().groups(). Define the groupings
with groupingSets(List[]), cube(List[]) or rollup(List[]), and end the
specification with Groups.GroupingSetsBuilder.distinct() or Groups.GroupingSetsBuilder.notDistinct().
For example, this pipeline computes the price sum by customerGroup and productGroup, by customerGroup only, and over all rows:
def qapi = api.queryApi()
def dm = qapi.tables().datamart("Sales")
qapi.source(dm, [dm.customerGroup, dm.productGroup, dm.price])
.aggregateByGroups(
{ prev -> qapi.groups().rollup([prev.customerGroup, prev.productGroup]).notDistinct() },
{ prev -> [prev.customerGroup, prev.productGroup, qapi.exprs().sum(prev.price).as("priceSum")] }
)
.stream { it.toList() }
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceRepresents a list of groupings to be used inPipelineStage.aggregateByGroups(Function, Function)static interface -
Method Summary
Modifier and TypeMethodDescriptioncube(List<Expression>... groups) Builds the cube list of grouping from a given list of expressions or sub lists of expressions.groupingSets(List<Expression>... groups) Builds a representation of a list of grouping to aggregate on.rollup(List<Expression>... groups) Builds the rollup grouping list from a given list of expressions or sub lists of expressions.
-
Method Details
-
cube
Builds the cube list of grouping from a given list of expressions or sub lists of expressions.It results to the given list plus all of its possible subsets (i.e. the power set).
For example,
If you provide several lists, then the cube will be computed over the listsqapi.groups().cube([a, b, c]) // is equivalent to qapi.groups().groupingSets( [a, b, c], [a, b], [a, c], [a], [b, c], [b], [c], [] )For example,
qapi.groups().cube([a, b], [c, d]) // is equivalent to qapi.groups().groupingSets( [a, b, c , d], [a, b], [c, d], [] )- Parameters:
groups- list of column groups to cube over; must not be null or empty, and no group may be empty- Returns:
- the current groups builder
- Since:
- 16.3.12 - Black Cat, 17.0.3 - Paloma
- See Also:
-
rollup
Builds the rollup grouping list from a given list of expressions or sub lists of expressions.It results to the given list of expressions and all prefixes of the list including the empty list. For example,
If you provide several lists, then the rollup will be computed over the listsqapi.groups().rollup([a, b, c]) // is equivalent to qapi.groups().groupingSets( [a, b, c], [a, b], [a], [] )For example,
qapi.groups().rollup([a], [b, c], [d]) // is equivalent to qapi.groups().groupingSets( [a, b, c , d], [a, b, c], [a], [] )- Parameters:
groups- list of column groups to roll up; must not be null or empty, and no group may be empty- Returns:
- the current groups builder
- Since:
- 16.3.12 - Black Cat, 17.0.3 - Paloma
- See Also:
-
groupingSets
Builds a representation of a list of grouping to aggregate on.Each sublist of groupingSets may specify zero or more expressions and is interpreted the same way as though it were directly in a
PipelineStage.aggregateBy(Function, Function)stage.An empty grouping set means that all rows are aggregated down to a single group.
For example, this
byfunction ofPipelineStage.aggregateByGroups(Function, Function)aggregates by customerGroup and productGroup, by productGroup only, and over all rows:{ prev -> qapi.groups() .groupingSets([prev.customerGroup, prev.productGroup], [prev.productGroup], []) .notDistinct() }- Parameters:
groups- explicit list of grouping sets; must not be null or empty- Returns:
- a builder to choose
Groups.GroupingSetsBuilder.distinct()orGroups.GroupingSetsBuilder.notDistinct() - Since:
- 16.3.12 - Black Cat, 17.0.3 - Paloma
- See Also:
-