Path: redb/redb.Core/Query/Aggregation/AggregateRequest.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/Aggregation/AggregateRequest.cs
----
using System.Collections.Generic;
using redb.Core.Query.Utils;
namespace redb.Core.Query.Aggregation;
///
/// Request for batch aggregation of multiple fields.
///
public class AggregateRequest
{
/// Field path (e.g. "Price" or "Items[].Price")
public string FieldPath { get; set; } = "";
/// Aggregation function
public AggregateFunction Function { get; set; }
/// Result alias
public string? Alias { get; set; }
///
/// Condition of a conditional count (Agg.Count(g, x => x.IsVerified)): a boolean expression in the JSON
/// form HAVING uses ($and/$or/$not, $eq/$ne/$gt/$gte/$lt/$lte, $isnull/$notnull,
/// $field, $const). Null for an aggregate without a condition. Only
/// carries one.
///
public string? ConditionJson { get; set; }
///
/// True when the path names a field of the object itself (x.Id, x.Name) and not a Props field.
/// The path alone cannot tell: x.Props.Name and x.Name both read "Name". A path marked "0$:" is a
/// base field whatever this says.
///
public bool IsBaseField { get; set; }
///
/// Whether an aggregate argument reads a field of the object itself: a member chain that starts
/// at a parameter typed as the object and does not pass through its Props. x.Id does,
/// x.Props.Id and anything under a parameter typed as Props do not.
///
public static bool ReadsObjectField(System.Linq.Expressions.Expression? expression)
{
var current = StripConvert(expression);
if (current is not System.Linq.Expressions.MemberExpression)
return false;
var throughProps = false;
while (current is System.Linq.Expressions.MemberExpression member)
{
if (member.Member.Name == "Props")
throughProps = true;
current = StripConvert(member.Expression);
}
return !throughProps
&& current is System.Linq.Expressions.ParameterExpression parameter
&& typeof(redb.Core.Models.Contracts.IRedbObject).IsAssignableFrom(parameter.Type);
}
private static System.Linq.Expressions.Expression? StripConvert(System.Linq.Expressions.Expression? expression)
{
while (expression is System.Linq.Expressions.UnaryExpression
{
NodeType: System.Linq.Expressions.ExpressionType.Convert
or System.Linq.Expressions.ExpressionType.ConvertChecked
} unary)
{
expression = unary.Operand;
}
return expression;
}
}
///
/// Batch aggregation result.
///
public class AggregateResult
{
/// Results by aliases
public Dictionary Values { get; set; } = new();
/// Get value by alias
public T? Get(string alias) where T : struct
{
if (Values.TryGetValue(alias, out var value) && value != null)
{
return (T)TemporalDecoder.ChangeType(value, typeof(T));
}
return null;
}
/// Get value by alias (nullable)
public T? GetNullable(string alias) where T : struct
{
if (Values.TryGetValue(alias, out var value))
{
if (value == null) return null;
return (T)TemporalDecoder.ChangeType(value, typeof(T));
}
return null;
}
}