Path: redb/redb.Core/Query/Grouping/RedbArrayGroupedQueryable.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/RedbArrayGroupedQueryable.cs ---- using System.Linq.Expressions; using System.Text.Json; using redb.Core.Models.Contracts; using redb.Core.Models.Entities; using redb.Core.Query.Aggregation; using redb.Core.Query.Base; using redb.Core.Query.QueryExpressions; using redb.Core.Query.Utils; using redb.Core.Query.Window; namespace redb.Core.Query.Grouping; /// /// Runs an on a provider: the flat one, or the tree one with its subtree. /// internal interface IArrayGroupRunner { /// Name of the provider for messages. string ProviderName { get; } Task ExecuteAsync(ArrayGroupQuery query, CancellationToken cancellationToken); Task GetSqlAsync(ArrayGroupQuery query, CancellationToken cancellationToken); /// The list provider the list item keys are read through (ListItemGroupKeys). redb.Core.Providers.IListProvider? ListItems { get; } } internal sealed class FlatArrayGroupRunner : IArrayGroupRunner { private readonly IRedbQueryProvider _provider; public FlatArrayGroupRunner(IRedbQueryProvider provider) => _provider = provider; public string ProviderName => _provider.GetType().Name; public redb.Core.Providers.IListProvider? ListItems => (_provider as IListItemKeySource)?.ListItems; public Task ExecuteAsync(ArrayGroupQuery query, CancellationToken cancellationToken) => _provider.ExecuteArrayGroupQueryAsync(query, cancellationToken); public Task GetSqlAsync(ArrayGroupQuery query, CancellationToken cancellationToken) => _provider.GetArrayGroupQuerySqlAsync(query, cancellationToken); } internal sealed class TreeArrayGroupRunner : IArrayGroupRunner where TProps : class, new() { private readonly ITreeQueryProvider _provider; private readonly TreeQueryContext _context; public TreeArrayGroupRunner(ITreeQueryProvider provider, TreeQueryContext context) { _provider = provider; _context = context; } public string ProviderName => _provider.GetType().Name; public redb.Core.Providers.IListProvider? ListItems => (_provider as IListItemKeySource)?.ListItems; public Task ExecuteAsync(ArrayGroupQuery query, CancellationToken cancellationToken) => _provider.ExecuteTreeArrayGroupQueryAsync(_context, query, cancellationToken); public Task GetSqlAsync(ArrayGroupQuery query, CancellationToken cancellationToken) => _provider.GetTreeArrayGroupQuerySqlAsync(_context, query, cancellationToken); } /// /// Grouping by the elements of an array: GroupByArray(e => e.Contacts, c => c.Type), an inner array /// (e => e.Contacts.SelectMany(c => c.Metadata)), an array of simple values (e => e.Skills, s => s). /// Every form it cannot translate is refused with . /// public class RedbArrayGroupedQueryable : IRedbGroupedQueryable where TProps : class, new() { private const string OwnerApi = "GroupByArray"; private readonly IArrayGroupRunner _runner; private readonly long _schemeId; private readonly FilterExpression? _filter; private readonly LambdaExpression _arraySelector; private readonly LambdaExpression _keySelector; private readonly List _havingPredicates = new(); private readonly GroupItemKind _kind; /// /// Flat grouping; is not used, the condition comes from . /// public RedbArrayGroupedQueryable( IRedbQueryProvider provider, long schemeId, string? filterJson, FilterExpression? filter, Expression>> arraySelector, Expression> keySelector) : this(new FlatArrayGroupRunner(provider), schemeId, filter, arraySelector, keySelector) { } internal RedbArrayGroupedQueryable( IArrayGroupRunner runner, long schemeId, FilterExpression? filter, LambdaExpression arraySelector, LambdaExpression keySelector) { _runner = runner; _schemeId = schemeId; _filter = filter; _arraySelector = arraySelector; _keySelector = keySelector; _kind = ResolveItemKind(); // Parse now: a form that cannot be translated is refused where it is written, not at execution. _ = ParseArrayPath(_arraySelector); _ = ParseGroupFields(alias: null); } public async Task> SelectAsync( Expression, TResult>> selector, CancellationToken cancellationToken = default) { var query = BuildQuery(selector, window: null); var json = await _runner.ExecuteAsync(query, cancellationToken); var rows = ArrayGroupMaterializer.Materialize(json, selector, query.GroupFields, _runner.ProviderName, OwnerApi); await ListItemGroupKeys.FillAsync(rows, _runner.ListItems, OwnerApi, cancellationToken); return rows; } public async Task CountAsync(CancellationToken cancellationToken = default) { var query = new ArrayGroupQuery { SchemeId = _schemeId, ArrayPath = ParseArrayPath(_arraySelector), GroupFields = ParseGroupFields(alias: null), Aggregations = new[] { new AggregateRequest { FieldPath = "*", Function = AggregateFunction.Count, Alias = "cnt" } }, Filter = _filter, HavingJson = BuildHavingJson() }; using var json = await _runner.ExecuteAsync(query, cancellationToken); return ArrayGroupMaterializer.RequireArray(json, _runner.ProviderName, OwnerApi).GetArrayLength(); } public Task ToSqlStringAsync( Expression, TResult>> selector, CancellationToken cancellationToken = default) => _runner.GetSqlAsync(BuildQuery(selector, window: null), cancellationToken); public IGroupedWindowedQueryable WithWindow(Action> windowConfig) { var windowSpec = new GroupedWindowSpec(); windowConfig(windowSpec); return new GroupedWindowedQueryable(new ArrayWindowSource(this), windowSpec); } /// public IRedbGroupedQueryable Having(Expression, bool>> predicate) { if (predicate is null) throw new ArgumentNullException(nameof(predicate)); // G-4: a copying builder - a branch does not leak into its sibling. var copy = new RedbArrayGroupedQueryable(_runner, _schemeId, _filter, _arraySelector, _keySelector); copy._havingPredicates.AddRange(_havingPredicates); copy._havingPredicates.Add(predicate); return copy; } // ---------------------------------------------------------------------------------------------------------------- private ArrayGroupQuery BuildQuery(LambdaExpression selector, ArrayGroupWindow? window) => new() { SchemeId = _schemeId, ArrayPath = ParseArrayPath(_arraySelector), GroupFields = ParseGroupFields(KeyAliasFromSelector(selector)), Aggregations = ParseAggregations(selector), Filter = _filter, HavingJson = BuildHavingJson(), Window = window }; private string? BuildHavingJson() { if (_havingPredicates.Count == 0) return null; var param = Expression.Parameter(typeof(IRedbGrouping), "g"); Expression? body = null; foreach (var p in _havingPredicates) { var next = new ParameterReplacer(p.Parameters[0], param).Visit(p.Body)!; body = body == null ? next : Expression.AndAlso(body, next); } var combined = Expression.Lambda, bool>>(body!, param); return HavingPredicateParser.ToJson(combined, _kind); } private sealed class ParameterReplacer : ExpressionVisitor { private readonly ParameterExpression _from; private readonly ParameterExpression _to; public ParameterReplacer(ParameterExpression from, ParameterExpression to) { _from = from; _to = to; } protected override Expression VisitParameter(ParameterExpression node) => node == _from ? _to : base.VisitParameter(node); } /// /// What an element of the array is. A class other than string is an element with fields; a list item and an /// object reference are refused by name; every other type is a simple value read from the element row. /// private static GroupItemKind ResolveItemKind() { var t = Nullable.GetUnderlyingType(typeof(TItem)) ?? typeof(TItem); if (t == typeof(string)) return GroupItemKind.ArrayValueElement; if (typeof(IRedbListItem).IsAssignableFrom(t)) throw new NotSupportedException( $"{OwnerApi} over an array of {t.Name} is not supported: group the objects by the list item instead " + "(GroupBy(e => e.Field)), or keep the item id in a long field."); if (typeof(IRedbObject).IsAssignableFrom(t)) throw new NotSupportedException( $"{OwnerApi} over an array of object references ({t.Name}) is not supported: group the referenced objects " + "by their own fields."); if (t == typeof(byte[])) throw new NotSupportedException($"{OwnerApi} over an array of byte[] is not supported."); if (t.IsClass || t.IsInterface) return GroupItemKind.ArrayClassElement; return GroupItemKind.ArrayValueElement; } /// /// The array path: a chain of fields (e => e.Contacts, e => e.HomeAddress.Building.Amenities) and /// SelectMany over an inner array (e => e.Contacts.SelectMany(c => c.Metadata)), joined with dots. /// private static string ParseArrayPath(LambdaExpression selector) => PathOf(selector.Body, selector.Parameters[0], selector); private static string PathOf(Expression expression, ParameterExpression parameter, LambdaExpression whole) { var e = GroupSelectorMembers.StripConvert(expression); if (e is MethodCallExpression mc && mc.Method.Name == "SelectMany" && mc.Method.DeclaringType == typeof(Enumerable)) { if (mc.Arguments.Count != 2) throw new NotSupportedException( $"{OwnerApi}: SelectMany with a result selector is not supported in the array path ('{mc}'). Group the " + "inner array alone (e => e.Contacts.SelectMany(c => c.Metadata)); keys and aggregates read its elements."); var inner = mc.Arguments[1] is UnaryExpression { NodeType: ExpressionType.Quote } q ? q.Operand : mc.Arguments[1]; if (inner is not LambdaExpression lambda || lambda.Parameters.Count != 1) throw new NotSupportedException( $"{OwnerApi}: SelectMany in the array path takes a lambda over the element (c => c.Inner); got '{mc.Arguments[1]}'."); return PathOf(mc.Arguments[0], parameter, whole) + "." + FieldChainPath(lambda.Body, lambda.Parameters[0], whole); } if (e is MemberExpression) return FieldChainPath(e, parameter, whole); throw new NotSupportedException( $"{OwnerApi}: the array path supports a chain of fields (e => e.Contacts, e => e.HomeAddress.Building.Amenities) " + $"and SelectMany over an inner array (e => e.Contacts.SelectMany(c => c.Metadata)); got '{whole}'."); } private static string FieldChainPath(Expression expression, ParameterExpression parameter, LambdaExpression whole) { var e = GroupSelectorMembers.StripConvert(expression); if (e is MemberExpression member && GroupSelectorMembers.IsFieldChain(member) && GroupAggregateParser.TryMemberPath(member, parameter, out var path)) { for (Expression? cur = member; cur is MemberExpression m; cur = m.Expression is null ? null : GroupSelectorMembers.StripConvert(m.Expression)) { if (IsDictionary(m.Type)) throw new NotSupportedException( $"{OwnerApi}: a dictionary in the array path is not supported ('{m}' in '{whole}')."); } return path; } throw new NotSupportedException( $"{OwnerApi}: '{expression}' in the array path '{whole}' is not a chain of stored fields."); } private static bool IsDictionary(Type type) => type.GetInterfaces().Concat(new[] { type }).Any(i => i.IsGenericType && (i.GetGenericTypeDefinition() == typeof(IDictionary<,>) || i.GetGenericTypeDefinition() == typeof(IReadOnlyDictionary<,>))); /// /// Keys: a field of the element (c => c.Type, c => c.Address.City), the element itself for an array /// of simple values (s => s), or an anonymous type of those. A key is a stored value, never a class. /// private List ParseGroupFields(string? alias) { var parameter = _keySelector.Parameters[0]; var body = GroupSelectorMembers.StripConvert(_keySelector.Body); var fields = new List(); if (body is NewExpression newExpr && newExpr.Members != null && body.Type.Name.Contains("AnonymousType")) { for (int i = 0; i < newExpr.Arguments.Count; i++) { var memberName = newExpr.Members[i].Name; fields.Add(new GroupFieldRequest { FieldPath = KeyPath(newExpr.Arguments[i], parameter, $"key member '{memberName}'"), Alias = memberName }); } return fields; } fields.Add(new GroupFieldRequest { FieldPath = KeyPath(body, parameter, "the key"), Alias = alias ?? DefaultKeyAlias(body) }); return fields; } private static string DefaultKeyAlias(Expression body) => body is MemberExpression m ? m.Member.Name : "Key"; private string KeyPath(Expression expression, ParameterExpression parameter, string what) { var e = GroupSelectorMembers.StripConvert(expression); var type = Nullable.GetUnderlyingType(e.Type) ?? e.Type; if (type != typeof(string) && (type.IsClass || type.IsInterface) && !typeof(IRedbListItem).IsAssignableFrom(type)) throw new NotSupportedException( $"{OwnerApi}: {what} ('{expression}') is a class; a key is a stored value: name a field of it."); return GroupAggregateParser.FieldPath(expression, parameter, _kind, OwnerApi, what); } /// The name the selector gives to g.Key (single key), used as the key's alias in SQL. private static string? KeyAliasFromSelector(LambdaExpression selector) { var groupParam = selector.Parameters[0]; foreach (var (name, _, expr) in GroupSelectorMembers.Extract(selector, OwnerApi + ".SelectAsync")) { if (GroupSelectorMembers.StripConvert(expr) is MemberExpression { Member.Name: "Key" } m && m.Expression == groupParam) return name; } return null; } private List ParseAggregations(LambdaExpression selector) { var aggregations = new List(); var groupParam = selector.Parameters[0]; foreach (var (name, _, rawExpr) in GroupSelectorMembers.Extract(selector, OwnerApi + ".SelectAsync")) { var expr = GroupSelectorMembers.StripConvert(rawExpr); if (GroupSelectorMembers.IsKeyAccess(expr, groupParam)) continue; if (GroupAggregateParser.IsAggCall(expr)) { aggregations.Add(GroupAggregateParser.Parse((MethodCallExpression)expr, groupParam, name, _kind, OwnerApi + ".SelectAsync")); continue; } if (expr is MethodCallExpression wc && wc.Method.DeclaringType == typeof(Win)) continue; // a window function: the window over the groups reads it if (!GroupSelectorMembers.ReferencesParameter(expr, groupParam)) continue; // a client value, computed once at materialization throw new NotSupportedException( $"{OwnerApi}.SelectAsync: member '{name}' uses the group but is neither g.Key nor a direct Agg.* call ('{rawExpr}')."); } return aggregations; } /// /// The array grouping as the source of a window over its groups. /// private sealed class ArrayWindowSource : IGroupedWindowSource { private readonly RedbArrayGroupedQueryable _owner; public ArrayWindowSource(RedbArrayGroupedQueryable owner) => _owner = owner; public string ProviderName => _owner._runner.ProviderName; public redb.Core.Providers.IListProvider? ListItems => _owner._runner.ListItems; public GroupItemKind Kind => _owner._kind; public List GroupFields(LambdaExpression selector) => _owner.ParseGroupFields(KeyAliasFromSelector(selector)); public Task ExecuteAsync(LambdaExpression selector, List aggregations, ArrayGroupWindow window, CancellationToken cancellationToken) => _owner._runner.ExecuteAsync(Query(selector, aggregations, window), cancellationToken); public Task GetSqlAsync(LambdaExpression selector, List aggregations, ArrayGroupWindow window, CancellationToken cancellationToken) => _owner._runner.GetSqlAsync(Query(selector, aggregations, window), cancellationToken); private ArrayGroupQuery Query(LambdaExpression selector, List aggregations, ArrayGroupWindow window) => new() { SchemeId = _owner._schemeId, ArrayPath = ParseArrayPath(_owner._arraySelector), GroupFields = GroupFields(selector), Aggregations = aggregations, Filter = _owner._filter, HavingJson = _owner.BuildHavingJson(), Window = window }; } }