feat(etl): cfg_* 视图入口统一 + NULL 兼容 + 3 处 _load_* 历史 Bug (W1-T2 / P0-1)

Wave 1 Day 3 沙箱配置参数切片。

DB 迁移 (20260504__cfg_views_null_compatible.sql):
- 4 个 v_cfg_* 视图 WHERE 加 NULL 兼容
  (effective_to IS NULL OR effective_to >= ...)
- 顺手清理 v_cfg_assistant_level_price / v_cfg_performance_tier
  WHERE 重复一次的 bug
- 测试库已执行,4 校验 PASS

ETL 代码 (4 处 SQL):
- base_index_task.py: FROM dws.cfg_index_parameters -> app.v_cfg_index_parameters
- base_dws_task.py 3 处 _load_* (performance_tier / level_price / bonus_rules)
  改 FROM app.v_cfg_*,顺手修历史 Bug:原 SQL 不带 effective_from/to WHERE

效果:
- 修复视图 NULL 行被过滤问题(SPI 27 行原本读到 0)
- 沙箱模式回放历史日期时,参数自动按 sandbox_date 切片
- _load_* 直接得到当前生效行(原 12/9 行历史 Python 挑)

参考:
- docs/audit/changes/2026-05-04__wave1_t2_scd2_view_unify.md
- docs/database/changes/2026-05-04__cfg_views_null_compatible.md
This commit is contained in:
Neo
2026-05-04 08:10:57 +08:00
parent 9eb495686f
commit b0340349bd
5 changed files with 357 additions and 16 deletions

View File

@@ -0,0 +1,114 @@
-- ===================================================================
-- 20260504 — cfg_* 视图 NULL 兼容性 + 清理重复 WHERE bug
-- ===================================================================
-- 触发:Wave 1 W1-T2(P0-1 沙箱配置参数切片)
--
-- 问题:
-- 1. 20260502 迁移产出的 4 个 v_cfg_* 视图 WHERE 写为
-- effective_to >= app.business_date_now()
-- effective_to 是 nullable(NULL 表示"至今仍生效"),NULL 行被过滤,
-- cfg_index_parameters 27 行参数中 effective_to IS NULL 的全部丢失。
-- 2. v_cfg_assistant_level_price 与 v_cfg_performance_tier 的 WHERE
-- 条件重复一次(逻辑等价,但代码瑕疵)。
--
-- 修复:
-- ALTER VIEW(实际 CREATE OR REPLACE)4 个视图,WHERE 改为
-- effective_from <= app.business_date_now()
-- AND (effective_to IS NULL OR effective_to >= app.business_date_now())
--
-- 影响:
-- - ETL 任务通过 app.v_cfg_* 读取时,正确包含 effective_to IS NULL 行。
-- - 后端通过 FDW 映射访问 fdw_etl.v_cfg_* 时同步生效(底层视图变更)。
-- - 沙箱模式下,app.business_date_now() 返回 sandbox_date,自动切片。
--
-- 回滚:
-- - 重新执行 20260502 迁移的 4 个 CREATE OR REPLACE VIEW 部分即可。
--
-- 验证 SQL:
-- 1. SELECT COUNT(*) FROM app.v_cfg_index_parameters WHERE index_type='SPI';
-- 应等于 27(测试库实际值)。
-- 2. SELECT COUNT(*) FROM dws.cfg_index_parameters WHERE index_type='SPI'
-- AND effective_from <= CURRENT_DATE
-- AND (effective_to IS NULL OR effective_to >= CURRENT_DATE);
-- 应与 1 的结果相同。
-- 3. SET LOCAL app.current_business_date = '2026-03-01';
-- SELECT COUNT(*) FROM app.v_cfg_index_parameters WHERE index_type='SPI';
-- 切沙箱时间后,行数应等于 2026-03-01 当时生效的参数数量。
-- ===================================================================
BEGIN;
-- ---- v_cfg_assistant_level_price ----
CREATE OR REPLACE VIEW app.v_cfg_assistant_level_price AS
SELECT price_id,
level_code,
level_name,
base_course_price,
bonus_course_price,
effective_from,
effective_to,
description,
created_at,
updated_at
FROM dws.cfg_assistant_level_price
WHERE effective_from <= app.business_date_now()
AND (effective_to IS NULL OR effective_to >= app.business_date_now());
-- ---- v_cfg_performance_tier ----
CREATE OR REPLACE VIEW app.v_cfg_performance_tier AS
SELECT tier_id,
tier_code,
tier_name,
tier_level,
min_hours,
max_hours,
base_deduction,
bonus_deduction_ratio,
vacation_days,
vacation_unlimited,
is_new_hire_tier,
effective_from,
effective_to,
description,
created_at,
updated_at
FROM dws.cfg_performance_tier
WHERE effective_from <= app.business_date_now()
AND (effective_to IS NULL OR effective_to >= app.business_date_now());
-- ---- v_cfg_bonus_rules ----
CREATE OR REPLACE VIEW app.v_cfg_bonus_rules AS
SELECT rule_id,
rule_type,
rule_code,
rule_name,
threshold_hours,
rank_position,
bonus_amount,
is_cumulative,
priority,
effective_from,
effective_to,
description,
created_at,
updated_at
FROM dws.cfg_bonus_rules
WHERE effective_from <= app.business_date_now()
AND (effective_to IS NULL OR effective_to >= app.business_date_now());
-- ---- v_cfg_index_parameters ----
CREATE OR REPLACE VIEW app.v_cfg_index_parameters AS
SELECT param_id,
index_type,
param_name,
param_value,
description,
effective_from,
effective_to,
created_at,
updated_at
FROM dws.cfg_index_parameters
WHERE effective_from <= app.business_date_now()
AND (effective_to IS NULL OR effective_to >= app.business_date_now());
COMMIT;