From 98451ee07cb75dcdd3612ef288a34e4e734fee97 Mon Sep 17 00:00:00 2001 From: Studio Date: Tue, 1 Dec 2026 09:39:58 -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 | 22 +++++++++ libs/research/test/feature-library.test.ts | 53 ++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/libs/research/src/feature-library.ts b/libs/research/src/feature-library.ts index d345a6ef..8a8c9e66 100644 --- a/libs/research/src/feature-library.ts +++ b/libs/research/src/feature-library.ts @@ -522,6 +522,7 @@ const JOIN_MODIFIER_KEYWORDS: ReadonlySet = new Set([ 'LEFT', 'NATURAL', 'OUTER', + 'POSITIONAL', 'RIGHT', 'SEMI', ]); @@ -1065,6 +1066,14 @@ function validateExpressionSlice( continue; } + // Fail closed if an unparsed JOIN token appears inside an expression or clause tail. + if (tok.kind === 'word' && tok.upper === 'JOIN') { + throw new FeatureLibraryError( + 'INVALID_SQL_STATEMENT', + 'Unexpected JOIN outside a parsed FROM/JOIN clause in feature SQL.' + ); + } + // Callable function check: followed by '('. if ( (tok.kind === 'word' || tok.kind === 'quoted_ident') && @@ -1373,6 +1382,19 @@ function validateSelectBranch( } break; } + if (pos < end) { + const afterFrom = tokens[pos] as SqlToken; + if ( + afterFrom.kind !== 'word' || + !CLAUSE_TERMINATOR_KEYWORDS.has(afterFrom.upper) || + afterFrom.upper === 'FROM' + ) { + throw new FeatureLibraryError( + 'INVALID_SQL_STATEMENT', + `Unexpected token "${afterFrom.raw}" after FROM/JOIN clause in feature SQL.` + ); + } + } continue; } diff --git a/libs/research/test/feature-library.test.ts b/libs/research/test/feature-library.test.ts index 1fe1994f..1bf4c185 100644 --- a/libs/research/test/feature-library.test.ts +++ b/libs/research/test/feature-library.test.ts @@ -1166,6 +1166,59 @@ describe('QA: relation-source allow-list and Vault boundary edge cases', () => { ); }); + it('allows POSITIONAL JOIN between declared inputs and fails closed on unknown join modifiers or unparsed JOIN tokens', () => { + expect(() => + validateFeatureDefinition( + makeSimpleDef({ + inputs: ['bars_1d', 'fx_1d'], + sql: 'SELECT b.listing_id, b.ts, b.ts AS known_at, b.close / f.rate AS value FROM {{input:bars_1d}} AS b POSITIONAL JOIN {{input:fx_1d}} AS f', + }) + ) + ).not.toThrow(); + + expect(() => + validateFeatureDefinition( + makeSimpleDef({ + inputs: ['bars_1d', 'fx_1d'], + sql: 'SELECT listing_id, ts, ts AS known_at, close AS value FROM {{input:bars_1d}} POSITIONAL JOIN {{input:fx_1d}} AS f', + }) + ) + ).not.toThrow(); + + // Unknown modifier immediately after FROM/alias must fail closed (never fall through to expression scan). + expectFeatureErrorCode( + () => + validateFeatureDefinition( + makeSimpleDef({ + sql: 'SELECT b.listing_id, b.ts, b.ts AS known_at, b.close AS value FROM {{input:bars_1d}} AS b LATERAL_UNKNOWN JOIN staging_tbl AS s', + }) + ), + 'INVALID_SQL_STATEMENT' + ); + + // Chained JOIN after an ON predicate with an unknown modifier still hits relation allow-list validation. + expectFeatureErrorCode( + () => + validateFeatureDefinition( + makeSimpleDef({ + sql: 'SELECT b.listing_id, b.ts, b.ts AS known_at, b.close AS value FROM {{input:bars_1d}} AS b JOIN {{input:bars_1d}} AS c ON b.ts = c.ts UNKNOWN_MOD JOIN staging_tbl AS s', + }) + ), + 'EXTERNAL_DATA_ACCESS' + ); + + // Unparsed JOIN keyword inside a WHERE expression tail must fail closed. + expectFeatureErrorCode( + () => + validateFeatureDefinition( + makeSimpleDef({ + sql: 'SELECT b.listing_id, b.ts, b.ts AS known_at, b.close AS value FROM {{input:bars_1d}} AS b WHERE b.close > 0 JOIN staging_tbl AS s', + }) + ), + 'INVALID_SQL_STATEMENT' + ); + }); + it('accepts a leap-day cutoff and clamps the cutoff year to the last microsecond of the prior day', () => { const writes = planFeatureBatch(RET_1D_V1, { curatedRoot: '/c',