T-212: M2.3t — Add --qm-financials-old merge and Option B --from-mongo streaming for M2 collections in curate-etl

This commit is contained in:
Studio 2026-12-05 18:15:41 -05:00
parent 60ab228207
commit f082aac436
12 changed files with 518 additions and 131 deletions

View file

@ -1650,6 +1650,69 @@ describe('macroAsOf and crossSection point-in-time edge cases via real DuckDB (Q
}
});
test('crossSection caps universe membership at end-of-date when asOf is later, while returning revised features', async () => {
const root = await mkdtemp(join(tmpdir(), 'stockbot-duckdb-xsec-late-bar-'));
tempDirs.push(root);
const curatedRoot = join(root, 'curated');
const duckTempDir = join(root, 'duckdb-tmp');
await mkdir(join(curatedRoot, 'bars_1d', 'year=2025'), { recursive: true });
await mkdir(join(curatedRoot, 'features', 'rev_feat', 'v1', 'year=2025'), { recursive: true });
await mkdir(duckTempDir, { recursive: true });
const vaultCutoffTs = Date.UTC(2025, 5, 20, 0, 0, 0, 0); // 2025-06-20T00:00:00Z
const writer = await createNodeDuckDbExecutor();
try {
// Listing 401: June 7 bar known on June 7 -> member of June 7 universe.
// Listing 402: only qualifying June 7 bar was first known on June 10 -> must NOT enter June 7 universe even when asOf is June 10.
await writer.run(`
COPY (
SELECT * FROM (VALUES
(401::INTEGER, DATE '2025-06-07', TIMESTAMPTZ '2025-06-07 20:00:00+00', 10.0::DOUBLE, 10.5::DOUBLE, 9.5::DOUBLE, 10.2::DOUBLE, 1000.0::DOUBLE, NULL::DOUBLE, 'USD'::VARCHAR, TIMESTAMPTZ '2025-06-07 20:00:00+00'),
(402::INTEGER, DATE '2025-06-07', TIMESTAMPTZ '2025-06-07 20:00:00+00', 20.0::DOUBLE, 20.5::DOUBLE, 19.5::DOUBLE, 20.2::DOUBLE, 1000.0::DOUBLE, NULL::DOUBLE, 'USD'::VARCHAR, TIMESTAMPTZ '2025-06-10 12:00:00+00')
) AS t(listing_id, session_date, ts, open, high, low, close, volume, vwap, currency, known_at)
) TO '${join(curatedRoot, 'bars_1d', 'year=2025', 'part.parquet')}' (FORMAT PARQUET)
`);
// rev_feat@v1: listing 401 has original (1.0 on June 7) and revision (2.5 on June 10);
// listing 402 also has a feature row on June 7/10, but must stay excluded from the universe.
await writer.run(`
COPY (
SELECT * FROM (VALUES
(401::INTEGER, TIMESTAMPTZ '2025-06-07 20:00:00+00', TIMESTAMPTZ '2025-06-07 20:00:00+00', 1.0::DOUBLE),
(401::INTEGER, TIMESTAMPTZ '2025-06-07 20:00:00+00', TIMESTAMPTZ '2025-06-10 12:00:00+00', 2.5::DOUBLE),
(402::INTEGER, TIMESTAMPTZ '2025-06-07 20:00:00+00', TIMESTAMPTZ '2025-06-10 12:00:00+00', 9.9::DOUBLE)
) AS t(listing_id, ts, known_at, value)
) TO '${join(curatedRoot, 'features', 'rev_feat', 'v1', 'year=2025', 'part.parquet')}' (FORMAT PARQUET)
`);
} finally {
await writer.close();
}
const opened = await openDuckDbMarketDataStore({
curatedRoot,
vaultCutoffTs,
env: budgetEnv(duckTempDir),
});
try {
const sameDay = await opened.store.crossSection({
features: ['rev_feat@v1'],
date: '2025-06-07',
universe: 'US_EQ',
});
expect(sameDay.rows).toEqual([{ listing_id: 401, 'rev_feat@v1': 1.0 }]);
const laterAsOf = await opened.store.crossSection({
features: ['rev_feat@v1'],
date: '2025-06-07',
universe: 'US_EQ',
asOf: '2025-06-10',
});
expect(laterAsOf.rows).toEqual([{ listing_id: 401, 'rev_feat@v1': 2.5 }]);
} finally {
await opened.close();
}
});
test('macroAsOf treats known_at == asOf as visible and honours the period_date from bound and country filter', async () => {
// Arrange
const root = await mkdtemp(join(tmpdir(), 'stockbot-duckdb-macro-pit-'));

View file

@ -2009,11 +2009,15 @@ function planCrossSectionInternal(
const lookbackStartDate = formatUtcDate(lookbackStartMs);
const endDate = formatUtcDate(fenced.toMs);
const fromIso = formatUtcIsoWithMicros(fenced.fromMs, 0);
const toIso = formatUtcIsoWithMicros(fenced.toMs, resolveToSubMsMicros(q.date, fenced));
const asOfIso = formatUtcIsoWithMicros(
fenced.asOfMs,
resolveAsOfSubMsMicros(rawAsOf, q.date, fenced)
);
const toSubMsMicros = resolveToSubMsMicros(q.date, fenced);
const asOfSubMsMicros = resolveAsOfSubMsMicros(rawAsOf, q.date, fenced);
const toIso = formatUtcIsoWithMicros(fenced.toMs, toSubMsMicros);
const asOfIso = formatUtcIsoWithMicros(fenced.asOfMs, asOfSubMsMicros);
const universeKnownAtIso =
fenced.asOfMs < fenced.toMs ||
(fenced.asOfMs === fenced.toMs && asOfSubMsMicros < toSubMsMicros)
? asOfIso
: toIso;
const featureYear = new Date(fenced.fromMs).getUTCFullYear();
const barsPath = joinCuratedPath(curatedRoot, 'bars_1d/year=*/part.parquet');
@ -2025,7 +2029,13 @@ function planCrossSectionInternal(
const ctes: string[] = [
`u AS (SELECT DISTINCT b.listing_id FROM read_parquet('${barsPath}', hive_partitioning = true) AS b WHERE ${currencyPredicate} AND b.session_date >= ? AND b.session_date <= ? AND b.ts <= ? AND b.known_at <= ?)`,
];
const params: DuckDbParam[] = [...currencies, lookbackStartDate, endDate, toIso, asOfIso];
const params: DuckDbParam[] = [
...currencies,
lookbackStartDate,
endDate,
toIso,
universeKnownAtIso,
];
const selectCols: string[] = ['u.listing_id'];
const joins: string[] = [];

View file

@ -3464,6 +3464,50 @@ describe('planCrossSectionQuery and store.crossSection', () => {
DEFAULT_CTX
)
).toThrow(VaultHoldoutFenceError);
// Later explicit asOf caps universe membership at end-of-date while retaining asOf for features.
const laterAsOf = planCrossSectionQuery(
{
features: ['ret_1d@v1'],
date: '2025-06-07',
universe: 'US_EQ',
asOf: '2025-06-10',
},
DEFAULT_CTX
);
expect(laterAsOf.params).toEqual([
'USD',
'2025-05-31',
'2025-06-07',
'2025-06-07T23:59:59.999999Z',
'2025-06-07T23:59:59.999999Z',
2025,
'2025-06-07T00:00:00.000Z',
'2025-06-07T23:59:59.999999Z',
'2025-06-10T23:59:59.999999Z',
]);
// Earlier explicit intraday asOf applies to both universe membership and features.
const earlierAsOf = planCrossSectionQuery(
{
features: ['ret_1d@v1'],
date: '2025-06-07',
universe: 'US_EQ',
asOf: '2025-06-07T21:00:00Z',
},
DEFAULT_CTX
);
expect(earlierAsOf.params).toEqual([
'USD',
'2025-05-31',
'2025-06-07',
'2025-06-07T23:59:59.999999Z',
'2025-06-07T21:00:00.000Z',
2025,
'2025-06-07T00:00:00.000Z',
'2025-06-07T23:59:59.999999Z',
'2025-06-07T21:00:00.000Z',
]);
});
it('maps nullable feature values via store.crossSection and rejects invalid/post-cutoff requests with zero extra calls', async () => {