Path: redb/redb.Core/Query/Projection/ProjectionFieldExtractor.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/Projection/ProjectionFieldExtractor.cs
----
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using redb.Core.Models.Contracts;
using redb.Core.Models.Entities;
using redb.Core.Query.Aggregation;
namespace redb.Core.Query.Projection;
///
/// Extracts structure_ids from Select expression to optimize _values loading.
/// SUPPORTS: simple fields, Class fields, arrays, nested arrays (graph of any depth)
///
public class ProjectionFieldExtractor
{
///
/// Extracts HashSet of structure_ids for optimized BULK SELECT.
///
///
/// Three different answers, and the caller must not mix them up:
/// null - the selector could not be reduced to a list of fields (a node was not recognized, the
/// object or its Props is used whole, a field is unknown to the scheme): load everything;
/// an empty set - the selector was read completely and touches no Props field (base fields
/// only): nothing has to be loaded;
/// a non-empty set - exactly these structures are read.
///
public HashSet? ExtractStructureIds(
IRedbScheme scheme,
Expression, TResult>> selector)
where TProps : class, new()
{
try
{
var result = new HashSet();
var paths = ExtractFieldPaths(selector.Body, selector.Parameters[0]);
foreach (var path in paths)
{
// A member of Props the scheme does not know is not a stored field: a computed
// property, most likely, reading fields this method cannot see.
if (!AddStructureIdsForPath(scheme, path, result))
return null;
}
return result;
}
catch (NotSupportedException)
{
// Явный сигнал «узел не разобран» из CollectFieldPaths/CollectBindings - откат на
// полную загрузку. Ловим ТОЛЬКО его: любая другая ошибка - баг экстрактора и обязана
// быть громкой (решение владельца 2026-09-03: никаких глухих catch).
return null;
}
}
///
/// Whether the selector was read completely and touches no Props field (base fields only). It is the empty-set answer
/// of , which needs no scheme: such a projection reads no value row.
///
public bool ReadsNoPropsField(Expression, TResult>> selector)
where TProps : class, new()
{
try
{
return ExtractFieldPaths(selector.Body, selector.Parameters[0]).Count == 0;
}
catch (NotSupportedException)
{
return false;
}
}
///
/// Extracts text field paths for SQL function search_objects_with_projection_by_paths.
/// Format: ["Name", "AddressBook[home].City", "Items[0].Price"]
///
///
/// List of text paths or null (if parsing failed)
///
public List? ExtractFieldPathStrings(
Expression, TResult>> selector)
where TProps : class, new()
{
try
{
var paths = ExtractFieldPaths(selector.Body, selector.Parameters[0]);
if (paths.Count == 0)
return null;
var result = paths
.Where(p => p.Segments.Count > 0)
.Select(p => p.FullPath)
.Distinct()
.ToList();
return result.Count > 0 ? result : null;
}
catch (NotSupportedException)
{
// Тот же явный fail-safe, что и в ExtractStructureIds; прочие ошибки - громкие.
return null;
}
}
///
/// Checks if expression contains aggregation calls (Agg.Sum, etc.)
///
public bool HasAggregations(
Expression, TResult>> selector)
where TProps : class, new()
{
return ContainsAggCall(selector.Body);
}
///
/// Extracts information about requested aggregations
///
public List ExtractAggregations(
Expression, TResult>> selector)
where TProps : class, new()
{
var result = new List();
CollectAggregations(selector.Body, result);
return result;
}
#region Private Methods - Path Extraction
///
/// Extracts all field paths from expression
///
/// Expression to read.
///
/// The selector's own parameter (the object). With it the walk can tell the object read whole
/// (Map(o), new { O = o }, o.Props) from a field of it, and throws
/// NotSupportedException for the former: such a selector needs every field. Null where there is
/// no such parameter (arguments of aggregates).
///
private List ExtractFieldPaths(Expression expression, ParameterExpression? source)
{
var paths = new List();
CollectFieldPaths(expression, paths, source);
return paths;
}
///
/// Recursively collects field paths from expression.
/// FAIL-SAFE contract (ревью 2026-09-03, S-5): каждый вид узла либо явно разобран, либо
/// метод бросает NotSupportedException - и публичные Extract* через свой try/catch отдают
/// null, что означает «грузим объект целиком». Раньше неизвестный узел молча давал ноль
/// путей из поддерева: при ЧАСТИЧНОМ разборе лямбды поле молча не загружалось и проекция
/// отдавала default вместо значения.
///
private void CollectFieldPaths(Expression expression, List paths, ParameterExpression? source)
{
switch (expression)
{
case NewExpression newExpr:
foreach (var arg in newExpr.Arguments)
{
CollectFieldPaths(arg, paths, source);
}
break;
case MemberInitExpression initExpr:
CollectFieldPaths(initExpr.NewExpression, paths, source);
CollectBindings(initExpr.Bindings, paths, source);
break;
case MemberExpression memberExpr:
CollectFromMemberChain(memberExpr, paths, source);
break;
case MethodCallExpression methodExpr:
CollectFromMethodCall(methodExpr, paths, source);
break;
case UnaryExpression unaryExpr:
CollectFieldPaths(unaryExpr.Operand, paths, source);
break;
case ConditionalExpression condExpr:
// S-5b: условие тернарника раньше НЕ сканировалось - поле из условия молча не
// загружалось и ветка выбиралась по default-значению.
CollectFieldPaths(condExpr.Test, paths, source);
CollectFieldPaths(condExpr.IfTrue, paths, source);
CollectFieldPaths(condExpr.IfFalse, paths, source);
break;
case BinaryExpression binaryExpr:
// o == null and o.Props != null read no field: the usual guard in front of a field
// must not turn the whole selector into a full load.
if (IsNullCheckOfSource(binaryExpr, source))
break;
CollectFieldPaths(binaryExpr.Left, paths, source);
CollectFieldPaths(binaryExpr.Right, paths, source);
break;
case LambdaExpression lambdaExpr:
CollectFieldPaths(lambdaExpr.Body, paths, source);
break;
case NewArrayExpression newArrayExpr:
foreach (var el in newArrayExpr.Expressions)
CollectFieldPaths(el, paths, source);
break;
case ListInitExpression listInitExpr:
CollectFieldPaths(listInitExpr.NewExpression, paths, source);
foreach (var init in listInitExpr.Initializers)
foreach (var arg in init.Arguments)
CollectFieldPaths(arg, paths, source);
break;
case InvocationExpression invocationExpr:
CollectFieldPaths(invocationExpr.Expression, paths, source);
foreach (var arg in invocationExpr.Arguments)
CollectFieldPaths(arg, paths, source);
break;
case TypeBinaryExpression typeBinaryExpr:
CollectFieldPaths(typeBinaryExpr.Expression, paths, source);
break;
case IndexExpression indexExpr:
if (indexExpr.Object != null)
CollectFieldPaths(indexExpr.Object, paths, source);
foreach (var arg in indexExpr.Arguments)
CollectFieldPaths(arg, paths, source);
break;
case ParameterExpression parameterExpr:
// The object itself handed on (Map(o), new { O = o }): whatever receives it may read
// any field. Parameters of inner lambdas are plain leaves.
if (source != null && parameterExpr == source)
throw new NotSupportedException(
"ProjectionFieldExtractor: the object is used whole - full load.");
break;
case ConstantExpression:
case DefaultExpression:
break; // листья без полей
default:
throw new NotSupportedException(
$"ProjectionFieldExtractor: unrecognized expression node {expression.NodeType} " +
"- falling back to full load (fail-safe).");
}
}
///
/// Reads a member chain (a.b.c, indexers included) by what it hangs on.
/// On the selector's parameter it is a field of the object: a Props path is recorded, a base
/// field needs nothing, Props taken whole or a computed property of Props means a full load.
/// On anything else that is an expression of its own - a call, ??, a conditional, a cast -
/// that expression is read in turn: (o.Props.A ?? "").Length reads A, and before this
/// method existed the chain simply ended there and A was never loaded.
///
private void CollectFromMemberChain(MemberExpression memberExpr, List paths, ParameterExpression? source)
{
// Same walk as ExtractMemberPath: members and indexers down to whatever carries them.
var members = new List();
Expression? root = memberExpr;
while (true)
{
if (root is MemberExpression member)
{
members.Insert(0, member.Member);
root = member.Expression;
}
else if (root is MethodCallExpression { Method.Name: "get_Item", Object: not null } indexer)
{
// The key of an indexer may itself read a field: Dict[o.Props.Key].
foreach (var arg in indexer.Arguments)
CollectFieldPaths(arg, paths, source);
root = indexer.Object;
}
else
{
break;
}
}
// A cast of the object changes nothing about which field is read: ((IRedbObject)o).Name.
var carrier = root;
while (carrier is UnaryExpression { NodeType: ExpressionType.Convert or ExpressionType.ConvertChecked or ExpressionType.TypeAs } cast)
carrier = cast.Operand;
switch (carrier)
{
case null: // static member
case ConstantExpression: // captured variable
return;
case ParameterExpression parameter when source == null || parameter == source:
var propsIndex = members.FindIndex(m => m.Name == "Props");
if (propsIndex < 0)
return; // base field of the object
if (source != null)
{
if (propsIndex == members.Count - 1)
throw new NotSupportedException(
"ProjectionFieldExtractor: Props is used whole - full load.");
// A property without a setter is not stored; what it computes from is unknown here.
if (members[propsIndex + 1] is PropertyInfo { CanWrite: false })
throw new NotSupportedException(
$"ProjectionFieldExtractor: {members[propsIndex + 1].Name} is a computed property - full load.");
// Behind a cast ExtractMemberPath cannot follow the chain; rare enough to load everything.
if (!ReferenceEquals(carrier, root))
throw new NotSupportedException(
"ProjectionFieldExtractor: Props read through a cast of the object - full load.");
}
var path = ExtractMemberPath(memberExpr);
if (path != null)
paths.Add(path);
return;
case ParameterExpression:
return; // parameter of an inner lambda: its collection is already recorded with all children
default:
CollectFieldPaths(root!, paths, source);
return;
}
}
///
/// True for a comparison of the object or of its Props with null.
///
private static bool IsNullCheckOfSource(BinaryExpression binaryExpr, ParameterExpression? source)
{
if (source == null || binaryExpr.NodeType is not (ExpressionType.Equal or ExpressionType.NotEqual))
return false;
return (IsNullConstant(binaryExpr.Right) && IsSourceOrItsProps(binaryExpr.Left))
|| (IsNullConstant(binaryExpr.Left) && IsSourceOrItsProps(binaryExpr.Right));
static Expression StripCasts(Expression e)
{
while (e is UnaryExpression { NodeType: ExpressionType.Convert or ExpressionType.ConvertChecked or ExpressionType.TypeAs } cast)
e = cast.Operand;
return e;
}
static bool IsNullConstant(Expression e) => StripCasts(e) is ConstantExpression { Value: null };
bool IsSourceOrItsProps(Expression e)
{
e = StripCasts(e);
if (e == source) return true;
return e is MemberExpression { Member.Name: "Props" } member && StripCasts(member.Expression!) == source;
}
}
///
/// Разбор всех видов member-биндингов, включая вложенные объектные инициализаторы
/// (MemberMemberBinding: new Dto { Sub = { X = p.Props.N } }) и списковые (MemberListBinding).
///
private void CollectBindings(System.Collections.ObjectModel.ReadOnlyCollection bindings, List paths, ParameterExpression? source)
{
foreach (var binding in bindings)
{
switch (binding)
{
case MemberAssignment assignment:
CollectFieldPaths(assignment.Expression, paths, source);
break;
case MemberMemberBinding nested:
CollectBindings(nested.Bindings, paths, source);
break;
case MemberListBinding list:
foreach (var init in list.Initializers)
foreach (var arg in init.Arguments)
CollectFieldPaths(arg, paths, source);
break;
default:
throw new NotSupportedException(
$"ProjectionFieldExtractor: unrecognized member binding {binding.BindingType}.");
}
}
}
///
/// Extracts path from MemberExpression (x.Props.Customer.Name → ["Customer", "Name"])
/// Supports Dictionary indexer: x.Props.AddressBook["home"].City → ["AddressBook[home]", "City"]
///
private FieldPathInfo? ExtractMemberPath(MemberExpression memberExpr)
{
var segments = new List();
string? dictKey = null;
int dictKeyInsertPosition = -1;
Expression? current = memberExpr;
while (current != null)
{
switch (current)
{
case MemberExpression member:
segments.Insert(0, member.Member.Name);
current = member.Expression;
break;
case MethodCallExpression indexerCall when indexerCall.Method.Name == "get_Item":
// Dictionary indexer: AddressBook["home"]
if (indexerCall.Arguments.Count > 0 && indexerCall.Arguments[0] is ConstantExpression keyConst)
{
dictKey = keyConst.Value?.ToString();
// Remember position where key needs to be inserted (after processing Object)
dictKeyInsertPosition = segments.Count;
}
current = indexerCall.Object;
break;
default:
current = null;
break;
}
}
// Check that path starts with Props
var propsIndex = segments.FindIndex(s => s == "Props");
if (propsIndex < 0)
return null;
// Take segments after Props
var propsSegments = segments.Skip(propsIndex + 1).ToList();
if (propsSegments.Count == 0)
return null;
// If there's a dictionary key, add it to the appropriate segment
// x.Props.AddressBook["home"].City → segments = ["Props", "AddressBook", "City"]
// dictKeyInsertPosition shows how many segments were AFTER indexer (in this case 1 = "City")
// So key should be added to segment at index (propsSegments.Count - dictKeyInsertPosition - 1)
if (dictKey != null && propsSegments.Count > 0)
{
// Index of segment to add key to
int targetIdx = propsSegments.Count - dictKeyInsertPosition - 1;
if (targetIdx >= 0 && targetIdx < propsSegments.Count)
{
propsSegments[targetIdx] = $"{propsSegments[targetIdx]}[{dictKey}]";
}
else if (propsSegments.Count > 0)
{
// Fallback: add to first segment
propsSegments[0] = $"{propsSegments[0]}[{dictKey}]";
}
}
return new FieldPathInfo
{
Segments = propsSegments,
IsArray = false,
DictKey = dictKey
};
}
///
/// Processes method calls: Select, Agg.Sum, Dictionary indexer, etc.
///
private void CollectFromMethodCall(MethodCallExpression methodExpr, List paths, ParameterExpression? source)
{
var methodName = methodExpr.Method.Name;
var declaringType = methodExpr.Method.DeclaringType;
// Dictionary indexer: x.Props.AddressBook["home"] or x.Props.AddressBook["home"].City
if (methodName == "get_Item" && declaringType != null && declaringType.IsGenericType)
{
var genericDef = declaringType.GetGenericTypeDefinition();
if (genericDef == typeof(Dictionary<,>) || genericDef == typeof(IDictionary<,>))
{
var dictPath = ExtractDictionaryIndexerPath(methodExpr);
if (dictPath != null)
{
paths.Add(dictPath);
}
return;
}
}
// Agg.Sum, Agg.Average, etc.
if (declaringType == typeof(Agg))
{
foreach (var arg in methodExpr.Arguments)
{
CollectFieldPaths(arg, paths, source);
}
return;
}
// LINQ Select: x.Props.Items.Select(i => i.Price)
if (methodName == "Select" && methodExpr.Arguments.Count >= 1)
{
// Base collection
CollectFieldPaths(methodExpr.Arguments[0], paths, source);
// Lambda inside Select
if (methodExpr.Arguments.Count >= 2 && methodExpr.Arguments[1] is LambdaExpression lambda)
{
CollectFieldPaths(lambda.Body, paths, source);
}
return;
}
// LINQ ToList
if (methodName == "ToList" && methodExpr.Arguments.Count >= 1)
{
CollectFieldPaths(methodExpr.Arguments[0], paths, source);
return;
}
// Extension methods (FirstOrDefault, etc.)
if (methodExpr.Object != null)
{
CollectFieldPaths(methodExpr.Object, paths, source);
}
foreach (var arg in methodExpr.Arguments)
{
CollectFieldPaths(arg, paths, source);
}
}
///
/// Extracts path from Dictionary indexer expression: x.Props.AddressBook["home"].City
///
private FieldPathInfo? ExtractDictionaryIndexerPath(MethodCallExpression indexerExpr)
{
var segments = new List();
string? dictKey = null;
// Extract dictionary key
if (indexerExpr.Arguments.Count > 0 && indexerExpr.Arguments[0] is ConstantExpression keyConst)
{
dictKey = keyConst.Value?.ToString();
}
// Recursively extract path to dictionary (x.Props.AddressBook)
Expression? current = indexerExpr.Object;
while (current != null)
{
switch (current)
{
case MemberExpression member:
segments.Insert(0, member.Member.Name);
current = member.Expression;
break;
case MethodCallExpression nestedIndexer when nestedIndexer.Method.Name == "get_Item":
// Nested Dictionary indexer - not supported yet
current = nestedIndexer.Object;
break;
default:
current = null;
break;
}
}
// Check that path starts with Props
var propsIndex = segments.FindIndex(s => s == "Props");
if (propsIndex < 0)
return null;
// Take segments after Props
var propsSegments = segments.Skip(propsIndex + 1).ToList();
if (propsSegments.Count == 0)
return null;
// Add key to dictionary name: AddressBook -> AddressBook[home]
if (dictKey != null && propsSegments.Count > 0)
{
propsSegments[propsSegments.Count - 1] = $"{propsSegments[propsSegments.Count - 1]}[{dictKey}]";
}
return new FieldPathInfo
{
Segments = propsSegments,
IsArray = false,
DictKey = dictKey
};
}
#endregion
#region Private Methods - Structure IDs
///
/// Adds structure_ids for path considering NESTED arrays
///
///
/// False when the first segment is not a structure of the scheme. Deeper segments may well be
/// missing: Name.Length or Date.Value.Year read a member of a loaded value, not a field.
///
private bool AddStructureIdsForPath(IRedbScheme scheme, FieldPathInfo pathInfo, HashSet result)
{
long? parentId = null;
foreach (var segment in pathInfo.Segments)
{
var cleanName = CleanSegmentName(segment);
var structure = scheme.Structures
.FirstOrDefault(s => s.Name == cleanName && s.IdParent == parentId);
if (structure == null)
return parentId != null;
result.Add(structure.Id);
// KEY LOGIC: For any Class/Array add ALL children RECURSIVELY
if (structure.CollectionType != null || IsClassField(scheme, structure))
{
AddAllChildStructuresRecursive(scheme, structure.Id, result);
}
parentId = structure.Id;
}
return true;
}
///
/// RECURSIVELY adds all child structures at ANY DEPTH
///
private void AddAllChildStructuresRecursive(IRedbScheme scheme, long parentId, HashSet result)
{
var children = scheme.Structures.Where(s => s.IdParent == parentId);
foreach (var child in children)
{
result.Add(child.Id);
// Recursion for nested arrays and Class inside arrays
AddAllChildStructuresRecursive(scheme, child.Id, result);
}
}
///
/// Cleans segment name from [] and indices
///
private string CleanSegmentName(string segment)
{
var bracketIndex = segment.IndexOf('[');
return bracketIndex >= 0 ? segment.Substring(0, bracketIndex) : segment;
}
///
/// Checks if structure is a Class field (has child structures)
///
private bool IsClassField(IRedbScheme scheme, IRedbStructure structure)
{
// Class field = has child structures and is not a collection
if (structure.CollectionType != null) return false;
return scheme.Structures.Any(s => s.IdParent == structure.Id);
}
#endregion
#region Private Methods - Aggregation Detection
///
/// Checks if expression contains Agg.* calls
///
private bool ContainsAggCall(Expression expression)
{
switch (expression)
{
case MethodCallExpression methodExpr:
if (methodExpr.Method.DeclaringType == typeof(Agg))
return true;
return methodExpr.Arguments.Any(ContainsAggCall) ||
(methodExpr.Object != null && ContainsAggCall(methodExpr.Object));
case NewExpression newExpr:
return newExpr.Arguments.Any(ContainsAggCall);
case MemberInitExpression initExpr:
return initExpr.Bindings.OfType()
.Any(b => ContainsAggCall(b.Expression));
case UnaryExpression unaryExpr:
return ContainsAggCall(unaryExpr.Operand);
case BinaryExpression binaryExpr:
return ContainsAggCall(binaryExpr.Left) || ContainsAggCall(binaryExpr.Right);
case LambdaExpression lambdaExpr:
return ContainsAggCall(lambdaExpr.Body);
default:
return false;
}
}
///
/// Collects aggregation information
///
private void CollectAggregations(Expression expression, List result)
{
switch (expression)
{
case MethodCallExpression methodExpr when methodExpr.Method.DeclaringType == typeof(Agg):
var aggInfo = new AggregationInfo
{
Function = ParseAggFunction(methodExpr.Method.Name),
FieldPaths = new List()
};
foreach (var arg in methodExpr.Arguments)
{
var argPaths = ExtractFieldPaths(arg, source: null);
aggInfo.FieldPaths.AddRange(argPaths);
}
result.Add(aggInfo);
break;
case NewExpression newExpr:
foreach (var arg in newExpr.Arguments)
CollectAggregations(arg, result);
break;
case MemberInitExpression initExpr:
foreach (var binding in initExpr.Bindings.OfType())
CollectAggregations(binding.Expression, result);
break;
case UnaryExpression unaryExpr:
CollectAggregations(unaryExpr.Operand, result);
break;
case BinaryExpression binaryExpr:
CollectAggregations(binaryExpr.Left, result);
CollectAggregations(binaryExpr.Right, result);
break;
}
}
private AggregateFunction ParseAggFunction(string methodName)
{
return methodName switch
{
"Sum" => AggregateFunction.Sum,
"Average" => AggregateFunction.Average,
"Min" => AggregateFunction.Min,
"Max" => AggregateFunction.Max,
"Count" => AggregateFunction.Count,
_ => throw new NotSupportedException($"Unknown aggregation: {methodName}")
};
}
#endregion
}
///
/// Field path information
///
public record FieldPathInfo
{
/// Path segments: ["Contacts", "Email"] or ["AddressBook[home]", "City"]
public List Segments { get; init; } = new();
/// Is array?
public bool IsArray { get; init; }
/// Array element index (for Tags[0])
public int? ArrayIndex { get; init; }
/// Dictionary key (for AddressBook["home"])
public string? DictKey { get; init; }
/// Full path as string
public string FullPath => string.Join(".", Segments);
}
///
/// Aggregation information
///
public record AggregationInfo
{
public AggregateFunction Function { get; init; }
public List FieldPaths { get; init; } = new();
}