Path: redb/redb.MSSql/Query/MssqlQueryProvider.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.MSSql/Query/MssqlQueryProvider.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.MSSql.Query; /// /// PVT-backed GroupBy + Window Functions for free MSSql. Mirrors /// PostgresQueryProvider.GroupedWindow.cs but uses T-SQL identifier /// quoting and FOR JSON PATH, INCLUDE_NULL_VALUES for JSON /// materialization. /// /// Architecture: calls BuildGroupByInnerSqlAsync (from /// MssqlQueryProvider.Grouping.cs, same partial class) to generate /// the inner GROUP BY query, then wraps it with an outer SELECT that adds /// window-function columns via OVER (PARTITION BY ... ORDER BY ...). /// public partial class MssqlQueryProvider { /// 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 outerSql = await BuildGroupedWindowSqlAsync( schemeId, groupFields, aggregations, windowFuncs, partitionBy, orderBy, filterJson, havingJson); var jsonArray = await _context.ExecuteScalarAsync(outerSql, System.Array.Empty(), cancellationToken); if (string.IsNullOrEmpty(jsonArray)) return JsonDocument.Parse("[]"); return JsonDocument.Parse(jsonArray); } private async Task BuildGroupedWindowSqlAsync( long schemeId, IEnumerable groupFields, IEnumerable aggregations, IEnumerable windowFuncs, IEnumerable partitionBy, IEnumerable orderBy, string? filterJson, string? havingJson) { var groupList = groupFields?.ToList() ?? new List(); var aggList = aggregations?.ToList() ?? new List(); var windowList = windowFuncs?.ToList() ?? new List(); var partitionList = partitionBy?.ToList() ?? new List(); var orderList = orderBy?.ToList() ?? new List(); if (groupList.Count == 0) throw new ArgumentException( "groupFields must contain at least one entry.", nameof(groupFields)); if (windowList.Count == 0) throw new ArgumentException( "windowFuncs must contain at least one entry.", nameof(windowFuncs)); // An aggregate the window sorts, partitions or reads by need not be in the selector: computed under a private alias. var shownAggregates = aggList.ToList(); aggList = GroupedWindowSelect.WithHiddenAggregates(shownAggregates, windowList, partitionList, orderList); var innerSql = await BuildGroupByInnerSqlAsync( schemeId, groupList, aggList, filterJson, havingJson); var windowedSelect = GroupedWindowSelect.Build( innerSql, groupList, aggList, shownAggregates, windowList, partitionList, orderList, QuoteGwndIdent, rankingNeedsOrder: true); return "SELECT (SELECT * FROM (" + windowedSelect + ") _gwnd_rows FOR JSON PATH, INCLUDE_NULL_VALUES)"; } // ---------------------------------------------------------------- // Windowed SELECT builder // ---------------------------------------------------------------- /// /// T-SQL square-bracket identifier quoting (mirrors PG's double-quote quoting). /// private static string QuoteGwndIdent(string identifier) => "[" + identifier.Replace("]", "]]") + "]"; }