diff --git a/libs/research/test/feature-library.test.ts b/libs/research/test/feature-library.test.ts index c95752e6..1ae89e24 100644 --- a/libs/research/test/feature-library.test.ts +++ b/libs/research/test/feature-library.test.ts @@ -1437,3 +1437,43 @@ describe('QA round 2: undeclared-relation probes rejected before any executor ca } }); }); + +describe('QA round 3: WITH RECURSIVE cannot shadow an undeclared catalog relation', () => { + // Reproduced in a scratch in-memory DuckDB 1.4.4 session holding `staging_tbl(v = 42.0)`: + // DuckDB treats a RECURSIVE CTE body without a recursive UNION term (or the anchor term of + // one that has it) as a non-recursive scope, so `FROM staging_tbl` inside the body binds to + // the catalog table. runFeatureBatch then wrote 42.0 into + // features/leak/v1/year=2021/part.parquet while the manifest reported inputs ['bars_1d']. + const recursiveShadowProbes: readonly string[] = [ + 'WITH RECURSIVE staging_tbl AS (SELECT * FROM staging_tbl) SELECT b.listing_id, b.ts, b.ts AS known_at, s.v AS value FROM {{input:bars_1d}} AS b, staging_tbl AS s', + 'WITH RECURSIVE staging_tbl AS (SELECT * FROM staging_tbl UNION ALL SELECT * FROM staging_tbl WHERE 1 = 0) SELECT b.listing_id, b.ts, b.ts AS known_at, s.v AS value FROM {{input:bars_1d}} AS b, staging_tbl AS s', + ]; + + it('rejects a RECURSIVE CTE whose body reads a same-named catalog table, with no executor call', async () => { + for (const sql of recursiveShadowProbes) { + const def = makeSimpleDef({ name: 'leak', sql }); + + let validateError: unknown; + try { + validateFeatureDefinition(def); + } catch (error) { + validateError = error; + } + expect(validateError).toBeInstanceOf(FeatureLibraryError); + expect(['EXTERNAL_DATA_ACCESS', 'INVALID_SQL_STATEMENT']).toContain( + (validateError as FeatureLibraryError).code + ); + expect(() => createFeatureRegistry().registerSignal(def)).toThrow(FeatureLibraryError); + + const fake = createRecordingFakeExecutor(); + await expect( + runFeatureBatch( + def, + { curatedRoot: '/c', fromYear: 2021, toYear: 2021, vaultCutoffDate: '2022-01-01' }, + fake.executor + ) + ).rejects.toBeInstanceOf(FeatureLibraryError); + expect(fake.calls).toHaveLength(0); + } + }); +});