Path: redb/redb.Postgres/Query/PostgresQueryProvider.Aggregation.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.Postgres/Query/PostgresQueryProvider.Aggregation.cs
----
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using redb.Core.Models.Entities;
using redb.Core.Query;
using redb.Core.Query.Aggregation;
using redb.Core.Query.QueryExpressions;
namespace redb.Postgres.Query;
///
/// PVT-backed terminal aggregations for free PostgreSQL. Generates the
/// aggregate SQL via the server-side compiler pvt_build_aggregate_sql
/// (free PVT v2 module) and executes the produced statement client-side,
/// wrapping the single result row with row_to_json to materialize
/// in a single round-trip after the build step.
///
/// Overrides the FilterExpression-typed virtual methods on the base provider.
/// Legacy facet-JSON overloads stay on the base type for dialects without
/// the PVT module (e.g. MSSql).
///
public partial class PostgresQueryProvider
{
///
/// Single-aggregate path. Delegates to
/// with a one-element request list and extracts the scalar value.
///
public override async Task ExecuteAggregateAsync(
long schemeId,
string fieldPath,
AggregateFunction function,
FilterExpression? filter, CancellationToken cancellationToken = default)
{
var request = new AggregateRequest
{
FieldPath = fieldPath ?? string.Empty,
Function = function,
Alias = "value"
};
var batch = await ExecuteAggregateBatchAsync(schemeId, new[] { request }, filter);
if (!batch.Values.TryGetValue("value", out var raw) || raw is null)
return null;
return raw switch
{
decimal d => d,
double dbl => redb.Core.Utils.RedbDecimal.FromDouble(dbl),
long l => l,
int i => i,
_ => Convert.ToDecimal(raw, CultureInfo.InvariantCulture)
};
}
///
/// Batch aggregate path: builds aggregations JSON, asks
/// pvt_build_aggregate_sql to compile the inner SQL, then executes
/// it through row_to_json to capture every alias in one row.
///
public override Task ExecuteAggregateBatchAsync(
long schemeId,
IEnumerable requests,
FilterExpression? filter, CancellationToken cancellationToken = default)
{
var facetFilters = filter is null ? null : _facetBuilder.BuildFacetFilters(filter);
return ExecuteAggregateBatchInternalAsync(schemeId, requests, facetFilters);
}
///
/// Batch aggregate path with pre-built facet-JSON filter. Same PVT pipeline
/// as the overload — overriding the base
/// virtual ensures custom-projection callers (RedbQueryable.AggregateAsync)
/// hit the v2-pvt module instead of the deprecated aggregate_batch
/// SQL function (now removed from PG dialect).
///
public override Task ExecuteAggregateBatchAsync(
long schemeId,
IEnumerable requests,
string? filterJson = null, CancellationToken cancellationToken = default)
{
return ExecuteAggregateBatchInternalAsync(schemeId, requests, filterJson);
}
internal async Task ExecuteAggregateBatchInternalAsync(
long schemeId,
IEnumerable requests,
string? filterJson, CancellationToken cancellationToken = default)
{
var list = requests?.ToList()
?? throw new ArgumentNullException(nameof(requests));
if (list.Count == 0)
return new AggregateResult();
var innerSql = await BuildAggregateSqlAsync(schemeId, list, filterJson, cancellationToken);
var wrapped = "SELECT row_to_json(t)::text AS \"Value\" FROM (" + innerSql + ") t";
var jsonRow = await _context.ExecuteScalarAsync(wrapped, System.Array.Empty