transceiver-db/sql/131-wavelength-column-sync.sql
root 91d19e152d fix(equivalences): resolve wavelength column mismatch + reach-tier substitution policy
Root cause of the equivalence review-queue backlog: spec-matcher read
transceivers.wavelengths (text), but the enricher wrote wavelength_tx_nm
(int) — 27,908 rows had the wavelength in the wrong column, and tx_nm
itself defaulted incorrectly to 1310nm in ~22% of disagreements with the
authoritative part-derived value.

sql/131: backfill + sync trigger from wavelength_tx_nm into wavelengths.
sql/132: correct authority (bare-number wavelengths, not tx_nm) + format
normalization + valid promotion on the full spec-matcher gate.
sql/133: one-directional reach-tier substitution (higher reach may satisfy
a lower-reach requirement, never the reverse; ZR/coherent excluded — real
link-budget/receiver-overload risk, not a simple tier comparison). Tagged
as its own status (compatible_superset), not merged into auto_approved.
2026-07-07 12:15:43 +00:00

61 lines
2.7 KiB
PL/PgSQL

-- sql/131 — Wavelength column sync (root-cause fix for equivalence review backlog)
--
-- Root cause: the enricher populates the structured integer column
-- transceivers.wavelength_tx_nm, but the equivalence spec-matcher reads the TEXT
-- column transceivers.wavelengths via tip_extract_wavelength_nm(). 27,908 rows had
-- wavelength_tx_nm set while wavelengths was NULL, so the matcher could not confirm
-- wavelength on either side and dumped ~3,155 genuine equivalences into manual review.
--
-- Fix (no fabrication — copies existing structured data into the format the matcher
-- expects, and keeps it in sync going forward):
-- A) one-off backfill of transceivers.wavelengths from wavelength_tx_nm
-- B) durable BEFORE-trigger so future ingests stay in sync
-- C) promote pending -> auto_approved ONLY where the FULL spec-matcher criteria now
-- agree (form_factor + speed + fiber_type + reach-tier + wavelength +/-10nm).
-- Everything with a genuine reach-tier / fiber / wavelength difference stays pending.
BEGIN;
-- A) one-off backfill (existing structured data -> text column the matcher reads)
UPDATE transceivers
SET wavelengths = wavelength_tx_nm || 'nm'
WHERE (wavelengths IS NULL OR wavelengths = '')
AND wavelength_tx_nm IS NOT NULL;
-- B) durable sync trigger for future inserts/updates
CREATE OR REPLACE FUNCTION tip_sync_wavelengths_from_tx() RETURNS trigger AS $fn$
BEGIN
IF (NEW.wavelengths IS NULL OR NEW.wavelengths = '')
AND NEW.wavelength_tx_nm IS NOT NULL THEN
NEW.wavelengths := NEW.wavelength_tx_nm || 'nm';
END IF;
RETURN NEW;
END;
$fn$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS trg_sync_wavelengths ON transceivers;
CREATE TRIGGER trg_sync_wavelengths
BEFORE INSERT OR UPDATE ON transceivers
FOR EACH ROW EXECUTE FUNCTION tip_sync_wavelengths_from_tx();
-- C) valid promotion: pending -> auto_approved where full spec now confirmed
UPDATE transceiver_equivalences e
SET status = 'auto_approved',
confidence = GREATEST(e.confidence, 0.85),
match_notes = COALESCE(e.match_notes,'') || ' | sql/131: full spec confirmed after wavelength column sync',
updated_at = now()
FROM transceivers fx, transceivers cx
WHERE e.flexoptix_id = fx.id
AND e.competitor_id = cx.id
AND e.status = 'pending'
AND fx.form_factor = cx.form_factor
AND fx.speed_gbps = cx.speed_gbps
AND fx.fiber_type IS NOT DISTINCT FROM cx.fiber_type
AND fx.reach_meters >= 10 AND cx.reach_meters >= 10
AND tip_reach_tier(fx.reach_meters) = tip_reach_tier(cx.reach_meters)
AND tip_extract_wavelength_nm(fx.wavelengths) IS NOT NULL
AND tip_extract_wavelength_nm(cx.wavelengths) IS NOT NULL
AND abs(tip_extract_wavelength_nm(fx.wavelengths) - tip_extract_wavelength_nm(cx.wavelengths)) <= 10;
COMMIT;