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 08:03:04 -05:00
parent d6d6bc8c8b
commit f86e520dec
2 changed files with 159 additions and 14 deletions

View file

@ -1019,7 +1019,7 @@ function validateExpressionSlice(
while (i < end) {
const tok = tokens[i] as SqlToken;
// Nested subquery: (SELECT ...) or (WITH ...)
// Nested subquery: (SELECT ...) or (WITH ...); reject DuckDB FROM-first / TABLE subqueries.
if (tok.kind === 'lparen') {
const closeIdx = findMatchingParen(tokens, i, end);
const innerFirst = tokens[i + 1];
@ -1031,6 +1031,15 @@ function validateExpressionSlice(
i = closeIdx + 1;
continue;
}
if (
innerFirst?.kind === 'word' &&
(innerFirst.upper === 'FROM' || innerFirst.upper === 'TABLE')
) {
throw new FeatureLibraryError(
'INVALID_SQL_STATEMENT',
`Unsupported subquery form "(${innerFirst.upper} ...)" in feature SQL; subqueries must begin with SELECT or WITH.`
);
}
i += 1;
continue;
}
@ -1157,6 +1166,21 @@ function parseTableReference(
return pos;
}
function isJoinClauseStart(tokens: readonly SqlToken[], start: number, end: number): boolean {
let pos = start;
while (pos < end) {
const tok = tokens[pos] as SqlToken;
if (tok.kind !== 'word' || !JOIN_MODIFIER_KEYWORDS.has(tok.upper)) {
return false;
}
if (tok.upper === 'JOIN') {
return true;
}
pos += 1;
}
return false;
}
function validateSelectBranch(
tokens: readonly SqlToken[],
start: number,
@ -1314,7 +1338,7 @@ function validateSelectBranch(
(ct.kind === 'comma' ||
(ct.kind === 'word' &&
(CLAUSE_TERMINATOR_KEYWORDS.has(ct.upper) ||
JOIN_MODIFIER_KEYWORDS.has(ct.upper))))
isJoinClauseStart(tokens, k, end))))
) {
condEnd = k;
break;
@ -1996,8 +2020,9 @@ export function planFeatureBatch(
);
const sql =
'COPY (SELECT CAST(listing_id AS INTEGER) AS listing_id, ts, known_at, CAST(value AS DOUBLE) AS value ' +
`FROM (${expandedSql}) AS __feature_source ` +
'COPY (SELECT listing_id, ts, known_at, value ' +
'FROM (SELECT CAST(listing_id AS INTEGER) AS listing_id, CAST(ts AS TIMESTAMPTZ) AS ts, CAST(known_at AS TIMESTAMPTZ) AS known_at, CAST(value AS DOUBLE) AS value ' +
`FROM (${expandedSql}) AS __feature_source) AS __feature_typed ` +
'WHERE year(ts) = ? AND ts <= ? AND known_at <= ? ' +
`ORDER BY listing_id, ts) TO '${outputPath}' (FORMAT PARQUET, COMPRESSION ZSTD)`;
@ -2039,10 +2064,13 @@ export async function runFeatureBatch(
);
}
const plannedWrites = planFeatureBatch(def, options);
const ref = featureRef(def);
const definitionHash = featureDefinitionHash(def);
const sortedInputs = [...new Set(def.inputs)].sort(compareAscii);
validateFeatureDefinitionInternal(def);
const snapshot = cloneFeatureDefinition(def);
const plannedWrites = planFeatureBatch(snapshot, options);
const ref = featureRef(snapshot);
const definitionHash = featureDefinitionHash(snapshot);
const sortedInputs = [...new Set(snapshot.inputs)].sort(compareAscii);
const { knownAtRule, warmupBars } = snapshot;
const completedWrites: FeatureBatchWriteReceipt[] = [];
for (let i = 0; i < plannedWrites.length; i += 1) {
@ -2074,8 +2102,8 @@ export async function runFeatureBatch(
return Object.freeze({
ref,
definitionHash,
knownAtRule: def.knownAtRule,
warmupBars: def.warmupBars,
knownAtRule,
warmupBars,
inputs: Object.freeze(sortedInputs),
writes: Object.freeze(completedWrites),
});