Path: redb/redb.Core/Query/Grouping/ArrayGroupQuery.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Query/Grouping/ArrayGroupQuery.cs
----
using System;
using System.Collections.Generic;
using redb.Core.Query.Aggregation;
using redb.Core.Query.QueryExpressions;
using redb.Core.Query.Window;
namespace redb.Core.Query.Grouping;
///
/// A grouping over the elements of an array, as every provider receives it: what to group, how to aggregate, which
/// objects take part and, for WithWindow, the window over the groups. One request serves execution and SQL
/// preview, so the previewed SQL is the SQL that runs.
///
public sealed class ArrayGroupQuery
{
/// Scheme of the objects that own the array.
public required long SchemeId { get; init; }
///
/// Path to the array whose elements are grouped, through classes and arrays, joined with dots:
/// Contacts, Contacts.Metadata, HomeAddress.Building.Amenities. Keys and aggregates read the
/// elements of the last array in the path.
///
public required string ArrayPath { get; init; }
///
/// Group keys: paths of element fields (Type, Address.City) or
/// for the element of an array of simple values. Never empty.
///
public required IReadOnlyList GroupFields { get; init; }
/// Aggregates over the elements of each group, with their aliases.
public required IReadOnlyList Aggregations { get; init; }
/// Condition on the owning objects (the query's Where), or null.
public FilterExpression? Filter { get; init; }
///
/// Condition on the owning objects in the facet JSON of the free provider, or null. Used by the free tree path
/// to pass the objects of a subtree; the Pro provider refuses a non-empty value.
///
public string? FilterJson { get; init; }
/// HAVING in the JSON of HavingPredicateParser, or null.
public string? HavingJson { get; init; }
/// Window over the groups (WithWindow), or null.
public ArrayGroupWindow? Window { get; init; }
///
/// Refuses a request no generator can answer: no array path, no key, a key or aggregate without a path or alias,
/// a window without functions. Every provider calls it first, so none of them answers such a request with nothing.
///
public void EnsureValid()
{
if (string.IsNullOrWhiteSpace(ArrayPath))
throw new ArgumentException("An array grouping needs the path of the array.", nameof(ArrayPath));
if (GroupFields.Count == 0)
throw new ArgumentException("An array grouping needs at least one key.", nameof(GroupFields));
foreach (var g in GroupFields)
{
if (string.IsNullOrWhiteSpace(g.FieldPath))
throw new ArgumentException("A key of an array grouping has no field path.", nameof(GroupFields));
if (string.IsNullOrWhiteSpace(g.Alias))
throw new ArgumentException($"The key '{g.FieldPath}' of an array grouping has no alias.", nameof(GroupFields));
}
foreach (var a in Aggregations)
{
if (string.IsNullOrWhiteSpace(a.FieldPath))
throw new ArgumentException($"The aggregate '{a.Alias}' of an array grouping has no field path.", nameof(Aggregations));
if (string.IsNullOrWhiteSpace(a.Alias))
throw new ArgumentException($"The aggregate {a.Function}({a.FieldPath}) of an array grouping has no alias.", nameof(Aggregations));
if (a.ConditionJson != null && a.Function != AggregateFunction.Count)
throw new ArgumentException($"Only a count takes a condition; {a.Function}({a.FieldPath}) has one.", nameof(Aggregations));
}
if (Window != null && Window.Functions.Count == 0)
throw new ArgumentException("A window over an array grouping needs at least one window function.", nameof(Window));
}
}
///
/// Window functions over the rows of an array grouping, in the form the grouped window of GroupBy uses.
///
public sealed class ArrayGroupWindow
{
/// Window functions with their aliases. Never empty.
public required IReadOnlyList Functions { get; init; }
/// PARTITION BY: group aliases or aggregate references.
public required IReadOnlyList PartitionBy { get; init; }
/// ORDER BY: group aliases or aggregate references.
public required IReadOnlyList OrderBy { get; init; }
}