T-139: M2.4a — Versioned Feature Library core: declarative DuckDB SQL feature definitions + registerSignal registry + versioned Parquet batch-plan runner over an injected DuckDbExecutor in libs/research/src/feature-library.ts (pure, no new deps)

This commit is contained in:
Studio 2026-12-01 09:25:50 -05:00
parent f86e520dec
commit 1bbe88947d
2 changed files with 74 additions and 6 deletions

View file

@ -1009,6 +1009,25 @@ function extractProjectionOutputName(tokens: readonly SqlToken[], start: number,
);
}
function hasTopLevelSetOperator(tokens: readonly SqlToken[], start: number, end: number): boolean {
let depth = 0;
for (let i = start; i < end; i += 1) {
const tok = tokens[i] as SqlToken;
if (tok.kind === 'lparen') {
depth += 1;
} else if (tok.kind === 'rparen') {
depth -= 1;
} else if (
depth === 0 &&
tok.kind === 'word' &&
(tok.upper === 'UNION' || tok.upper === 'INTERSECT' || tok.upper === 'EXCEPT')
) {
return true;
}
}
return false;
}
function validateExpressionSlice(
tokens: readonly SqlToken[],
start: number,
@ -1019,13 +1038,15 @@ function validateExpressionSlice(
while (i < end) {
const tok = tokens[i] as SqlToken;
// Nested subquery: (SELECT ...) or (WITH ...); reject DuckDB FROM-first / TABLE subqueries.
// Nested subquery: (SELECT ...), (WITH ...), or compound subquery ((SELECT ...) UNION ...);
// reject DuckDB FROM-first / TABLE subqueries.
if (tok.kind === 'lparen') {
const closeIdx = findMatchingParen(tokens, i, end);
const innerFirst = tokens[i + 1];
if (
innerFirst?.kind === 'word' &&
(innerFirst.upper === 'SELECT' || innerFirst.upper === 'WITH')
(innerFirst?.kind === 'word' &&
(innerFirst.upper === 'SELECT' || innerFirst.upper === 'WITH')) ||
hasTopLevelSetOperator(tokens, i + 1, closeIdx)
) {
validateQuerySlice(tokens, i + 1, closeIdx, inScopeCtes, false);
i = closeIdx + 1;

View file

@ -184,7 +184,8 @@ describe('validateFeatureDefinition', () => {
};
expect(() => validateFeatureDefinition(joinedDef)).not.toThrow();
// Two-input join using LEFT(...) and RIGHT(...) string functions in the ON predicate.
// Two-input join using LEFT(...) and RIGHT(...) string functions in the ON predicate,
// followed immediately by a real LEFT JOIN to verify join-boundary detection.
const leftInJoinOnDef: FeatureDefinition = {
name: 'fx_base_ccy_close',
version: 1,
@ -198,16 +199,46 @@ describe('validateFeatureDefinition', () => {
b.listing_id,
b.ts,
GREATEST(b.known_at, f.known_at) AS known_at,
b.close / f.close AS value
b.close / COALESCE(f2.close, f.close) AS value
FROM {{input:bars_1d}} AS b
INNER JOIN {{input:fx_1d}} AS f
ON b.session_date = f.session_date
AND b.currency = LEFT(f.pair, 3)
AND RIGHT(f.pair, 3) = 'USD'
LEFT JOIN {{input:fx_1d}} AS f2
ON b.session_date = f2.session_date
AND f2.pair = 'USDCAD'
WHERE f.close > 0
`,
};
expect(() => validateFeatureDefinition(leftInJoinOnDef)).not.toThrow();
// Valid compound subquery with parenthesized branches referencing declared inputs,
// and scalar subquery inside parenthesized arithmetic.
const compoundExistsDef: FeatureDefinition = {
name: 'eligible_close',
version: 1,
family: 'event',
kind: 'duckdb_sql',
inputs: ['bars_1d', 'events'],
warmupBars: 0,
knownAtRule: 'max_input_known_at',
sql: `
SELECT
b.listing_id,
b.ts,
b.known_at,
b.close AS value
FROM {{input:bars_1d}} AS b
WHERE EXISTS (
(SELECT 1 FROM {{input:bars_1d}} AS b2 WHERE b2.listing_id = b.listing_id)
UNION ALL
(SELECT 1 FROM {{input:events}} AS e WHERE e.listing_id = b.listing_id)
)
AND ((SELECT MAX(close) FROM {{input:bars_1d}}) + 1) > b.close
`,
};
expect(() => validateFeatureDefinition(compoundExistsDef)).not.toThrow();
});
it('rejects invalid names, versions, families, kinds, warmupBars, and knownAtRule', () => {
@ -368,6 +399,7 @@ describe('validateFeatureDefinition', () => {
'SELECT listing_id, ts, ts AS known_at, close AS value FROM {{input:bars_1d}} WHERE EXISTS ((FROM secret_table))',
'SELECT listing_id, ts, ts AS known_at, close AS value FROM {{input:bars_1d}} WHERE listing_id IN (FROM secret_table SELECT listing_id)',
'SELECT listing_id, ts, ts AS known_at, close AS value FROM {{input:bars_1d}} WHERE EXISTS (TABLE secret_table)',
'SELECT listing_id, ts, ts AS known_at, close AS value FROM {{input:bars_1d}} WHERE EXISTS ((SELECT 1 FROM {{input:bars_1d}}) UNION ALL FROM staging_eligibility)',
];
for (const sql of unsupportedSubqueries) {
expectFeatureErrorCode(
@ -375,6 +407,21 @@ describe('validateFeatureDefinition', () => {
'INVALID_SQL_STATEMENT'
);
}
// Reject compound subqueries with a parenthesized first branch when any later branch references an unauthorized relation.
const compoundBypassSubqueries = [
'SELECT listing_id, ts, ts AS known_at, close AS value FROM {{input:bars_1d}} WHERE EXISTS ((SELECT 1 FROM {{input:bars_1d}}) UNION ALL SELECT 1 FROM staging_eligibility)',
'SELECT listing_id, ts, ts AS known_at, close AS value FROM {{input:bars_1d}} WHERE EXISTS ((SELECT 1 FROM {{input:bars_1d}}) UNION ALL (SELECT 1 FROM staging_eligibility))',
'SELECT listing_id, ts, ts AS known_at, close AS value FROM {{input:bars_1d}} WHERE EXISTS ((SELECT 1 FROM {{input:bars_1d}}) INTERSECT SELECT 1 FROM staging_eligibility)',
'SELECT listing_id, ts, ts AS known_at, close AS value FROM {{input:bars_1d}} WHERE EXISTS ((SELECT 1 FROM {{input:bars_1d}}) EXCEPT SELECT 1 FROM staging_eligibility)',
'SELECT listing_id, ts, ts AS known_at, close AS value FROM {{input:bars_1d}} WHERE EXISTS (((SELECT 1 FROM {{input:bars_1d}}) UNION ALL SELECT 1 FROM staging_eligibility))',
];
for (const sql of compoundBypassSubqueries) {
expectFeatureErrorCode(
() => validateFeatureDefinition(makeSimpleDef({ sql })),
'EXTERNAL_DATA_ACCESS'
);
}
});
it('rejects incorrect final projections with INVALID_SQL_PROJECTION', () => {
@ -644,7 +691,7 @@ describe('planFeatureBatch', () => {
inputs: ['shorts'],
warmupBars: 0,
knownAtRule: 'source_known_at',
sql: 'SELECT listing_id, report_date AS ts, known_at, short_interest AS value FROM {{input:shorts}}',
sql: 'SELECT listing_id, report_date AS ts, known_at, shares_short AS value FROM {{input:shorts}}',
};
const [shortsWrite] = planFeatureBatch(shortsDateDef, {
curatedRoot: '/c',