Skip to content

Commit 5ad0e85

Browse files
sahrizviclaude
andcommitted
fix(review): [R20 S1] address consensus-review findings on grain-key detector
Bundle addresses PR #1029's consensus review: - MAJOR #1 — model-level and column-level primary_key / not_null constraints now count as coverage. dbt 1.5+ supports model-level constraints via `constraints: [{type: primary_key, columns: [a, b]}]`, and a primary_key inherently enforces NOT NULL on Postgres/Snowflake/ BigQuery/Databricks. Grain columns declared via a model-level PK were previously falsely flagged as missing not_null. Fix scans both `mm.constraints` (model-level, honouring the `columns:` list) and column-level `constraints: [{type: primary_key}]`. - MINOR #3 — contract-precedence bug. Earlier ternary short-circuited when `cfg.contract` was any object (e.g. `config: {contract: {alias: X}}` with no `enforced` key), masking a top-level `contract: {enforced: true}`. Now evaluated independently at both locations and OR'd. - MINOR #4 — `dbt.` prefix on namespaced test names (`dbt.not_null` in dbt 1.8+) is stripped in `testName()` before matching, so it counts as coverage. - MINOR #6 — `{name: <alias>, test_name: not_null}` alternative object form is recognised. Reading `Object.keys(t)[0]` returned `name` (the alias) rather than the underlying test type. Now `test_name` wins when present, falling back to the first key. - NIT #7 — `norm()` hoisted from per-model to function scope. Consensus items NOT addressed this round: - NIT #8 (dedup GrainKeyGap for a column listed twice / multiple grain tests) — collapses downstream via the global finding fingerprint; cosmetic rather than correctness. - NIT #9 (model-level `data_tests:` scanned as a grain-test source) — reviewer noted "harmless (no false match)"; no action. - NIT #10 (documentation of fallback-skip) — no code change needed. - MINOR #5 (contract resolved from dbt_project.yml or SQL config) — cross-file / cross-context, deferred. Regression tests (all pass; 82 pass / 0 fail in review-dbt-patterns suite, 3828 pass / 0 fail in full altimate suite — up from 3785): - Model-level primary_key constraint covers grain columns - Model-level not_null constraint with `columns:` list covers named cols - Column-level primary_key counts as coverage - Precision guard: PK missing cols still flagged - Model-level constraints on non-contracted model don't count - `config.contract` without `enforced` does NOT mask top-level `contract: {enforced: true}` (MINOR #3) - `dbt.not_null` covers (MINOR #4) - `{name:, test_name: not_null}` alternative form covers (MINOR #6) Co-Authored-By: Claude Opus 4.7 <[email protected]> Claude-Session: https://claude.ai/code/session_017zXDXMiNFh4qDPxPCfa2of
1 parent 7d74cec commit 5ad0e85

2 files changed

Lines changed: 290 additions & 22 deletions

File tree

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

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -957,13 +957,30 @@ function extractGrainKeyGaps(doc: unknown): GrainKeyGap[] {
957957
const d = doc as Record<string, unknown>
958958
if (!Array.isArray(d.models)) return gaps
959959

960-
// Small local helper: pull the string test-name from a YAML test entry.
961-
// Bare form: `- not_null`, block form: `- not_null: {config: {severity: warn}}`.
960+
// Normalise column names for coverage comparison. dbt YAML often uses
961+
// adapter-cased column names (Snowflake folds unquoted identifiers to
962+
// uppercase; other adapters differ). Lowercase both sides so `WORKSPACE_ID`
963+
// in `combination_of_columns` matches `workspace_id` in `columns:`. Hoisted
964+
// to function scope per consensus NIT #7 (was redeclared per-model).
965+
const norm = (s: string): string => s.toLowerCase()
966+
967+
// Pull the string test-name from a YAML test entry. Consensus fixes:
968+
// - MINOR #6: dbt's documented alternative object form is
969+
// `{name: <alias>, test_name: not_null, ...}`. `Object.keys(t)[0]`
970+
// would return `name`, missing the underlying test type. Prefer
971+
// `test_name` when present.
972+
// - MINOR #4: dbt 1.8+ allows namespaced names like `dbt.not_null`;
973+
// strip a leading `dbt.` prefix so `dbt.not_null` matches `not_null`.
974+
// Bare form: `- not_null`. Block form: `- not_null: {config: ...}`.
962975
const testName = (t: unknown): string | undefined => {
963-
if (typeof t === "string") return t.toLowerCase()
976+
if (typeof t === "string") return t.toLowerCase().replace(/^dbt\./, "")
964977
if (t && typeof t === "object") {
965-
const k = Object.keys(t as Record<string, unknown>)[0]
966-
return k ? k.toLowerCase() : undefined
978+
const obj = t as Record<string, unknown>
979+
// Alternative form: `{name: <alias>, test_name: <macro>, ...}`.
980+
// If both `name` and `test_name` are present, `test_name` wins.
981+
if (typeof obj.test_name === "string") return obj.test_name.toLowerCase().replace(/^dbt\./, "")
982+
const k = Object.keys(obj)[0]
983+
return k ? k.toLowerCase().replace(/^dbt\./, "") : undefined
967984
}
968985
return undefined
969986
}
@@ -975,23 +992,18 @@ function extractGrainKeyGaps(doc: unknown): GrainKeyGap[] {
975992
if (!mname) continue
976993

977994
// Contract enforcement: either at model-level `config.contract.enforced`
978-
// OR at top-level `contract:` (dbt supports both shapes).
995+
// OR at top-level `contract:` (dbt supports both shapes). Consensus
996+
// MINOR #3 — earlier ternary short-circuited on `cfg.contract` being
997+
// an object (e.g. `config: {contract: {alias: ...}}` with no
998+
// `enforced` key), masking a top-level `contract: {enforced: true}`.
999+
// Evaluate `enforced === true` at both locations independently and OR
1000+
// them so either declaration counts.
9791001
const cfg = mm.config && typeof mm.config === "object" ? (mm.config as Record<string, unknown>) : {}
980-
const contractCfg =
981-
cfg.contract && typeof cfg.contract === "object"
982-
? (cfg.contract as Record<string, unknown>)
983-
: mm.contract && typeof mm.contract === "object"
984-
? (mm.contract as Record<string, unknown>)
985-
: {}
986-
const contractEnforced = contractCfg.enforced === true
987-
988-
// Normalize column names for coverage comparison. dbt YAML often uses
989-
// adapter-cased column names (Snowflake folds unquoted identifiers to
990-
// uppercase; other adapters differ). Lowercase both sides so
991-
// `WORKSPACE_ID` in `combination_of_columns` matches `workspace_id`
992-
// in `columns:`. Original spelling is preserved for display in findings
993-
// (populated below from the grain-combo entry).
994-
const norm = (s: string): string => s.toLowerCase()
1002+
const cfgContract =
1003+
cfg.contract && typeof cfg.contract === "object" ? (cfg.contract as Record<string, unknown>) : undefined
1004+
const topContract =
1005+
mm.contract && typeof mm.contract === "object" ? (mm.contract as Record<string, unknown>) : undefined
1006+
const contractEnforced = cfgContract?.enforced === true || topContract?.enforced === true
9951007

9961008
// Coverage per column, split by mechanism (per codex R20 S1 review):
9971009
// - constraint coverage counts ONLY when `contract.enforced == true`.
@@ -1000,8 +1012,38 @@ function extractGrainKeyGaps(doc: unknown): GrainKeyGap[] {
10001012
// contract-enforced means we don't miss a real gap on views.
10011013
// - test coverage (`tests:` / `data_tests: [not_null]`) always counts,
10021014
// since dbt's test runner enforces it independent of contract state.
1015+
//
1016+
// Consensus MAJOR #1 — also count MODEL-LEVEL constraints:
1017+
// - `constraints: [{type: primary_key, columns: [a, b]}]` (dbt 1.5+)
1018+
// inherently enforces NOT NULL on every named column on
1019+
// Postgres/Snowflake/BigQuery/Databricks, so grain columns declared
1020+
// via a model-level PK are already covered.
1021+
// - `constraints: [{type: not_null, columns: [...]}]` (dbt's model-
1022+
// level form) — same coverage, just spelled out.
1023+
// - Column-level `constraints: [{type: primary_key}]` — same rationale
1024+
// at column granularity.
10031025
const coveredByConstraint = new Set<string>()
10041026
const coveredByTest = new Set<string>()
1027+
1028+
// Model-level constraints — dbt allows a `constraints:` list under
1029+
// the model itself (not per-column) that names one or more columns.
1030+
if (Array.isArray(mm.constraints)) {
1031+
for (const cn of mm.constraints) {
1032+
if (!cn || typeof cn !== "object") continue
1033+
const cnRec = cn as Record<string, unknown>
1034+
const type = typeof cnRec.type === "string" ? cnRec.type.toLowerCase() : ""
1035+
// `primary_key` implies NOT NULL on every listed column across the
1036+
// adapters dbt supports for enforced contracts; `not_null` at
1037+
// model level is the explicit multi-column variant of the column
1038+
// form.
1039+
if (type !== "not_null" && type !== "primary_key") continue
1040+
const cols = Array.isArray(cnRec.columns) ? (cnRec.columns as unknown[]) : []
1041+
for (const c of cols) {
1042+
if (typeof c === "string") coveredByConstraint.add(norm(c))
1043+
}
1044+
}
1045+
}
1046+
10051047
if (Array.isArray(mm.columns)) {
10061048
for (const col of mm.columns) {
10071049
if (!col || typeof col !== "object") continue
@@ -1013,7 +1055,10 @@ function extractGrainKeyGaps(doc: unknown): GrainKeyGap[] {
10131055
for (const cn of c.constraints) {
10141056
if (cn && typeof cn === "object") {
10151057
const type = (cn as Record<string, unknown>).type
1016-
if (typeof type === "string" && type.toLowerCase() === "not_null") {
1058+
const t = typeof type === "string" ? type.toLowerCase() : ""
1059+
// Column-level `not_null` OR `primary_key` — the latter
1060+
// inherently enforces NOT NULL (consensus MAJOR #1).
1061+
if (t === "not_null" || t === "primary_key") {
10171062
coveredByConstraint.add(key)
10181063
}
10191064
}

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

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,229 @@ models:
10501050
expect(f.filter((x) => (x.evidence?.result as any)?.rule === "grain_key_not_null_missing").length).toBe(0)
10511051
})
10521052

1053+
test("R20 S1: MODEL-level primary_key constraint covers every listed grain column (consensus MAJOR #1)", () => {
1054+
// Consensus MAJOR #1 — dbt 1.5+ supports model-level constraints via
1055+
// `constraints: [{type: primary_key, columns: [a, b]}]`. A primary key
1056+
// inherently enforces NOT NULL on Postgres/Snowflake/BigQuery/
1057+
// Databricks. Grain columns declared via a model-level PK constraint
1058+
// must not be flagged as missing not_null.
1059+
const newContent = `version: 2
1060+
models:
1061+
- name: mrt_x
1062+
config:
1063+
contract:
1064+
enforced: true
1065+
constraints:
1066+
- type: primary_key
1067+
columns: [metastore_id, sku_name, price_start_time]
1068+
columns:
1069+
- name: metastore_id
1070+
- name: sku_name
1071+
- name: price_start_time
1072+
- name: currency
1073+
data_tests:
1074+
- dbt_utils.unique_combination_of_columns:
1075+
arguments:
1076+
combination_of_columns: [metastore_id, sku_name, price_start_time]
1077+
`
1078+
const f = detectSchemaYmlPatterns(
1079+
{ path: "models/marts/mrt_x.yml", status: "added", diff: undefined },
1080+
DEFAULT_RUBRIC,
1081+
{ oldContent: undefined, newContent },
1082+
)
1083+
expect(f.filter((x) => (x.evidence?.result as any)?.rule === "grain_key_not_null_missing").length).toBe(0)
1084+
})
1085+
1086+
test("R20 S1: MODEL-level not_null constraint with `columns:` list covers each named column", () => {
1087+
// Explicit multi-column form of the model-level constraint. Same
1088+
// coverage effect as the primary_key case.
1089+
const newContent = `version: 2
1090+
models:
1091+
- name: mrt_x
1092+
config:
1093+
contract:
1094+
enforced: true
1095+
constraints:
1096+
- type: not_null
1097+
columns: [a, b]
1098+
columns:
1099+
- name: a
1100+
- name: b
1101+
- name: c
1102+
data_tests:
1103+
- dbt_utils.unique_combination_of_columns:
1104+
arguments:
1105+
combination_of_columns: [a, b]
1106+
`
1107+
const f = detectSchemaYmlPatterns(
1108+
{ path: "models/marts/mrt_x.yml", status: "added", diff: undefined },
1109+
DEFAULT_RUBRIC,
1110+
{ oldContent: undefined, newContent },
1111+
)
1112+
expect(f.filter((x) => (x.evidence?.result as any)?.rule === "grain_key_not_null_missing").length).toBe(0)
1113+
})
1114+
1115+
test("R20 S1: COLUMN-level primary_key constraint also counts as not_null coverage", () => {
1116+
const newContent = `version: 2
1117+
models:
1118+
- name: mrt_x
1119+
config:
1120+
contract:
1121+
enforced: true
1122+
columns:
1123+
- name: id
1124+
constraints:
1125+
- type: primary_key
1126+
data_tests:
1127+
- dbt_utils.unique_combination_of_columns:
1128+
arguments:
1129+
combination_of_columns: [id]
1130+
`
1131+
const f = detectSchemaYmlPatterns(
1132+
{ path: "models/marts/mrt_x.yml", status: "added", diff: undefined },
1133+
DEFAULT_RUBRIC,
1134+
{ oldContent: undefined, newContent },
1135+
)
1136+
expect(f.filter((x) => (x.evidence?.result as any)?.rule === "grain_key_not_null_missing").length).toBe(0)
1137+
})
1138+
1139+
test("R20 S1: model-level primary_key still misses columns NOT in its list", () => {
1140+
// Precision guard — a PK that names only some grain cols must still
1141+
// leave the OTHER grain cols flagged.
1142+
const newContent = `version: 2
1143+
models:
1144+
- name: mrt_x
1145+
config:
1146+
contract:
1147+
enforced: true
1148+
constraints:
1149+
- type: primary_key
1150+
columns: [a] # only covers a
1151+
columns:
1152+
- name: a
1153+
- name: b
1154+
data_tests:
1155+
- dbt_utils.unique_combination_of_columns:
1156+
arguments:
1157+
combination_of_columns: [a, b]
1158+
`
1159+
const f = detectSchemaYmlPatterns(
1160+
{ path: "models/marts/mrt_x.yml", status: "added", diff: undefined },
1161+
DEFAULT_RUBRIC,
1162+
{ oldContent: undefined, newContent },
1163+
)
1164+
const gaps = f.filter((x) => (x.evidence?.result as any)?.rule === "grain_key_not_null_missing")
1165+
expect(gaps.length).toBe(1)
1166+
expect(gaps[0].column).toBe("b")
1167+
})
1168+
1169+
test("R20 S1: model-level constraints on NON-CONTRACTED model do NOT count as coverage (documentation-only)", () => {
1170+
// Consistent with column-level rule — model-level constraints without
1171+
// contract enforcement are documentation, not enforcement, on most
1172+
// adapters.
1173+
const newContent = `version: 2
1174+
models:
1175+
- name: stg_x
1176+
constraints:
1177+
- type: primary_key
1178+
columns: [a, b]
1179+
columns:
1180+
- name: a
1181+
- name: b
1182+
data_tests:
1183+
- dbt_utils.unique_combination_of_columns:
1184+
combination_of_columns: [a, b]
1185+
`
1186+
const f = detectSchemaYmlPatterns(
1187+
{ path: "models/staging/stg_x.yml", status: "added", diff: undefined },
1188+
DEFAULT_RUBRIC,
1189+
{ oldContent: undefined, newContent },
1190+
)
1191+
const gaps = f.filter((x) => (x.evidence?.result as any)?.rule === "grain_key_not_null_missing")
1192+
// Both columns flagged (no test-level not_null, no contract).
1193+
expect(gaps.length).toBe(2)
1194+
})
1195+
1196+
test("R20 S1: `config.contract` object without `enforced` does NOT mask a top-level `contract: {enforced: true}` (MINOR #3)", () => {
1197+
// Consensus MINOR #3 — an earlier ternary short-circuited when
1198+
// `cfg.contract` was any object. `config: {contract: {alias: X}}`
1199+
// with no `enforced` key hid a top-level `contract: {enforced: true}`.
1200+
// Now both locations are OR'd; either declaration counts.
1201+
const newContent = `version: 2
1202+
models:
1203+
- name: mrt_x
1204+
config:
1205+
contract:
1206+
alias: SOMETHING_ELSE # no enforced key here
1207+
contract:
1208+
enforced: true # ← must still count as contracted
1209+
columns:
1210+
- name: id
1211+
constraints:
1212+
- type: not_null
1213+
- name: change_time # ← grain col, no not_null
1214+
data_tests:
1215+
- dbt_utils.unique_combination_of_columns:
1216+
combination_of_columns: [id, change_time]
1217+
`
1218+
const f = detectSchemaYmlPatterns(
1219+
{ path: "models/marts/mrt_x.yml", status: "added", diff: undefined },
1220+
DEFAULT_RUBRIC,
1221+
{ oldContent: undefined, newContent },
1222+
)
1223+
const gaps = f.filter((x) => (x.evidence?.result as any)?.rule === "grain_key_not_null_missing")
1224+
expect(gaps.length).toBe(1)
1225+
expect(gaps[0].column).toBe("change_time")
1226+
// Contract IS enforced — recommendation must point at constraints.
1227+
expect(gaps[0].body).toContain("constraints: [{type: not_null}]")
1228+
})
1229+
1230+
test("R20 S1: `dbt.not_null` namespaced test alias counts as coverage (MINOR #4)", () => {
1231+
// Consensus MINOR #4 — dbt 1.8+ allows namespaced test names. Column
1232+
// covered by `data_tests: [dbt.not_null]` must not be flagged.
1233+
const newContent = `version: 2
1234+
models:
1235+
- name: mrt_x
1236+
columns:
1237+
- name: id
1238+
data_tests: [dbt.not_null]
1239+
data_tests:
1240+
- unique_combination_of_columns:
1241+
combination_of_columns: [id]
1242+
`
1243+
const f = detectSchemaYmlPatterns(
1244+
{ path: "models/marts/mrt_x.yml", status: "added", diff: undefined },
1245+
DEFAULT_RUBRIC,
1246+
{ oldContent: undefined, newContent },
1247+
)
1248+
expect(f.filter((x) => (x.evidence?.result as any)?.rule === "grain_key_not_null_missing").length).toBe(0)
1249+
})
1250+
1251+
test("R20 S1: `{name:, test_name: not_null}` alternative test form counts as coverage (MINOR #6)", () => {
1252+
// Consensus MINOR #6 — dbt's documented alternative object form is
1253+
// `{name: my_test, test_name: not_null, ...}`. Reading only the first
1254+
// key returns `name` (an alias), missing the underlying test type. The
1255+
// `test_name` field is authoritative when present.
1256+
const newContent = `version: 2
1257+
models:
1258+
- name: mrt_x
1259+
columns:
1260+
- name: id
1261+
data_tests:
1262+
- name: id_never_null
1263+
test_name: not_null
1264+
data_tests:
1265+
- unique_combination_of_columns:
1266+
combination_of_columns: [id]
1267+
`
1268+
const f = detectSchemaYmlPatterns(
1269+
{ path: "models/marts/mrt_x.yml", status: "added", diff: undefined },
1270+
DEFAULT_RUBRIC,
1271+
{ oldContent: undefined, newContent },
1272+
)
1273+
expect(f.filter((x) => (x.evidence?.result as any)?.rule === "grain_key_not_null_missing").length).toBe(0)
1274+
})
1275+
10531276
test("R20 S1: dbt 1.9+ `arguments:` nesting is recognised (real corpus shape)", () => {
10541277
// The real internal corpus PRs use the dbt 1.9+ shape:
10551278
// `- dbt_utils.unique_combination_of_columns: {arguments: {combination_of_columns: [...]}}`

0 commit comments

Comments
 (0)