Path: redb/redb.Postgres/Query/PostgresQueryProvider.Grouping.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.Grouping.cs
----
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using redb.Core.Query.Aggregation;
using redb.Core.Query.Grouping;
using redb.Core.Query.QueryExpressions;
namespace redb.Postgres.Query;
///
/// PVT-backed GROUP BY for free PostgreSQL. Generates the projection SQL
/// via the server-side compiler pvt_build_groupby_sql and wraps it
/// with json_agg(row_to_json(t)) to materialize a JSON array of
/// group rows in one round-trip after the build step.
///
/// Overrides both string-based and FilterExpression-based virtual methods
/// on so callers reach
/// the PVT compiler regardless of which surface they use.
///
public partial class PostgresQueryProvider
{
///
public override Task ExecuteGroupedAggregateAsync(
long schemeId,
IEnumerable groupFields,
IEnumerable aggregations,
string? filterJson = null,
string? havingJson = null, CancellationToken cancellationToken = default)
{
return ExecuteGroupedAggregateInternalAsync(schemeId, groupFields, aggregations, filterJson, havingJson);
}
///
public override Task ExecuteGroupedAggregateAsync(
long schemeId,
IEnumerable groupFields,
IEnumerable aggregations,
FilterExpression? filter,
string? havingJson = null, CancellationToken cancellationToken = default)
{
var filterJson = filter is null ? null : _facetBuilder.BuildFacetFilters(filter);
return ExecuteGroupedAggregateInternalAsync(schemeId, groupFields, aggregations, filterJson, havingJson);
}
private async Task ExecuteGroupedAggregateInternalAsync(
long schemeId,
IEnumerable groupFields,
IEnumerable aggregations,
string? filterJson,
string? havingJson = null, CancellationToken cancellationToken = default)
{
var groupList = groupFields?.ToList() ?? new List();
var aggList = aggregations?.ToList() ?? new List();
// Empty group-by => degenerate to a flat batch aggregate; *RedbAsync helpers
// (SumRedbAsync, AverageRedbAsync, ...) intentionally pass zero group keys to
// request a single-row aggregate over base IRedbObject columns.
if (groupList.Count == 0)
{
if (!string.IsNullOrEmpty(havingJson))
throw new NotSupportedException(
"HAVING requires GROUP BY keys; cannot be combined with a flat aggregate batch.");
if (aggList.Count == 0)
return null;
var aggResult = await ExecuteAggregateBatchInternalAsync(schemeId, aggList, filterJson);
var rowJson = JsonSerializer.Serialize(new[] { aggResult.Values });
return JsonDocument.Parse(rowJson);
}
var innerSql = await BuildGroupByInnerSqlAsync(schemeId, groupList, aggList, filterJson, havingJson);
var wrapped = "SELECT COALESCE(json_agg(row_to_json(t)), '[]'::json)::text AS \"Value\" FROM (" + innerSql + ") t";
var jsonArray = await _context.ExecuteScalarAsync(wrapped, System.Array.Empty