/* Foundation fact: ICE certified Coffee C (Arabica) aging report. */ /* Reads directly from the landing zone, casts varchar columns to proper types, */ /* and deduplicates via hash key. */ /* Grain: one row per (report_date, age_bucket). */ /* Age buckets represent how long coffee has been in certified storage. */ /* Port columns are in bags (60kg). */
MODEL (
  name foundation.fct_ice_aging_stocks,
  kind INCREMENTAL_BY_TIME_RANGE (
    time_column report_date
  ),
  grain (report_date, age_bucket),
  start '2020-01-01',
  cron '@daily'
);

WITH src AS (
  SELECT
    *
  FROM READ_CSV(
    @ice_aging_glob(),
    compression = 'gzip',
    header = TRUE,
    union_by_name = TRUE,
    filename = TRUE,
    all_varchar = TRUE
  )
), cast_and_clean AS (
  SELECT
    TRY_CAST(report_date AS DATE) AS report_date,
    age_bucket,
    TRY_CAST(antwerp_bags AS BIGINT) AS antwerp_bags,
    TRY_CAST(hamburg_bremen_bags AS BIGINT) AS hamburg_bremen_bags,
    TRY_CAST(houston_bags AS BIGINT) AS houston_bags,
    TRY_CAST(miami_bags AS BIGINT) AS miami_bags,
    TRY_CAST(new_orleans_bags AS BIGINT) AS new_orleans_bags,
    TRY_CAST(new_york_bags AS BIGINT) AS new_york_bags,
    TRY_CAST(total_bags AS BIGINT) AS total_bags,
    filename AS source_file,
    HASH(report_date, age_bucket, total_bags) AS hkey
  FROM src
  WHERE
    NOT TRY_CAST(report_date AS DATE) IS NULL
    AND NOT age_bucket IS NULL
    AND age_bucket <> ''
), deduplicated AS (
  SELECT
    ANY_VALUE(report_date) AS report_date,
    ANY_VALUE(age_bucket) AS age_bucket,
    ANY_VALUE(antwerp_bags) AS antwerp_bags,
    ANY_VALUE(hamburg_bremen_bags) AS hamburg_bremen_bags,
    ANY_VALUE(houston_bags) AS houston_bags,
    ANY_VALUE(miami_bags) AS miami_bags,
    ANY_VALUE(new_orleans_bags) AS new_orleans_bags,
    ANY_VALUE(new_york_bags) AS new_york_bags,
    ANY_VALUE(total_bags) AS total_bags,
    ANY_VALUE(source_file) AS source_file,
    hkey
  FROM cast_and_clean
  GROUP BY
    hkey
)
SELECT
  *
FROM deduplicated
WHERE
  report_date BETWEEN @start_ds AND @end_ds