Path: redb/redb.Tests.Integration/Tests/MsSqlPro/MsSqlProWholeSchemeGroupingHintTests.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.Tests.Integration/Tests/MsSqlPro/MsSqlProWholeSchemeGroupingHintTests.cs
----
using redb.Core;
using redb.Core.Models.Entities;
using redb.Core.Query.Aggregation;
using redb.Core.Query.Window;
using redb.Tests.Integration.Fixtures;
using redb.Tests.Integration.Helpers;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.MsSqlPro;
///
/// A GroupBy over the whole scheme on the streams source (planner on) closes with hash joins: with two or more fields
/// the optimizer otherwise joined their value rows many-to-many through worktables (E130a). Any condition, the aggregate
/// pivot of the planner-off pass, a window over the groups and a tree keep the statement as it was.
///
[Collection("MsSqlPro")]
public class MsSqlProWholeSchemeGroupingHintTests
{
private const string Hint = "OPTION (HASH JOIN, MAXDOP 1)";
private readonly IRedbService _redb;
public MsSqlProWholeSchemeGroupingHintTests(MsSqlProFixture fixture) => _redb = fixture.Redb;
[Fact]
public async Task WholeSchemeGrouping_TakesTheHashJoinHint_OnlyOnTheStreamsSource()
{
await _redb.SyncSchemeAsync();
var sql = await _redb.Query()
.GroupBy(e => new { e.Department, e.Position })
.ToSqlStringAsync(g => new { g.Key.Department, g.Key.Position, Total = Agg.Sum(g, e => e.Salary), Count = Agg.Count(g) });
if (ProTestOptions.PvtPrefilter)
sql.TrimEnd().Should().EndWith(Hint);
else
sql.Should().NotContain("HASH JOIN", "the aggregate pivot lost with hash joins (1.7 s -> 6.3 s)");
}
[Fact]
public async Task WholeSchemeGroupingWithHaving_TakesTheHint_OnlyOnTheStreamsSource()
{
await _redb.SyncSchemeAsync();
var sql = await _redb.Query()
.GroupBy(e => e.Department)
.Having(g => Agg.Count(g) > 5)
.ToSqlStringAsync(g => new { Department = g.Key, Count = Agg.Count(g) });
if (ProTestOptions.PvtPrefilter)
sql.TrimEnd().Should().EndWith(Hint);
else
sql.Should().NotContain("HASH JOIN");
}
[Fact]
public async Task GroupingWithAWhere_KeepsItsStatement()
{
await _redb.SyncSchemeAsync();
var sql = await _redb.Query()
.Where(e => e.Salary > 60000m)
.GroupBy(e => new { e.Department, e.Position })
.ToSqlStringAsync(g => new { g.Key.Department, g.Key.Position, Count = Agg.Count(g) });
sql.Should().NotContain("HASH JOIN");
}
[Fact]
public async Task GroupingWithABaseFieldCondition_KeepsItsStatement()
{
await _redb.SyncSchemeAsync();
var sql = await _redb.Query()
.WhereRedb(o => o.Id > 0)
.GroupBy(e => new { e.Department, e.Position })
.ToSqlStringAsync(g => new { g.Key.Department, g.Key.Position, Count = Agg.Count(g) });
sql.Should().NotContain("HASH JOIN");
}
[Fact]
public async Task WindowOverTheGroups_KeepsItsStatement()
{
await _redb.SyncSchemeAsync();
var sql = await _redb.Query()
.GroupBy(e => e.Department)
.WithWindow(w => w.OrderByDesc(g => Agg.Sum(g, e => e.Salary)))
.ToSqlStringAsync(g => new { Department = g.Key, Total = Agg.Sum(g, e => e.Salary), Rank = Win.Rank() });
sql.Should().NotContain("HASH JOIN");
}
[Fact]
public async Task TreeGrouping_KeepsItsStatement()
{
await _redb.SyncSchemeAsync();
var root = new TreeRedbObject
{
name = "grp-hint-root-" + Guid.NewGuid().ToString("N")[..8],
Props = TestDataFactory.CreateEmployee(0).Props
};
root.id = await _redb.SaveAsync(root);
var sql = await _redb.TreeQuery(root.id)
.GroupBy(e => new { e.Department, e.Position })
.ToSqlStringAsync(g => new { g.Key.Department, g.Key.Position, Count = Agg.Count(g) });
sql.Should().NotContain("HASH JOIN");
}
}