Path: redb/redb.Tests.Integration/Tests/PostgresPro/PostgresProArrayGroupByDeepTests.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/PostgresPro/PostgresProArrayGroupByDeepTests.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.PostgresPro; [Collection("PostgresPro")] public class PostgresProArrayGroupByDeepTests : ArrayGroupByDeepTestsBase { private readonly IRedbService _redb; public PostgresProArrayGroupByDeepTests(PostgresProFixture fixture) : base(fixture.Redb) => _redb = fixture.Redb; private Task UnconditionedPreviewAsync() => _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") }); private Task> UnconditionedGroupingAsync() => _redb.Query() .GroupByArray(e => e.Contacts!.SelectMany(c => c.Metadata!), m => m.Label) .SelectAsync(g => new GroupRow { Label = g.Key, Entries = Agg.Count(g) }); private sealed class GroupRow { public string? Label { get; set; } public int Entries { get; set; } } private Task NestedLoopsAsync() => _redb.Context.ExecuteScalarAsync("SELECT current_setting('enable_nestloop')"); // Without a condition the grouping runs with nested loops off for its one statement: every element is read anyway, // and the merge of each field's range read 29 000 buffers where the walk over the elements read 1.6 million. [Fact] public async Task UnconditionedGrouping_PreviewShowsTheNestedLoopsSetting() { var sql = await UnconditionedPreviewAsync(); sql.Should().Contain("set_config('enable_nestloop', 'off', true)") .And.Contain("set_config('enable_nestloop', current_setting('redb.nestloop_before'), true)"); } // With a condition the walk over the elements is right for a few objects, so the statement keeps its plan. [Fact] public async Task ConditionedGrouping_PreviewHasNoPlannerSetting() { 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) }); sql.Should().NotContain("enable_nestloop"); } // A tree grouping reads the nodes of its subtree, a narrower set than the scheme, so it counts as conditioned. [Fact] public async Task TreeGrouping_PreviewHasNoPlannerSetting() { await _redb.SyncSchemeAsync(); var root = new TreeRedbObject { name = "nl-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) }); sql.Should().NotContain("enable_nestloop"); } // The setting lives for the grouping statement only: the next statement on the same connection plans with nested // loops on again, outside a transaction and inside the caller's. [Fact] public async Task UnconditionedGrouping_LeavesNestedLoopsOn_OutsideATransaction() { await _redb.SyncSchemeAsync(); (await NestedLoopsAsync()).Should().Be("on", "precondition"); await UnconditionedGroupingAsync(); (await NestedLoopsAsync()).Should().Be("on"); } [Fact] public async Task UnconditionedGrouping_LeavesNestedLoopsOn_InsideTheCallersTransaction() { await _redb.SyncSchemeAsync(); await using var transaction = await _redb.Context.BeginTransactionAsync(); (await NestedLoopsAsync()).Should().Be("on", "precondition"); await UnconditionedGroupingAsync(); (await NestedLoopsAsync()).Should().Be("on", "the setting is put back inside the caller's transaction"); await transaction.CommitAsync(); (await NestedLoopsAsync()).Should().Be("on"); } // A caller that turned nested loops off itself keeps them off: the grouping changes nothing and puts nothing back. [Fact] public async Task UnconditionedGrouping_KeepsTheCallersOwnSetting() { await _redb.SyncSchemeAsync(); await using var transaction = await _redb.Context.BeginTransactionAsync(); await _redb.Context.ExecuteScalarAsync("SELECT set_config('enable_nestloop', 'off', true)"); await UnconditionedGroupingAsync(); (await NestedLoopsAsync()).Should().Be("off"); await transaction.RollbackAsync(); } }