Semantic Layer Implementation - Revision Summary
Revision Requests Addressed
1. ✅ NUMERIC/BIGNUMERIC for Money Fields
Changed: AllFLOAT64 money fields → NUMERIC(18, 2)
Files Updated:
semantic_view_commission_traceability.sql:stage1_credit,stage1_debit,stage1_total,allocated_commission_amount→NUMERIC(18, 2)
2. ✅ Stage1 Row-Grain Redesign
Changed:view_commission_traceability grain from transaction-level → Stage1 row-level
New Grain: (tenant_id, period_label, stage1_row_id, component_type) - one row per Stage1 record + component allocation
Key Fields Added:
stage1_row_id: Deterministic hash of(tenant_id, period_label, business_label, member_id, payee_agent_id, period_code)allocated_commission_amount: How much of this Stage1 row went to each component (proportional allocation)component_type: TPA, OWNER, MIXED_OWNER, etc.
3. ✅ Deterministic IDs (No created_at)
Changed: Removedcreated_at from ID generation
Old: CONCAT(..., CAST(created_at AS STRING))New:
TO_HEX(SHA256(CONCAT(tenant_id, period_label, business_label, member_id, payee_agent_id, period_code)))
Fields Updated:
stage1_row_id: Deterministic hash (no timestamps)rule_hash: Deterministic from inputs + plan_version_id (no timestamps)
4. ✅ Proper Stubbing Strategy
Changed: Stub views now reference empty tables (notWHERE 1=0)
Files Created:
dim_plan_versions.sql: Empty table with schemadim_modifier_windows.sql: Empty table with schemasemantic_view_plan_versions.sql: Queries empty table (returns 0 rows)semantic_view_modifier_windows.sql: Queries empty table (returns 0 rows)
WHERE 1=0 workarounds needed.
5. ✅ Tenant-Aware dim_rule_definitions
Changed: Addedtenant_id and plan_version_id to schema
Schema Updates:
creative_benefit_strategies tenant:
TPA_PEPM_RATEOWNER_RESIDUALMIXED_OWNER_RESIDUAL
6. ✅ Tenant_ID in All JOIN ON Clauses
Changed: All dimension table JOINs includetenant_id in ON clause
Pattern:
business_mappingjoinagent_hierarchyjoinrule_metadatajoinplan_metadatajoin
7. ✅ QUALIFY ROW_NUMBER = 1 for Join Uniqueness
Changed: ReplacedDISTINCT with QUALIFY ROW_NUMBER() = 1 pattern
Pattern:
rule_metadataCTE (ensures one rule per tenant+rule_id)plan_metadataCTE (ensures one plan per tenant+plan_version_id)business_mappingCTE (ensures one business per tenant+normalized_name)agent_hierarchyCTE (ensures one agent per tenant+agent_id)stage1_with_componentsCTE (ensures one row per Stage1+component)
8. ✅ Enhanced QA Plan
Added Tests:-
No-Row-Drop Checks:
-
Deterministic ID Stability:
-
Full Tie-Out:
SQL Files Created
Tables
integration/bigquery/sql/tables/dim_rule_definitions.sql- Tenant-aware rule metadataintegration/bigquery/sql/tables/dim_plan_versions.sql- Empty stub tableintegration/bigquery/sql/tables/dim_modifier_windows.sql- Empty stub table
Views
integration/bigquery/sql/views/semantic_view_commission_traceability.sql- Stage1 grain, NUMERIC, deterministic IDsintegration/bigquery/sql/views/semantic_view_plan_versions.sql- Stub view (queries empty table)integration/bigquery/sql/views/semantic_view_modifier_windows.sql- Stub view (queries empty table)
Deployment Order
-
Create dimension tables (empty stubs OK):
-
Create semantic views:
-
Validate:
- Run QA tests (no-row-drop, deterministic IDs, full tie-out)
- Check partition pruning (EXPLAIN plan)
- Verify tenant isolation
Key Design Decisions
Stage1 Grain Allocation Logic
The view uses proportional allocation to map Stage1 rows to Stage3 components:- Aggregate Stage1 by business:
SUM(stage1_total) per business - Aggregate Stage3 by business:
SUM(agent_total) per business - Calculate ratio:
stage3_total / stage1_total - Allocate each Stage1 row proportionally:
(Stage1 row total) * (component amount / business Stage3 total)
- Stage1: Business “NUG” has 10 rows totaling $6,883.11
- Stage3: Business “NUG” has 4 components totaling $6,883.11
- Agent1: $1,106.30
- Agent2: $1,553.62
- Agent3: $442.52
- Owner: $3,766.36
- Each Stage1 row gets allocated proportionally across these 4 components
Remaining Work
- Create
semantic_view_rep_performance.sql(not yet implemented) - Create
semantic_view_owner_hierarchy.sql(not yet implemented) - Add query functions to
api/bigquery/queries.py - Create validation test suite (
tests/test_semantic_views.py)