From c4083a8f323d360458da7074ef3af8f7bce59ec0 Mon Sep 17 00:00:00 2001 From: Studio Date: Tue, 1 Dec 2026 10:36:49 -0500 Subject: [PATCH] T-139: tests --- libs/research/test/feature-library.test.ts | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/libs/research/test/feature-library.test.ts b/libs/research/test/feature-library.test.ts index dd5dbd6a..fbb76490 100644 --- a/libs/research/test/feature-library.test.ts +++ b/libs/research/test/feature-library.test.ts @@ -1475,3 +1475,62 @@ describe('QA round 3: WITH RECURSIVE cannot shadow an undeclared catalog relatio } }); }); + +describe('QA round 4: CTE scope must match DuckDB identifier folding (ASCII-only)', () => { + // Reproduced in a scratch in-memory DuckDB 1.4.4 session (`SET TimeZone='UTC'`) holding catalog + // tables `kv(v = 42.0)` and `"ä"(v = 43.0)` plus one bars_1d row (listing 1, 2021-03-01). DuckDB + // folds identifier case for ASCII only, so `"Kv"` (KELVIN SIGN) is not the same name as `kv` + // and `"Ä"` is not `"ä"`; the validator's `toLowerCase()` treated them as the same in-scope CTE. + // runFeatureBatch then wrote 42.0 / 43.0 into features/leak_*/v1/year=2021/part.parquet while the + // manifest reported inputs ['bars_1d']. + const unicodeFoldProbes: readonly string[] = [ + 'WITH "Kv" AS (SELECT 1.0 AS v) SELECT b.listing_id, b.ts, b.ts AS known_at, s.v AS value FROM {{input:bars_1d}} AS b, kv AS s', + 'WITH "Ä" AS (SELECT 1.0 AS v) SELECT b.listing_id, b.ts, b.ts AS known_at, s.v AS value FROM {{input:bars_1d}} AS b, "ä" AS s', + 'SELECT b.listing_id, b.ts, b.ts AS known_at, b.close AS value FROM {{input:bars_1d}} AS b WHERE EXISTS (WITH "Kv" AS (SELECT 1 AS v) SELECT 1 FROM kv)', + ]; + + it('rejects a reference that only matches a CTE under Unicode case folding, with zero executor calls', async () => { + for (const sql of unicodeFoldProbes) { + const def = makeSimpleDef({ name: 'leak', sql }); + + for (const attempt of [ + () => validateFeatureDefinition(def), + () => createFeatureRegistry().registerSignal(def), + ]) { + let error: unknown; + try { + attempt(); + } catch (caught) { + error = caught; + } + expect(error).toBeInstanceOf(FeatureLibraryError); + expect(['EXTERNAL_DATA_ACCESS', 'INVALID_SQL_STATEMENT']).toContain( + (error as FeatureLibraryError).code + ); + } + + const fake = createRecordingFakeExecutor(); + let execError: unknown; + try { + await runFeatureBatch( + def, + { curatedRoot: '/c', fromYear: 2021, toYear: 2021, vaultCutoffDate: '2022-01-01' }, + fake.executor + ); + } catch (error) { + execError = error; + } + expect(execError).toBeInstanceOf(FeatureLibraryError); + expect(fake.calls).toHaveLength(0); + } + }); + + it('still accepts an ASCII case-insensitive quoted CTE reference, which DuckDB binds to the CTE', () => { + // Verified in DuckDB 1.4.4: `WITH "STAGING" AS (SELECT 1.0 AS v) SELECT v FROM staging` returns 1.0 + // even when a catalog table `staging(v = 42.0)` exists. + const def = makeSimpleDef({ + sql: 'WITH "BARS" AS (SELECT listing_id, ts, close FROM {{input:bars_1d}}) SELECT listing_id, ts, ts AS known_at, close AS value FROM bars', + }); + expect(() => validateFeatureDefinition(def)).not.toThrow(); + }); +});