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:39:58 -05:00
parent 94d8631d40
commit 98451ee07c
2 changed files with 75 additions and 0 deletions

View file

@ -522,6 +522,7 @@ const JOIN_MODIFIER_KEYWORDS: ReadonlySet<string> = 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: <word|quoted_ident> 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;
}

View file

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