Path: redb/redb.Tests.Integration/Tests/MsSqlPro/MsSqlProArrayGroupByDeepTests.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/MsSqlProArrayGroupByDeepTests.cs ---- using redb.Core; using redb.Core.Models.Entities; using redb.Core.Query.Aggregation; using redb.Tests.Integration.Fixtures; using redb.Tests.Integration.Helpers; using redb.Tests.Integration.Models; using redb.Tests.Integration.Tests.Base; namespace redb.Tests.Integration.Tests.MsSqlPro; [Collection("MsSqlPro")] public class MsSqlProArrayGroupByDeepTests : ArrayGroupByDeepTestsBase { private readonly IRedbService _redb; public MsSqlProArrayGroupByDeepTests(MsSqlProFixture fixture) : base(fixture.Redb) => _redb = fixture.Redb; // Without a condition every element is read anyway: the flat grouping always takes hash joins, since with two fields // of an element the second one otherwise became a many-to-many merge join (E142). [Fact] public async Task UnconditionedGrouping_CarriesTheHashJoinHint() { var sql = await _redb.Query() .GroupByArray(e => e.Contacts!.SelectMany(c => c.Metadata!), m => m.Label) .ToSqlStringAsync(g => new { Label = g.Key, Entries = Agg.Count(g), OfA = Agg.Count(g, m => m.Value == "a") }); sql.Should().Contain("OPTION (HASH JOIN, MAXDOP 1)").And.NotContain("IF (SELECT COUNT(*)"); } // With a condition the hint pays only over many objects and loses on a few, where nested loops read only the // matching elements. The streams source (planner on) puts the ids into a temporary table, and the batch picks the // statement by their count; the pivot source (planner off) has no count and runs without the hint. [Fact] public async Task ConditionedGrouping_TakesTheHintOnlyOverManyIds() { var sql = await _redb.Query() .Where(e => e.HomeAddress!.Building!.Floor > 10) .GroupByArray(e => e.Contacts!.SelectMany(c => c.Metadata!), m => m.Label) .ToSqlStringAsync(g => new { Label = g.Key, Entries = Agg.Count(g), OfA = Agg.Count(g, m => m.Value == "a") }); if (ProTestOptions.PvtPrefilter) { sql.Should().Contain("IF (SELECT COUNT(*) FROM (SELECT TOP (10000) _id FROM #redb_ids") .And.Contain(") >= 10000") .And.Contain("OPTION (HASH JOIN, MAXDOP 1);") .And.Contain("\nELSE\n"); sql.Split("FOR JSON PATH").Length.Should().Be(3, "both branches carry the grouping"); } else sql.Should().NotContain("HASH JOIN").And.NotContain("IF (SELECT COUNT(*)"); } // The branch with the hint runs over 10 000 ids, more than the test database holds, so this test lowers the threshold // for one call and the same few objects go through IF ... OPTION (HASH JOIN, MAXDOP 1). The collection runs its tests // one after another, and the threshold is put back in finally. Planner off: the pivot source has no count and no hint. [Fact] public async Task ConditionedGrouping_TheBranchWithTheHint_MatchesTheObjects() { var threshold = typeof(redb.MSSql.Pro.Query.ProQueryProvider).GetProperty( "ArrayGroupHashJoinThreshold", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)!; var before = (int)threshold.GetValue(null)!; threshold.SetValue(null, 1); try { await ConditionedGrouping_OverAFewObjects_MatchesTheObjects(); } finally { threshold.SetValue(null, before); } } // A condition over a few objects: the ids go into the temporary table (planner on) and the batch runs the grouping // without the hint. The answer is the same LINQ over the objects. [Fact] public async Task ConditionedGrouping_OverAFewObjects_MatchesTheObjects() { await _redb.SyncSchemeAsync(); var marker = "gh" + Guid.NewGuid().ToString("N")[..10]; var seeded = new List>(); for (var i = 0; i < 6; i++) { var e = TestDataFactory.CreateEmployee(i, firstName: $"{marker}-{i:D2}"); e.Props.Contacts![0].Metadata = [new TestContactDetail { Label = "work", Value = i % 2 == 0 ? "a" : "b" }]; e.Props.Contacts[1].Metadata = [new TestContactDetail { Label = "night", Value = "a" }]; e.Props.HomeAddress!.Building!.Floor = 5 + i * 3; await _redb.SaveAsync(e); seeded.Add(e); } var rows = await _redb.Query() .Where(e => e.FirstName.StartsWith(marker) && e.HomeAddress!.Building!.Floor > 10) .GroupByArray(e => e.Contacts!.SelectMany(c => c.Metadata!), m => m.Label) .SelectAsync(g => new { Label = g.Key, Entries = Agg.Count(g), OfA = Agg.Count(g, m => m.Value == "a") }); var expected = seeded.Where(e => e.Props.HomeAddress!.Building!.Floor > 10) .SelectMany(e => e.Props.Contacts!).SelectMany(c => c.Metadata ?? []) .GroupBy(m => m.Label) .Select(g => new { Label = g.Key, Entries = g.Count(), OfA = g.Count(m => m.Value == "a") }) .ToList(); expected.Should().NotBeEmpty("precondition: some of the objects match"); rows.Should().BeEquivalentTo(expected); } // The tree grouping does not: its node set may be a recursive CTE, and a hash-join hint leaves SQL Server without a // plan for it (error 8622). A change here is a change of the tree path, not of this test. [Fact] public async Task TreeGroupingStatement_CarriesNoJoinHint() { await _redb.SyncSchemeAsync(); var root = new TreeRedbObject { name = "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) .GroupByArray(e => e.Contacts!, c => c.Type) .ToSqlStringAsync(g => new { Type = g.Key, Count = Agg.Count(g), Verified = Agg.Count(g, c => c.IsVerified == true) }); sql.Should().NotContain("HASH JOIN"); } }