-- Core schema for the local market-data collection database. -- Timestamps are stored in UTC. CIK identifies issuers; security_key identifies instruments. -- Production datasets are persisted in Parquet under release/data/. CREATE TABLE IF NOT EXISTS schema_versions ( version INTEGER PRIMARY KEY, applied_at TIMESTAMPTZ NOT NULL, description VARCHAR NOT NULL ); CREATE TABLE IF NOT EXISTS collection_runs ( run_id VARCHAR PRIMARY KEY, run_type VARCHAR NOT NULL, started_at TIMESTAMPTZ NOT NULL, finished_at TIMESTAMPTZ, status VARCHAR NOT NULL, notes VARCHAR ); CREATE TABLE IF NOT EXISTS issuers ( cik VARCHAR PRIMARY KEY, issuer_name VARCHAR, first_seen_at TIMESTAMPTZ NOT NULL, last_seen_at TIMESTAMPTZ NOT NULL ); CREATE TABLE IF NOT EXISTS securities ( security_key VARCHAR PRIMARY KEY, source_security_id VARCHAR, source_code VARCHAR NOT NULL, ticker VARCHAR, security_name VARCHAR, exchange_code VARCHAR, instrument_type VARCHAR, cik VARCHAR, listing_date DATE, is_active BOOLEAN NOT NULL, first_seen_at TIMESTAMPTZ NOT NULL, last_seen_at TIMESTAMPTZ NOT NULL ); CREATE TABLE IF NOT EXISTS security_master_snapshots ( snapshot_date DATE NOT NULL, security_key VARCHAR NOT NULL, source_security_id VARCHAR, source_code VARCHAR NOT NULL, ticker VARCHAR, security_name VARCHAR, exchange_code VARCHAR, instrument_type VARCHAR, listing_date DATE, is_delisted BOOLEAN, is_in_scope BOOLEAN NOT NULL, cik VARCHAR, cik_candidates VARCHAR, cik_match_status VARCHAR, raw_payload JSON, retrieved_at TIMESTAMPTZ NOT NULL, run_id VARCHAR NOT NULL, PRIMARY KEY (snapshot_date, security_key) ); -- Regular-session, non-adjusted 60-minute price bars. -- Partitioned by year=YYYY/month=MM/data_0.parquet under release/data/hourly-bars/. CREATE TABLE IF NOT EXISTS hourly_bars ( security_key VARCHAR NOT NULL, source_security_id VARCHAR, cik VARCHAR, ticker VARCHAR, exchange_code VARCHAR, bar_timestamp TIMESTAMPTZ NOT NULL, interval_minutes SMALLINT NOT NULL, adjustment_mode VARCHAR NOT NULL, session_type VARCHAR NOT NULL, open DECIMAL(20, 8), high DECIMAL(20, 8), low DECIMAL(20, 8), close DECIMAL(20, 8), volume BIGINT, turnover DECIMAL(28, 8), currency VARCHAR, retrieved_at TIMESTAMPTZ NOT NULL, run_id VARCHAR NOT NULL, year INTEGER NOT NULL, month SMALLINT NOT NULL, PRIMARY KEY (security_key, bar_timestamp, interval_minutes, adjustment_mode) ); -- Standardized, brand-neutral company fundamentals table. -- Consolidated from Income Statement, Balance Sheet, Cash Flow, and Key Indicators. -- Net common stock issuance flow (net_common_stock_issuance) is negative for buybacks, positive for issuance. -- For foreign ADRs and non-USD filers, real_shareholder_return is left NULL because statutory cash flows -- and USD-denominated market capitalizations cannot be reconciled without cross-currency exchange rates. -- Persisted at release/data/fundamentals/company_fundamentals.parquet. CREATE TABLE IF NOT EXISTS company_fundamentals ( cik VARCHAR NOT NULL, ticker VARCHAR NOT NULL, company_name VARCHAR, time VARCHAR, fiscal_year VARCHAR, period VARCHAR, currency_code VARCHAR, net_common_stock_issuance DOUBLE, market_cap DOUBLE, real_shareholder_return DOUBLE, operating_cash_flow DOUBLE, free_cash_flow DOUBLE, net_profit DOUBLE, total_revenue DOUBLE, gross_profit DOUBLE, total_assets DOUBLE, total_liabilities DOUBLE, total_equity DOUBLE, basic_eps DOUBLE, diluted_eps DOUBLE, retrieved_at TIMESTAMPTZ, run_id VARCHAR, PRIMARY KEY (cik, ticker, time) ); CREATE TABLE IF NOT EXISTS dividend_events ( event_key VARCHAR PRIMARY KEY, security_key VARCHAR NOT NULL, cik VARCHAR, announcement_date DATE, ex_dividend_date DATE, record_date DATE, payable_date DATE, amount DECIMAL(20, 8), currency VARCHAR, distribution_description VARCHAR, status VARCHAR, raw_payload JSON, first_seen_at TIMESTAMPTZ NOT NULL, last_seen_at TIMESTAMPTZ NOT NULL, run_id VARCHAR NOT NULL ); -- One row per security, dataset, and requested date range; update status as work progresses. CREATE TABLE IF NOT EXISTS collection_tasks ( task_id VARCHAR PRIMARY KEY, run_id VARCHAR, security_key VARCHAR, dataset_name VARCHAR NOT NULL, range_start DATE, range_end DATE, status VARCHAR NOT NULL, attempt_count INTEGER NOT NULL DEFAULT 0, last_error VARCHAR, started_at TIMESTAMPTZ, finished_at TIMESTAMPTZ, updated_at TIMESTAMPTZ NOT NULL ); CREATE TABLE IF NOT EXISTS data_quality_checks ( run_id VARCHAR NOT NULL, dataset_name VARCHAR NOT NULL, check_name VARCHAR NOT NULL, status VARCHAR NOT NULL, severity VARCHAR NOT NULL, checked_at TIMESTAMPTZ NOT NULL, details JSON, PRIMARY KEY (run_id, dataset_name, check_name) ); -- Active securities view CREATE OR REPLACE VIEW current_securities AS SELECT * FROM securities WHERE is_active; -- Crosswalk coverage view CREATE OR REPLACE VIEW cik_mapping_coverage AS SELECT snapshot_date, cik_match_status, COUNT(*) AS security_count FROM security_master_snapshots WHERE is_in_scope GROUP BY snapshot_date, cik_match_status; -- Live convenience views over published Parquet datasets CREATE OR REPLACE VIEW hourly_bars_parquet AS SELECT * FROM read_parquet('release/data/hourly-bars/*/*/*.parquet'); CREATE OR REPLACE VIEW company_fundamentals_parquet AS SELECT * FROM read_parquet('release/data/fundamentals/company_fundamentals.parquet'); CREATE OR REPLACE VIEW issuer_security_map_parquet AS SELECT * FROM read_parquet('release/data/fundamentals/issuer_security_map_*.parquet');