Path: redb/redb.Postgres/Query/PostgresQueryProvider.Window.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.Window.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.QueryExpressions;
using redb.Core.Query.Window;
namespace redb.Postgres.Query;
///
/// PVT-backed window functions for free PostgreSQL. Generates the SELECT
/// SQL via the server-side compiler pvt_build_window_sql and wraps
/// it with json_agg(row_to_json(t)) to materialize a JSON array in
/// one round-trip.
///
/// Window calls are expressed as scalar $over nodes inside the
/// p_select array; all funcs share the same partition_by / order_by
/// / frame supplied through the public API.
///
public partial class PostgresQueryProvider
{
///
public override Task ExecuteWindowQueryAsync(
long schemeId,
IEnumerable selectFields,
IEnumerable windowFuncs,
IEnumerable partitionBy,
IEnumerable orderBy,
string? filterJson = null,
string? frameJson = null,
int? take = null,
int? skip = null, CancellationToken cancellationToken = default)
{
return ExecuteWindowQueryInternalAsync(schemeId, selectFields, windowFuncs, partitionBy, orderBy,
filterJson, frameJson, take, skip);
}
///
public override Task ExecuteWindowQueryAsync(
long schemeId,
IEnumerable selectFields,
IEnumerable windowFuncs,
IEnumerable partitionBy,
IEnumerable orderBy,
FilterExpression? filter,
string? frameJson = null,
int? take = null,
int? skip = null, CancellationToken cancellationToken = default)
{
var filterJson = filter is null ? null : _facetBuilder.BuildFacetFilters(filter);
return ExecuteWindowQueryInternalAsync(schemeId, selectFields, windowFuncs, partitionBy, orderBy,
filterJson, frameJson, take, skip);
}
private async Task ExecuteWindowQueryInternalAsync(
long schemeId,
IEnumerable selectFields,
IEnumerable windowFuncs,
IEnumerable partitionBy,
IEnumerable orderBy,
string? filterJson,
string? frameJson,
int? take,
int? skip, CancellationToken cancellationToken = default)
{
var innerSql = await BuildWindowInnerSqlAsync(schemeId, selectFields, windowFuncs, partitionBy, orderBy,
filterJson, frameJson, take, skip);
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