Fix COT pipeline: TRY_CAST nulls, dim_commodity leading zeros, correct CFTC codes
- config.yaml: remove ambiguousorinvalidcolumn linter rule (false positives on read_csv TVFs) - fct_cot_positioning: use TRY_CAST throughout — CFTC uses '.' as null in many columns - raw/cot_disaggregated: add columns() declaration for 33 varchar cols - dim_commodity: switch from SEED to FULL model with SQL VALUES to preserve leading zeros Pandas auto-converts '083' → 83 even with varchar column declarations in SEED models - seeds/dim_commodity.csv: correct cftc_commodity_code from '083731' (contract market code) to '083' (3-digit CFTC commodity code); add CSV quoting - test_cot_foundation.yaml: fix output key name, vars for time range, partial: true, and correct cftc_commodity_code to '083' - analytics.py: COFFEE_CFTC_CODE '083731' → '083' to match actual data Result: serving.cot_positioning has 685 rows (2013-01-08 to 2026-02-17), 23/23 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -29,63 +29,64 @@ WITH cast_and_clean AS (
|
||||
trim(contract_units) AS contract_units,
|
||||
|
||||
-- Open interest
|
||||
open_interest_all::int AS open_interest,
|
||||
-- CFTC uses '.' as null for any field — use TRY_CAST throughout
|
||||
TRY_CAST(open_interest_all AS int) AS open_interest,
|
||||
|
||||
-- Producer / Merchant (commercial hedgers: exporters, processors)
|
||||
prod_merc_positions_long_all::int AS prod_merc_long,
|
||||
prod_merc_positions_short_all::int AS prod_merc_short,
|
||||
TRY_CAST(prod_merc_positions_long_all AS int) AS prod_merc_long,
|
||||
TRY_CAST(prod_merc_positions_short_all AS int) AS prod_merc_short,
|
||||
|
||||
-- Swap dealers
|
||||
swap_positions_long_all::int AS swap_long,
|
||||
swap_positions_short_all::int AS swap_short,
|
||||
swap_positions_spread_all::int AS swap_spread,
|
||||
TRY_CAST(swap_positions_long_all AS int) AS swap_long,
|
||||
TRY_CAST(swap_positions_short_all AS int) AS swap_short,
|
||||
TRY_CAST(swap_positions_spread_all AS int) AS swap_spread,
|
||||
|
||||
-- Managed money (hedge funds, CTAs — the primary speculative signal)
|
||||
m_money_positions_long_all::int AS managed_money_long,
|
||||
m_money_positions_short_all::int AS managed_money_short,
|
||||
m_money_positions_spread_all::int AS managed_money_spread,
|
||||
TRY_CAST(m_money_positions_long_all AS int) AS managed_money_long,
|
||||
TRY_CAST(m_money_positions_short_all AS int) AS managed_money_short,
|
||||
TRY_CAST(m_money_positions_spread_all AS int) AS managed_money_spread,
|
||||
|
||||
-- Other reportables
|
||||
other_rept_positions_long_all::int AS other_reportable_long,
|
||||
other_rept_positions_short_all::int AS other_reportable_short,
|
||||
other_rept_positions_spread_all::int AS other_reportable_spread,
|
||||
TRY_CAST(other_rept_positions_long_all AS int) AS other_reportable_long,
|
||||
TRY_CAST(other_rept_positions_short_all AS int) AS other_reportable_short,
|
||||
TRY_CAST(other_rept_positions_spread_all AS int) AS other_reportable_spread,
|
||||
|
||||
-- Non-reportable (small speculators, below reporting threshold)
|
||||
nonrept_positions_long_all::int AS nonreportable_long,
|
||||
nonrept_positions_short_all::int AS nonreportable_short,
|
||||
TRY_CAST(nonrept_positions_long_all AS int) AS nonreportable_long,
|
||||
TRY_CAST(nonrept_positions_short_all AS int) AS nonreportable_short,
|
||||
|
||||
-- Net positions (long minus short per category)
|
||||
prod_merc_positions_long_all::int
|
||||
- prod_merc_positions_short_all::int AS prod_merc_net,
|
||||
m_money_positions_long_all::int
|
||||
- m_money_positions_short_all::int AS managed_money_net,
|
||||
swap_positions_long_all::int
|
||||
- swap_positions_short_all::int AS swap_net,
|
||||
other_rept_positions_long_all::int
|
||||
- other_rept_positions_short_all::int AS other_reportable_net,
|
||||
nonrept_positions_long_all::int
|
||||
- nonrept_positions_short_all::int AS nonreportable_net,
|
||||
TRY_CAST(prod_merc_positions_long_all AS int)
|
||||
- TRY_CAST(prod_merc_positions_short_all AS int) AS prod_merc_net,
|
||||
TRY_CAST(m_money_positions_long_all AS int)
|
||||
- TRY_CAST(m_money_positions_short_all AS int) AS managed_money_net,
|
||||
TRY_CAST(swap_positions_long_all AS int)
|
||||
- TRY_CAST(swap_positions_short_all AS int) AS swap_net,
|
||||
TRY_CAST(other_rept_positions_long_all AS int)
|
||||
- TRY_CAST(other_rept_positions_short_all AS int) AS other_reportable_net,
|
||||
TRY_CAST(nonrept_positions_long_all AS int)
|
||||
- TRY_CAST(nonrept_positions_short_all AS int) AS nonreportable_net,
|
||||
|
||||
-- Week-over-week changes
|
||||
change_in_open_interest_all::int AS change_open_interest,
|
||||
change_in_m_money_long_all::int AS change_managed_money_long,
|
||||
change_in_m_money_short_all::int AS change_managed_money_short,
|
||||
change_in_m_money_long_all::int
|
||||
- change_in_m_money_short_all::int AS change_managed_money_net,
|
||||
change_in_prod_merc_long_all::int AS change_prod_merc_long,
|
||||
change_in_prod_merc_short_all::int AS change_prod_merc_short,
|
||||
TRY_CAST(change_in_open_interest_all AS int) AS change_open_interest,
|
||||
TRY_CAST(change_in_m_money_long_all AS int) AS change_managed_money_long,
|
||||
TRY_CAST(change_in_m_money_short_all AS int) AS change_managed_money_short,
|
||||
TRY_CAST(change_in_m_money_long_all AS int)
|
||||
- TRY_CAST(change_in_m_money_short_all AS int) AS change_managed_money_net,
|
||||
TRY_CAST(change_in_prod_merc_long_all AS int) AS change_prod_merc_long,
|
||||
TRY_CAST(change_in_prod_merc_short_all AS int) AS change_prod_merc_short,
|
||||
|
||||
-- Concentration ratios (% of OI held by top 4 / top 8 traders)
|
||||
conc_gross_le_4_tdr_long_all::float AS concentration_top4_long_pct,
|
||||
conc_gross_le_4_tdr_short_all::float AS concentration_top4_short_pct,
|
||||
conc_gross_le_8_tdr_long_all::float AS concentration_top8_long_pct,
|
||||
conc_gross_le_8_tdr_short_all::float AS concentration_top8_short_pct,
|
||||
TRY_CAST(conc_gross_le_4_tdr_long_all AS float) AS concentration_top4_long_pct,
|
||||
TRY_CAST(conc_gross_le_4_tdr_short_all AS float) AS concentration_top4_short_pct,
|
||||
TRY_CAST(conc_gross_le_8_tdr_long_all AS float) AS concentration_top8_long_pct,
|
||||
TRY_CAST(conc_gross_le_8_tdr_short_all AS float) AS concentration_top8_short_pct,
|
||||
|
||||
-- Trader counts
|
||||
traders_tot_all::int AS traders_total,
|
||||
traders_m_money_long_all::int AS traders_managed_money_long,
|
||||
traders_m_money_short_all::int AS traders_managed_money_short,
|
||||
traders_m_money_spread_all::int AS traders_managed_money_spread,
|
||||
TRY_CAST(traders_tot_all AS int) AS traders_total,
|
||||
TRY_CAST(traders_m_money_long_all AS int) AS traders_managed_money_long,
|
||||
TRY_CAST(traders_m_money_short_all AS int) AS traders_managed_money_short,
|
||||
TRY_CAST(traders_m_money_spread_all AS int) AS traders_managed_money_spread,
|
||||
|
||||
-- Ingest date: derived from landing path year directory
|
||||
-- Path: .../cot/{year}/{etag}.csv.gzip → extract year from [-2]
|
||||
|
||||
Reference in New Issue
Block a user