Path: redb/redb.SQLite/Query/SqliteQueryProvider.Grouping.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.Grouping.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.Aggregation; using redb.Core.Query.Grouping; using redb.Core.Query.QueryExpressions; namespace redb.SQLite.Query; /// /// PVT-backed GROUP BY for free SQLite. Generates the projection SQL /// via the server-side compiler pvt_build_groupby_sql and wraps it /// with json_agg(row_to_json(t)) to materialize a JSON array of /// group rows in one round-trip after the build step. /// /// Overrides both string-based and FilterExpression-based virtual methods /// on so callers reach /// the PVT compiler regardless of which surface they use. /// public partial class SqliteQueryProvider { /// public override Task ExecuteGroupedAggregateAsync( long schemeId, IEnumerable groupFields, IEnumerable aggregations, string? filterJson = null, string? havingJson = null, CancellationToken cancellationToken = default) { return ExecuteGroupedAggregateInternalAsync(schemeId, groupFields, aggregations, filterJson, havingJson); } /// public override Task ExecuteGroupedAggregateAsync( long schemeId, IEnumerable groupFields, IEnumerable aggregations, FilterExpression? filter, string? havingJson = null, CancellationToken cancellationToken = default) { var filterJson = filter is null ? null : _facetBuilder.BuildFacetFilters(filter); return ExecuteGroupedAggregateInternalAsync(schemeId, groupFields, aggregations, filterJson, havingJson); } private async Task ExecuteGroupedAggregateInternalAsync( long schemeId, IEnumerable groupFields, IEnumerable aggregations, string? filterJson, string? havingJson = null) { var groupList = groupFields?.ToList() ?? new List(); var aggList = aggregations?.ToList() ?? new List(); // Empty group-by => degenerate to a flat batch aggregate; *RedbAsync helpers // (SumRedbAsync, AverageRedbAsync, ...) intentionally pass zero group keys to // request a single-row aggregate over base IRedbObject columns. if (groupList.Count == 0) { if (!string.IsNullOrEmpty(havingJson)) throw new NotSupportedException( "HAVING requires GROUP BY keys; cannot be combined with a flat aggregate batch."); if (aggList.Count == 0) return null; var aggResult = await ExecuteAggregateBatchInternalAsync(schemeId, aggList, filterJson); var rowJson = JsonSerializer.Serialize(new[] { aggResult.Values }); return JsonDocument.Parse(rowJson); } var innerSql = await BuildGroupByInnerSqlAsync(schemeId, groupList, aggList, filterJson, havingJson); // SQLite has no json_agg/row_to_json: run the inner SQL and serialize rows in C#. var jsonArray = await _context.QueryRowsAsJsonAsync(innerSql, System.Array.Empty()); if (string.IsNullOrEmpty(jsonArray)) return null; return JsonDocument.Parse(jsonArray); } /// /// Returns the inner SELECT compiled by pvt_build_groupby_sql for the /// requested grouping. This is the same first-pass SQL that /// later wraps with /// json_agg(row_to_json(t)); exposed so ToSqlStringAsync and /// other diagnostics surfaces can see the actual SQL. /// public override async Task GetGroupBySqlPreviewAsync( long schemeId, IEnumerable groupFields, IEnumerable aggregations, string? filterJson, string? havingJson = null, CancellationToken cancellationToken = default) { var groupList = groupFields?.ToList() ?? new List(); var aggList = aggregations?.ToList() ?? new List(); if (groupList.Count == 0) throw new ArgumentException("A GroupBy SQL preview needs at least one key.", nameof(groupFields)); return await BuildGroupByInnerSqlAsync(schemeId, groupList, aggList, filterJson, havingJson); } /// /// FilterExpression overload of . /// public override Task GetGroupBySqlPreviewAsync( long schemeId, IEnumerable groupFields, IEnumerable aggregations, FilterExpression? filter, string? havingJson = null, CancellationToken cancellationToken = default) { var filterJson = filter is null ? null : _facetBuilder.BuildFacetFilters(filter); return GetGroupBySqlPreviewAsync(schemeId, groupFields, aggregations, filterJson, havingJson); } private async Task BuildGroupByInnerSqlAsync( long schemeId, IList groupList, IList aggList, string? filterJson, string? havingJson = null, CancellationToken cancellationToken = default) { 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 == "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 GroupBy Build: SchemeId={SchemeId}, GroupBy={GroupBy}, Aggs={Aggs}, Filter={Filter}, Having={Having}", schemeId, groupByJson, aggregationsJson ?? "null", filterJson ?? "null", havingJson ?? "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 + "."); return innerSql!; } /// /// Serializes group fields to the JSON shape consumed by /// pvt_build_groupby_sql: [{"field":"Department","alias":"d"}]. /// Base fields () get the 0$: /// prefix so PVT's pvt_normalize_base_field_name resolves them to the /// underlying _objects column. Underscore-prefixed names and inputs /// that already carry 0$: are passed through. /// private static string BuildPvtGroupByJson(IList groupFields) { var entries = new List>(groupFields.Count); foreach (var g in groupFields) { var entry = new Dictionary { ["field"] = NormalizePvtFieldName(g.FieldPath, g.IsBaseField) }; if (!string.IsNullOrEmpty(g.Alias)) entry["alias"] = g.Alias!; entries.Add(entry); } return JsonSerializer.Serialize(entries); } /// /// Returns with the 0$: base-field prefix /// applied when is true and the path is not /// already prefixed / underscore-prefixed. Used by GROUP BY, ORDER BY and /// window partition / order entries that flow through PVT. /// internal static string NormalizePvtFieldName(string? fieldPath, bool isBaseField) { var path = fieldPath ?? string.Empty; if (!isBaseField) return path; if (path.Length == 0 || path.StartsWith("0$:", StringComparison.Ordinal) || path[0] == '_') return path; return "0$:" + path; } /// /// GroupByArray on the free SQLite provider: pvt_build_array_groupby_sql of the extension builds the /// grouping (and its HAVING); a window over the groups is written around it by . /// SQLite has no json_agg, so the rows are packed into JSON in C#, NULL values included. /// public override async Task ExecuteArrayGroupQueryAsync(ArrayGroupQuery query, CancellationToken cancellationToken = default) { var sql = await BuildArrayGroupSqlAsync(query, cancellationToken); var json = await _context.QueryRowsAsJsonAsync(sql, System.Array.Empty(), cancellationToken); if (string.IsNullOrEmpty(json)) throw new InvalidOperationException("GroupByArray: the grouping returned no rows document for scheme " + query.SchemeId + "."); return JsonDocument.Parse(json); } /// public override Task GetArrayGroupQuerySqlAsync(ArrayGroupQuery query, CancellationToken cancellationToken = default) => BuildArrayGroupSqlAsync(query, cancellationToken); private async Task BuildArrayGroupSqlAsync(ArrayGroupQuery query, CancellationToken cancellationToken) { query.EnsureValid(); var filterJson = query.Filter != null ? _facetBuilder.BuildFacetFilters(query.Filter) : query.FilterJson; var shown = query.Aggregations.ToList(); var aggregations = query.Window == null ? shown : GroupedWindowSelect.WithHiddenAggregates(shown, query.Window.Functions, query.Window.PartitionBy, query.Window.OrderBy); var invocation = "SELECT pvt_build_array_groupby_sql(" + query.SchemeId.ToString(CultureInfo.InvariantCulture) + ", $1, $2, $3, $4, $5, NULL, NULL, 0) AS \"Value\""; var innerSql = await _context.ExecuteScalarAsync(invocation, new object[] { query.ArrayPath, ArrayGroupModuleJson.Param(filterJson), ArrayGroupModuleJson.GroupBy(query.GroupFields), ArrayGroupModuleJson.Param(ArrayGroupModuleJson.Aggregations(aggregations)), ArrayGroupModuleJson.Param(query.HavingJson) }, cancellationToken); if (string.IsNullOrWhiteSpace(innerSql)) throw new InvalidOperationException( "pvt_build_array_groupby_sql returned an empty SQL string for scheme " + query.SchemeId + "."); return query.Window == null ? innerSql : GroupedWindowSelect.Build(innerSql, query.GroupFields, aggregations, shown, query.Window.Functions, query.Window.PartitionBy, query.Window.OrderBy, QuoteIdent, rankingNeedsOrder: false); } }