Path: redb/redb.Tests.Integration/Tests/MsSqlPro/MsSqlProDeepTreeTests.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/MsSqlProDeepTreeTests.cs ---- using redb.Core; using redb.Core.Models.Entities; using redb.Core.Query; using redb.Core.Query.Aggregation; using redb.Tests.Integration.Fixtures; using redb.Tests.Integration.Models; namespace redb.Tests.Integration.Tests.MsSqlPro; /// /// An unrooted tree query that needs levels (WhereLevel, a query maxDepth) walks every node up to the real root of its /// tree with a recursive CTE. SQL Server stops a recursion at 100 levels unless the statement says otherwise, and every /// statement form that read that walk failed on a deeper tree ("The maximum recursion 100 has been exhausted"). A chain /// of 120 levels on a scheme of its own, read by each form: the list, the count, Any, the query's maxDepth, Sum, a batch /// aggregate, GroupBy, a window, GroupByArray, and the listing path of WhereHasAncestor. /// [Collection("MsSqlPro")] public class MsSqlProDeepTreeTests { private const int Depth = 120; private readonly IRedbService _redb; public MsSqlProDeepTreeTests(MsSqlProFixture fixture) => _redb = fixture.Redb; private async Task SeedChainAsync() { await _redb.SyncSchemeAsync(); var tag = "deep-" + Guid.NewGuid().ToString("N")[..8]; TreeRedbObject Node(int i) => new() { name = $"{tag}-{i}", Props = new DeepChainProps { Tag = tag, Index = i, Bucket = i % 2 == 0 ? "even" : "odd", Marks = [new() { Kind = "k" }] } }; var previous = Node(0); previous.id = await _redb.SaveAsync(previous); for (var i = 1; i < Depth; i++) { var node = Node(i); node.id = await _redb.CreateChildAsync(node, previous); previous = node; } return tag; } [Fact] public async Task EveryForm_ReadsATreeDeeperThan100Levels() { var tag = await SeedChainAsync(); // Levels count from the real root (level 0): the deepest ten nodes are levels 110..119. IRedbQueryable Deep() => _redb.TreeQuery().Where(n => n.Tag == tag).WhereLevel(l => l >= 110); var deepIndexes = Enumerable.Range(110, 10).ToList(); (await Deep().ToListAsync()).Select(o => o.Props.Index).Should().BeEquivalentTo(deepIndexes, "the list"); (await Deep().CountAsync()).Should().Be(10, "the count"); (await _redb.TreeQuery().Where(n => n.Tag == tag).WhereLevel(l => l >= Depth - 1).AnyAsync()) .Should().BeTrue("Any"); (await _redb.TreeQuery().Where(n => n.Tag == tag).WithMaxDepth(115).CountAsync()) .Should().Be(116, "the query's maxDepth, levels 0..115"); (await Deep().SumAsync(n => n.Index)).Should().Be(deepIndexes.Sum(), "Sum"); var batch = await Deep().AggregateAsync(x => new { Count = Agg.Count(), Max = Agg.Max(x.Props.Index) }); batch.Count.Should().Be(10, "a batch aggregate"); batch.Max.Should().Be(Depth - 1); var groups = await Deep().GroupBy(n => n.Bucket).SelectAsync(g => new { g.Key, C = Agg.Count(g) }); groups.ToDictionary(g => g.Key, g => g.C).Should().BeEquivalentTo(new Dictionary { ["even"] = 5, ["odd"] = 5 }, "GroupBy"); var window = await Deep().WithWindow(w => w.OrderBy(n => n.Index)).SelectAsync(x => new { x.Id, Row = Win.RowNumber() }); window.Should().HaveCount(10, "a window"); var marks = await Deep().GroupByArray(n => n.Marks!, m => m.Kind).SelectAsync(g => new { g.Key, C = Agg.Count(g) }); marks.Single().C.Should().Be(10, "GroupByArray"); var listed = await _redb.TreeQuery().Where(n => n.Tag == tag) .WhereHasAncestor(n => n.Tag == tag && n.Index == 100).WhereLevel(l => l >= 110).ToListAsync(); // The ancestor at level 100: the listing reaches the default 50 levels below it, so 110..119 are in. listed.Select(o => o.Props.Index).Should().BeEquivalentTo(deepIndexes, "the listing path of WhereHasAncestor"); } /// /// A tree query under one root or one parent walks down with a recursive CTE when the planner is off; with a depth /// limit above 100 that recursion went past SQL Server's default of 100 levels on the chain. The list, the count and /// the subtree that WhereHasAncestor looks for its ancestors in, each under the node at the top of the chain. /// [Fact] public async Task UnderOneRootOrParent_PlannerOff_ReadsMoreThan100LevelsDown() { var tag = await SeedChainAsync(); var top = (await _redb.TreeQuery().Where(n => n.Tag == tag && n.Index == 0).ToListAsync()).Single().Id; var below = Enumerable.Range(1, Depth - 1).ToList(); var restore = _redb.Configuration.EnablePvtPrefilter; try { _redb.UpdateConfiguration(c => c.EnablePvtPrefilter = false); (await _redb.TreeQuery(top, 150).Where(n => n.Tag == tag).ToListAsync()) .Select(o => o.Props.Index).Should().BeEquivalentTo(below, "under the root"); (await _redb.TreeQuery(top, 150).Where(n => n.Tag == tag).CountAsync()) .Should().Be(Depth - 1, "the count under the root"); (await _redb.TreeQuery(new[] { top }, 150).Where(n => n.Tag == tag).ToListAsync()) .Select(o => o.Props.Index).Should().BeEquivalentTo(below, "under one parent"); var under = await _redb.TreeQuery(top, 150).Where(n => n.Tag == tag) .WhereHasAncestor(n => n.Tag == tag && n.Index == 105).ToListAsync(); under.Select(o => o.Props.Index).Should().BeEquivalentTo(Enumerable.Range(106, Depth - 106), "WhereHasAncestor looks for the ancestor in the subtree of the root"); } finally { _redb.UpdateConfiguration(c => c.EnablePvtPrefilter = restore); } } /// /// WhereHasAncestor under a root lists the descendants of the matching ancestors first, with a recursion as deep as its /// maxDepth: 150 below the node at level 5 of the chain reaches the bottom, 114 levels down, past SQL Server's default /// of 100. /// [Fact] public async Task UnderARoot_WhereHasAncestor_ListsMoreThan100LevelsDown() { var tag = await SeedChainAsync(); var top = (await _redb.TreeQuery().Where(n => n.Tag == tag && n.Index == 0).ToListAsync()).Single().Id; var under = await _redb.TreeQuery(top, 150).Where(n => n.Tag == tag) .WhereHasAncestor(n => n.Tag == tag && n.Index == 5, 150).ToListAsync(); under.Select(o => o.Props.Index).Should().BeEquivalentTo(Enumerable.Range(6, Depth - 6)); } /// /// WhereHasDescendant with no root, no parents and no other tree filter climbs in the batch of the statement that /// reads the nodes (a loop over a table variable, one level per pass). A match at the bottom of the chain with maxDepth /// 150 climbs 119 levels to the top; without maxDepth the default 50. List and count, planner off and on. /// [Fact] public async Task InBatch_WhereHasDescendant_ClimbsMoreThan100Levels() { var tag = await SeedChainAsync(); IRedbQueryable Above(int? maxDepth) => _redb.TreeQuery().Where(n => n.Tag == tag) .WhereHasDescendant(n => n.Tag == tag && n.Index == Depth - 1, maxDepth); var restore = _redb.Configuration.EnablePvtPrefilter; try { foreach (var on in new[] { false, true }) { _redb.UpdateConfiguration(c => c.EnablePvtPrefilter = on); var form = on ? "planner on" : "planner off"; (await Above(150).ToListAsync()).Select(o => o.Props.Index) .Should().BeEquivalentTo(Enumerable.Range(0, Depth - 1), $"every node above the bottom, {form}"); (await Above(150).CountAsync()).Should().Be(Depth - 1, $"the count, {form}"); (await Above(null).ToListAsync()).Select(o => o.Props.Index) .Should().BeEquivalentTo(Enumerable.Range(Depth - 51, 50), $"the default 50 levels, {form}"); } } finally { _redb.UpdateConfiguration(c => c.EnablePvtPrefilter = restore); } } }