LEFT JOIN collapsed to INNER by a WHERE clause
Problem
Section titled “Problem”The Org Admin dashboard shows ticket counts by status. The chart renders fine for active orgs. When a new org is created — or an existing org has no reopened tickets — the corresponding bucket simply doesn’t appear. The frontend assumes a complete enum and renders undefined.
The code
Section titled “The code”Why review misses it
Section titled “Why review misses it”Engineers know LEFT JOIN ... WHERE right_side = X is risky in the abstract — but here the WHERE reads as the tenant filter. It scans as “show every status; filter by org.” The “filter by org” reads as a constraint on the result, not as a transformation of the join.
The example test that passes
Section titled “The example test that passes”Seeds one ticket in each status. All four buckets present. Test green. The bug only fires when a bucket is empty.
The SqlProof property
Section titled “The SqlProof property”A second property asserts sum(counts) == count(*) — an even tighter aggregation invariant that holds even for the buggy version (dropping zero-count buckets doesn’t change the sum), demonstrating that one property doesn’t always cover everything.
The counterexample
Section titled “The counterexample”Illustrative — Hypothesis would print the actual draw and assertion:
The fix
Section titled “The fix”Move the org filter into the join condition:
Now the LEFT JOIN really is a LEFT JOIN, regardless of how many tickets each bucket contains.