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',