# BI-P014, P016–P022, P025–P026, P028–P031 — query, metric, quality Extends the analytics core merged in `0pt#34` (BI-P002..P012). Nothing there is rebuilt. This document is organised around what is **proven** and what is **not**, because the second list is the one that gets dropped. --- ## Where things live | prompt | module | live proof | | -------------------------------------- | ----------------------------------------------------------------------------------------- | ----------------------------------------------------------------- | | BI-P014 DuckDB provider | `src/analytics/providers/duckdb-provider.ts` | `tests/duckdb/duckdb-provider.live.test.ts` | | BI-P016 query request contract | `src/analytics/contract/query-request.ts`, `compile-clickhouse.ts`, `compile-semantic.ts` | unit only (pure compilation) | | BI-P017 analyst SQL contract | `src/analytics/sql/` | `tests/integration/analyst-sql-clickhouse.integration.test.ts` | | BI-P018 execution model | `src/analytics/execution/query-execution-record.ts` | `sql/009` + Postgres realinfra | | BI-P019 cancellation | `src/analytics/execution/cancellation.ts` | `tests/integration/query-cancellation-budget.integration.test.ts` | | BI-P020 query cache | `src/analytics/cache/query-cache.ts` | unit only (no engine involved) | | BI-P021 cost / budget | `src/analytics/cost/query-budget.ts` | same live suite as BI-P019 | | BI-P022 dbt contract | `src/analytics/transform/dbt-contract.ts`, `analytics/dbt/` | contract only — execution is `n0d#38` | | BI-P025 quality provider | `src/analytics/quality/` | `tests/integration/data-quality-clickhouse.integration.test.ts` | | BI-P026 freshness | `src/analytics/quality/freshness.ts` | unit + `sql/009` defaults | | BI-P028/P031 metric model + governance | `src/analytics/metrics/metric-model.ts` | Postgres trigger, realinfra | | BI-P029 semantic provider | `src/analytics/metrics/semantic-provider.ts` | **partially — see below** | | BI-P030 semantic registry | `src/analytics/metrics/semantic-registry.ts` | unit + Postgres constraints | --- ## Five findings that changed the implementation Each was found by writing a test that could fail, and each is pinned so it cannot quietly stop being true. ### 1. ClickHouse `readonly = 1` on a user profile forbids **every** request setting Measured on 24.8.14.39. A user whose profile carries `readonly = 1` answers `Code: 164. Cannot modify 'max_rows_to_read' setting in readonly mode` to any setting sent with the statement. So BI-P012's ceilings **cannot travel with an analyst statement** — they are baked into the user's `SETTINGS PROFILE` at provisioning. Proven both ways: an over-budget scan fails with code 158 from the profile with no setting sent, and an attempt to raise it from the request fails with 164. ### 2. `CREATE ROW POLICY IF NOT EXISTS` is a silent no-op on re-provisioning Dropping a user empties the policy's apply-to list. The next provisioning run sees the policy exists and does nothing, so it survives with `apply_to_list = []` — applying to **nobody** — while `system.row_policies` still shows the correct `select_filter`. The restricted user read 100 of 100 rows, **both tenants**, from the base table. Fixed with `OR REPLACE`, plus `rowPolicyVerificationSql()` which turns "the policy is applied" from an assumption into a query, and a test asserting `applied_to_user = 1` per object. `CREATE SETTINGS PROFILE IF NOT EXISTS` has the identical shape and is fixed the same way. ### 3. DuckDB's `allowed_directories` **alone** is a no-op Measured on 1.5.5. With `allowed_directories` set to one mount directory and nothing else, `read_csv('/etc/passwd')` **returned the file** and an `https://` URL was fetched over the network. Only `enable_external_access = false` alongside it makes the allow-list bind. The obvious spelling of this control appears in `duckdb_settings()`, looks like a sandbox, and permits everything. The measurement is pinned by a test that never skips, so simplifying the bootstrap breaks the build rather than the guarantee. ### 4. `pg_constraint.conname` is not schema-scoped `SELECT 1 FROM pg_constraint WHERE conname = '...'` matches a same-named constraint in **any** schema, so `sql/009` applied into a second schema silently skipped every guarded `CHECK`. Every guard now scopes via `conrelid = '