Skip to content

Commit b9a8c99

Browse files
committed
fix(review): [R20 S1] qualify source-table entity names as <source>.<table> (cubic-review P2)
Two sources both containing a table named `orders` (e.g. `raw.orders` and `legacy.orders`) previously conflated in `iterateGrainEntities`: - Grain-change detection collapsed them into a single map entry, so a change in one source's grain declaration could surface or suppress gaps for the other. - Finding fingerprint uses the entity name; two distinct source tables with the same table name would dedupe to a single finding. Fix: `iterateGrainEntities` now returns `{name, body}` pairs. Source tables are qualified as `${sourceName}.${tableName}` while models, snapshots, and seeds retain their unqualified name (they live in a flat namespace already). Tests: `grain detector also covers source tables` updated to assert the qualified name; new test `same source-table name in two sources does NOT conflate` locks in the fix. 257/257 review-* tests pass.
1 parent 15c165e commit b9a8c99

2 files changed

Lines changed: 61 additions & 12 deletions

File tree

‎packages/opencode/src/altimate/review/dbt-patterns.ts‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -962,25 +962,37 @@ export interface GrainKeyGap {
962962
* are rare but legal (altimate-harness-bot review, PR #1029
963963
* dbt-patterns.ts:963).
964964
*/
965-
function iterateGrainEntities(d: Record<string, unknown>): Array<Record<string, unknown>> {
966-
const out: Array<Record<string, unknown>> = []
965+
function iterateGrainEntities(d: Record<string, unknown>): Array<{ name: string; body: Record<string, unknown> }> {
966+
const out: Array<{ name: string; body: Record<string, unknown> }> = []
967967
for (const key of ["models", "snapshots", "seeds"] as const) {
968968
const arr = d[key]
969969
if (!Array.isArray(arr)) continue
970970
for (const e of arr) {
971-
if (e && typeof e === "object") out.push(e as Record<string, unknown>)
971+
if (!e || typeof e !== "object") continue
972+
const body = e as Record<string, unknown>
973+
const name = typeof body.name === "string" ? body.name : undefined
974+
if (name) out.push({ name, body })
972975
}
973976
}
974977
// sources nest per-table entities under `.tables[]`; each table has its own
975978
// `name` + `columns` + `tests`/`data_tests`, matching the model shape.
979+
// Qualify with the enclosing source name (`<source>.<table>`) so two
980+
// sources with a table of the same name don't conflate in change-scoping
981+
// OR finding fingerprint (cubic-review P2 on PR #1029).
976982
const sources = d.sources
977983
if (Array.isArray(sources)) {
978984
for (const s of sources) {
979985
if (!s || typeof s !== "object") continue
980-
const tables = (s as Record<string, unknown>).tables
986+
const src = s as Record<string, unknown>
987+
const srcName = typeof src.name === "string" ? src.name : undefined
988+
if (!srcName) continue
989+
const tables = src.tables
981990
if (!Array.isArray(tables)) continue
982991
for (const t of tables) {
983-
if (t && typeof t === "object") out.push(t as Record<string, unknown>)
992+
if (!t || typeof t !== "object") continue
993+
const body = t as Record<string, unknown>
994+
const tblName = typeof body.name === "string" ? body.name : undefined
995+
if (tblName) out.push({ name: `${srcName}.${tblName}`, body })
984996
}
985997
}
986998
}
@@ -999,9 +1011,7 @@ function extractGrainDeclarations(doc: unknown): Map<string, Set<string>> {
9991011
const out = new Map<string, Set<string>>()
10001012
if (!doc || typeof doc !== "object") return out
10011013
const d = doc as Record<string, unknown>
1002-
for (const mm of iterateGrainEntities(d)) {
1003-
const name = typeof mm.name === "string" ? mm.name : undefined
1004-
if (!name) continue
1014+
for (const { name, body: mm } of iterateGrainEntities(d)) {
10051015
const cols = new Set<string>()
10061016
for (const testsKey of ["tests", "data_tests"] as const) {
10071017
const tests = mm[testsKey]
@@ -1087,9 +1097,7 @@ function extractGrainKeyGaps(doc: unknown): GrainKeyGap[] {
10871097
return undefined
10881098
}
10891099

1090-
for (const mm of entities) {
1091-
const mname = typeof mm.name === "string" ? mm.name : undefined
1092-
if (!mname) continue
1100+
for (const { name: mname, body: mm } of entities) {
10931101

10941102
// Contract enforcement: either at model-level `config.contract.enforced`
10951103
// OR at top-level `contract:` (dbt supports both shapes). Consensus

‎packages/opencode/test/altimate/review-dbt-patterns.test.ts‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,10 +957,51 @@ sources:
957957
)
958958
const gaps = f.filter((x) => (x.evidence?.result as any)?.rule === "grain_key_not_null_missing")
959959
expect(gaps.length).toBe(1)
960-
expect(gaps[0].model).toBe("orders")
960+
// Source-table entity is qualified as `<source>.<table>` so two
961+
// sources with same-named tables don't conflate (cubic-review P2).
962+
expect(gaps[0].model).toBe("raw.orders")
961963
expect(gaps[0].column).toBe("event_ts")
962964
})
963965

966+
test("R20 S1: same source-table name in two sources does NOT conflate (cubic-review P2)", () => {
967+
// Two sources both containing a table named `orders`. A grain gap on
968+
// one must not surface / suppress gaps on the other, and the finding
969+
// fingerprint must distinguish them.
970+
const newContent = `version: 2
971+
sources:
972+
- name: raw
973+
tables:
974+
- name: orders
975+
columns:
976+
- name: order_id
977+
data_tests: [not_null]
978+
- name: event_ts # ← grain col, no not_null
979+
data_tests:
980+
- dbt_utils.unique_combination_of_columns:
981+
combination_of_columns: [order_id, event_ts]
982+
- name: legacy
983+
tables:
984+
- name: orders
985+
columns:
986+
- name: order_id
987+
data_tests: [not_null]
988+
- name: event_ts # ← same shape, same grain gap in the OTHER source
989+
data_tests:
990+
- dbt_utils.unique_combination_of_columns:
991+
combination_of_columns: [order_id, event_ts]
992+
`
993+
const f = detectSchemaYmlPatterns(
994+
{ path: "models/sources.yml", status: "added", diff: undefined },
995+
DEFAULT_RUBRIC,
996+
{ oldContent: undefined, newContent },
997+
)
998+
const gaps = f.filter((x) => (x.evidence?.result as any)?.rule === "grain_key_not_null_missing")
999+
// Two distinct entities → two distinct gaps.
1000+
expect(gaps.length).toBe(2)
1001+
const models = new Set(gaps.map((g) => g.model))
1002+
expect(models).toEqual(new Set(["raw.orders", "legacy.orders"]))
1003+
})
1004+
9641005
test("R20 S1: grain detector also covers seeds", () => {
9651006
// Grain-key tests on seeds are rare but legal — coverage for symmetry
9661007
// with `extractTestOccurrences` which iterates all four entity types.

0 commit comments

Comments
 (0)