createFromAggregateQuery method
Creates the pipeline that computes the aggregations of the classic
aggregateQuery (query.count(), query.aggregate(...)).
The single result has one field per aggregation, named after the alias
given to aggregateWithAliases, or count, sum_<field> and
average_<field> without aliases.
Implementation
Pipeline createFromAggregateQuery(AggregateQuery aggregateQuery) {
final Map<String, AggregateField> fields =
aggregateQuery.aliases ??
<String, AggregateField>{
for (final AggregateField field in aggregateQuery.aggregateFields)
field.alias: field,
};
AggregateFunction convert(AggregateField aggregate) {
if (aggregate is CountAggregateField) {
return AggregateFunction('count');
}
final Field field = Field(
model.FieldPath.fromServerFormat(aggregate.fieldPath),
);
return aggregate is SumAggregateField ? field.sum() : field.average();
}
return createFrom(aggregateQuery.query).aggregate(<AliasedAggregate>[
for (final MapEntry<String, AggregateField> entry in fields.entries)
// The server reads an alias as a field path; this keeps a name with
// dots or other special characters one field.
convert(
entry.value,
).as(model.FieldPath.fromSingleSegment(entry.key).canonicalString),
]);
}