Path: redb/redb.Core/Query/Grouping/GroupAggregateParser.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/GroupAggregateParser.cs
----
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using redb.Core.Query.Aggregation;
namespace redb.Core.Query.Grouping;
///
/// What the parameter of an aggregate or condition lambda stands for in a grouping.
///
internal enum GroupItemKind
{
/// An object's Props (GroupBy, tree GroupBy, grouped window).
Props,
/// An element of an array of classes (GroupByArray over Contacts).
ArrayClassElement,
/// An element of an array of simple values (GroupByArray over Skills): the element itself is a value.
ArrayValueElement
}
///
/// One strict reading of Agg.* calls for every grouping (GroupBy, tree GroupBy, grouped window, GroupByArray).
/// A call it cannot translate is refused with ; nothing becomes "*" or a
/// path the caller did not write.
///
internal static class GroupAggregateParser
{
/// Whether the expression is a call of a method of .
public static bool IsAggCall(Expression expression)
=> GroupSelectorMembers.StripConvert(expression) is MethodCallExpression mc && mc.Method.DeclaringType == typeof(Agg);
///
/// Reads one Agg.* call over the group parameter into an aggregate request.
///
/// The call, already known to be a method of .
/// Parameter of the group in the selector or HAVING lambda.
/// Alias of the result column.
/// What the lambda parameter of the call stands for.
/// Name of the API for messages, e.g. "GroupByArray.SelectAsync".
public static AggregateRequest Parse(
MethodCallExpression call, ParameterExpression groupParam, string alias, GroupItemKind kind, string ownerApi)
{
var name = call.Method.Name;
var function = name switch
{
"Sum" => AggregateFunction.Sum,
"Average" => AggregateFunction.Average,
"Min" => AggregateFunction.Min,
"Max" => AggregateFunction.Max,
"Count" => AggregateFunction.Count,
_ => throw new NotSupportedException($"{ownerApi}: Agg.{name} is not an aggregate of a group.")
};
if (call.Arguments.Count == 0 || GroupSelectorMembers.StripConvert(call.Arguments[0]) != groupParam)
throw new NotSupportedException(
$"{ownerApi}: Agg.{name} takes the group as its first argument (Agg.{name}(g, ...)); got '{call}'.");
if (function == AggregateFunction.Count)
{
if (call.Arguments.Count == 1)
return new AggregateRequest { FieldPath = "*", Function = function, Alias = alias };
var predicate = UnwrapLambda(call.Arguments[1])
?? throw new NotSupportedException($"{ownerApi}: the condition of Agg.Count must be a lambda; got '{call.Arguments[1]}'.");
return new AggregateRequest
{
FieldPath = "*",
Function = function,
Alias = alias,
ConditionJson = ItemPredicateParser.ToJson(predicate, kind, ownerApi)
};
}
if (call.Arguments.Count < 2)
throw new NotSupportedException($"{ownerApi}: Agg.{name} needs a selector over the group: Agg.{name}(g, x => x.Field).");
var selector = UnwrapLambda(call.Arguments[1])
?? throw new NotSupportedException($"{ownerApi}: the selector of Agg.{name} must be a lambda; got '{call.Arguments[1]}'.");
return new AggregateRequest
{
FieldPath = FieldPath(selector, kind, ownerApi, $"the selector of Agg.{name}"),
Function = function,
Alias = alias
};
}
///
/// The stored field a lambda names: a chain of members from its parameter (x => x.Address.City), or the
/// parameter itself for an element of an array of simple values ().
/// Anything else is a computation and is refused.
///
public static string FieldPath(LambdaExpression lambda, GroupItemKind kind, string ownerApi, string what)
=> FieldPath(lambda.Body, lambda.Parameters[0], kind, ownerApi, what);
///
/// The stored field an expression over names; see .
///
public static string FieldPath(Expression body, ParameterExpression parameter, GroupItemKind kind, string ownerApi, string what)
{
var expr = GroupSelectorMembers.StripConvert(body);
if (expr == parameter)
{
if (kind == GroupItemKind.ArrayValueElement)
return GroupFieldRequest.ElementPath;
throw new NotSupportedException(kind == GroupItemKind.ArrayClassElement
? $"{ownerApi}: {what} names the whole element of an array of classes; name a field of the element (x => x.Field)."
: $"{ownerApi}: {what} names the whole object; name a field (x => x.Field).");
}
if (expr is MemberExpression member && GroupSelectorMembers.IsFieldChain(member)
&& TryMemberPath(member, parameter, out var path))
return path;
throw new NotSupportedException(
$"{ownerApi}: {what} must name a stored field ({Example(kind)}); '{body}' is a computation, which is not translated to SQL.");
}
///
/// Path of a member chain that starts at , joined with dots. A Props member and
/// the Value of a are not part of the stored path.
///
public static bool TryMemberPath(MemberExpression member, ParameterExpression parameter, out string path)
{
var parts = new List();
Expression? current = member;
while (current is MemberExpression m)
{
var declaring = m.Member.DeclaringType;
var isNullableValue = m.Member.Name == "Value" && declaring is { IsGenericType: true }
&& declaring.GetGenericTypeDefinition() == typeof(Nullable<>);
if (!isNullableValue && m.Member.Name != "Props")
parts.Insert(0, m.Member.Name);
current = m.Expression is null ? null : GroupSelectorMembers.StripConvert(m.Expression);
}
path = string.Join(".", parts);
return current == parameter && parts.Count > 0;
}
private static string Example(GroupItemKind kind) => kind switch
{
GroupItemKind.ArrayValueElement => "x => x for the element itself",
GroupItemKind.ArrayClassElement => "x => x.Field or x => x.Inner.Field of the element",
_ => "x => x.Field or x => x.Inner.Field"
};
private static LambdaExpression? UnwrapLambda(Expression expression)
{
if (expression is UnaryExpression quote && quote.NodeType == ExpressionType.Quote)
expression = quote.Operand;
return expression as LambdaExpression;
}
}