T-145: M2.2d — fundamentalsAsOf in the DuckDB MarketDataStore: Vault-fenced point-in-time read of fundamentals_pit/year=*/part.parquet with a real-DuckDB restatement fixture test (no new deps)
This commit is contained in:
parent
1a318f0773
commit
dd8dc692af
4 changed files with 1096 additions and 13 deletions
|
|
@ -247,4 +247,485 @@ describe('openDuckDbMarketDataStore (@duckdb/node-api)', () => {
|
|||
await opened.close();
|
||||
}
|
||||
});
|
||||
|
||||
test('fundamentalsAsOf enforces restatement non-overwrite, multi-year Hive glob, Vault microsecond fence, and allowEstimatedLag semantics', async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), 'stockbot-duckdb-fnd-'));
|
||||
tempDirs.push(root);
|
||||
const curatedRoot = join(root, 'curated');
|
||||
const duckTempDir = join(root, 'duckdb-tmp');
|
||||
|
||||
await mkdir(join(curatedRoot, 'fundamentals_pit', 'year=2023'), { recursive: true });
|
||||
await mkdir(join(curatedRoot, 'fundamentals_pit', 'year=2024'), { recursive: true });
|
||||
await mkdir(join(curatedRoot, 'fundamentals_pit', 'year=2025'), { recursive: true });
|
||||
await mkdir(duckTempDir, { recursive: true });
|
||||
|
||||
// Vault cutoff = 2025-02-13T00:00:00.000Z
|
||||
const vaultCutoffTs = computeVaultCutoffTs('2025-08-13');
|
||||
const s1KnownMs = Date.UTC(2024, 4, 1, 20, 0, 0, 0); // 2024-05-01T20:00:00Z
|
||||
const s2KnownMs = Date.UTC(2024, 7, 1, 20, 0, 0, 0); // 2024-08-01T20:00:00Z
|
||||
const s3KnownMs = Date.UTC(2024, 8, 15, 20, 0, 0, 0); // 2024-09-15T20:00:00Z
|
||||
|
||||
const writer = await createNodeDuckDbExecutor();
|
||||
try {
|
||||
// year=2023 partition: listing 50 (only exists in 2023 partition)
|
||||
await writer.run(`
|
||||
COPY (
|
||||
SELECT * FROM (VALUES
|
||||
(
|
||||
50::INTEGER,
|
||||
TIMESTAMPTZ '2023-11-05 20:00:00+00',
|
||||
'filing_date'::VARCHAR,
|
||||
FALSE::BOOLEAN,
|
||||
DATE '2023-09-30',
|
||||
'Q3'::VARCHAR,
|
||||
'USD'::VARCHAR,
|
||||
TIMESTAMPTZ '2023-11-05 20:00:00+00',
|
||||
80.0::DOUBLE,
|
||||
8.0::DOUBLE,
|
||||
12.0::DOUBLE,
|
||||
500.0::DOUBLE,
|
||||
250.0::DOUBLE,
|
||||
10.0::DOUBLE,
|
||||
DATE '2023-09-30',
|
||||
'USD'::VARCHAR,
|
||||
310.0::DOUBLE,
|
||||
31.0::DOUBLE,
|
||||
46.0::DOUBLE,
|
||||
FALSE::BOOLEAN
|
||||
)
|
||||
) AS t(
|
||||
listing_id, effective_known_at, known_at_source, has_estimated_lag,
|
||||
period_end, fiscal_period, currency, period_known_at,
|
||||
revenue, net_income, operating_cash_flow, total_assets, total_equity, shares_outstanding,
|
||||
ttm_period_end, ttm_currency, ttm_revenue, ttm_net_income, ttm_operating_cash_flow, ttm_has_estimated_lag
|
||||
)
|
||||
) TO '${join(curatedRoot, 'fundamentals_pit', 'year=2023', 'part.parquet')}' (FORMAT PARQUET)
|
||||
`);
|
||||
|
||||
// year=2024 partition:
|
||||
// - listing 101: S1 (Q1), S2 (Q2), S3 (Q1 restatement known after Q2 -> latest period stays Q2, TTM changes)
|
||||
// - listing 201: older verified snapshot + newer estimated-lag snapshot (strict mode must omit without fallback)
|
||||
// - listing 202: verified snapshot with estimated-lag TTM only (strict mode keeps row, nulls ttm_* + ttm_period_end + ttm_currency, preserves ttm_has_estimated_lag)
|
||||
await writer.run(`
|
||||
COPY (
|
||||
SELECT * FROM (VALUES
|
||||
(
|
||||
101::INTEGER,
|
||||
TIMESTAMPTZ '2024-05-01 20:00:00+00',
|
||||
'filing_date'::VARCHAR,
|
||||
FALSE::BOOLEAN,
|
||||
DATE '2024-03-31',
|
||||
'Q1'::VARCHAR,
|
||||
'USD'::VARCHAR,
|
||||
TIMESTAMPTZ '2024-05-01 20:00:00+00',
|
||||
100.0::DOUBLE,
|
||||
10.0::DOUBLE,
|
||||
15.0::DOUBLE,
|
||||
1000.0::DOUBLE,
|
||||
500.0::DOUBLE,
|
||||
50.0::DOUBLE,
|
||||
DATE '2024-03-31',
|
||||
'USD'::VARCHAR,
|
||||
380.0::DOUBLE,
|
||||
38.0::DOUBLE,
|
||||
55.0::DOUBLE,
|
||||
FALSE::BOOLEAN
|
||||
),
|
||||
(
|
||||
101::INTEGER,
|
||||
TIMESTAMPTZ '2024-08-01 20:00:00+00',
|
||||
'filing_date'::VARCHAR,
|
||||
FALSE::BOOLEAN,
|
||||
DATE '2024-06-30',
|
||||
'Q2'::VARCHAR,
|
||||
'USD'::VARCHAR,
|
||||
TIMESTAMPTZ '2024-08-01 20:00:00+00',
|
||||
120.0::DOUBLE,
|
||||
14.0::DOUBLE,
|
||||
20.0::DOUBLE,
|
||||
1050.0::DOUBLE,
|
||||
520.0::DOUBLE,
|
||||
50.0::DOUBLE,
|
||||
DATE '2024-06-30',
|
||||
'USD'::VARCHAR,
|
||||
420.0::DOUBLE,
|
||||
44.0::DOUBLE,
|
||||
65.0::DOUBLE,
|
||||
FALSE::BOOLEAN
|
||||
),
|
||||
(
|
||||
101::INTEGER,
|
||||
TIMESTAMPTZ '2024-09-15 20:00:00+00',
|
||||
'filing_date'::VARCHAR,
|
||||
FALSE::BOOLEAN,
|
||||
DATE '2024-06-30',
|
||||
'Q2'::VARCHAR,
|
||||
'USD'::VARCHAR,
|
||||
TIMESTAMPTZ '2024-08-01 20:00:00+00',
|
||||
120.0::DOUBLE,
|
||||
14.0::DOUBLE,
|
||||
20.0::DOUBLE,
|
||||
1050.0::DOUBLE,
|
||||
520.0::DOUBLE,
|
||||
50.0::DOUBLE,
|
||||
DATE '2024-06-30',
|
||||
'USD'::VARCHAR,
|
||||
415.0::DOUBLE,
|
||||
42.0::DOUBLE,
|
||||
63.0::DOUBLE,
|
||||
FALSE::BOOLEAN
|
||||
),
|
||||
(
|
||||
201::INTEGER,
|
||||
TIMESTAMPTZ '2024-05-01 20:00:00+00',
|
||||
'filing_date'::VARCHAR,
|
||||
FALSE::BOOLEAN,
|
||||
DATE '2024-03-31',
|
||||
'Q1'::VARCHAR,
|
||||
'USD'::VARCHAR,
|
||||
TIMESTAMPTZ '2024-05-01 20:00:00+00',
|
||||
50.0::DOUBLE,
|
||||
5.0::DOUBLE,
|
||||
7.0::DOUBLE,
|
||||
400.0::DOUBLE,
|
||||
200.0::DOUBLE,
|
||||
20.0::DOUBLE,
|
||||
DATE '2024-03-31',
|
||||
'USD'::VARCHAR,
|
||||
190.0::DOUBLE,
|
||||
19.0::DOUBLE,
|
||||
27.0::DOUBLE,
|
||||
FALSE::BOOLEAN
|
||||
),
|
||||
(
|
||||
201::INTEGER,
|
||||
TIMESTAMPTZ '2024-08-01 20:00:00+00',
|
||||
'estimated_lag'::VARCHAR,
|
||||
TRUE::BOOLEAN,
|
||||
DATE '2024-06-30',
|
||||
'Q2'::VARCHAR,
|
||||
'USD'::VARCHAR,
|
||||
TIMESTAMPTZ '2024-08-01 20:00:00+00',
|
||||
60.0::DOUBLE,
|
||||
6.0::DOUBLE,
|
||||
8.0::DOUBLE,
|
||||
420.0::DOUBLE,
|
||||
210.0::DOUBLE,
|
||||
20.0::DOUBLE,
|
||||
DATE '2024-06-30',
|
||||
'USD'::VARCHAR,
|
||||
210.0::DOUBLE,
|
||||
21.0::DOUBLE,
|
||||
30.0::DOUBLE,
|
||||
TRUE::BOOLEAN
|
||||
),
|
||||
(
|
||||
202::INTEGER,
|
||||
TIMESTAMPTZ '2024-08-01 20:00:00+00',
|
||||
'filing_date'::VARCHAR,
|
||||
FALSE::BOOLEAN,
|
||||
DATE '2024-06-30',
|
||||
'Q2'::VARCHAR,
|
||||
'CAD'::VARCHAR,
|
||||
TIMESTAMPTZ '2024-08-01 20:00:00+00',
|
||||
75.0::DOUBLE,
|
||||
9.0::DOUBLE,
|
||||
11.0::DOUBLE,
|
||||
600.0::DOUBLE,
|
||||
300.0::DOUBLE,
|
||||
30.0::DOUBLE,
|
||||
DATE '2024-06-30',
|
||||
'CAD'::VARCHAR,
|
||||
300.0::DOUBLE,
|
||||
30.0::DOUBLE,
|
||||
45.0::DOUBLE,
|
||||
TRUE::BOOLEAN
|
||||
)
|
||||
) AS t(
|
||||
listing_id, effective_known_at, known_at_source, has_estimated_lag,
|
||||
period_end, fiscal_period, currency, period_known_at,
|
||||
revenue, net_income, operating_cash_flow, total_assets, total_equity, shares_outstanding,
|
||||
ttm_period_end, ttm_currency, ttm_revenue, ttm_net_income, ttm_operating_cash_flow, ttm_has_estimated_lag
|
||||
)
|
||||
) TO '${join(curatedRoot, 'fundamentals_pit', 'year=2024', 'part.parquet')}' (FORMAT PARQUET)
|
||||
`);
|
||||
|
||||
// year=2025 partition: Vault microsecond boundary rows
|
||||
// - listing 102: 1 microsecond before cutoff (eligible), exact cutoff (excluded), post-cutoff (excluded)
|
||||
// - listing 103: exact cutoff only (must return 0 rows)
|
||||
await writer.run(`
|
||||
COPY (
|
||||
SELECT * FROM (VALUES
|
||||
(
|
||||
102::INTEGER,
|
||||
TIMESTAMPTZ '2025-02-12 23:59:59.999999+00',
|
||||
'filing_date'::VARCHAR,
|
||||
FALSE::BOOLEAN,
|
||||
DATE '2024-12-31',
|
||||
'Q4'::VARCHAR,
|
||||
'USD'::VARCHAR,
|
||||
TIMESTAMPTZ '2025-02-12 23:59:59.999999+00',
|
||||
210.0::DOUBLE,
|
||||
21.0::DOUBLE,
|
||||
31.0::DOUBLE,
|
||||
1500.0::DOUBLE,
|
||||
750.0::DOUBLE,
|
||||
60.0::DOUBLE,
|
||||
DATE '2024-12-31',
|
||||
'USD'::VARCHAR,
|
||||
800.0::DOUBLE,
|
||||
80.0::DOUBLE,
|
||||
120.0::DOUBLE,
|
||||
FALSE::BOOLEAN
|
||||
),
|
||||
(
|
||||
102::INTEGER,
|
||||
TIMESTAMPTZ '2025-02-13 00:00:00.000000+00',
|
||||
'filing_date'::VARCHAR,
|
||||
FALSE::BOOLEAN,
|
||||
DATE '2024-12-31',
|
||||
'Q4'::VARCHAR,
|
||||
'USD'::VARCHAR,
|
||||
TIMESTAMPTZ '2025-02-13 00:00:00.000000+00',
|
||||
999.0::DOUBLE,
|
||||
99.0::DOUBLE,
|
||||
99.0::DOUBLE,
|
||||
9999.0::DOUBLE,
|
||||
4999.0::DOUBLE,
|
||||
60.0::DOUBLE,
|
||||
DATE '2024-12-31',
|
||||
'USD'::VARCHAR,
|
||||
9999.0::DOUBLE,
|
||||
999.0::DOUBLE,
|
||||
999.0::DOUBLE,
|
||||
FALSE::BOOLEAN
|
||||
),
|
||||
(
|
||||
102::INTEGER,
|
||||
TIMESTAMPTZ '2025-02-14 12:00:00.000000+00',
|
||||
'filing_date'::VARCHAR,
|
||||
FALSE::BOOLEAN,
|
||||
DATE '2025-03-31',
|
||||
'Q1'::VARCHAR,
|
||||
'USD'::VARCHAR,
|
||||
TIMESTAMPTZ '2025-02-14 12:00:00.000000+00',
|
||||
1111.0::DOUBLE,
|
||||
111.0::DOUBLE,
|
||||
111.0::DOUBLE,
|
||||
11111.0::DOUBLE,
|
||||
5555.0::DOUBLE,
|
||||
60.0::DOUBLE,
|
||||
DATE '2025-03-31',
|
||||
'USD'::VARCHAR,
|
||||
11111.0::DOUBLE,
|
||||
1111.0::DOUBLE,
|
||||
1111.0::DOUBLE,
|
||||
FALSE::BOOLEAN
|
||||
),
|
||||
(
|
||||
103::INTEGER,
|
||||
TIMESTAMPTZ '2025-02-13 00:00:00.000000+00',
|
||||
'filing_date'::VARCHAR,
|
||||
FALSE::BOOLEAN,
|
||||
DATE '2024-12-31',
|
||||
'Q4'::VARCHAR,
|
||||
'USD'::VARCHAR,
|
||||
TIMESTAMPTZ '2025-02-13 00:00:00.000000+00',
|
||||
777.0::DOUBLE,
|
||||
77.0::DOUBLE,
|
||||
77.0::DOUBLE,
|
||||
7777.0::DOUBLE,
|
||||
3333.0::DOUBLE,
|
||||
40.0::DOUBLE,
|
||||
DATE '2024-12-31',
|
||||
'USD'::VARCHAR,
|
||||
7777.0::DOUBLE,
|
||||
777.0::DOUBLE,
|
||||
777.0::DOUBLE,
|
||||
FALSE::BOOLEAN
|
||||
)
|
||||
) AS t(
|
||||
listing_id, effective_known_at, known_at_source, has_estimated_lag,
|
||||
period_end, fiscal_period, currency, period_known_at,
|
||||
revenue, net_income, operating_cash_flow, total_assets, total_equity, shares_outstanding,
|
||||
ttm_period_end, ttm_currency, ttm_revenue, ttm_net_income, ttm_operating_cash_flow, ttm_has_estimated_lag
|
||||
)
|
||||
) TO '${join(curatedRoot, 'fundamentals_pit', 'year=2025', 'part.parquet')}' (FORMAT PARQUET)
|
||||
`);
|
||||
} finally {
|
||||
await writer.close();
|
||||
}
|
||||
|
||||
const opened = await openDuckDbMarketDataStore({
|
||||
curatedRoot,
|
||||
vaultCutoffTs,
|
||||
env: {
|
||||
STOCK_BOT_DUCKDB_MEMORY_LIMIT: '2GB',
|
||||
STOCK_BOT_DUCKDB_THREADS: '2',
|
||||
STOCK_BOT_DUCKDB_TEMP_DIR: duckTempDir,
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
// 1. Before S1: no row for listing 101
|
||||
const beforeS1 = await opened.store.fundamentalsAsOf({
|
||||
listingIds: [101],
|
||||
fields: ['revenue', 'ttm_revenue'],
|
||||
asOf: '2024-04-30T23:59:59Z',
|
||||
});
|
||||
expect(beforeS1.rows).toEqual([]);
|
||||
|
||||
// 2. Between S1 and S2: Q1 row (revenue 100, ttm_revenue 380)
|
||||
const betweenS1S2 = await opened.store.fundamentalsAsOf({
|
||||
listingIds: [101],
|
||||
fields: ['revenue', 'ttm_revenue'],
|
||||
asOf: '2024-06-15',
|
||||
});
|
||||
expect(betweenS1S2.rows).toHaveLength(1);
|
||||
expect(betweenS1S2.rows[0]).toEqual({
|
||||
listing_id: 101,
|
||||
effective_known_at: s1KnownMs,
|
||||
known_at_source: 'filing_date',
|
||||
has_estimated_lag: false,
|
||||
period_end: '2024-03-31',
|
||||
fiscal_period: 'Q1',
|
||||
currency: 'USD',
|
||||
period_known_at: s1KnownMs,
|
||||
revenue: 100,
|
||||
ttm_revenue: 380,
|
||||
ttm_period_end: '2024-03-31',
|
||||
ttm_currency: 'USD',
|
||||
ttm_has_estimated_lag: false,
|
||||
});
|
||||
|
||||
// 3. Between S2 and S3: Q2 baseline (revenue 120, period_known_at = S2, pre-restatement TTM 420)
|
||||
const betweenS2S3 = await opened.store.fundamentalsAsOf({
|
||||
listingIds: [101],
|
||||
fields: ['revenue', 'ttm_revenue'],
|
||||
asOf: '2024-08-15',
|
||||
});
|
||||
expect(betweenS2S3.rows).toHaveLength(1);
|
||||
expect(betweenS2S3.rows[0]).toEqual({
|
||||
listing_id: 101,
|
||||
effective_known_at: s2KnownMs,
|
||||
known_at_source: 'filing_date',
|
||||
has_estimated_lag: false,
|
||||
period_end: '2024-06-30',
|
||||
fiscal_period: 'Q2',
|
||||
currency: 'USD',
|
||||
period_known_at: s2KnownMs,
|
||||
revenue: 120,
|
||||
ttm_revenue: 420,
|
||||
ttm_period_end: '2024-06-30',
|
||||
ttm_currency: 'USD',
|
||||
ttm_has_estimated_lag: false,
|
||||
});
|
||||
|
||||
// 4. At and after S3 (Q1 restatement with known_at > Q2): Q2 period_end, revenue, and period_known_at stay fixed while ttm_revenue updates to 415
|
||||
for (const asOf of ['2024-09-15T20:00:00Z', '2024-10-01']) {
|
||||
const afterS3 = await opened.store.fundamentalsAsOf({
|
||||
listingIds: [101],
|
||||
fields: ['revenue', 'ttm_revenue'],
|
||||
asOf,
|
||||
});
|
||||
expect(afterS3.rows).toHaveLength(1);
|
||||
expect(afterS3.rows[0]).toEqual({
|
||||
listing_id: 101,
|
||||
effective_known_at: s3KnownMs,
|
||||
known_at_source: 'filing_date',
|
||||
has_estimated_lag: false,
|
||||
period_end: '2024-06-30',
|
||||
fiscal_period: 'Q2',
|
||||
currency: 'USD',
|
||||
period_known_at: s2KnownMs,
|
||||
revenue: 120,
|
||||
ttm_revenue: 415,
|
||||
ttm_period_end: '2024-06-30',
|
||||
ttm_currency: 'USD',
|
||||
ttm_has_estimated_lag: false,
|
||||
});
|
||||
}
|
||||
|
||||
// 5. Multi-partition glob (year=2023 + year=2024) sorted by listing_id ASC, with non-TTM projection omitting TTM metadata columns
|
||||
const multiYear = await opened.store.fundamentalsAsOf({
|
||||
listingIds: [101, 50],
|
||||
fields: ['revenue'],
|
||||
asOf: '2024-10-01',
|
||||
});
|
||||
expect(multiYear.columns).toEqual([
|
||||
'listing_id',
|
||||
'effective_known_at',
|
||||
'known_at_source',
|
||||
'has_estimated_lag',
|
||||
'period_end',
|
||||
'fiscal_period',
|
||||
'currency',
|
||||
'period_known_at',
|
||||
'revenue',
|
||||
]);
|
||||
expect(multiYear.rows.map(r => r.listing_id)).toEqual([50, 101]);
|
||||
expect(multiYear.rows[0]!.period_end).toBe('2023-09-30');
|
||||
expect(multiYear.rows[0]!.revenue).toBe(80);
|
||||
expect(multiYear.rows[1]!.revenue).toBe(120);
|
||||
|
||||
// 6. Vault microsecond fence: last pre-cutoff microsecond is eligible; exact cutoff and post-cutoff are excluded
|
||||
for (const asOf of ['2025-02-13T00:00:00.000Z', '2025-06-01']) {
|
||||
const fenced = await opened.store.fundamentalsAsOf({
|
||||
listingIds: [103, 102],
|
||||
fields: ['revenue'],
|
||||
asOf,
|
||||
});
|
||||
expect(fenced.rows).toHaveLength(1);
|
||||
expect(fenced.rows[0]!.listing_id).toBe(102);
|
||||
expect(fenced.rows[0]!.revenue).toBe(210);
|
||||
expect(fenced.rows[0]!.period_end).toBe('2024-12-31');
|
||||
}
|
||||
|
||||
// 7. allowEstimatedLag default (true) vs strict (false):
|
||||
// - listing 201 (has_estimated_lag = true on latest snapshot) returned by default, omitted in strict mode without falling back to older verified Q1 row
|
||||
// - listing 202 (has_estimated_lag = false, ttm_has_estimated_lag = true) kept in strict mode with all ttm_* values + ttm_period_end + ttm_currency nulled and ttm_has_estimated_lag preserved
|
||||
const lagAllowed = await opened.store.fundamentalsAsOf({
|
||||
listingIds: [202, 201],
|
||||
fields: ['revenue', 'ttm_revenue', 'ttm_net_income', 'ttm_operating_cash_flow'],
|
||||
asOf: '2024-09-01',
|
||||
});
|
||||
expect(lagAllowed.rows.map(r => r.listing_id)).toEqual([201, 202]);
|
||||
expect(lagAllowed.rows[0]!.revenue).toBe(60);
|
||||
expect(lagAllowed.rows[0]!.has_estimated_lag).toBe(true);
|
||||
expect(lagAllowed.rows[1]!.revenue).toBe(75);
|
||||
expect(lagAllowed.rows[1]!.ttm_revenue).toBe(300);
|
||||
expect(lagAllowed.rows[1]!.ttm_net_income).toBe(30);
|
||||
expect(lagAllowed.rows[1]!.ttm_operating_cash_flow).toBe(45);
|
||||
expect(lagAllowed.rows[1]!.ttm_period_end).toBe('2024-06-30');
|
||||
expect(lagAllowed.rows[1]!.ttm_currency).toBe('CAD');
|
||||
expect(lagAllowed.rows[1]!.ttm_has_estimated_lag).toBe(true);
|
||||
|
||||
const lagStrict = await opened.store.fundamentalsAsOf({
|
||||
listingIds: [202, 201],
|
||||
fields: ['revenue', 'ttm_revenue', 'ttm_net_income', 'ttm_operating_cash_flow'],
|
||||
asOf: '2024-09-01',
|
||||
allowEstimatedLag: false,
|
||||
});
|
||||
expect(lagStrict.rows).toHaveLength(1);
|
||||
expect(lagStrict.rows[0]).toEqual({
|
||||
listing_id: 202,
|
||||
effective_known_at: s2KnownMs,
|
||||
known_at_source: 'filing_date',
|
||||
has_estimated_lag: false,
|
||||
period_end: '2024-06-30',
|
||||
fiscal_period: 'Q2',
|
||||
currency: 'CAD',
|
||||
period_known_at: s2KnownMs,
|
||||
revenue: 75,
|
||||
ttm_revenue: null,
|
||||
ttm_net_income: null,
|
||||
ttm_operating_cash_flow: null,
|
||||
ttm_period_end: null,
|
||||
ttm_currency: null,
|
||||
ttm_has_estimated_lag: true,
|
||||
});
|
||||
} finally {
|
||||
await opened.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue