Path: redb/redb.Examples/Examples/E201_NullCheckBaseIdAmbiguity.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.Examples/Examples/E201_NullCheckBaseIdAmbiguity.cs ---- using System.Diagnostics; using System.Text; using System.Text.RegularExpressions; using redb.Core; using redb.Core.Query; using redb.Examples.Models; using redb.Examples.Output; namespace redb.Examples.Examples; /// /// Reproduces the ambiguous _id defect in the PVT CTE. /// /// A props filter == null switches the PVT CTE into the two-table form /// (FROM _objects o_src LEFT JOIN _values v), while the base-field filter /// is compiled without a table alias. _id is the only column present in /// BOTH _objects and _values, so the combination produces /// column reference "_id" is ambiguous. /// /// Four cases: two controls that must stay green, the repro, and a control that /// pins the diagnosis to _id specifically (_id_parent exists only /// in _objects, so it must stay green even in the two-table form). /// /// Scope: verified against redb.Postgres.Pro. MSSql.Pro and SQLite.Pro generate /// the same shape but are out of scope for now. /// [ExampleMeta("E201", "NullCheck + base Id: ambiguous _id in PVT CTE", "Query", ExampleTier.Pro, 200, "Sql", "Debug", "Bug", "Pro", "Pvt", "NullCheck", RelatedApis = ["IRedbQueryable.ToSqlStringAsync", "IRedbQueryable.WhereRedb"])] public class E201_NullCheckBaseIdAmbiguity : ExampleBase { /// Bare _id: not preceded by a dot or word char, not followed by a word char. private static readonly Regex BareId = new(@"(? RunAsync(IRedbService redb) { var sw = Stopwatch.StartNew(); var log = new StringBuilder(); var summary = new List(); var suspects = 0; async Task CaseAsync(string label, string expectation, IRedbQueryable query) { log.AppendLine("-----------------------------------------------------------------"); log.AppendLine($"-- {label}"); log.AppendLine($"-- expected: {expectation}"); log.AppendLine("-----------------------------------------------------------------"); string sql; try { sql = await query.ToSqlStringAsync(); } catch (Exception ex) { log.AppendLine($"-- SQL BUILD FAIL: {ex.GetType().Name}: {ex.Message}"); log.AppendLine(); summary.Add($"{label}: SQL build failed ({ex.GetType().Name})"); Console.WriteLine($"[BUILD-FAIL] {label}: {ex.Message}"); return; } log.AppendLine(sql); log.AppendLine(); // Console.WriteLine(); // Console.WriteLine($"--- {label} (expected: {expectation}) ---"); // Console.WriteLine(sql); // Static verdict: the two-table CTE form together with an unqualified _id. var twoTableForm = sql.Contains("FROM _objects o_src"); var hasBareId = BareId.IsMatch(sql); var verdict = twoTableForm && hasBareId ? "AMBIGUOUS" : "clean"; if (verdict == "AMBIGUOUS") suspects++; // Dynamic verdict: does the database actually accept it. string execVerdict; try { var rows = await query.ToListAsync(); execVerdict = $"executed, {rows.Count} rows"; } catch (Exception ex) { execVerdict = $"{ex.GetType().Name}: {ex.Message.Split('\n')[0]}"; } log.AppendLine($"-- static: two-table form={twoTableForm}, bare _id={hasBareId} => {verdict}"); log.AppendLine($"-- runtime: {execVerdict}"); log.AppendLine(); summary.Add($"{label}: {verdict} | {execVerdict}"); Console.WriteLine($"[{verdict,9}] {label} -> {execVerdict}"); } // 1. Control. Props == null alone: two-table form, but no base filter, so nothing to qualify. await CaseAsync( "control: props == null only", "clean", redb.Query() .Where(e => e.EmployeeCode == null) .Take(10)); // 2. Control. Base Id alone: single-table subquery over _objects, name resolves in its own scope. await CaseAsync( "control: base Id only", "clean", redb.Query() .WhereRedb(o => o.Id > 0) .Where(e => e.Age > 0) .Take(10)); // 3. Repro. Both at once. await CaseAsync( "repro: props == null + base Id", "AMBIGUOUS", redb.Query() .Where(e => e.EmployeeCode == null) .WhereRedb(o => o.Id > 0) .Take(10)); // 4. Control. Same shape, but the base column exists only in _objects. // Proves the defect is specific to _id and not to base-field pushdown in general. await CaseAsync( "control: props == null + base ParentId", "clean", redb.Query() .Where(e => e.EmployeeCode == null) .WhereRedb(o => o.ParentId != null) .Take(10)); string? outPath = null; try { var dir = Path.Combine(AppContext.BaseDirectory, "Output"); Directory.CreateDirectory(dir); outPath = Path.Combine(dir, "nullcheck_baseid_ambiguity.txt"); await File.WriteAllTextAsync(outPath, log.ToString()); } catch (Exception ex) { Console.WriteLine($"[WARN] failed to write dump file: {ex.Message}"); } sw.Stop(); // The run time above is the sum over all the queries of the example, each printed and then // executed, so the count comes first: without it the figure reads as one slow query. var output = new List { $"{summary.Count} queries in this example: SQL printed, then each executed" }; output.AddRange(summary); output.AddRange(new[] { $"Suspect cases: {suspects} (expected 1: the repro)", outPath != null ? $"Output: {outPath}" : "Output file: " }); return Ok("E201", "NullCheck + base Id: ambiguous _id in PVT CTE", ExampleTier.Pro, sw.ElapsedMilliseconds, summary.Count, output.ToArray()); } }