Path: redb/redb.MSSql/Query/MssqlQueryProvider.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.MSSql/Query/MssqlQueryProvider.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.MSSql.Query;
///
/// PVT-backed window functions for free MSSql. Generates the SELECT SQL
/// via the server-side compiler dbo.pvt_build_window_sql and wraps
/// it with FOR JSON PATH, INCLUDE_NULL_VALUES 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. The JSON shape is identical to
/// the PostgreSQL dialect so BuildPvtWindowSelectJson logic is shared.
///
public partial class MssqlQueryProvider
{
///
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 (SELECT * FROM (" + innerSql
+ ") _win_rows FOR JSON PATH, INCLUDE_NULL_VALUES)";
var jsonArray = await _context.ExecuteScalarAsync(wrapped, System.Array.Empty