Path: redb/redb.SQLite/Query/SqliteQueryProvider.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.SQLite/Query/SqliteQueryProvider.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.SQLite.Query; /// /// PVT-backed GroupBy + Window Functions for free SQLite. 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 SqliteQueryProvider { /// 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) { var sql = await BuildGroupedWindowSqlAsync( schemeId, groupFields, aggregations, windowFuncs, partitionBy, orderBy, filterJson, havingJson); // SQLite has no json_agg/row_to_json: run the windowed SELECT and serialize rows in C#. var jsonArray = await _context.QueryRowsAsJsonAsync(sql, System.Array.Empty()); if (string.IsNullOrEmpty(jsonArray)) return null; return JsonDocument.Parse(jsonArray); } private async Task BuildGroupedWindowSqlAsync( long schemeId, IEnumerable groupFields, IEnumerable aggregations, IEnumerable windowFuncs, IEnumerable partitionBy, IEnumerable orderBy, string? filterJson, string? havingJson, CancellationToken cancellationToken = default) { 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 groupByJson = BuildPvtGroupByJson(groupList); var aggregationsJson = aggList.Count == 0 ? null : BuildPvtAggregationsJson(aggList); object filterParam = string.IsNullOrEmpty(filterJson) || filterJson == "{}" || filterJson == "null" ? DBNull.Value : (object)filterJson!; object aggParam = aggregationsJson is null ? DBNull.Value : (object)aggregationsJson; object havingParam = string.IsNullOrEmpty(havingJson) || havingJson == "{}" || havingJson == "null" ? DBNull.Value : (object)havingJson!; var invocation = "SELECT pvt_build_groupby_sql(" + schemeId.ToString(CultureInfo.InvariantCulture) + ", $1, $2, $3, $4, NULL, NULL, 0, 'flat', NULL, NULL, 1, 1) AS \"Value\""; _logger?.LogDebug("PVT GroupedWindow Build: SchemeId={SchemeId}, GroupBy={GroupBy}, Aggs={Aggs}, Filter={Filter}", schemeId, groupByJson, aggregationsJson ?? "null", filterJson ?? "null"); var innerSql = await _context.ExecuteScalarAsync(invocation, new object[] { filterParam, groupByJson, aggParam, havingParam }, cancellationToken); if (string.IsNullOrWhiteSpace(innerSql)) throw new InvalidOperationException( "pvt_build_groupby_sql returned an empty SQL string for scheme " + schemeId + "."); // Returns the windowed SELECT itself; the caller serializes rows to JSON in C# // (SQLite has no json_agg/row_to_json). Preview surfaces see the real SQL. return GroupedWindowSelect.Build( innerSql, groupList, aggList, shownAggregates, windowList, partitionList, orderList, QuoteIdent, rankingNeedsOrder: false); } private static string QuoteIdent(string identifier) { // Match SQLite's standard double-quote identifier escaping. return "\"" + identifier.Replace("\"", "\"\"") + "\""; } }