Path: redb/redb.MSSql/Query/MssqlQueryProvider.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.MSSql/Query/MssqlQueryProvider.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.MSSql.Query;
///
/// PVT-backed GROUP BY for free MSSql. Generates the projection SQL via the
/// server-side compiler dbo.pvt_build_groupby_sql and wraps it with
/// FOR JSON PATH to materialize a JSON array of group rows in one
/// round-trip after the build step.
///
/// Mirrors PostgresQueryProvider.Grouping.cs — every grouping request
/// is compiled by the SQL UDF so non-.NET callers can submit the same
/// filter/group-by/aggregations JSON directly.
///
public partial class MssqlQueryProvider
{
///
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, CancellationToken cancellationToken = default)
{
var groupList = groupFields?.ToList() ?? new List();
var aggList = aggregations?.ToList() ?? new List();
// Empty group-by => degenerate to a flat batch aggregate.
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);
// Wrap the inner SELECT with FOR JSON PATH to materialize a JSON array
// of group rows. INCLUDE_NULL_VALUES preserves null group keys.
var wrapped =
"SELECT (SELECT * FROM (" + innerSql + ") _grp_rows FOR JSON PATH, INCLUDE_NULL_VALUES)";
var jsonArray = await _context.ExecuteScalarAsync(wrapped, System.Array.Empty