Path: redb/redb.Postgres/Query/PostgresQueryProvider.GroupedWindow.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.GroupedWindow.cs
----
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
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;
using redb.Core.Query.Window;
namespace redb.Postgres.Query;
///
/// PVT-backed GroupBy + Window Functions for free PostgreSQL. Builds the
/// inner grouped SQL via pvt_build_groupby_sql, then wraps it in an
/// outer SELECT that applies the requested window functions over the
/// already-grouped row set, finally wrapping the whole thing in
/// json_agg(row_to_json(t)) for one-shot materialization.
///
/// Window OVER clause refs:
/// * partition_by: must reference a group alias.
/// * order_by: either a group alias or the synthetic
/// Agg_<func>_<field> pattern emitted by
/// ;
/// the latter is resolved against the aggregations list
/// to recover the actual aggregate alias.
///
public partial class PostgresQueryProvider
{
///
public override Task ExecuteGroupedWindowQueryAsync(
long schemeId,
IEnumerable groupFields,
IEnumerable aggregations,
IEnumerable windowFuncs,
IEnumerable partitionBy,
IEnumerable orderBy,
string? filterJson = null,
string? havingJson = null, CancellationToken cancellationToken = default)
{
return ExecuteGroupedWindowInternalAsync(
schemeId, groupFields, aggregations, windowFuncs, partitionBy, orderBy, filterJson, havingJson);
}
///
public override Task ExecuteGroupedWindowQueryAsync(
long schemeId,
IEnumerable groupFields,
IEnumerable aggregations,
IEnumerable windowFuncs,
IEnumerable partitionBy,
IEnumerable orderBy,
FilterExpression? filter,
string? havingJson = null, CancellationToken cancellationToken = default)
{
var filterJson = filter is null ? null : _facetBuilder.BuildFacetFilters(filter);
return ExecuteGroupedWindowInternalAsync(
schemeId, groupFields, aggregations, windowFuncs, partitionBy, orderBy, filterJson, havingJson);
}
///
public override async Task GetGroupedWindowSqlPreviewAsync(
long schemeId,
IEnumerable groupFields,
IEnumerable aggregations,
IEnumerable windowFuncs,
IEnumerable partitionBy,
IEnumerable orderBy,
string? filterJson = null,
string? havingJson = null, CancellationToken cancellationToken = default)
{
return await BuildGroupedWindowSqlAsync(
schemeId, groupFields, aggregations, windowFuncs, partitionBy, orderBy, filterJson, havingJson);
}
///
public override async Task GetGroupedWindowSqlPreviewAsync(
long schemeId,
IEnumerable groupFields,
IEnumerable aggregations,
IEnumerable windowFuncs,
IEnumerable partitionBy,
IEnumerable orderBy,
FilterExpression? filter,
string? havingJson = null, CancellationToken cancellationToken = default)
{
var filterJson = filter is null ? null : _facetBuilder.BuildFacetFilters(filter);
return await BuildGroupedWindowSqlAsync(
schemeId, groupFields, aggregations, windowFuncs, partitionBy, orderBy, filterJson, havingJson);
}
private async Task ExecuteGroupedWindowInternalAsync(
long schemeId,
IEnumerable groupFields,
IEnumerable aggregations,
IEnumerable windowFuncs,
IEnumerable partitionBy,
IEnumerable orderBy,
string? filterJson, string? havingJson, CancellationToken cancellationToken = default)
{
var sql = await BuildGroupedWindowSqlAsync(
schemeId, groupFields, aggregations, windowFuncs, partitionBy, orderBy, filterJson, havingJson);
var jsonArray = await _context.ExecuteScalarAsync(sql, System.Array.Empty