From 1bbe88947dfaa007d708a7fa5938864231edfbcb Mon Sep 17 00:00:00 2001 From: Studio Date: Tue, 1 Dec 2026 09:25:50 -0500 Subject: [PATCH] =?UTF-8?q?T-139:=20M2.4a=20=E2=80=94=20Versioned=20Featur?= =?UTF-8?q?e=20Library=20core:=20declarative=20DuckDB=20SQL=20feature=20de?= =?UTF-8?q?finitions=20+=20`registerSignal`=20registry=20+=20versioned=20P?= =?UTF-8?q?arquet=20batch-plan=20runner=20over=20an=20injected=20`DuckDbEx?= =?UTF-8?q?ecutor`=20in=20`libs/research/src/feature-library.ts`=20(pure,?= =?UTF-8?q?=20no=20new=20deps)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/research/src/feature-library.ts | 27 +++++++++-- libs/research/test/feature-library.test.ts | 53 ++++++++++++++++++++-- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/libs/research/src/feature-library.ts b/libs/research/src/feature-library.ts index 8bda0847..d345a6ef 100644 --- a/libs/research/src/feature-library.ts +++ b/libs/research/src/feature-library.ts @@ -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; diff --git a/libs/research/test/feature-library.test.ts b/libs/research/test/feature-library.test.ts index 322c92bd..4200a10e 100644 --- a/libs/research/test/feature-library.test.ts +++ b/libs/research/test/feature-library.test.ts @@ -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',