diff --git a/libs/research/test/feature-library.test.ts b/libs/research/test/feature-library.test.ts index 1bf4c185..c95752e6 100644 --- a/libs/research/test/feature-library.test.ts +++ b/libs/research/test/feature-library.test.ts @@ -1255,3 +1255,185 @@ describe('QA: relation-source allow-list and Vault boundary edge cases', () => { } }); }); + +describe('QA round 2: undeclared-relation probes rejected before any executor call', () => { + // Each probe was also checked in a scratch in-memory DuckDB 1.4.4 session holding a + // `staging_tbl` relation; every form here is valid DuckDB that would read that table. + const undeclaredRelationProbes: ReadonlyArray<{ sql: string; code: FeatureLibraryErrorCode }> = [ + { + sql: 'SELECT b.listing_id, b.ts, b.known_at, s.v AS value FROM {{input:bars_1d}} b NATURAL JOIN staging_tbl s', + code: 'EXTERNAL_DATA_ACCESS', + }, + { + sql: 'SELECT b.listing_id, b.ts, b.known_at, b.close AS value FROM {{input:bars_1d}} b ANTI JOIN staging_tbl s ON s.listing_id = b.listing_id', + code: 'EXTERNAL_DATA_ACCESS', + }, + { + sql: 'SELECT b.listing_id, b.ts, b.known_at, b.close AS value FROM {{input:bars_1d}} b SEMI JOIN staging_tbl USING (listing_id)', + code: 'EXTERNAL_DATA_ACCESS', + }, + { + sql: 'SELECT b.listing_id, b.ts, b.known_at, b.close AS value FROM {{input:bars_1d}} AS b(listing_id) ASOF JOIN staging_tbl s ON s.listing_id <= b.listing_id', + code: 'EXTERNAL_DATA_ACCESS', + }, + { + sql: 'SELECT b.listing_id, b.ts, b.known_at, b.close AS value FROM {{input:bars_1d}} b, LATERAL (SELECT v FROM staging_tbl) s', + code: 'EXTERNAL_DATA_ACCESS', + }, + { + sql: 'SELECT b.listing_id, b.ts, b.known_at, (SELECT v FROM staging_tbl) AS value FROM {{input:bars_1d}} b', + code: 'EXTERNAL_DATA_ACCESS', + }, + { + sql: 'SELECT b.listing_id, b.ts, b.known_at, b.close AS value FROM {{input:bars_1d}} b WHERE EXISTS ((FROM staging_tbl))', + code: 'INVALID_SQL_STATEMENT', + }, + { + sql: 'WITH x AS (SELECT * FROM {{input:bars_1d}}) SELECT x.listing_id, x.ts, x.known_at, x.close AS value FROM x WHERE x.listing_id IN (TABLE staging_tbl)', + code: 'INVALID_SQL_STATEMENT', + }, + { + sql: 'SELECT b.listing_id, b.ts, b.known_at, b.close AS value FROM {{input:bars_1d}} b WHERE b.listing_id = ANY (SELECT listing_id FROM "staging_tbl")', + code: 'EXTERNAL_DATA_ACCESS', + }, + { + sql: 'SELECT b.listing_id, b.ts, b.known_at, b.close AS value FROM {{input:bars_1d}} b QUALIFY b.close IN (SELECT v FROM staging_tbl)', + code: 'EXTERNAL_DATA_ACCESS', + }, + { + sql: 'SELECT b.listing_id, b.ts, b.known_at, b.close AS value FROM {{input:bars_1d}} b WINDOW w AS (ORDER BY (SELECT 1 FROM staging_tbl))', + code: 'EXTERNAL_DATA_ACCESS', + }, + { + // An unused CTE over a catalog table still counts as an undeclared relation source. + sql: 'WITH x AS (SELECT * FROM staging_tbl) SELECT b.listing_id, b.ts, b.known_at, b.close AS value FROM {{input:bars_1d}} b', + code: 'EXTERNAL_DATA_ACCESS', + }, + { + // DuckDB method-call syntax resolves to the table function query_table(). + sql: 'SELECT b.listing_id, b.ts, b.known_at, b.close.query_table() AS value FROM {{input:bars_1d}} b', + code: 'EXTERNAL_DATA_ACCESS', + }, + { + sql: 'SELECT listing_id, ts, known_at, close AS value FROM {{input:bars_1d}} UNION ALL BY NAME SELECT listing_id, ts, ts AS known_at, v AS value FROM staging_tbl', + code: 'INVALID_SQL_STATEMENT', + }, + { + // DuckDB E-strings treat \' as an escaped quote; the tokenizer must not be + // desynchronised into hiding a subquery inside what it believes is a literal. + sql: "SELECT b.listing_id, b.ts, b.known_at, E'\\' ' || (SELECT max(v) FROM staging_tbl) || ' ' AS value FROM {{input:bars_1d}} b", + code: 'INVALID_SQL_STATEMENT', + }, + ]; + + it('rejects each undeclared-relation form with a specific code from validate, registerSignal and runFeatureBatch', async () => { + for (const probe of undeclaredRelationProbes) { + const def = makeSimpleDef({ name: 'probe_leak', sql: probe.sql }); + + expectFeatureErrorCode(() => validateFeatureDefinition(def), probe.code); + expectFeatureErrorCode(() => createFeatureRegistry().registerSignal(def), probe.code); + + const fake = createRecordingFakeExecutor(); + let thrown: unknown; + try { + await runFeatureBatch( + def, + { curatedRoot: '/c', fromYear: 2021, toYear: 2021, vaultCutoffDate: '2022-06-30' }, + fake.executor + ); + } catch (error) { + thrown = error; + } + expect((thrown as FeatureLibraryError).code).toBe(probe.code); + expect(fake.calls).toHaveLength(0); + } + }); + + it('plans contract-schema definitions for every input dataset with inner availability predicates bound before the outer filters', () => { + const cutoff = '2022-06-30'; + const ceiling = '2022-06-29T23:59:59.999999Z'; + const yearEnd = '2021-12-31T23:59:59.999999Z'; + const base = { version: 1, warmupBars: 0, knownAtRule: 'source_known_at' as const }; + const cases: ReadonlyArray<{ def: FeatureDefinition; scanParams: DuckDbParam[] }> = [ + { + def: makeSimpleDef({ + ...base, + name: 'fx_close', + family: 'fx', + inputs: ['bars_1d', 'fx_1d'], + sql: 'SELECT b.listing_id, b.ts, GREATEST(b.known_at, f.known_at) AS known_at, b.close * f.close AS value FROM {{input:bars_1d}} AS b JOIN {{input:fx_1d}} AS f ON f.session_date = b.session_date AND LEFT(f.pair, 3) = b.currency', + }), + scanParams: [yearEnd, ceiling, yearEnd, ceiling], + }, + { + def: makeSimpleDef({ + ...base, + name: 'short_int', + inputs: ['shorts'], + sql: 'SELECT listing_id, report_date AS ts, known_at, shares_short AS value FROM {{input:shorts}}', + }), + scanParams: [ceiling], + }, + { + def: makeSimpleDef({ + ...base, + name: 'evt', + family: 'event', + inputs: ['events'], + sql: 'SELECT listing_id, ts, known_at, 1.0 AS value FROM {{input:events}}', + }), + scanParams: [yearEnd, ceiling], + }, + { + def: makeSimpleDef({ + ...base, + name: 'adj', + inputs: ['adj_factors'], + sql: 'SELECT listing_id, ex_date AS ts, known_at, cum_split_factor AS value FROM {{input:adj_factors}}', + }), + scanParams: [ceiling], + }, + { + def: makeSimpleDef({ + ...base, + name: 'fund', + family: 'fundamental', + inputs: ['fundamentals_pit'], + sql: 'SELECT listing_id, effective_known_at AS ts, effective_known_at AS known_at, revenue_ttm AS value FROM {{input:fundamentals_pit}}', + }), + scanParams: [ceiling], + }, + ]; + + for (const { def, scanParams } of cases) { + const writes = planFeatureBatch(def, { + curatedRoot: '/c', + fromYear: 2021, + toYear: 2021, + vaultCutoffDate: cutoff, + }); + expect(writes).toHaveLength(1); + const write = writes[0] as (typeof writes)[number]; + expect(write.outputPath).toBe(`/c/features/${def.name}/v1/year=2021/part.parquet`); + expect(write.params).toEqual([...scanParams, 2021, yearEnd, ceiling]); + // Bind markers in SQL match the parameter count exactly. + expect(write.sql.split('?').length - 1).toBe(write.params.length); + // Every inner scan predicate appears before the outer year/Vault filter. + const outerFilter = write.sql.indexOf('WHERE year(ts) = ?'); + expect(outerFilter).toBeGreaterThan(0); + let lastScan = -1; + for (let at = write.sql.indexOf('read_parquet('); at !== -1; at = write.sql.indexOf('read_parquet(', at + 1)) { + lastScan = at; + } + expect(lastScan).toBeGreaterThan(-1); + expect(lastScan).toBeLessThan(outerFilter); + expect(write.sql).toContain('CAST(ts AS TIMESTAMPTZ) AS ts'); + expect(write.sql).toContain('CAST(known_at AS TIMESTAMPTZ) AS known_at'); + for (const param of write.params) { + if (typeof param === 'string') { + expect(param < cutoff).toBe(true); + } + } + } + }); +});