File size: 5,914 Bytes
1754844
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
-- 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');