Path: redb/redb.Tests.Integration/Tests/Base/LiteralStructureIdsTestsBase.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/Base/LiteralStructureIdsTestsBase.cs
----
using System.Text.RegularExpressions;
using redb.Core;
using redb.Core.Query.Aggregation;
using redb.Tests.Integration.Models;
namespace redb.Tests.Integration.Tests.Base;
///
/// SQL Server Pro and SQLite Pro write the structure id of a field into the statement text, never as a parameter: on
/// SQL Server a plan compiled for one field was reused for another of the same statement shape (12 rows against 100 000,
/// 404 000 reads against 1 700), and filtered or partial indexes serve only a statement that names the structure id.
/// The statements below cover the forms that passed it as a parameter before: the filter and its prefilter, a sort, an
/// array, a dictionary entry, a list item and a grouping. Values stay parameters.
///
public abstract class LiteralStructureIdsTestsBase
{
protected readonly IRedbService Redb;
protected LiteralStructureIdsTestsBase(IRedbService redb) => Redb = redb;
// A structure id compared with or listed against a parameter (@p0, $1, :p0), or read from a JSON array parameter.
private static readonly Regex StructureIdAsParameter =
new(@"_id_structure\s*(=|IN\s*\()\s*(\(?\s*SELECT\s+value\s+FROM\s+json_each\s*\()?\s*[@$:]", RegexOptions.IgnoreCase);
private static readonly Regex StructureIdAsLiteral = new(@"_id_structure\s*(=\s*-?\d+|IN\s*\(\s*-?\d+)", RegexOptions.IgnoreCase);
private static void ShouldNameStructureIdsInTheText(string sql, string what)
{
StructureIdAsParameter.Matches(sql).Select(m => m.Value).Should().BeEmpty($"{what}: a structure id is metadata, written into the text");
StructureIdAsLiteral.IsMatch(sql).Should().BeTrue($"{what}: the statement names its structures");
}
[Fact]
public async Task FilterAndSort_NameTheirStructuresInTheText()
{
await Redb.SyncSchemeAsync();
var sql = await Redb.Query()
.Where(e => e.Salary > 50000m && e.Age >= 30)
.OrderBy(e => e.Department).ThenByDescending(e => e.HireDate)
.Take(10)
.ToSqlStringAsync();
ShouldNameStructureIdsInTheText(sql, "filter, sort and page");
}
[Fact]
public async Task ArrayAndDictionaryFilters_NameTheirStructuresInTheText()
{
await Redb.SyncSchemeAsync();
var array = await Redb.Query().Where(e => e.Skills!.Contains("C#")).Take(10).ToSqlStringAsync();
ShouldNameStructureIdsInTheText(array, "array Contains");
var entry = await Redb.Query().Where(e => e.PhoneDirectory!["home"] == "555").Take(10).ToSqlStringAsync();
ShouldNameStructureIdsInTheText(entry, "dictionary entry");
}
[Fact]
public async Task ListItemFilter_NamesItsStructureInTheText()
{
await Redb.SyncSchemeAsync();
var sql = await Redb.Query().Where(p => p.Status!.Value == "Active").Take(10).ToSqlStringAsync();
ShouldNameStructureIdsInTheText(sql, "list item");
}
[Fact]
public async Task Grouping_NamesItsStructuresInTheText()
{
await Redb.SyncSchemeAsync();
var sql = await Redb.Query()
.Where(e => e.Salary > 50000m)
.GroupBy(e => e.Department)
.ToSqlStringAsync(g => new { Department = g.Key, Count = Agg.Count(g), Total = Agg.Sum(g, e => e.Salary) });
ShouldNameStructureIdsInTheText(sql, "grouping");
}
}