Semantic View QA Invariants - Fixes Applied
Summary
Fixed 3 critical issues in QA invariants to prevent false PASS/FAIL signals and ensure accurate validation.Fix 1: Verified stage3_component_id Projection ✅
Status: Already correct - stage3_component_id is projected in view final SELECT (line 236)
The view projects s1c.stage3_component_id which is derived from the stage3_allocations CTE using this hash:
Fix 2: Removed FLOAT64 Paths in Tolerance Logic ✅
Problem: Tolerance comparisons could accidentally coerce NUMERIC to FLOAT64. Solution: All tolerance checks now use explicit NUMERIC casts:- Invariant #1: Stage3 Component Allocation Tie-Out
- Invariant #4: Allocation Sum Reconciliation
Fix 3: Robust Tenant Isolation Checks ✅
Problem: Original tenant isolation query had two issues:- NULL handling in join comparisons could undercount leakage
- Only checked Stage1 hash match, not dimension joins
Check A: Dimension Join Tenant Scoping
Validates that all dimension table joins (dim_business_mapping, dim_agent_hierarchy, dim_rule_definitions, dim_plan_versions) are tenant-scoped:
Check B: Stage1 Source Tenant Match
Validates that every view row has a matching Stage1 source row with the same tenant_id:Improvement A: Tenant+Period Scoping ✅
Added: Parameterized scoping comments to all Stage3 source queries:- Reduces query cost during testing
- Prevents misinterpretation of results
- Easy to enable/disable scoping
- Invariant #1: Stage3 Component Allocation Tie-Out
- Invariant #2: No Stage3 Component Row Loss
- Invariant #4: Allocation Sum Reconciliation
Hash Signature Verification
stage3_component_id Hash (View & Tests)
Both use identical signature:
stage1_row_id Hash (View & Tests)
Both use identical signature:
Validation Checklist
Before running QA invariants:- View projects
stage3_component_idin final SELECT - Hash signatures match exactly between view and tests
- All tolerance checks use NUMERIC casts (no FLOAT64)
- Tenant isolation checks validate dimension joins + Stage1 source
- Tenant+period scoping comments added (can be enabled for testing)
Expected Results
All invariants should return 0 failure rows when run against a correctly deployed view:- Invariant #1: 0 rows (all components tie out within 0.01 tolerance)
- Invariant #2: 0 rows (all Stage3 components present)
- Invariant #3: 0 rows (all Stage1 rows present)
- Invariant #4: 0 rows (all businesses reconcile within tolerance)
- Invariant #5: Deterministic counts (same on rerun)
- Invariant #6: All checks show
mismatch_count = 0(PASS)