feat(extract): add OpenWeatherMap daily weather extractor
Adds extract/openweathermap package with daily weather extraction for 8
coffee-growing regions (Brazil, Vietnam, Colombia, Ethiopia, Honduras,
Guatemala, Indonesia). Feeds crop stress signal for commodity sentiment score.
Extractor:
- OWM One Call API 3.0 / Day Summary — one JSON.gz per (location, date)
- extract_weather: daily, fetches yesterday + today (16 calls max)
- extract_weather_backfill: fills 2020-01-01 to yesterday, capped at 500
calls/run with resume cursor '{location_id}:{date}' for crash safety
- Full idempotency via file existence check; state tracking via extract_core
SQLMesh:
- seeds.weather_locations (8 regions with lat/lon/variety)
- foundation.fct_weather_daily: INCREMENTAL_BY_TIME_RANGE, grain
(location_id, observation_date), dedup via hash key, crop stress flags:
is_frost (<2°C), is_heat_stress (>35°C), is_drought (<1mm), in_growing_season
Landing path: LANDING_DIR/weather/{location_id}/{year}/{date}.json.gz
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,23 +1,15 @@
|
||||
-- Commodity dimension: conforms identifiers across source systems.
|
||||
--
|
||||
-- This is the ontology. Each row is a commodity tracked by BeanFlows.
|
||||
-- As new sources are added (ICO, futures prices, satellite), their
|
||||
-- commodity identifiers are added as columns here — not as separate tables.
|
||||
-- As new commodities are added (cocoa, sugar), rows are added here.
|
||||
--
|
||||
-- References:
|
||||
-- usda_commodity_code → staging.psdalldata__commodity.commodity_code (numeric string, e.g. '0711100')
|
||||
-- cftc_commodity_code → foundation.fct_cot_positioning.cftc_commodity_code (3-char, e.g. '083')
|
||||
--
|
||||
-- NOTE: Defined as FULL model (not SEED) to guarantee leading-zero preservation.
|
||||
-- Pandas CSV loading converts '083' → 83 even with varchar column declarations.
|
||||
|
||||
/* Commodity dimension: conforms identifiers across source systems. */ /* This is the ontology. Each row is a commodity tracked by BeanFlows. */ /* As new sources are added (ICO, futures prices, satellite), their */ /* commodity identifiers are added as columns here — not as separate tables. */ /* As new commodities are added (cocoa, sugar), rows are added here. */ /* References: */ /* usda_commodity_code → staging.psdalldata__commodity.commodity_code (numeric string, e.g. '0711100') */ /* cftc_commodity_code → foundation.fct_cot_positioning.cftc_commodity_code (3-char, e.g. '083') */ /* NOTE: Defined as FULL model (not SEED) to guarantee leading-zero preservation. */ /* Pandas CSV loading converts '083' → 83 even with varchar column declarations. */
|
||||
MODEL (
|
||||
name foundation.dim_commodity,
|
||||
kind FULL
|
||||
);
|
||||
|
||||
SELECT usda_commodity_code, cftc_commodity_code, ticker, ice_stock_report_code, commodity_name, commodity_group
|
||||
SELECT
|
||||
usda_commodity_code,
|
||||
cftc_commodity_code,
|
||||
ticker,
|
||||
ice_stock_report_code,
|
||||
commodity_name,
|
||||
commodity_group
|
||||
FROM (VALUES
|
||||
('0711100', '083', 'KC=F', 'COFFEE-C', 'Coffee, Green', 'Softs')
|
||||
) AS t(usda_commodity_code, cftc_commodity_code, ticker, ice_stock_report_code, commodity_name, commodity_group)
|
||||
('0711100', '083', 'KC=F', 'COFFEE-C', 'Coffee, Green', 'Softs')) AS t(usda_commodity_code, cftc_commodity_code, ticker, ice_stock_report_code, commodity_name, commodity_group)
|
||||
@@ -1,69 +1,58 @@
|
||||
-- Foundation fact: daily KC=F Coffee C futures prices.
|
||||
--
|
||||
-- Reads directly from the landing zone, casts varchar columns to proper types,
|
||||
-- and deduplicates via hash key.
|
||||
-- Covers all available history from the landing directory.
|
||||
--
|
||||
-- Grain: one row per trade_date.
|
||||
-- Dedup: hash of (trade_date, close) — if Yahoo Finance corrects a price,
|
||||
-- the new hash triggers a re-ingest on the next incremental run.
|
||||
|
||||
/* Foundation fact: daily KC=F Coffee C futures prices. */ /* Reads directly from the landing zone, casts varchar columns to proper types, */ /* and deduplicates via hash key. */ /* Covers all available history from the landing directory. */ /* Grain: one row per trade_date. */ /* Dedup: hash of (trade_date, close) — if Yahoo Finance corrects a price, */ /* the new hash triggers a re-ingest on the next incremental run. */
|
||||
MODEL (
|
||||
name foundation.fct_coffee_prices,
|
||||
kind INCREMENTAL_BY_TIME_RANGE (
|
||||
time_column trade_date
|
||||
),
|
||||
grain (trade_date),
|
||||
grain (
|
||||
trade_date
|
||||
),
|
||||
start '1971-08-16',
|
||||
cron '@daily'
|
||||
);
|
||||
|
||||
WITH src AS (
|
||||
SELECT * FROM read_csv(
|
||||
SELECT
|
||||
*
|
||||
FROM READ_CSV(
|
||||
@prices_glob(),
|
||||
compression = 'gzip',
|
||||
header = true,
|
||||
union_by_name = true,
|
||||
filename = true,
|
||||
all_varchar = true
|
||||
compression = 'gzip',
|
||||
header = TRUE,
|
||||
union_by_name = TRUE,
|
||||
filename = TRUE,
|
||||
all_varchar = TRUE
|
||||
)
|
||||
),
|
||||
|
||||
cast_and_clean AS (
|
||||
), cast_and_clean AS (
|
||||
SELECT
|
||||
TRY_CAST(Date AS date) AS trade_date,
|
||||
TRY_CAST(Open AS double) AS open,
|
||||
TRY_CAST(High AS double) AS high,
|
||||
TRY_CAST(Low AS double) AS low,
|
||||
TRY_CAST(Close AS double) AS close,
|
||||
TRY_CAST(Adj_Close AS double) AS adj_close,
|
||||
TRY_CAST(Volume AS bigint) AS volume,
|
||||
|
||||
-- Filename encodes the content hash — use as ingest identifier
|
||||
filename AS source_file,
|
||||
|
||||
-- Dedup key: trade date + close price
|
||||
hash(Date, Close) AS hkey
|
||||
TRY_CAST(Date AS DATE) AS trade_date,
|
||||
TRY_CAST(Open AS DOUBLE) AS open,
|
||||
TRY_CAST(High AS DOUBLE) AS high,
|
||||
TRY_CAST(Low AS DOUBLE) AS low,
|
||||
TRY_CAST(Close AS DOUBLE) AS close,
|
||||
TRY_CAST(Adj_Close AS DOUBLE) AS adj_close,
|
||||
TRY_CAST(Volume AS BIGINT) AS volume,
|
||||
filename AS source_file, /* Filename encodes the content hash — use as ingest identifier */
|
||||
HASH(Date, Close) AS hkey /* Dedup key: trade date + close price */
|
||||
FROM src
|
||||
WHERE TRY_CAST(Date AS date) IS NOT NULL
|
||||
AND TRY_CAST(Close AS double) IS NOT NULL
|
||||
),
|
||||
|
||||
deduplicated AS (
|
||||
WHERE
|
||||
NOT TRY_CAST(Date AS DATE) IS NULL AND NOT TRY_CAST(Close AS DOUBLE) IS NULL
|
||||
), deduplicated AS (
|
||||
SELECT
|
||||
any_value(trade_date) AS trade_date,
|
||||
any_value(open) AS open,
|
||||
any_value(high) AS high,
|
||||
any_value(low) AS low,
|
||||
any_value(close) AS close,
|
||||
any_value(adj_close) AS adj_close,
|
||||
any_value(volume) AS volume,
|
||||
any_value(source_file) AS source_file,
|
||||
ANY_VALUE(trade_date) AS trade_date,
|
||||
ANY_VALUE(open) AS open,
|
||||
ANY_VALUE(high) AS high,
|
||||
ANY_VALUE(low) AS low,
|
||||
ANY_VALUE(close) AS close,
|
||||
ANY_VALUE(adj_close) AS adj_close,
|
||||
ANY_VALUE(volume) AS volume,
|
||||
ANY_VALUE(source_file) AS source_file,
|
||||
hkey
|
||||
FROM cast_and_clean
|
||||
GROUP BY hkey
|
||||
GROUP BY
|
||||
hkey
|
||||
)
|
||||
|
||||
SELECT *
|
||||
SELECT
|
||||
*
|
||||
FROM deduplicated
|
||||
WHERE trade_date BETWEEN @start_ds AND @end_ds
|
||||
WHERE
|
||||
trade_date BETWEEN @start_ds AND @end_ds
|
||||
@@ -1,14 +1,4 @@
|
||||
-- Foundation fact: CFTC COT positioning, weekly grain, all commodities.
|
||||
--
|
||||
-- Reads directly from the landing zone, casts varchar columns to proper types,
|
||||
-- cleans column names, computes net positions (long - short) per trader category,
|
||||
-- and deduplicates via hash key. Covers all commodities — filtering to
|
||||
-- a specific commodity happens in the serving layer.
|
||||
--
|
||||
-- Grain: one row per (cftc_commodity_code, report_date, cftc_contract_market_code)
|
||||
-- History: revisions appear as new rows with a later ingest_date.
|
||||
-- Serving layer picks max(ingest_date) per grain for latest view.
|
||||
|
||||
/* Foundation fact: CFTC COT positioning, weekly grain, all commodities. */ /* Reads directly from the landing zone, casts varchar columns to proper types, */ /* cleans column names, computes net positions (long - short) per trader category, */ /* and deduplicates via hash key. Covers all commodities — filtering to */ /* a specific commodity happens in the serving layer. */ /* Grain: one row per (cftc_commodity_code, report_date, cftc_contract_market_code) */ /* History: revisions appear as new rows with a later ingest_date. */ /* Serving layer picks max(ingest_date) per grain for latest view. */
|
||||
MODEL (
|
||||
name foundation.fct_cot_positioning,
|
||||
kind INCREMENTAL_BY_TIME_RANGE (
|
||||
@@ -20,92 +10,59 @@ MODEL (
|
||||
);
|
||||
|
||||
WITH src AS (
|
||||
SELECT * FROM read_csv(
|
||||
SELECT
|
||||
*
|
||||
FROM READ_CSV(
|
||||
@cot_glob(),
|
||||
compression = 'gzip',
|
||||
header = true,
|
||||
union_by_name = true,
|
||||
filename = true,
|
||||
all_varchar = true,
|
||||
compression = 'gzip',
|
||||
header = TRUE,
|
||||
union_by_name = TRUE,
|
||||
filename = TRUE,
|
||||
all_varchar = TRUE,
|
||||
max_line_size = 10000000
|
||||
)
|
||||
),
|
||||
|
||||
cast_and_clean AS (
|
||||
), cast_and_clean AS (
|
||||
SELECT
|
||||
-- Identifiers
|
||||
trim(market_and_exchange_names) AS market_and_exchange_name,
|
||||
report_date_as_yyyy_mm_dd::date AS report_date,
|
||||
trim(cftc_commodity_code) AS cftc_commodity_code,
|
||||
trim(cftc_contract_market_code) AS cftc_contract_market_code,
|
||||
trim(contract_units) AS contract_units,
|
||||
|
||||
-- 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)
|
||||
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
|
||||
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)
|
||||
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
|
||||
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)
|
||||
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)
|
||||
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
|
||||
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)
|
||||
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
|
||||
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]
|
||||
make_date(split(filename, '/')[-2]::int, 1, 1) AS ingest_date,
|
||||
|
||||
-- Dedup key: hash of business grain + key metrics
|
||||
hash(
|
||||
TRIM(market_and_exchange_names) AS market_and_exchange_name, /* Identifiers */
|
||||
report_date_as_yyyy_mm_dd::DATE AS report_date,
|
||||
TRIM(cftc_commodity_code) AS cftc_commodity_code,
|
||||
TRIM(cftc_contract_market_code) AS cftc_contract_market_code,
|
||||
TRIM(contract_units) AS contract_units,
|
||||
TRY_CAST(open_interest_all AS INT) AS open_interest, /* Open interest */ /* CFTC uses '.' as null for any field — use TRY_CAST throughout */
|
||||
TRY_CAST(prod_merc_positions_long_all AS INT) AS prod_merc_long, /* Producer / Merchant (commercial hedgers: exporters, processors) */
|
||||
TRY_CAST(prod_merc_positions_short_all AS INT) AS prod_merc_short,
|
||||
TRY_CAST(swap_positions_long_all AS INT) AS swap_long, /* Swap dealers */
|
||||
TRY_CAST(swap_positions_short_all AS INT) AS swap_short,
|
||||
TRY_CAST(swap_positions_spread_all AS INT) AS swap_spread,
|
||||
TRY_CAST(m_money_positions_long_all AS INT) AS managed_money_long, /* Managed money (hedge funds, CTAs — the primary speculative signal) */
|
||||
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,
|
||||
TRY_CAST(other_rept_positions_long_all AS INT) AS other_reportable_long, /* Other reportables */
|
||||
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,
|
||||
TRY_CAST(nonrept_positions_long_all AS INT) AS nonreportable_long, /* Non-reportable (small speculators, below reporting threshold) */
|
||||
TRY_CAST(nonrept_positions_short_all AS INT) AS nonreportable_short,
|
||||
TRY_CAST(prod_merc_positions_long_all AS INT) /* Net positions (long minus short per category) */ - 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,
|
||||
TRY_CAST(change_in_open_interest_all AS INT) AS change_open_interest, /* Week-over-week changes */
|
||||
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,
|
||||
TRY_CAST(conc_gross_le_4_tdr_long_all AS REAL) AS concentration_top4_long_pct, /* Concentration ratios (% of OI held by top 4 / top 8 traders) */
|
||||
TRY_CAST(conc_gross_le_4_tdr_short_all AS REAL) AS concentration_top4_short_pct,
|
||||
TRY_CAST(conc_gross_le_8_tdr_long_all AS REAL) AS concentration_top8_long_pct,
|
||||
TRY_CAST(conc_gross_le_8_tdr_short_all AS REAL) AS concentration_top8_short_pct,
|
||||
TRY_CAST(traders_tot_all AS INT) AS traders_total, /* Trader counts */
|
||||
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,
|
||||
MAKE_DATE(STR_SPLIT(filename, '/')[-2]::INT, 1, 1) AS ingest_date, /* Ingest date: derived from landing path year directory */ /* Path: .../cot/{year}/{etag}.csv.gzip → extract year from [-2] */
|
||||
HASH(
|
||||
cftc_commodity_code,
|
||||
report_date_as_yyyy_mm_dd,
|
||||
cftc_contract_market_code,
|
||||
@@ -114,60 +71,61 @@ cast_and_clean AS (
|
||||
m_money_positions_short_all,
|
||||
prod_merc_positions_long_all,
|
||||
prod_merc_positions_short_all
|
||||
) AS hkey
|
||||
) AS hkey /* Dedup key: hash of business grain + key metrics */
|
||||
FROM src
|
||||
-- Reject rows with null commodity code or malformed date
|
||||
WHERE trim(cftc_commodity_code) IS NOT NULL
|
||||
AND len(trim(cftc_commodity_code)) > 0
|
||||
AND report_date_as_yyyy_mm_dd::date IS NOT NULL
|
||||
),
|
||||
|
||||
deduplicated AS (
|
||||
/* Reject rows with null commodity code or malformed date */
|
||||
WHERE
|
||||
NOT TRIM(cftc_commodity_code) IS NULL
|
||||
AND LENGTH(TRIM(cftc_commodity_code)) > 0
|
||||
AND NOT report_date_as_yyyy_mm_dd::DATE IS NULL
|
||||
), deduplicated AS (
|
||||
SELECT
|
||||
any_value(market_and_exchange_name) AS market_and_exchange_name,
|
||||
any_value(report_date) AS report_date,
|
||||
any_value(cftc_commodity_code) AS cftc_commodity_code,
|
||||
any_value(cftc_contract_market_code) AS cftc_contract_market_code,
|
||||
any_value(contract_units) AS contract_units,
|
||||
any_value(open_interest) AS open_interest,
|
||||
any_value(prod_merc_long) AS prod_merc_long,
|
||||
any_value(prod_merc_short) AS prod_merc_short,
|
||||
any_value(prod_merc_net) AS prod_merc_net,
|
||||
any_value(swap_long) AS swap_long,
|
||||
any_value(swap_short) AS swap_short,
|
||||
any_value(swap_spread) AS swap_spread,
|
||||
any_value(swap_net) AS swap_net,
|
||||
any_value(managed_money_long) AS managed_money_long,
|
||||
any_value(managed_money_short) AS managed_money_short,
|
||||
any_value(managed_money_spread) AS managed_money_spread,
|
||||
any_value(managed_money_net) AS managed_money_net,
|
||||
any_value(other_reportable_long) AS other_reportable_long,
|
||||
any_value(other_reportable_short) AS other_reportable_short,
|
||||
any_value(other_reportable_spread) AS other_reportable_spread,
|
||||
any_value(other_reportable_net) AS other_reportable_net,
|
||||
any_value(nonreportable_long) AS nonreportable_long,
|
||||
any_value(nonreportable_short) AS nonreportable_short,
|
||||
any_value(nonreportable_net) AS nonreportable_net,
|
||||
any_value(change_open_interest) AS change_open_interest,
|
||||
any_value(change_managed_money_long) AS change_managed_money_long,
|
||||
any_value(change_managed_money_short) AS change_managed_money_short,
|
||||
any_value(change_managed_money_net) AS change_managed_money_net,
|
||||
any_value(change_prod_merc_long) AS change_prod_merc_long,
|
||||
any_value(change_prod_merc_short) AS change_prod_merc_short,
|
||||
any_value(concentration_top4_long_pct) AS concentration_top4_long_pct,
|
||||
any_value(concentration_top4_short_pct) AS concentration_top4_short_pct,
|
||||
any_value(concentration_top8_long_pct) AS concentration_top8_long_pct,
|
||||
any_value(concentration_top8_short_pct) AS concentration_top8_short_pct,
|
||||
any_value(traders_total) AS traders_total,
|
||||
any_value(traders_managed_money_long) AS traders_managed_money_long,
|
||||
any_value(traders_managed_money_short) AS traders_managed_money_short,
|
||||
any_value(traders_managed_money_spread) AS traders_managed_money_spread,
|
||||
any_value(ingest_date) AS ingest_date,
|
||||
ANY_VALUE(market_and_exchange_name) AS market_and_exchange_name,
|
||||
ANY_VALUE(report_date) AS report_date,
|
||||
ANY_VALUE(cftc_commodity_code) AS cftc_commodity_code,
|
||||
ANY_VALUE(cftc_contract_market_code) AS cftc_contract_market_code,
|
||||
ANY_VALUE(contract_units) AS contract_units,
|
||||
ANY_VALUE(open_interest) AS open_interest,
|
||||
ANY_VALUE(prod_merc_long) AS prod_merc_long,
|
||||
ANY_VALUE(prod_merc_short) AS prod_merc_short,
|
||||
ANY_VALUE(prod_merc_net) AS prod_merc_net,
|
||||
ANY_VALUE(swap_long) AS swap_long,
|
||||
ANY_VALUE(swap_short) AS swap_short,
|
||||
ANY_VALUE(swap_spread) AS swap_spread,
|
||||
ANY_VALUE(swap_net) AS swap_net,
|
||||
ANY_VALUE(managed_money_long) AS managed_money_long,
|
||||
ANY_VALUE(managed_money_short) AS managed_money_short,
|
||||
ANY_VALUE(managed_money_spread) AS managed_money_spread,
|
||||
ANY_VALUE(managed_money_net) AS managed_money_net,
|
||||
ANY_VALUE(other_reportable_long) AS other_reportable_long,
|
||||
ANY_VALUE(other_reportable_short) AS other_reportable_short,
|
||||
ANY_VALUE(other_reportable_spread) AS other_reportable_spread,
|
||||
ANY_VALUE(other_reportable_net) AS other_reportable_net,
|
||||
ANY_VALUE(nonreportable_long) AS nonreportable_long,
|
||||
ANY_VALUE(nonreportable_short) AS nonreportable_short,
|
||||
ANY_VALUE(nonreportable_net) AS nonreportable_net,
|
||||
ANY_VALUE(change_open_interest) AS change_open_interest,
|
||||
ANY_VALUE(change_managed_money_long) AS change_managed_money_long,
|
||||
ANY_VALUE(change_managed_money_short) AS change_managed_money_short,
|
||||
ANY_VALUE(change_managed_money_net) AS change_managed_money_net,
|
||||
ANY_VALUE(change_prod_merc_long) AS change_prod_merc_long,
|
||||
ANY_VALUE(change_prod_merc_short) AS change_prod_merc_short,
|
||||
ANY_VALUE(concentration_top4_long_pct) AS concentration_top4_long_pct,
|
||||
ANY_VALUE(concentration_top4_short_pct) AS concentration_top4_short_pct,
|
||||
ANY_VALUE(concentration_top8_long_pct) AS concentration_top8_long_pct,
|
||||
ANY_VALUE(concentration_top8_short_pct) AS concentration_top8_short_pct,
|
||||
ANY_VALUE(traders_total) AS traders_total,
|
||||
ANY_VALUE(traders_managed_money_long) AS traders_managed_money_long,
|
||||
ANY_VALUE(traders_managed_money_short) AS traders_managed_money_short,
|
||||
ANY_VALUE(traders_managed_money_spread) AS traders_managed_money_spread,
|
||||
ANY_VALUE(ingest_date) AS ingest_date,
|
||||
hkey
|
||||
FROM cast_and_clean
|
||||
GROUP BY hkey
|
||||
GROUP BY
|
||||
hkey
|
||||
)
|
||||
|
||||
SELECT *
|
||||
SELECT
|
||||
*
|
||||
FROM deduplicated
|
||||
WHERE report_date BETWEEN @start_ds AND @end_ds
|
||||
WHERE
|
||||
report_date BETWEEN @start_ds AND @end_ds
|
||||
@@ -1,11 +1,4 @@
|
||||
-- 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).
|
||||
|
||||
/* 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 (
|
||||
@@ -17,54 +10,53 @@ MODEL (
|
||||
);
|
||||
|
||||
WITH src AS (
|
||||
SELECT * FROM read_csv(
|
||||
SELECT
|
||||
*
|
||||
FROM READ_CSV(
|
||||
@ice_aging_glob(),
|
||||
compression = 'gzip',
|
||||
header = true,
|
||||
union_by_name = true,
|
||||
filename = true,
|
||||
all_varchar = true
|
||||
compression = 'gzip',
|
||||
header = TRUE,
|
||||
union_by_name = TRUE,
|
||||
filename = TRUE,
|
||||
all_varchar = TRUE
|
||||
)
|
||||
),
|
||||
|
||||
cast_and_clean AS (
|
||||
), cast_and_clean AS (
|
||||
SELECT
|
||||
TRY_CAST(report_date AS date) AS report_date,
|
||||
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
|
||||
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 TRY_CAST(report_date AS date) IS NOT NULL
|
||||
AND age_bucket IS NOT NULL
|
||||
AND age_bucket != ''
|
||||
),
|
||||
|
||||
deduplicated AS (
|
||||
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,
|
||||
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
|
||||
GROUP BY
|
||||
hkey
|
||||
)
|
||||
|
||||
SELECT *
|
||||
SELECT
|
||||
*
|
||||
FROM deduplicated
|
||||
WHERE report_date BETWEEN @start_ds AND @end_ds
|
||||
WHERE
|
||||
report_date BETWEEN @start_ds AND @end_ds
|
||||
@@ -1,59 +1,51 @@
|
||||
-- Foundation fact: ICE certified Coffee C (Arabica) warehouse stocks.
|
||||
--
|
||||
-- Reads directly from the landing zone, casts varchar columns to proper types,
|
||||
-- and deduplicates via hash key.
|
||||
-- "Certified" means Coffee C graded and stamped as delivery-eligible
|
||||
-- against ICE futures contracts — a key physical supply indicator.
|
||||
--
|
||||
-- Grain: one row per report_date.
|
||||
|
||||
/* Foundation fact: ICE certified Coffee C (Arabica) warehouse stocks. */ /* Reads directly from the landing zone, casts varchar columns to proper types, */ /* and deduplicates via hash key. */ /* "Certified" means Coffee C graded and stamped as delivery-eligible */ /* against ICE futures contracts — a key physical supply indicator. */ /* Grain: one row per report_date. */
|
||||
MODEL (
|
||||
name foundation.fct_ice_warehouse_stocks,
|
||||
kind INCREMENTAL_BY_TIME_RANGE (
|
||||
time_column report_date
|
||||
),
|
||||
grain (report_date),
|
||||
grain (
|
||||
report_date
|
||||
),
|
||||
start '2000-01-01',
|
||||
cron '@daily'
|
||||
);
|
||||
|
||||
WITH src AS (
|
||||
SELECT * FROM read_csv(
|
||||
SELECT
|
||||
*
|
||||
FROM READ_CSV(
|
||||
@ice_stocks_glob(),
|
||||
compression = 'gzip',
|
||||
header = true,
|
||||
union_by_name = true,
|
||||
filename = true,
|
||||
all_varchar = true
|
||||
compression = 'gzip',
|
||||
header = TRUE,
|
||||
union_by_name = TRUE,
|
||||
filename = TRUE,
|
||||
all_varchar = TRUE
|
||||
)
|
||||
),
|
||||
|
||||
cast_and_clean AS (
|
||||
), cast_and_clean AS (
|
||||
SELECT
|
||||
TRY_CAST(report_date AS date) AS report_date,
|
||||
TRY_CAST(total_certified_bags AS bigint) AS total_certified_bags,
|
||||
TRY_CAST(pending_grading_bags AS bigint) AS pending_grading_bags,
|
||||
|
||||
filename AS source_file,
|
||||
|
||||
-- Dedup key: report date + total bags
|
||||
hash(report_date, total_certified_bags) AS hkey
|
||||
TRY_CAST(report_date AS DATE) AS report_date,
|
||||
TRY_CAST(total_certified_bags AS BIGINT) AS total_certified_bags,
|
||||
TRY_CAST(pending_grading_bags AS BIGINT) AS pending_grading_bags,
|
||||
filename AS source_file,
|
||||
HASH(report_date, total_certified_bags) AS hkey /* Dedup key: report date + total bags */
|
||||
FROM src
|
||||
WHERE TRY_CAST(report_date AS date) IS NOT NULL
|
||||
AND TRY_CAST(total_certified_bags AS bigint) IS NOT NULL
|
||||
),
|
||||
|
||||
deduplicated AS (
|
||||
WHERE
|
||||
NOT TRY_CAST(report_date AS DATE) IS NULL
|
||||
AND NOT TRY_CAST(total_certified_bags AS BIGINT) IS NULL
|
||||
), deduplicated AS (
|
||||
SELECT
|
||||
any_value(report_date) AS report_date,
|
||||
any_value(total_certified_bags) AS total_certified_bags,
|
||||
any_value(pending_grading_bags) AS pending_grading_bags,
|
||||
any_value(source_file) AS source_file,
|
||||
ANY_VALUE(report_date) AS report_date,
|
||||
ANY_VALUE(total_certified_bags) AS total_certified_bags,
|
||||
ANY_VALUE(pending_grading_bags) AS pending_grading_bags,
|
||||
ANY_VALUE(source_file) AS source_file,
|
||||
hkey
|
||||
FROM cast_and_clean
|
||||
GROUP BY hkey
|
||||
GROUP BY
|
||||
hkey
|
||||
)
|
||||
|
||||
SELECT *
|
||||
SELECT
|
||||
*
|
||||
FROM deduplicated
|
||||
WHERE report_date BETWEEN @start_ds AND @end_ds
|
||||
WHERE
|
||||
report_date BETWEEN @start_ds AND @end_ds
|
||||
@@ -1,72 +1,65 @@
|
||||
-- Foundation fact: ICE historical end-of-month Coffee C certified warehouse stocks by port.
|
||||
--
|
||||
-- Reads directly from the landing zone, casts varchar columns to proper types,
|
||||
-- and deduplicates via hash key.
|
||||
-- Covers November 1996 to present (30-year history).
|
||||
--
|
||||
-- Grain: one row per report_date (end-of-month).
|
||||
-- Port columns are in bags (60kg).
|
||||
|
||||
/* Foundation fact: ICE historical end-of-month Coffee C certified warehouse stocks by port. */ /* Reads directly from the landing zone, casts varchar columns to proper types, */ /* and deduplicates via hash key. */ /* Covers November 1996 to present (30-year history). */ /* Grain: one row per report_date (end-of-month). */ /* Port columns are in bags (60kg). */
|
||||
MODEL (
|
||||
name foundation.fct_ice_warehouse_stocks_by_port,
|
||||
kind INCREMENTAL_BY_TIME_RANGE (
|
||||
time_column report_date
|
||||
),
|
||||
grain (report_date),
|
||||
grain (
|
||||
report_date
|
||||
),
|
||||
start '1996-11-01',
|
||||
cron '@daily'
|
||||
);
|
||||
|
||||
WITH src AS (
|
||||
SELECT * FROM read_csv(
|
||||
SELECT
|
||||
*
|
||||
FROM READ_CSV(
|
||||
@ice_stocks_by_port_glob(),
|
||||
compression = 'gzip',
|
||||
header = true,
|
||||
union_by_name = true,
|
||||
filename = true,
|
||||
all_varchar = true
|
||||
compression = 'gzip',
|
||||
header = TRUE,
|
||||
union_by_name = TRUE,
|
||||
filename = TRUE,
|
||||
all_varchar = TRUE
|
||||
)
|
||||
),
|
||||
|
||||
cast_and_clean AS (
|
||||
), cast_and_clean AS (
|
||||
SELECT
|
||||
TRY_CAST(report_date AS date) AS report_date,
|
||||
TRY_CAST(new_york_bags AS bigint) AS new_york_bags,
|
||||
TRY_CAST(new_orleans_bags AS bigint) AS new_orleans_bags,
|
||||
TRY_CAST(houston_bags AS bigint) AS houston_bags,
|
||||
TRY_CAST(miami_bags AS bigint) AS miami_bags,
|
||||
TRY_CAST(antwerp_bags AS bigint) AS antwerp_bags,
|
||||
TRY_CAST(hamburg_bremen_bags AS bigint) AS hamburg_bremen_bags,
|
||||
TRY_CAST(barcelona_bags AS bigint) AS barcelona_bags,
|
||||
TRY_CAST(virginia_bags AS bigint) AS virginia_bags,
|
||||
TRY_CAST(total_bags AS bigint) AS total_bags,
|
||||
|
||||
filename AS source_file,
|
||||
|
||||
hash(report_date, total_bags) AS hkey
|
||||
TRY_CAST(report_date AS DATE) AS report_date,
|
||||
TRY_CAST(new_york_bags AS BIGINT) AS new_york_bags,
|
||||
TRY_CAST(new_orleans_bags AS BIGINT) AS new_orleans_bags,
|
||||
TRY_CAST(houston_bags AS BIGINT) AS houston_bags,
|
||||
TRY_CAST(miami_bags AS BIGINT) AS miami_bags,
|
||||
TRY_CAST(antwerp_bags AS BIGINT) AS antwerp_bags,
|
||||
TRY_CAST(hamburg_bremen_bags AS BIGINT) AS hamburg_bremen_bags,
|
||||
TRY_CAST(barcelona_bags AS BIGINT) AS barcelona_bags,
|
||||
TRY_CAST(virginia_bags AS BIGINT) AS virginia_bags,
|
||||
TRY_CAST(total_bags AS BIGINT) AS total_bags,
|
||||
filename AS source_file,
|
||||
HASH(report_date, total_bags) AS hkey
|
||||
FROM src
|
||||
WHERE TRY_CAST(report_date AS date) IS NOT NULL
|
||||
AND TRY_CAST(total_bags AS bigint) IS NOT NULL
|
||||
),
|
||||
|
||||
deduplicated AS (
|
||||
WHERE
|
||||
NOT TRY_CAST(report_date AS DATE) IS NULL
|
||||
AND NOT TRY_CAST(total_bags AS BIGINT) IS NULL
|
||||
), deduplicated AS (
|
||||
SELECT
|
||||
any_value(report_date) AS report_date,
|
||||
any_value(new_york_bags) AS new_york_bags,
|
||||
any_value(new_orleans_bags) AS new_orleans_bags,
|
||||
any_value(houston_bags) AS houston_bags,
|
||||
any_value(miami_bags) AS miami_bags,
|
||||
any_value(antwerp_bags) AS antwerp_bags,
|
||||
any_value(hamburg_bremen_bags) AS hamburg_bremen_bags,
|
||||
any_value(barcelona_bags) AS barcelona_bags,
|
||||
any_value(virginia_bags) AS virginia_bags,
|
||||
any_value(total_bags) AS total_bags,
|
||||
any_value(source_file) AS source_file,
|
||||
ANY_VALUE(report_date) AS report_date,
|
||||
ANY_VALUE(new_york_bags) AS new_york_bags,
|
||||
ANY_VALUE(new_orleans_bags) AS new_orleans_bags,
|
||||
ANY_VALUE(houston_bags) AS houston_bags,
|
||||
ANY_VALUE(miami_bags) AS miami_bags,
|
||||
ANY_VALUE(antwerp_bags) AS antwerp_bags,
|
||||
ANY_VALUE(hamburg_bremen_bags) AS hamburg_bremen_bags,
|
||||
ANY_VALUE(barcelona_bags) AS barcelona_bags,
|
||||
ANY_VALUE(virginia_bags) AS virginia_bags,
|
||||
ANY_VALUE(total_bags) AS total_bags,
|
||||
ANY_VALUE(source_file) AS source_file,
|
||||
hkey
|
||||
FROM cast_and_clean
|
||||
GROUP BY hkey
|
||||
GROUP BY
|
||||
hkey
|
||||
)
|
||||
|
||||
SELECT *
|
||||
SELECT
|
||||
*
|
||||
FROM deduplicated
|
||||
WHERE report_date BETWEEN @start_ds AND @end_ds
|
||||
WHERE
|
||||
report_date BETWEEN @start_ds AND @end_ds
|
||||
@@ -0,0 +1,93 @@
|
||||
/* Foundation fact: daily weather observations for 8 coffee-growing regions. */ /* Source: OpenWeatherMap One Call API 3.0 / Day Summary */ /* Landing: LANDING_DIR/weather/{location_id}/{year}/{date}.json.gz */ /* One file per (location_id, date). Content: raw OWM day summary JSON. */ /* Each file is a single JSON object (not newline-delimited), so format='auto'. */ /* Grain: (location_id, observation_date) — one row per location per day. */ /* Dedup key: hash(location_id, date) — past weather is immutable. */ /* location_id is parsed from the filename path: split(filename, '/')[-3] */ /* Path structure: .../weather/{location_id}/{year}/{date}.json.gz */ /* Crop stress flags (agronomic thresholds for Arabica coffee): */ /* is_frost — temp_min_c < 2.0°C (ICO frost damage threshold) */ /* is_heat_stress — temp_max_c > 35.0°C (photosynthesis impairment) */ /* is_drought — precipitation_mm < 1.0 (dry day; OWM omits field when 0) */ /* in_growing_season — simplified month-range flag by variety */
|
||||
MODEL (
|
||||
name foundation.fct_weather_daily,
|
||||
kind INCREMENTAL_BY_TIME_RANGE (
|
||||
time_column observation_date
|
||||
),
|
||||
grain (location_id, observation_date),
|
||||
start '2020-01-01',
|
||||
cron '@daily'
|
||||
);
|
||||
|
||||
WITH src AS (
|
||||
/* Each file is a single JSON object with nested fields: */ /* temperature.{min,max,afternoon,morning,evening,night} */ /* precipitation.total (absent when 0 — COALESCE to 0 downstream) */ /* humidity.afternoon */ /* cloud_cover.afternoon */ /* wind.max.{speed,direction} */ /* pressure.afternoon */ /* DuckDB read_json(format='auto') creates STRUCT columns for nested objects; */ /* fields are accessed with dot notation (temperature.min, wind.max.speed). */
|
||||
SELECT
|
||||
*
|
||||
FROM READ_JSON(@weather_glob(), format = 'auto', compression = 'gzip', filename = TRUE)
|
||||
), located AS (
|
||||
SELECT
|
||||
src.*,
|
||||
STR_SPLIT(filename, '/')[-3] AS location_id, /* location_id is the 3rd-from-last path segment: */ /* e.g. .../weather/brazil_minas_gerais/2024/2024-01-15.json.gz → 'brazil_minas_gerais' */
|
||||
TRY_CAST(src."date" AS DATE) AS observation_date
|
||||
FROM src
|
||||
), cast_and_clean AS (
|
||||
SELECT
|
||||
location_id,
|
||||
observation_date,
|
||||
TRY_CAST(located.temperature.min AS DOUBLE) AS temp_min_c, /* Temperature (°C, metric units) */
|
||||
TRY_CAST(located.temperature.max AS DOUBLE) AS temp_max_c,
|
||||
TRY_CAST(located.temperature.afternoon AS DOUBLE) AS temp_afternoon_c,
|
||||
COALESCE(TRY_CAST(located.precipitation.total AS DOUBLE), 0.0) AS precipitation_mm, /* Precipitation (mm total for the day; OWM omits field when 0) */
|
||||
TRY_CAST(located.humidity.afternoon AS DOUBLE) AS humidity_afternoon_pct, /* Humidity (% afternoon reading) */
|
||||
TRY_CAST(located.cloud_cover.afternoon AS DOUBLE) AS cloud_cover_afternoon_pct, /* Cloud cover (% afternoon) */
|
||||
TRY_CAST(located.wind.max.speed AS DOUBLE) AS wind_max_speed_ms, /* Wind (m/s max speed, degrees direction) */
|
||||
TRY_CAST(located.pressure.afternoon AS DOUBLE) AS pressure_afternoon_hpa, /* Pressure (hPa afternoon) */
|
||||
TRY_CAST(located.temperature.min AS DOUBLE) /* Crop stress flags */ < 2.0 AS is_frost,
|
||||
TRY_CAST(located.temperature.max AS DOUBLE) > 35.0 AS is_heat_stress,
|
||||
COALESCE(TRY_CAST(located.precipitation.total AS DOUBLE), 0.0) < 1.0 AS is_drought,
|
||||
HASH(location_id, src."date") AS hkey,
|
||||
filename
|
||||
FROM located
|
||||
WHERE
|
||||
NOT observation_date IS NULL AND NOT location_id IS NULL AND location_id <> ''
|
||||
), deduplicated AS (
|
||||
SELECT
|
||||
ANY_VALUE(location_id) AS location_id,
|
||||
ANY_VALUE(observation_date) AS observation_date,
|
||||
ANY_VALUE(temp_min_c) AS temp_min_c,
|
||||
ANY_VALUE(temp_max_c) AS temp_max_c,
|
||||
ANY_VALUE(temp_afternoon_c) AS temp_afternoon_c,
|
||||
ANY_VALUE(precipitation_mm) AS precipitation_mm,
|
||||
ANY_VALUE(humidity_afternoon_pct) AS humidity_afternoon_pct,
|
||||
ANY_VALUE(cloud_cover_afternoon_pct) AS cloud_cover_afternoon_pct,
|
||||
ANY_VALUE(wind_max_speed_ms) AS wind_max_speed_ms,
|
||||
ANY_VALUE(pressure_afternoon_hpa) AS pressure_afternoon_hpa,
|
||||
ANY_VALUE(is_frost) AS is_frost,
|
||||
ANY_VALUE(is_heat_stress) AS is_heat_stress,
|
||||
ANY_VALUE(is_drought) AS is_drought,
|
||||
hkey
|
||||
FROM cast_and_clean
|
||||
GROUP BY
|
||||
hkey
|
||||
)
|
||||
SELECT
|
||||
d.observation_date,
|
||||
d.location_id,
|
||||
loc.name AS location_name,
|
||||
loc.country,
|
||||
loc.lat,
|
||||
loc.lon,
|
||||
loc.variety,
|
||||
d.temp_min_c,
|
||||
d.temp_max_c,
|
||||
d.temp_afternoon_c,
|
||||
d.precipitation_mm,
|
||||
d.humidity_afternoon_pct,
|
||||
d.cloud_cover_afternoon_pct,
|
||||
d.wind_max_speed_ms,
|
||||
d.pressure_afternoon_hpa,
|
||||
d.is_frost,
|
||||
d.is_heat_stress,
|
||||
d.is_drought,
|
||||
CASE loc.variety
|
||||
WHEN 'Arabica'
|
||||
THEN EXTRACT(MONTH FROM d.observation_date) BETWEEN 4 AND 10
|
||||
WHEN 'Robusta'
|
||||
THEN EXTRACT(MONTH FROM d.observation_date) BETWEEN 4 AND 11
|
||||
ELSE FALSE
|
||||
END AS in_growing_season /* Growing season: simplified month-range flag by variety. */ /* Arabica: Apr–Oct (covers northern + southern hemisphere risk windows). */ /* Robusta: Apr–Nov (Vietnam/Indonesia main cycle). */
|
||||
FROM deduplicated AS d
|
||||
LEFT JOIN seeds.weather_locations AS loc
|
||||
ON d.location_id = loc.location_id
|
||||
WHERE
|
||||
d.observation_date BETWEEN @start_ds AND @end_ds
|
||||
Reference in New Issue
Block a user