Path: redb/redb.Core/Query/Grouping/GroupedWindowSelect.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.Core/Query/Grouping/GroupedWindowSelect.cs
----
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using redb.Core.Query.Aggregation;
using redb.Core.Query.Window;
namespace redb.Core.Query.Grouping;
///
/// The window over the rows of a grouping, as the free providers write it around the grouping SQL their module
/// builds: SELECT g.<columns>, <func> OVER (...) AS <alias> FROM (<grouping>) g. One builder
/// for GroupBy and GroupByArray on every free provider; a reference that names no key and no aggregate is refused.
///
public static class GroupedWindowSelect
{
///
/// The aggregates the grouping must compute: those of the selector, then those the window partitions, orders or
/// reads by without the selector asking for them, under private aliases.
///
public static List WithHiddenAggregates(
IReadOnlyList shown,
IEnumerable functions,
IEnumerable partitionBy,
IEnumerable orderBy)
{
var all = shown.ToList();
all.AddRange(GroupedWindowNames.HiddenAggregates(
partitionBy.Select(p => p.FieldPath)
.Concat(orderBy.Select(o => o.FieldPath))
.Concat(functions.Select(f => f.FieldPath)),
shown));
return all;
}
/// The grouping, computing every aggregate of .
/// Keys of the grouping.
/// Every aggregate the grouping computes, hidden ones included.
/// The aggregates of the selector; hidden ones are left out of the result.
/// Window functions with their aliases.
/// PARTITION BY references.
/// ORDER BY references.
/// Identifier quoting of the dialect.
/// True for a dialect whose window functions require ORDER BY (SQL Server): without
/// an order of its own the window is ordered by the first key.
public static string Build(
string innerSql,
IReadOnlyList groupFields,
IReadOnlyList aggregations,
IReadOnlyList shownAggregates,
IReadOnlyList functions,
IReadOnlyList partitionBy,
IReadOnlyList orderBy,
Func quote,
bool rankingNeedsOrder)
{
if (functions.Count == 0)
throw new ArgumentException("A window over groups needs at least one window function.", nameof(functions));
string Column(string? reference) => "g." + quote(GroupedWindowNames.ResolveColumn(reference, aggregations, groupFields));
var over = new List();
if (partitionBy.Count > 0)
over.Add("PARTITION BY " + string.Join(", ", partitionBy.Select(p => Column(p.FieldPath))));
if (orderBy.Count > 0)
over.Add("ORDER BY " + string.Join(", ", orderBy.Select(o => Column(o.FieldPath) + (o.Descending ? " DESC" : " ASC"))));
else if (rankingNeedsOrder)
over.Add("ORDER BY g." + quote(groupFields[0].Alias) + " ASC");
var overClause = string.Join(" ", over);
var columns = groupFields.Select(g => g.Alias)
.Concat(shownAggregates.Select(a => a.Alias ?? a.Function.ToString()))
.Select(c => "g." + quote(c));
var sb = new StringBuilder("SELECT ");
sb.Append(string.Join(", ", columns));
foreach (var f in functions)
{
var alias = string.IsNullOrEmpty(f.Alias) ? (f.Func ?? string.Empty).ToLowerInvariant() : f.Alias;
sb.Append(", ").Append(FunctionCall(f, Column)).Append(" OVER (").Append(overClause).Append(") AS ").Append(quote(alias));
}
sb.Append(" FROM (").Append(innerSql).Append(") g");
return sb.ToString();
}
private static string FunctionCall(WindowFuncRequest f, Func column)
{
var hasArg = !string.IsNullOrEmpty(f.FieldPath);
return (f.Func ?? string.Empty).ToUpperInvariant() switch
{
"ROWNUMBER" or "ROW_NUMBER" => "ROW_NUMBER()",
"RANK" => "RANK()",
"DENSERANK" or "DENSE_RANK" => "DENSE_RANK()",
"PERCENTRANK" or "PERCENT_RANK" => "PERCENT_RANK()",
"CUMEDIST" or "CUME_DIST" => "CUME_DIST()",
"NTILE" => "NTILE(" + (f.Buckets ?? 4).ToString(CultureInfo.InvariantCulture) + ")",
"SUM" when hasArg => "SUM(" + column(f.FieldPath) + ")",
"AVG" when hasArg => "AVG(" + column(f.FieldPath) + ")",
"MIN" when hasArg => "MIN(" + column(f.FieldPath) + ")",
"MAX" when hasArg => "MAX(" + column(f.FieldPath) + ")",
"COUNT" when hasArg => "COUNT(" + column(f.FieldPath) + ")",
"COUNT" => "COUNT(*)",
"LAG" when hasArg => "LAG(" + column(f.FieldPath) + ")",
"LEAD" when hasArg => "LEAD(" + column(f.FieldPath) + ")",
"FIRSTVALUE" or "FIRST_VALUE" when hasArg => "FIRST_VALUE(" + column(f.FieldPath) + ")",
"LASTVALUE" or "LAST_VALUE" when hasArg => "LAST_VALUE(" + column(f.FieldPath) + ")",
_ => WindowFunctionName.ZeroArgCall(f.Func)
};
}
}