diff --git a/docs/docs/usage/dbt-pr-review.md b/docs/docs/usage/dbt-pr-review.md index 264fae95c5..aa5060dd9b 100644 --- a/docs/docs/usage/dbt-pr-review.md +++ b/docs/docs/usage/dbt-pr-review.md @@ -278,8 +278,10 @@ mode: comment # comment | gate severityThreshold: suggestion manifestPath: target/manifest.json dialect: snowflake -portability: false # cross-engine advice; default off once the engine is known - # (set true for packages that support several adapters) +portability: false # cross-engine advice; default off once the engine is known. + # A project whose macros have variants for several adapters + # (`postgres__x`, `snowflake__x`) still gets advice on constructs + # one of those adapters lacks. Set true to see all of it. reviewers: [] # empty = risk-tier defaults; or pin lanes dataDiff: # OFF by default — see "Data-diff in CI" below enabled: false @@ -357,8 +359,10 @@ consumerColumnLists: 5,000 characters. Critical findings come first; anything that does not fit is counted, and the full list is always in the verdict envelope (`altimate review --json`). Local CLI output is never truncated. -- **Repeated rules are grouped:** the same rule on three or more models is one - bullet listing the models. +- **Repeated rules are grouped:** the same rule several times in one file (one + finding per column of a grain test, say) is one finding listing each + instance; critical findings stay separate. The same rule on two or more + models is one summary bullet listing the models. - **Inline comments only on lines of the PR's diff.** Findings computed on compiled SQL, or on unchanged lines, stay in the summary. - **No re-posting on reruns:** each inline comment carries the finding's @@ -579,6 +583,9 @@ across 12 dialects all plan; anything still unrecognized stays *undecidable*, ne 3. **AI reviewer lane (layer 3).** When a model is configured, the LLM reviewer runs **after** the deterministic lanes, grounded in their findings, and adds advisory contextual comments (intent, naming, business logic, test coverage). + It sees every changed dbt file, not only models: macros, singular data tests, + snapshots, schema/source yml, seeds (diff only) and project config, each + labeled with its role, with models first under the 20-file cap. Findings are clamped to ≤ warning and never block. Skipped (clean degrade) if no model is available. 4. **Rubric → verdict.** Findings map to a verdict by a versioned rubric (data, diff --git a/packages/opencode/src/altimate/review/ai-review.ts b/packages/opencode/src/altimate/review/ai-review.ts index bd59dd54ed..f59009e458 100644 --- a/packages/opencode/src/altimate/review/ai-review.ts +++ b/packages/opencode/src/altimate/review/ai-review.ts @@ -19,13 +19,45 @@ const MAX_FILES = 20 export interface AiReviewFile { path: string status: string + /** Model name for a model; the file's base name for any other dbt file. */ model: string + /** dbt role of the file (`model_sql`, `macro`, `schema_yml`, `test`, …); omitted means a model. */ + kind?: string /** Unified diff (added/removed lines) for this file. */ diff?: string - /** dbt-compiled (preferred) or raw SQL for context. */ + /** Current content: dbt-compiled (preferred) or raw SQL for a model, the file itself otherwise. */ sql?: string } +/** Order under the file cap: models first, then the files whose defects reach the most models. */ +const KIND_ORDER = ["model_sql", "python_model", "macro", "test", "snapshot", "schema_yml", "source_yml", "seed", "project_config"] +const KIND_LABEL: Record = { + macro: "macro (Jinja; changes every model that calls it)", + test: "singular data test", + snapshot: "snapshot", + schema_yml: "schema yml (tests, contracts, descriptions)", + source_yml: "sources yml", + seed: "seed CSV", + project_config: "project config", + python_model: "Python model", +} +const FENCE: Record = { schema_yml: "yaml", source_yml: "yaml", project_config: "yaml", seed: "csv", python_model: "python" } + +function kindRank(kind: string | undefined): number { + const i = KIND_ORDER.indexOf(kind ?? "model_sql") + return i < 0 ? KIND_ORDER.length : i +} + +/** Files the lane sends, in priority order, capped at MAX_FILES. */ +export function selectAiFiles(files: AiReviewFile[]): AiReviewFile[] { + return files + .filter((f) => f.status !== "deleted" && (f.diff || f.sql)) + .map((f, i) => ({ f, i })) + .sort((a, b) => kindRank(a.f.kind) - kindRank(b.f.kind) || a.i - b.i) + .map(({ f }) => f) + .slice(0, MAX_FILES) +} + export interface AiReviewInput { files: AiReviewFile[] /** Deterministic engine findings — grounding the AI must NOT duplicate. */ @@ -81,7 +113,7 @@ function truncate(s: string | undefined, n: number): string { } /** Assemble the user message (mechanical formatting — not IP). */ -function buildUserMessage(input: AiReviewInput): string { +export function buildUserMessage(input: AiReviewInput): string { const parts: string[] = [] if (input.prTitle) parts.push(`PR title: ${truncate(input.prTitle, 200)}`) if (input.prBody) parts.push(`PR description:\n${truncate(input.prBody, 1500)}`) @@ -97,9 +129,13 @@ function buildUserMessage(input: AiReviewInput): string { parts.push("\n## CHANGED FILES (review for what the engine missed):") for (const f of input.files.slice(0, MAX_FILES)) { - parts.push(`\n### ${f.path} (${f.status})`) + const label = f.kind && KIND_LABEL[f.kind] + parts.push(`\n### ${f.path} (${f.status}${label ? `, ${label}` : ""})`) if (f.diff) parts.push("Diff:\n```diff\n" + truncate(f.diff, MAX_DIFF_CHARS) + "\n```") - if (f.sql) parts.push("Current SQL:\n```sql\n" + truncate(f.sql, MAX_DIFF_CHARS) + "\n```") + if (f.sql) { + const isModel = !f.kind || f.kind === "model_sql" + parts.push(`${isModel ? "Current SQL" : "Current file"}:\n\`\`\`${FENCE[f.kind ?? ""] ?? "sql"}\n` + truncate(f.sql, MAX_DIFF_CHARS) + "\n```") + } } parts.push("\nReturn ONLY the JSON array per the output contract.") return parts.join("\n") @@ -118,8 +154,8 @@ export async function runAiReview(input: AiReviewInput): Promise findings, status: { ...status, durationMs: Date.now() - startedAt }, }) - const files = input.files.filter((f) => f.status !== "deleted" && (f.diff || f.sql)) - if (!files.length) return done([], { state: "skipped", reason: "no changed model SQL to review" }) + const files = selectAiFiles(input.files) + if (!files.length) return done([], { state: "skipped", reason: "no changed dbt files to review" }) const message = buildUserMessage({ ...input, files }) const controller = new AbortController() diff --git a/packages/opencode/src/altimate/review/dbt-patterns.ts b/packages/opencode/src/altimate/review/dbt-patterns.ts index f96eca2649..ca5ad46dae 100644 --- a/packages/opencode/src/altimate/review/dbt-patterns.ts +++ b/packages/opencode/src/altimate/review/dbt-patterns.ts @@ -140,24 +140,10 @@ const detectClock: Detector = (c) => { }) } -// 4. SELECT * added (warehouse cost + fragility on columnar warehouses). -const detectSelectStar: Detector = (c) => { - const hit = c.added.find((l) => /^\s*select\s+\*/i.test(stripComments(l)) && !/select\s+\*\s+from\s+\{\{/i.test(l)) - if (!hit) return null - return makeFinding({ - severity: "suggestion", - category: "warehouse_cost", - title: `${c.model}: SELECT * scans all columns`, - body: - "`SELECT *` reads every column on a columnar warehouse (Snowflake/BigQuery) and makes the model fragile to " + - "upstream column adds. Select only the columns you need.", - file: c.file.path, - model: c.model, - confidence: "high", - evidence: { tool: "dbt-patterns", result: { rule: "select_star", line: hit.trim() } }, - ruleKey: "warehouse_cost:select-star", - }) -} +// 4. SELECT * — covered by the catalog's "SELECT * in final projection" rule, which +// fires only when the model's final SELECT reads straight from another relation. +// A `select *` from a CTE or an import CTE is the dbt style-guide idiom, and +// columnar engines prune the unused columns, so it is not flagged per line. // 5. LEFT/RIGHT JOIN silently collapsed to INNER by a WHERE/AND on the outer table. @@ -188,7 +174,8 @@ const detectFanout: Detector = (c) => { const newJoins = c.added.filter((l) => /\bjoin\b/i.test(stripComments(l)) && !/\bcross\s+join\b/i.test(l)) if (newJoins.length < 1) return null return makeFinding({ - severity: "warning", + // A "may": the join's cardinality is unknown here (1 partial match in 254 on replay). + severity: "suggestion", category: "fanout", title: `${c.model}: new join before an aggregate may fan out and inflate metrics`, body: @@ -308,11 +295,37 @@ const detectDedupLoadTimestamp: Detector = (c) => { return null } +const NOT_COLUMN = new Set([ + "year", "month", "day", "week", "quarter", "hour", "minute", "second", "epoch", "dow", "doy", "from", "as", "date", "timestamp", + "timestamp_ntz", "timestamp_tz", "datetime", "time", "boolean", "bool", "int", "integer", "bigint", "smallint", "numeric", "decimal", + "float", "double", "real", "varchar", "char", "string", "text", "current_date", "current_timestamp", "now", "null", "true", "false", + "interval", "with", "zone", "at", "precision", +]) + +/** True when a partition-defeating function in the line wraps a bare column (not a literal, var or clock). */ +function wrapsColumn(line: string): boolean { + const s = line.replace(/'(?:[^']|'')*'/g, "''").replace(/\{\{[\s\S]*?\}\}/g, " ") + for (const m of s.matchAll(/\b(extract|date_trunc|date|year|month|cast|trunc)\s*\(/gi)) { + let depth = 1 + let i = (m.index ?? 0) + m[0].length + const start = i + while (i < s.length && depth > 0) { + if (s[i] === "(") depth++ + else if (s[i] === ")") depth-- + i++ + } + const args = s.slice(start, i - 1) + const idents = [...args.matchAll(/(? x[1].toLowerCase()) + if (idents.some((x) => !NOT_COLUMN.has(x.split(".").pop()!))) return true + } + return false +} + // 11. Function/cast wrapped around a column in an added WHERE — defeats partition pruning. const detectPartitionFunction: Detector = (c) => { const hit = c.added.find((l) => { const s = stripComments(l) - return /^\s*(where|and)\b/i.test(s) && /\b(extract|date_trunc|date|year|month|cast|trunc)\s*\(/i.test(s) + return /^\s*(where|and)\b/i.test(s) && wrapsColumn(s) }) if (!hit) return null return makeFinding({ @@ -677,7 +690,26 @@ const detectSelfJoin: Detector = (c) => { // 39. Window function without PARTITION BY (whole-table window — often unintended). const detectWindowNoPartition: Detector = (c) => { - const hit = c.added.find((l) => /\bover\s*\(/i.test(stripComments(l)) && !/partition\s+by/i.test(l)) + // Read each OVER clause whole: `over (` on one line and `partition by` on the next is partitioned. + const changed = c.added.map((l) => stripComments(l).trim()).filter((l) => /\bover\s*\(/i.test(l)) + const whole = stripComments(c.newSql ?? "") + const sql = /\bover\s*\(/i.test(whole) ? whole : changed.join("\n") + const unpartitioned = [...sql.matchAll(/\bover\s*\(/gi)].some((m) => { + const open = m.index! + m[0].length - 1 + let depth = 0 + let end = sql.length + for (let i = open; i < sql.length; i++) { + if (sql[i] === "(") depth++ + else if (sql[i] === ")" && --depth === 0) { + end = i + break + } + } + const lineStart = sql.lastIndexOf("\n", m.index!) + 1 + const head = sql.slice(lineStart, open + 1).trim() + return changed.some((l) => l.includes(head)) && !/partition\s+by/i.test(sql.slice(open, end)) + }) + const hit = unpartitioned ? changed[0] : undefined if (!hit) return null return pattern( c, @@ -789,7 +821,6 @@ const detectMultiCountDistinct: Detector = (c) => { const MODEL_DETECTORS: Detector[] = [ detectClock, - detectSelectStar, detectCrossJoin, detectFanout, detectNotIn, @@ -824,6 +855,64 @@ const MODEL_DETECTORS: Detector[] = [ detectMultiCountDistinct, ] +/** + * A changed macro: the catalog rules marked `macros`, and `var('x')` read with no + * default when `x` isn't set in dbt_project.yml (in a package, every project that + * doesn't set it fails to compile). + */ +export const ADAPTERS = ["bigquery", "snowflake", "databricks", "spark", "redshift", "postgres", "trino", "athena", "duckdb", "exasol", "synapse", "sqlserver", "fabric", "oracle", "clickhouse", "presto"] + +/** The adapter a macro file is specific to, from its directory or its macros' `__` prefix. */ +function macroAdapter(p: string, sql: string): string | undefined { + const seg = p.toLowerCase().split("/").find((s) => ADAPTERS.includes(s)) + if (seg) return seg + const names = [...sql.matchAll(/\{%-?\s*macro\s+(\w+?)__\w+/g)].map((m) => m[1].toLowerCase()) + return names.length && names.every((n) => n === names[0]) && ADAPTERS.includes(names[0]) ? names[0] : undefined +} + +export function detectMacroPatterns( + file: ChangedFile, + newSql: string, + rubric: Rubric, + projectVars: Set, + catalogCtx?: CatalogContext, +): Finding[] { + if (fileKind(file) !== "macro" || file.status === "deleted") return [] + const { added, removed } = splitDiff(file.diff) + // An adapter's own variant (`macros/…/bigquery/x.sql`, `{% macro bigquery__x %}`) runs only on that + // adapter, so portability advice applies only where that adapter lacks the construct. + const adapter = macroAdapter(file.path, newSql) + const out = evaluateCatalog(file, newSql, added, removed, rubric, catalogCtx).filter((f) => { + if (!adapter || f.evidence?.tool !== "rule-catalog" || (f.evidence.result as { portability?: boolean })?.portability !== true) return true + const worksOn = (f.evidence.result as { worksOn?: string[] }).worksOn + return !!worksOn && !worksOn.includes(adapter) + }) + const model = modelNameFromPath(file.path) + const seen = new Set() + for (const line of added) { + for (const m of stripComments(line).matchAll(/\bvar\(\s*['"]([\w.]+)['"]\s*\)/g)) { + const name = m[1] + const esc = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + if (seen.has(name) || projectVars.has(name) || new RegExp(`var\\(\\s*['"]${esc}['"]\\s*,`).test(newSql)) continue + seen.add(name) + out.push( + makeFinding({ + severity: "suggestion", + category: "materialization", + title: `${model}: var('${name}') read with no default in a macro`, + body: `\`var('${name}')\` has no default and isn't set in dbt_project.yml, so any project that calls this macro without setting it fails to compile. Pass a default: \`var('${name}', )\`.`, + file: file.path, + model, + confidence: "high", + evidence: { tool: "dbt-patterns", result: { rule: "macro-var-no-default", var: name } }, + ruleKey: `materialization:macro-var-no-default`, + }), + ) + } + } + return out.filter((f) => !exclusionReason(f, rubric)) +} + /** Run the dbt anti-pattern detectors over a changed MODEL file. */ export function detectModelPatterns( file: ChangedFile, @@ -1430,9 +1519,30 @@ export function detectSchemaYmlPatterns( if (canCommit) { const oldSet = extractTestOccurrences(oldDoc ?? {}) const newSet = extractTestOccurrences(isDeletedFile ? {} : newDoc) + // A test that moved with a column rename (old column gone, a new column on + // the same model carries the same test) is not a removal. + const cols = (set: Set) => { + const m = new Map>() + for (const k of set) { + const [mod, col] = k.split("\x00") + if (!m.has(mod)) m.set(mod, new Set()) + m.get(mod)!.add(col) + } + return m + } + const oldCols = cols(oldSet) + const newCols = cols(newSet) + const movedTo = (model: string, column: string, testField: string) => + !!column && + !newCols.get(model)?.has(column) && + [...newSet].some((k) => { + const [mod, col, tf] = k.split("\x00") + return mod === model && tf === testField && !!col && !oldCols.get(model)?.has(col) + }) for (const key of oldSet) { if (newSet.has(key)) continue const [model, column, testField] = key.split("\x00") + if (movedTo(model, column, testField)) continue // `testField` may carry a `\x01\x02` discriminator for // `relationships` tests so multiple relationships on the same column // don't collapse to one removal (MINOR #4). Strip it for display; @@ -1626,14 +1736,15 @@ export function detectSchemaYmlPatterns( : `Add \`not_null\` to \`${g.column}\`'s \`data_tests:\` on \`${g.model}\` (contract is not enforced, so a \`constraints:\` entry would be inert — use a data_test).` findings.push( makeFinding({ - severity: clampSeverity("test_coverage", "warning", "high"), + severity: "suggestion", category: "test_coverage", title: `${filename}: grain column \`${g.column}\` in unique_combination_of_columns lacks \`not_null\` on \`${g.model}\``, body: `The \`unique_combination_of_columns\` test on \`${g.model}\` names \`${g.column}\` as a grain key, but no \`not_null\` ` + `coverage is declared for it (either as a \`constraints:\` entry on a contracted model, or as a \`data_tests: [not_null]\` ` + - `on a view). A NULL grain-key value silently passes the uniqueness test, so a fan-out or duplicate bug can ship without ` + - `any test catching it. ${recommendation}`, + `on a view). The uniqueness test still catches duplicates (rows with NULL keys group together), but nothing stops NULL ` + + `grain keys themselves, and as an incremental \`unique_key\` a NULL never matches on merge, so those rows are re-inserted every run. ` + + `${recommendation}`, file: file.path, model: g.model, column: g.column, diff --git a/packages/opencode/src/altimate/review/finding.ts b/packages/opencode/src/altimate/review/finding.ts index 41009731a7..a0f028ceff 100644 --- a/packages/opencode/src/altimate/review/finding.ts +++ b/packages/opencode/src/altimate/review/finding.ts @@ -203,3 +203,46 @@ export function dedupe(findings: Finding[]): Finding[] { } return [...byId.values()] } + +/** A finding's title without its leading `: `, with backticked + * names blanked, so the same rule on different models/columns shares a key. */ +export function ruleTitle(f: Pick): string { + return f.title + .replace(/^[\w.\-/]+: /, "") + .replace(/ \(\+\d+ more\)$/, "") + .replace(/`[^`]*`/g, "`…`") +} + +/** + * Merge repeats of one rule within one file (one finding per column of a grain + * test, one per join onto the same kind of key) into a single finding that + * lists every instance. Critical findings stay separate: each one can block. + */ +export function mergeRepeats(findings: Finding[]): Finding[] { + const groups = new Map() + const out: Finding[] = [] + for (const f of findings) { + if (f.severity === "critical") { + out.push(f) + continue + } + const key = [f.file, f.category, f.severity, !!f.degraded, ruleTitle(f)].join("|") + const g = groups.get(key) + if (g) g.push(f) + else groups.set(key, [f]) + } + for (const g of groups.values()) { + if (g.length === 1) { + out.push(g[0]) + continue + } + g.sort((a, b) => tieKey(a).localeCompare(tieKey(b))) + const [first, ...rest] = g + out.push({ + ...first, + title: `${first.title} (+${rest.length} more)`, + body: `${first.body}\n\nSame rule, also here:\n${rest.map((f) => `- ${f.title.replace(/^[\w.\-/]+: /, "")}`).join("\n")}`, + }) + } + return out +} diff --git a/packages/opencode/src/altimate/review/format.ts b/packages/opencode/src/altimate/review/format.ts index 0ae0c40acb..b17359e1f5 100644 --- a/packages/opencode/src/altimate/review/format.ts +++ b/packages/opencode/src/altimate/review/format.ts @@ -1,4 +1,4 @@ -import { type Finding, type Severity } from "./finding" +import { type Finding, type Severity, ruleTitle } from "./finding" import { type VerdictEnvelope } from "./verdict" /** @@ -39,7 +39,7 @@ export function verdictHeadline(env: VerdictEnvelope): string { /** Default size budget for the posted PR comment (characters). */ export const SUMMARY_BUDGET_CHARS = 5_000 /** The same rule on this many models or more collapses into one bullet. */ -export const GROUP_MIN = 3 +export const GROUP_MIN = 2 export interface RenderOptions { /** Character budget for the comment; findings beyond it are counted, not listed. @@ -153,13 +153,6 @@ export function renderSummary(env: VerdictEnvelope, opts: RenderOptions = {}): s return [...lines, ...tail].join("\n") } -/** A finding's title without its leading `model: `, with backticked names - * blanked, so the same rule on different models/columns shares a key. */ -function ruleTitle(f: Finding): string { - const t = f.model && f.title.startsWith(`${f.model}: `) ? f.title.slice(f.model.length + 2) : f.title - return t.replace(/`[^`]*`/g, "`…`") -} - /** Render findings of one severity as bullets; a rule repeated on GROUP_MIN or * more models collapses into a single bullet listing the models. */ function bulletsFor(items: Finding[]): Array<{ text: string; count: number }> { diff --git a/packages/opencode/src/altimate/review/git.ts b/packages/opencode/src/altimate/review/git.ts index 5dd6ee7f65..9a76083cb9 100644 --- a/packages/opencode/src/altimate/review/git.ts +++ b/packages/opencode/src/altimate/review/git.ts @@ -65,7 +65,10 @@ export async function collectChangedFiles(opts: CollectOptions): Promise `:(top)${p}`) let diff = "" try { diff = await git(["diff", "-M", ...range, "--", ...pathspec], opts.cwd) @@ -154,6 +157,27 @@ export async function gitRepoRoot(cwd: string): Promise { } } +/** Every tracked file at `ref` (default HEAD), repo-root relative; undefined when git can't list it. */ +export async function listFilesAt(cwd: string, ref?: string): Promise { + try { + const out = await git(["ls-tree", "-r", "--full-tree", "--name-only", ref || "HEAD"], cwd) + return out.split("\n").filter(Boolean) + } catch { + return undefined + } +} + +/** Matches of `pattern` (extended regex) in tracked files at `ref` under the repo-root-relative `pathspec`, as `ref:path:match` lines; undefined when git fails. */ +export async function grepMatchesAt(cwd: string, ref: string | undefined, pattern: string, pathspec: string): Promise { + try { + const out = await git(["grep", "--full-name", "-o", "-E", "-e", pattern, ref || "HEAD", "--", `:(top)${pathspec}`], cwd) + return out.split("\n").filter(Boolean) + } catch (e) { + // git grep exits 1 when nothing matches + return (e as { code?: number }).code === 1 ? [] : undefined + } +} + /** Where the review's base came from, recorded on the verdict envelope. */ export type BaseSource = | "flag" // --base diff --git a/packages/opencode/src/altimate/review/incremental-semantics.ts b/packages/opencode/src/altimate/review/incremental-semantics.ts index 826ff7ace2..ea95a4391b 100644 --- a/packages/opencode/src/altimate/review/incremental-semantics.ts +++ b/packages/opencode/src/altimate/review/incremental-semantics.ts @@ -140,10 +140,37 @@ export function bodyTokens(sql: string): string { return (modelBody(sql).match(/[a-z0-9_$]+|'(?:[^']|'')*'|[^\s\w]/gi) ?? []).join(" ") } -/** The `{% if is_incremental() %}` block(s): what each incremental run picks up. */ +const CLAUSE_END = /^(group\s+by|order\s+by|select|union|limit|qualify|window|from|join|left|right|inner|full|cross|\{%)\b/i + +/** + * The predicates inside a block: `where`/`having`/`on` clauses, or the whole + * block when it is a bare predicate fragment (`and updated_at > …`). Select + * lists and CTE bodies in the block are not filters. + */ +function predicates(block: string): string[] { + const s = modelBody(block) + if (/^(and|or|where|having)\b/.test(s)) return [s] + const out: string[] = [] + for (const m of s.matchAll(/\b(where|having)\b/g)) { + let depth = 0 + let i = (m.index ?? 0) + m[0].length + const start = i + for (; i < s.length; i++) { + if (s[i] === "(") depth++ + else if (s[i] === ")") { if (depth === 0) break; depth-- } + else if (depth === 0 && /\s/.test(s[i - 1] ?? " ") && CLAUSE_END.test(s.slice(i, i + 12))) break + } + out.push(m[1] + " " + s.slice(start, i).trim()) + } + return out +} + +/** The predicates of the `{% if is_incremental() %}` branch(es): what each incremental run picks up. */ export function incrementalFilter(sql: string): string { - const blocks = [...sql.matchAll(/\{%-?\s*if\s+is_incremental\(\)\s*-?%\}([\s\S]*?)\{%-?\s*endif\s*-?%\}/gi)].map((m) => m[1]) - return blocks.map((b) => bodyTokens(b)).join(" | ") + const blocks = [...sql.matchAll(/\{%-?\s*if\s+is_incremental\(\)[^%]*-?%\}([\s\S]*?)\{%-?\s*endif\s*-?%\}/gi)].map( + (m) => m[1].split(/\{%-?\s*(?:else|elif)\b/i)[0], + ) + return blocks.flatMap((b) => predicates(b).map((p) => bodyTokens(p))).join(" | ") } /** diff --git a/packages/opencode/src/altimate/review/orchestrate.ts b/packages/opencode/src/altimate/review/orchestrate.ts index a62a08dade..ed7bada706 100644 --- a/packages/opencode/src/altimate/review/orchestrate.ts +++ b/packages/opencode/src/altimate/review/orchestrate.ts @@ -1,4 +1,5 @@ import path from "node:path" +import YAML from "yaml" import { type Finding, type ReviewCategory, @@ -6,6 +7,7 @@ import { ReviewCategory as ReviewCategoryEnum, makeFinding, dedupe, + mergeRepeats, SEVERITY_ORDER, } from "./finding" import { type ChangedFile, type DbtProjectLayout, filterChangedFiles, hasOldSide } from "./diff-filter" @@ -13,13 +15,16 @@ import { classifyPR, compilePathTokenResolver, TIER_LANES } from "./risk-tier" import { type Rubric, exclusionReason, clampSeverity } from "./rubric" import { type ReviewConfig } from "./config" import { type ReviewMode, type ReviewScope, type VerdictEnvelope, buildEnvelope, signEnvelope } from "./verdict" -import { detectModelPatterns, detectSchemaYmlPatterns, splitDiff } from "./dbt-patterns" +import { detectMacroPatterns, detectModelPatterns, detectSchemaYmlPatterns, splitDiff } from "./dbt-patterns" import { listMirrorsModel, type ConsumerColumnList } from "./consumer-lists" import { detectUnitTestFixtures } from "./unit-tests" import { selectListNames } from "./select-list" import { detectTestAdequacy, detectThresholdLoosening } from "./test-adequacy" import { incrementalFindings, snapshotFindings, snapshotYmlFindings } from "./incremental-semantics" -import { type AiLaneStatus, type AiReviewInput, type AiReviewResult } from "./ai-review" +import { dbtProjectFindings, jinjaFindings, joinTargets, missingRefFindings, modelConfigFindings, packagesFindings, testsOnNullColumns } from "./project-rules" +import { type AiLaneStatus, type AiReviewFile, type AiReviewInput, type AiReviewResult } from "./ai-review" + +const AI_SUPPORTING_KINDS = new Set(["macro", "test", "snapshot", "schema_yml", "source_yml", "seed", "project_config"]) /** * The deterministic review recipe. @@ -149,6 +154,8 @@ export interface ReviewRunner { > /** A model's or snapshot's merged dbt `config` from the manifest (project + yml + inline). */ modelConfig?(name: string): Promise | undefined> + /** Column sets the model's `unique` / `unique_combination_of_columns` tests declare unique. */ + declaredUniqueKeys?(model: string): Promise /** Column-lineage edges for a query: source `"table"."col"` → target output column. */ columnLineage?(sql: string, dialect: string): Promise> /** Classify a set of column names as PII (Email/SSN/…) with confidence + masking. */ @@ -226,10 +233,14 @@ export interface OrchestrateInput { staleManifest?: boolean /** The dbt project's directories from dbt_project.yml (custom model-paths). */ layout?: DbtProjectLayout + /** Names `ref()` can resolve to at head (repo resources + installed packages); undefined = unknown. */ + resourceNames?: () => Promise | undefined> /** What the review covered: base resolution and files left out of scope. */ scope?: Omit /** Drop the regex catalog's cross-engine portability findings (single known engine). */ suppressPortability?: boolean + /** Other adapters the project supports (from its adapter-specific macros); portability advice is kept when any of them lacks the construct. */ + portabilityTargets?: string[] /** The dialect came from config or the manifest, not the default guess. */ dialectKnown?: boolean /** Opt-in explicit column lists outside dbt (see consumer-lists.ts). */ @@ -245,13 +256,13 @@ export function isCatalogPortability(f: Finding): boolean { /** * Portability advice that is noise for a project on `dialect`: dropped unless - * the rule names the engines that support the construct and `dialect` isn't - * one of them — then it is an error on this project's own engine. + * the rule names the engines that support the construct and `dialect`, or one + * of the project's other supported adapters (`targets`), isn't among them. */ -export function isSuppressiblePortability(f: Finding, dialect: string): boolean { +export function isSuppressiblePortability(f: Finding, dialect: string, targets: string[] = []): boolean { if (!isCatalogPortability(f)) return false const worksOn = (f.evidence?.result as { worksOn?: string[] })?.worksOn - return !worksOn || worksOn.includes(dialect.toLowerCase()) + return !worksOn || [dialect.toLowerCase(), ...targets].every((d) => worksOn.includes(d)) } /** Line numbers on the head side of a unified diff that GitHub accepts for an @@ -326,21 +337,6 @@ function mapCheckCategory(raw?: unknown): ReviewCategory { return "sql_quality" } -/** Map an impact severity bucket to a finding severity. */ -function impactToSeverity(impact: ImpactResult): Severity | null { - switch (impact.severity) { - case "BREAKING": - case "HIGH": - return "critical" - case "MEDIUM": - return "warning" - case "LOW": - return "suggestion" - default: - return null // SAFE / UNKNOWN → no standalone finding - } -} - function lineageBreakageLane(file: ChangedFile & { kind: string }, impact: ImpactResult, rubric: Rubric): Finding[] { const model = modelNameFromPath(file.path) const total = impact.directCount + impact.transitiveCount @@ -379,33 +375,10 @@ function lineageBreakageLane(file: ChangedFile & { kind: string }, impact: Impac ] } - const sev = impactToSeverity(impact) - if (!sev) return [] - // For a MODIFIED model, blast radius alone is NOT a breaking change — a wide - // fan-out doesn't mean the edit broke anything (it may be additive/safe). Until - // a real column-drop classifier exists, cap modified-model lineage impact at - // `warning` so merely touching a popular model never blocks. Only delete/rename - // (handled above) is treated as a genuine break. - const capped: Severity = sev === "critical" ? "warning" : sev - const clamped = clampSeverity("lineage_breakage", capped, degraded ? "unknown" : "high") - return [ - makeFinding({ - severity: clamped, - category: "lineage_breakage", - title: `${model}: high downstream fan-out (${total} model${total !== 1 ? "s" : ""})`, - body: - `\`${model}\` has ${impact.directCount} direct and ${impact.transitiveCount} transitive` + - ` downstream models (+${impact.testCount} tests). Blast radius is informational — verify the` + - ` change is backward-compatible (no removed/renamed columns) for these consumers.` + - (degraded ? "\n\n_Lint-only: no manifest, blast radius unverified._" : ""), - file: file.path, - model, - confidence: degraded ? "unknown" : "high", - degraded, - evidence: { tool: "impact_analysis", result: impact }, - ruleKey: "lineage_breakage:impact", - }), - ] + // A modified model's blast radius is not a defect: it feeds risk tiering + // (tier reasons name the downstream count), and concrete breakage on a + // modified model comes from the column-removal lanes. + return [] } async function semanticChangeLane( @@ -428,10 +401,11 @@ async function semanticChangeLane( if (eq.decided && eq.equivalent) return [] if (!eq.decided) { - // Undecidable / no schema → WARNING with unknown confidence, never block. + // Undecidable / no schema → a suggestion to data-diff: the engine not knowing is + // not evidence of a change (1 of 426 such findings matched a real defect on replay). return [ makeFinding({ - severity: clampSeverity("semantic_change", "critical", "unknown"), + severity: "suggestion", category: "semantic_change", title: `${model}: refactor could not be proven equivalent`, body: @@ -507,8 +481,17 @@ async function qualityLane( // (and false-fire on idiomatic dbt SQL like a correlated NOT EXISTS): if (rule === "correlated-subquery" || rule === "correlated_subquery") continue if (rule === "function-on-filter-column" || rule === "function-on-join-column") continue + // Injection rules target queries assembled from user input; a model's UNION or + // comment is ordinary SQL. + if (INJECTION_RULE.test(rule)) continue + // The engine's planner and parser cover less than the warehouses do: a type it can't + // coerce, a function it doesn't know, or syntax it can't parse is a gap in the engine, + // not an error in the model. Unresolved columns and tables are kept. + const msg = String(issue.message ?? "") + if (rule === "validate" && ENGINE_LIMIT_MESSAGE.test(msg)) continue + const parserGap = rule === "validate" && /^syntax error/i.test(msg) const cat = mapCheckCategory(issue.category) - const isError = issue.severity === "error" + const isError = issue.severity === "error" && !parserGap // Error-severity contract/PII issues are blockable; others are warning/suggestion. let sev: Severity = isError ? "warning" : "suggestion" if (isError && (cat === "contract_violation" || cat === "pii_exposure")) sev = "critical" @@ -538,6 +521,9 @@ async function qualityLane( * compiled core's `review_lexical_scan` over the raw ADDED diff lines (these * are pre-parse lexical checks; the curated lists live in the binary). Advisory. */ +const INJECTION_RULE = /injection|^unbalanced_quote$/ +const ENGINE_LIMIT_MESSAGE = /^(error during planning|execution error|this feature is not implemented)/i + async function lexicalLane(file: ChangedFile & { kind: string }, runner: ReviewRunner): Promise { if (!runner.lexicalScan || file.status === "deleted") return [] const added = splitDiff(file.diff).added @@ -686,6 +672,10 @@ function rawSelectList(sql: string): Set { return new Set(names) } +const FINAL_LAYER = /(^|\/)(marts?|reporting|dimensional|presentation|extracts|exports)\//i +// Intermediate/staging models nested under a final-layer folder are not read directly. +const NOT_FINAL = /(^|\/)(intermediate|staging|base)\/|(^|\/)(int|stg|base)_[^/]*$/i + const ticks = (xs: string[]) => xs.map((x) => `\`${x}\``).join(", ") /** @@ -704,11 +694,33 @@ async function consumerContractLane( if (!runner.modelConsumers || !runner.columnLineage) return [] const model = modelNameFromPath(ctx.file.path) const consumers = await runner.modelConsumers(model) - if (!consumers || (!consumers.exposures.length && !consumers.contractEnforced)) return [] + if (!consumers) return [] + const declared = consumers.exposures.length > 0 || consumers.contractEnforced + // A final-layer model is read outside dbt (BI, exports) even when nothing is declared. + const finalLayer = FINAL_LAYER.test(ctx.file.path) && !NOT_FINAL.test(ctx.file.path) + if (!declared && !(finalLayer && hasOldSide(ctx.file))) return [] const { added, removed } = await columnDelta(ctx, runner, dialect) const out: Finding[] = [] - if (removed.length) { + if (removed.length && !declared) { + out.push( + makeFinding({ + severity: "suggestion", + category: "lineage_breakage", + title: `${model}: removed column(s) on a final-layer model`, + body: + `This change removes ${ticks(removed)} from \`${model}\`, a final-layer model that dashboards and exports usually read directly; ` + + `those readers aren't in the DAG. Keep the old column alongside the new one for a deprecation period, or declare the consumers as exposures.`, + file: ctx.file.path, + model, + confidence: "medium", + evidence: { tool: "dbt.manifest", result: { removed, finalLayer: true } }, + ruleKey: "lineage_breakage:final-layer-column-removed", + }), + ) + } + + if (removed.length && declared) { const who: string[] = [] if (consumers.exposures.length) who.push(`exposure(s) ${consumers.exposures.map((e) => `\`${e.name}\`${e.type ? ` (${e.type})` : ""}`).join(", ")}`) @@ -1078,14 +1090,81 @@ async function missingGrainTestLane(ctx: ModelContext, runner: ReviewRunner): Pr * var-without-default, and materialization change (vs base). This replaces the regex * dbt-patterns config detectors — the parsing now lives in core. */ -async function dbtConfigLane(ctx: ModelContext, runner: ReviewRunner): Promise { +/** Var names defined under `vars:` in dbt_project.yml (top level and project-scoped). */ +export function definedVars(projectYml?: string): Set { + const out = new Set() + if (!projectYml) return out + let doc: any + try { + doc = YAML.parse(projectYml) + } catch { + return out + } + const vars = doc?.vars + if (!vars || typeof vars !== "object") return out + for (const [k, v] of Object.entries(vars)) { + out.add(k) + if (v && typeof v === "object" && !Array.isArray(v)) for (const k2 of Object.keys(v)) out.add(k2) + } + return out +} + +/** + * `{% set cfg = {"materialized": "incremental", ...} %}` + `config(**cfg)` is a + * common way to share config; inline a flat literal dict's entries as kwargs so + * the config lint sees them. + */ +export function inlineConfigSplats(sql: string): string { + const dicts = new Map() + for (const m of sql.matchAll(/\{%-?\s*set\s+(\w+)\s*=\s*\{/g)) { + // The dict body up to its matching brace; nested dicts (a BigQuery partition_by) stay as written. + const open = m.index! + m[0].length - 1 + let depth = 0 + let end = -1 + for (let i = open; i < sql.length; i++) { + if (sql[i] === "{") depth++ + else if (sql[i] === "}" && --depth === 0) { + end = i + break + } + } + if (end < 0 || !/^\s*-?%\}/.test(sql.slice(end + 1))) continue + const body = sql.slice(open + 1, end) + // Only top-level `"key":` becomes `key=`. + let out = "" + let d = 0 + for (let i = 0; i < body.length; i++) { + const c = body[i] + if ("{[(".includes(c)) d++ + else if ("}])".includes(c)) d-- + const key = d === 0 ? /^["'](\w+)["']\s*:/.exec(body.slice(i)) : null + if (key) { + out += key[1] + "=" + i += key[0].length - 1 + } else out += c + } + dicts.set(m[1], out.replace(/,\s*$/, "")) + } + if (!dicts.size) return sql + return sql.replace(/\bconfig\s*\(([\s\S]*?)\)\s*\}\}/g, (whole) => + whole.replace(/\*\*(\w+)/g, (splat, name) => dicts.get(name) ?? splat), + ) +} + +async function dbtConfigLane(ctx: ModelContext, runner: ReviewRunner, projectVars: Set = new Set()): Promise { if (!runner.dbtConfigLint) return [] const { file, newSql, oldSql } = ctx if (!newSql || file.status === "deleted") return [] const model = modelNameFromPath(file.path) - const raw = await runner.dbtConfigLint(newSql, hasOldSide(file) ? oldSql : undefined) + const raw = await runner.dbtConfigLint(inlineConfigSplats(newSql), hasOldSide(file) && oldSql ? inlineConfigSplats(oldSql) : undefined) const out: Finding[] = [] for (const f of raw) { + // A var defined in dbt_project.yml, or given a default elsewhere in the model, compiles fine. + if (f.rule === "var_no_default") { + const name = /var\(\s*['"]([^'"]+)['"]/.exec(f.message)?.[1] + const esc = name?.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + if (name && (projectVars.has(name) || new RegExp(`var\\(\\s*['"]${esc}['"]\\s*,`).test(newSql))) continue + } const cat: ReviewCategory = /var_no_default|microbatch|incremental|materialization/.test(f.rule) ? "materialization" : /contract/.test(f.rule) @@ -1112,6 +1191,92 @@ async function dbtConfigLane(ctx: ModelContext, runner: ReviewRunner): Promise { + if (!runner.declaredUniqueKeys || !runner.modelConfig || !ctx.newSql || ctx.file.status === "deleted") return [] + const added = (ctx.file.diff ?? "").split("\n").filter((l) => l.startsWith("+") && !l.startsWith("+++")).map((l) => l.slice(1)) + const out: Finding[] = [] + const model = modelNameFromPath(ctx.file.path) + for (const t of joinTargets(ctx.newSql, added)) { + // Unknown to the manifest (a package model, a typo): nothing to compare against. + if (!t.left || !(await runner.modelConfig(t.model))) continue + const declared = (await runner.declaredUniqueKeys(t.model)).filter((k) => k.length) + if (!declared.length || declared.some((k) => k.every((c) => t.keys.includes(c.toLowerCase())))) continue + out.push( + makeFinding({ + severity: "suggestion", + category: "fanout", + title: `${model}: LEFT JOIN key on \`${t.model}\` isn't its declared unique key`, + body: + `\`${t.join}\` matches \`${t.model}\` on [${t.keys.join(", ")}], but its uniqueness tests cover ` + + `${declared.map((k) => `[${k.join(", ")}]`).join(", ")}, which the join doesn't fully constrain.` + + ` If a key repeats there, this join multiplies rows. Add a \`unique\` / \`unique_combination_of_columns\` test on the join key, or join on the declared key.`, + file: ctx.file.path, + model, + confidence: "medium", + evidence: { tool: "dbt.manifest", result: { joined: t.model, keys: t.keys, declared } }, + ruleKey: `fanout:join-key-untested:${t.model}`, + }), + ) + } + return out +} + +const TIME_PARTITION_COLUMN = /(^|_)(date|day|week|month|quarter|year|period|hour|ts|time|timestamp)(_|$)|_at$/i + +async function uniqueKeyMismatchLane(ctx: ModelContext, runner: ReviewRunner): Promise { + if (!runner.modelConfig || !runner.declaredUniqueKeys || ctx.file.status === "deleted") return [] + const model = modelNameFromPath(ctx.file.path) + const cfg = await runner.modelConfig(model) + if (cfg?.materialized !== "incremental" || !cfg.unique_key) return [] + const uk = (Array.isArray(cfg.unique_key) ? cfg.unique_key : String(cfg.unique_key).split(",")) + .map((c) => String(c).trim().toLowerCase()) + .filter(Boolean) + const declared = await runner.declaredUniqueKeys(model) + if (!uk.length || !declared.length) return [] + const same = (k: string[]) => k.length === uk.length && k.every((c) => uk.includes(c.toLowerCase())) + if (declared.some(same)) return [] + // A merge key that is a declared key plus date/partition columns (`block_month`, + // `effective_date`) is the common pruning pattern; those columns don't change which rows match. + const timeOnlyExtra = (k: string[]) => { + const lk = k.map((c) => c.toLowerCase()) + return lk.every((c) => uk.includes(c)) && uk.filter((c) => !lk.includes(c)).every((c) => TIME_PARTITION_COLUMN.test(c)) + } + if (declared.some(timeOnlyExtra)) return [] + // A wider merge key only duplicates rows if an extra column changes for an existing row; + // a narrower or different key can collapse distinct rows into one. + const wider = declared.some((k) => k.every((c) => uk.includes(c.toLowerCase()))) + const tests = declared.map((k) => `[${k.join(", ")}]`).join(", ") + return [ + makeFinding({ + severity: wider ? "suggestion" : "warning", + category: "materialization", + title: `${model}: incremental unique_key differs from the declared uniqueness test`, + body: + `\`${model}\` merges on \`unique_key\` [${uk.join(", ")}], but its uniqueness test${declared.length > 1 ? "s declare" : " declares"} ${tests}. ` + + `If the merge key is right the test fails as soon as two rows share the test key; if the test is right the merge collapses or duplicates rows. Make them the same column set.`, + file: ctx.file.path, + model, + confidence: "high", + evidence: { tool: "dbt.manifest", result: { unique_key: uk, declared } }, + ruleKey: "materialization:unique-key-test-mismatch", + }), + ] +} + /** * STRUCTURAL base-vs-head diff — the `*_change` SQL rules (DISTINCT/UNION flip, * GROUP BY grain shift, surrogate-key change, COALESCE removed, predicate @@ -1508,6 +1673,10 @@ export async function runReview(input: OrchestrateInput): Promise() const diffScopedPiiFindingFiles = new Set() const classifiedPiiColumnsByFile = new Map>() + // The head's dbt_project.yml, read once: inherited test thresholds and defined vars. + const projectYmlPath = `${input.layout?.root ? `${input.layout.root.replace(/\/$/, "")}/` : ""}dbt_project.yml` + const projectYml = await getContent?.(projectYmlPath, "new").catch(() => undefined) + const projectVars = definedVars(projectYml) for (const ctx of ctxByPath.values()) { const tasks: Promise[] = [] // Engine lanes consume COMPILED SQL (rendered by dbt) when available. @@ -1590,7 +1759,17 @@ export async function runReview(input: OrchestrateInput): Promise undefined) + .then((mc) => [ + ...jinjaFindings(ctx.file, ctx.oldSql, ctx.newSql), + ...modelConfigFindings(ctx.file, ctx.newSql && inlineConfigSplats(ctx.newSql), dialect, mc), + ]), + ) } // Base-vs-head structural diff is a `semantic_change` concern (DISTINCT/UNION/grain/ // surrogate-key/predicate changes) — gate on semantic_change, keeping dbt_patterns @@ -1644,6 +1823,60 @@ export async function runReview(input: OrchestrateInput): Promise f.kind === "macro" && f.status !== "deleted") + const sets = await Promise.all( + macros.map(async (file) => { + try { + const [newSql, oldSql] = await Promise.all([ + getContent?.(file.path, "new"), + file.status === "added" ? Promise.resolve(undefined) : getContent?.(file.oldPath ?? file.path, "old")?.catch(() => undefined), + ]) + if (!newSql) return [] + return [ + ...detectMacroPatterns(file, newSql, input.rubric, projectVars, { dialect, dialectKnown: !!input.dialectKnown }), + ...jinjaFindings(file, oldSql, newSql), + ] + } catch (err) { + failedFile(file, err) + return [] + } + }), + ) + for (const findings of sets) all.push(findings) + } + + // ref() to a name no model, seed or snapshot carries: dbt fails to parse the project. + if (lanes.has("dbt_patterns") && input.resourceNames) { + const models = [...ctxByPath.values()].filter((c) => c.file.status !== "deleted" && /\bref\s*\(/.test(c.file.diff ?? "")) + const names = models.length ? await input.resourceNames().catch(() => undefined) : undefined + if (names) for (const c of models) all.push(missingRefFindings(c.file, names)) + } + + // Singular tests and snapshots: Jinja structure. packages.yml / dbt_project.yml: pins and config. + if (lanes.has("dbt_patterns")) { + const others = reviewable.filter( + (f) => f.status !== "deleted" && ((f.kind === "test" || f.kind === "snapshot") || f.kind === "project_config"), + ) + const sets = await Promise.all( + others.map(async (file) => { + try { + const [newContent, oldContent] = await Promise.all([ + getContent?.(file.path, "new"), + file.status === "added" ? Promise.resolve(undefined) : getContent?.(file.oldPath ?? file.path, "old")?.catch(() => undefined), + ]) + if (file.kind === "project_config") return [...packagesFindings(file, newContent), ...dbtProjectFindings(file, oldContent, newContent)] + return jinjaFindings(file, oldContent, newContent) + } catch (err) { + failedFile(file, err) + return [] + } + }), + ) + for (const findings of sets) all.push(findings) + } + // schema.yml-level detectors (test removal) — run on changed YAML files // regardless of tier, since deleting a guardrail test is always worth flagging. // Pass old + new content when available so the detector diffs structural YAML @@ -1666,9 +1899,7 @@ export async function runReview(input: OrchestrateInput): Promise f.kind === "schema_yml") - // The head's dbt_project.yml (test thresholds inherited by new tests), read once. - const projectYmlPath = `${input.layout?.root ? `${input.layout.root.replace(/\/$/, "")}/` : ""}dbt_project.yml` - const projectYml = schemaFiles.length ? await getContent?.(projectYmlPath, "new").catch(() => undefined) : undefined + const sqlByModel = new Map([...ctxByPath.values()].filter((c) => c.newSql).map((c) => [modelNameFromPath(c.file.path).toLowerCase(), c.newSql!])) if (schemaFiles.length) { const schemaFindingSets = await Promise.all( schemaFiles.map(async (file) => { @@ -1679,6 +1910,7 @@ export async function runReview(input: OrchestrateInput): Promise ({ + const aiFiles: AiReviewFile[] = [...ctxByPath.values()].map((ctx) => ({ path: ctx.file.path, status: ctx.file.status, model: modelNameFromPath(ctx.file.path), + kind: ctx.file.kind, diff: ctx.file.diff, sql: ctx.engineNewSql ?? ctx.newSql, })) + // Macros, data tests, snapshots, yml and seeds carry defects no model diff shows. + // Seeds go as a diff only: the full CSV is data, not logic. + const supporting = reviewable.filter((f) => AI_SUPPORTING_KINDS.has(f.kind) && f.status !== "deleted") + const contents = await Promise.all( + supporting.map((f) => (f.kind === "seed" ? undefined : getContent?.(f.path, "new")?.catch(() => undefined))), + ) + supporting.forEach((f, i) => + aiFiles.push({ path: f.path, status: f.status, model: modelNameFromPath(f.path), kind: f.kind, diff: f.diff, sql: contents[i] }), + ) try { const res = await input.aiReview({ files: aiFiles, @@ -1842,8 +2084,9 @@ export async function runReview(input: OrchestrateInput): Promise !isSuppressiblePortability(f, dialect)) + if (input.suppressPortability) findings = findings.filter((f) => !isSuppressiblePortability(f, dialect, input.portabilityTargets)) findings = anchorToDiff(findings, input.changedFiles) + findings = mergeRepeats(findings) const minSev = SEVERITY_ORDER[input.config.severityThreshold] findings = findings.filter((f) => SEVERITY_ORDER[f.severity] >= minSev) // Sort by severity desc, then file. diff --git a/packages/opencode/src/altimate/review/project-rules.ts b/packages/opencode/src/altimate/review/project-rules.ts new file mode 100644 index 0000000000..3ee3d05b70 --- /dev/null +++ b/packages/opencode/src/altimate/review/project-rules.ts @@ -0,0 +1,574 @@ +// altimate_change - review rules for dbt files outside the SELECT: packages.yml, +// dbt_project.yml, Jinja structure in macros/models, and model config combinations. +import path from "path" +import YAML from "yaml" +import { type Finding, type ReviewCategory, type Severity, makeFinding } from "./finding" +import type { ChangedFile } from "./diff-filter" +import { incrementalFilter, inlineConfig } from "./incremental-semantics" +import { parenBody, topLevel } from "./rule-catalog" + +const nameOf = (p: string) => path.basename(p).replace(/\.(sql|py|ya?ml|csv)$/i, "") + +function finding( + file: ChangedFile, + rule: string, + severity: Severity, + category: ReviewCategory, + title: string, + body: string, +): Finding { + const model = nameOf(file.path) + return makeFinding({ + severity, + category, + title: `${model}: ${title}`, + body, + file: file.path, + model, + confidence: "high", + evidence: { tool: "dbt-project-rules", result: { rule } }, + ruleKey: `${category}:${rule}`, + }) +} + +const addedLines = (diff: string | undefined) => + (diff ?? "").split("\n").filter((l) => l.startsWith("+") && !l.startsWith("+++")).map((l) => l.slice(1)) + +function parseYaml(text: string | undefined): any { + if (!text) return undefined + try { + return YAML.parse(text) + } catch { + return undefined + } +} + +// ── packages.yml ──────────────────────────────────────────────────────────── + +/** A release tag (`v1.2.3`, `1.2.3`, `0.9.0-b1`) or a full commit sha: both reproducible. */ +const PINNED_REVISION = /^(v?\d+(\.\d+){1,2}([-.+][\w.]+)?|[0-9a-f]{40})$/i + +/** packages.yml / dependencies.yml entries that track a git branch instead of a release. */ +export function packagesFindings(file: ChangedFile, newContent: string | undefined): Finding[] { + if (!/(^|\/)(packages|dependencies)\.ya?ml$/i.test(file.path) || file.status === "deleted") return [] + const doc = parseYaml(newContent) + const pkgs: any[] = Array.isArray(doc?.packages) ? doc.packages : [] + const added = addedLines(file.diff).join("\n") + const branchPinned = pkgs.filter( + (p) => p && typeof p.git === "string" && (p.revision === undefined || !PINNED_REVISION.test(String(p.revision))), + ) + // Only entries this PR touched: a long-standing branch pin is not this change's decision. + const touched = branchPinned.filter((p) => added.includes(p.git) || (p.revision !== undefined && added.includes(String(p.revision)))) + if (!touched.length) return [] + const list = touched.map((p) => `\`${p.git}\` @ \`${p.revision ?? "default branch"}\``).join(", ") + return [ + finding( + file, + "package-git-branch", + "warning", + "materialization", + "package installed from a git branch", + `${list} tracks a branch, so \`dbt deps\` installs whatever the branch holds on the day it runs. Pin a release (\`package:\` + \`version:\` range) or a tag/commit sha before merging.`, + ), + ] +} + +// ── dbt_project.yml ───────────────────────────────────────────────────────── + +function semverParts(v: unknown): number[] | undefined { + const m = /^\s*v?(\d+)\.(\d+)(?:\.(\d+))?/.exec(String(v ?? "")) + return m ? [Number(m[1]), Number(m[2]), Number(m[3] ?? 0)] : undefined +} + +function lowerVersion(a: number[], b: number[]): boolean { + for (let i = 0; i < 3; i++) if (a[i] !== b[i]) return a[i] < b[i] + return false +} + +/** Every `grants:` mapping anywhere under the given node, with its path. */ +function grantsBlocks(node: any, at: string[] = [], out: { at: string; grants: Record }[] = []) { + if (!node || typeof node !== "object") return out + for (const [k, v] of Object.entries(node)) { + if ((k === "grants" || k === "+grants") && v && typeof v === "object" && !Array.isArray(v)) out.push({ at: [...at, k].join("."), grants: v as Record }) + else grantsBlocks(v, [...at, k], out) + } + return out +} + +export function dbtProjectFindings(file: ChangedFile, oldContent: string | undefined, newContent: string | undefined): Finding[] { + if (!/(^|\/)dbt_project\.ya?ml$/i.test(file.path) || file.status === "deleted") return [] + const now = parseYaml(newContent) + if (!now) return [] + const before = parseYaml(oldContent) + const out: Finding[] = [] + + const vNew = semverParts(now.version) + const vOld = semverParts(before?.version) + if (vNew && vOld && lowerVersion(vNew, vOld)) + out.push( + finding( + file, + "project-version-lowered", + "warning", + "materialization", + "project version goes down", + `\`version\` changes from \`${before.version}\` to \`${now.version}\`. A release must move the version up, or consumers resolving a version range get the older release.`, + ), + ) + + const oldDup = new Set(grantsBlocks(before).map((g) => g.at)) + for (const { at, grants } of grantsBlocks(now)) { + for (const [priv, who] of Object.entries(grants)) { + if (!Array.isArray(who)) continue + const seen = new Set() + const dups = who.map(String).filter((r) => (seen.has(r.toLowerCase()) ? true : (seen.add(r.toLowerCase()), false))) + if (dups.length && !(oldDup.has(at) && sameDup(before, at, priv, dups))) + out.push( + finding( + file, + "grants-duplicate", + "suggestion", + "sql_quality", + `duplicate grantee under \`${at}.${priv}\``, + `${[...new Set(dups)].map((d) => `\`${d}\``).join(", ")} is listed more than once. Usually a copy-paste where another role was meant; check the list is complete.`, + ), + ) + } + } + + const disabledNow = (newContent?.match(/^\s*\+?enabled\s*:\s*false\b/gim) ?? []).length + const disabledBefore = (oldContent?.match(/^\s*\+?enabled\s*:\s*false\b/gim) ?? []).length + if (disabledNow > disabledBefore && addedLines(file.diff).some((l) => /^\s*\+?enabled\s*:\s*false\b/i.test(l))) + out.push( + finding( + file, + "models-disabled", + "suggestion", + "lineage_breakage", + "resources disabled in dbt_project.yml", + "This change adds `enabled: false`. Disabled models drop out of the DAG: anything that refs them fails to compile and their tables stop refreshing. Say in the PR why, and whether downstream models are disabled too.", + ), + ) + return out +} + +function sameDup(before: any, at: string, priv: string, dups: string[]): boolean { + const blk = grantsBlocks(before).find((g) => g.at === at) + const who = blk?.grants[priv] + if (!Array.isArray(who)) return false + const lower = who.map((x) => String(x).toLowerCase()) + return dups.every((d) => lower.filter((x) => x === d.toLowerCase()).length > 1) +} + +// ── Jinja structure (macros, models, tests, snapshots) ────────────────────── + +const BLOCK_OPEN = ["if", "for", "macro", "call", "filter", "raw", "materialization", "test", "snapshot", "docs"] + +/** Open/close balance of Jinja blocks; returns a message for the first problem, or undefined. */ +export function jinjaBalance(sql: string): string | undefined { + const text = sql.replace(/\{%-?\s*raw\s*-?%\}[\s\S]*?\{%-?\s*endraw\s*-?%\}/g, " ").replace(/\{#[\s\S]*?#\}/g, " ") + const stack: string[] = [] + for (const m of text.matchAll(/\{%-?\s*(\w+)([\s\S]*?)-?%\}/g)) { + const tag = m[1].toLowerCase() + const rest = m[2] + if (tag === "set") { + // `{% set x %}…{% endset %}` is a block; `{% set x = … %}` is not. + if (!/=/.test(rest)) stack.push("set") + continue + } + if (BLOCK_OPEN.includes(tag)) { + if (tag === "macro" && !/^\s*[\w.]+\s*\(/.test(rest)) return `\`{% macro${rest.trimEnd()} %}\` has no parameter list; Jinja requires \`()\` even when empty` + stack.push(tag) + continue + } + if (tag.startsWith("end")) { + const want = tag.slice(3) + if (stack[stack.length - 1] === want) stack.pop() + else if (!stack.includes(want)) return `\`{% ${tag} %}\` closes no open \`{% ${want} %}\`` + else return `\`{% ${tag} %}\` closes \`{% ${want} %}\` while \`{% ${stack[stack.length - 1]} %}\` is still open` + } + } + if (stack.length) return `\`{% ${stack[stack.length - 1]} %}\` is never closed (missing \`{% end${stack[stack.length - 1]} %}\`)` + return undefined +} + +const macroSignatures = (sql: string | undefined) => { + const out = new Map() + for (const m of (sql ?? "").matchAll(/\{%-?\s*macro\s+([\w.]+)\s*\(([^)]*)\)/g)) + out.set(m[1], m[2].split(",").map((p) => p.split("=")[0].trim()).filter(Boolean)) + return out +} + +/** The comment text on a line (`-- …`, `/* … *\/`, `{# … #}`), or "". */ +const commentOf = (l: string) => /--(.*)$|\/\*(.*?)(\*\/|$)|\{#(.*?)(#\}|$)/.exec(l)?.slice(1).filter(Boolean).join(" ") ?? "" +const TEMP_MARKER = + /\b(temp(orary)?\b.{0,40}\b(ci|test|validation|sample|debug|hack)|revert\b.{0,30}\b(to|before|after)\b|do\s+not\s+merge|remove\s+before\s+(merge|release)|ci\s+test\b|for\s+testing\s+only)/i + +const varNames = (sql: string | undefined) => new Set([...(sql ?? "").matchAll(/\bvar\s*\(\s*['"]([^'"]+)['"]/g)].map((m) => m[1])) + +export function jinjaFindings(file: ChangedFile, oldContent: string | undefined, newContent: string | undefined): Finding[] { + if (!newContent || file.status === "deleted" || !/\.sql$/i.test(file.path)) return [] + const out: Finding[] = [] + + const problem = jinjaBalance(newContent) + if (problem && !(oldContent && jinjaBalance(oldContent))) + out.push( + finding( + file, + "jinja-unbalanced", + "warning", + "sql_correctness", + "Jinja block structure is broken", + `${problem}. dbt fails to compile this file and every model that uses it.`, + ), + ) + + // A parameter inserted before existing ones rebinds every positional call. + if (oldContent) { + const before = macroSignatures(oldContent) + for (const [name, params] of macroSignatures(newContent)) { + const old = before.get(name) + if (!old || old.length === 0) continue + const kept = old.filter((p) => params.includes(p)) + const shifted = kept.filter((p) => params.indexOf(p) !== old.indexOf(p)) + if (shifted.length) + out.push( + finding( + file, + "macro-param-shifted", + "warning", + "sql_correctness", + `\`${name}\` parameters moved position`, + `${shifted.map((p) => `\`${p}\``).join(", ")} ${shifted.length === 1 ? "is" : "are"} no longer at the same position in \`${name}(${params.join(", ")})\`. Callers that pass arguments positionally now bind them to different parameters. Add new parameters at the end, or check every caller uses keywords.`, + ), + ) + } + + // A var() key renamed with no fallback: projects that set the old key lose their override silently. + const oldVars = varNames(oldContent) + const newVars = varNames(newContent) + // A key still named as a string literal is usually still read (handed to a macro that reads it). + const gone = [...oldVars].filter((v) => !newVars.has(v) && !new RegExp(`['"]${v}['"]`).test(newContent)) + const fresh = [...newVars].filter((v) => !oldVars.has(v)) + if (gone.length && fresh.length) + out.push( + finding( + file, + "var-renamed-no-fallback", + "warning", + "semantic_change", + "var() key renamed without a fallback", + `This file no longer reads ${gone.map((v) => `\`${v}\``).join(", ")} and now reads ${fresh.map((v) => `\`${v}\``).join(", ")}. Projects that set the old key in \`dbt_project.yml\` keep running with the default and no error. Read the new key with the old one as its default, e.g. \`var('${fresh[0]}', var('${gone[0]}', ))\`.`, + ), + ) + } + + // Code marked as temporary in its own comment: a CI sample, a test date, a "revert before merge". + const marker = addedLines(file.diff).find((l) => TEMP_MARKER.test(commentOf(l))) + if (marker) + out.push( + finding( + file, + "temporary-code-marker", + "warning", + "sql_correctness", + "code marked temporary is part of the change", + `\`${marker.trim().slice(0, 160)}\` says it is temporary. If this merges, the temporary filter, date or sample ships to production. Revert it, or drop the comment if it is meant to stay.`, + ), + ) + + if (addedLines(file.diff).some((l) => /modules\.datetime\.datetime\.utcnow\s*\(/.test(l))) + out.push( + finding( + file, + "jinja-utcnow", + "suggestion", + "idempotency", + "`datetime.utcnow()` returns a naive timestamp", + "`utcnow()` is deprecated since Python 3.12 and returns a timestamp with no timezone, which compares wrongly against aware ones. Use `modules.datetime.datetime.now(modules.datetime.timezone.utc)`.", + ), + ) + return out +} + +// ── model config combinations ─────────────────────────────────────────────── + +const unquote = (v: string | undefined) => (v ?? "").trim().replace(/^['"]|['"]$/g, "") +const literals = (v: string | undefined) => [...(v ?? "").matchAll(/['"]([^'"]+)['"]/g)].map((m) => m[1].toLowerCase()) + +/** Partition column names from a `partition_by` config value (BigQuery dict, list, or string). */ +function partitionColumns(v: string | undefined): string[] { + if (!v) return [] + const field = /['"]field['"]\s*:\s*['"]([^'"]+)['"]/i.exec(v) + if (field) return [field[1].toLowerCase()] + return literals(v) +} + +/** Literals a config value can take: its own, or those of the `{% set name = … %}` it names. */ +function valueLiterals(v: string | undefined, sql: string): string[] { + const own = literals(v) + if (own.length || !v || !/^\s*\w+\s*$/.test(v)) return own + const set = new RegExp(`\\{%-?\\s*set\\s+${v.trim()}\\s*=([^%]*)%\\}`).exec(sql) + return set ? literals(set[1]) : [] +} + +export function modelConfigFindings( + file: ChangedFile, + newSql: string | undefined, + dialect: string, + /** The manifest's config for the model: fills what the file leaves to project defaults. */ + manifestConfig?: Record, +): Finding[] { + if (!newSql || file.status === "deleted") return [] + const cfg = inlineConfig(newSql) + const touched = addedLines(file.diff).join("\n") + // Only when the config or the incremental filter is part of this change. + if (!/config\s*\(|unique_key|partition_by|batch_size|materialized|incremental|is_incremental/i.test(touched)) return [] + const out: Finding[] = [] + const materialized = (unquote(cfg.materialized) || String(manifestConfig?.materialized ?? "")).toLowerCase() + const strategy = unquote(cfg.incremental_strategy).toLowerCase() + + if (cfg.unique_key && (materialized === "table" || materialized === "view")) + out.push( + finding( + file, + "unique-key-not-incremental", + "suggestion", + "materialization", + `\`unique_key\` has no effect on a ${materialized}`, + `\`unique_key\` only drives incremental merges; on a \`${materialized}\` it neither deduplicates nor checks anything. Add a \`unique\` / \`unique_combination_of_columns\` test to enforce it.`, + ), + ) + + // dbt-bigquery requires microbatch `batch_size` to equal the partition granularity. + // A `partition_by` dict with `granularity` is BigQuery's syntax, so it identifies the adapter too. + if (strategy === "microbatch" && (/bigquery/i.test(dialect) || /['"]granularity['"]\s*:/.test(cfg.partition_by ?? ""))) { + const sizes = valueLiterals(cfg.batch_size, newSql).filter((s) => ["hour", "day", "month", "year"].includes(s)) + const gran = /['"]granularity['"]\s*:\s*['"](\w+)['"]/i.exec(cfg.partition_by ?? "")?.[1]?.toLowerCase() ?? (cfg.partition_by ? "day" : undefined) + const off = gran ? sizes.filter((s) => s !== gran) : [] + if (off.length) + out.push( + finding( + file, + "microbatch-granularity-mismatch", + "warning", + "materialization", + "microbatch `batch_size` differs from the partition granularity", + `\`batch_size\` can be ${off.map((s) => `\`${s}\``).join(", ")} but \`partition_by\` granularity is \`${gran}\`. dbt-bigquery rejects a microbatch model whose batch size and partition granularity differ, so those runs fail before executing.`, + ), + ) + } + + if (materialized === "incremental") { + const parts = partitionColumns(cfg.partition_by) + const uk = literals(cfg.unique_key).length ? literals(cfg.unique_key) : cfg.unique_key ? [unquote(cfg.unique_key).toLowerCase()] : [] + if (parts.length && uk.length && strategy === "merge" && !/bigquery/i.test(dialect)) { + const missing = parts.filter((p) => !uk.includes(p)) + if (missing.length) + out.push( + finding( + file, + "partition-not-in-merge-key", + "suggestion", + "warehouse_cost", + "partition column is not in the merge `unique_key`", + `The table is partitioned by ${missing.map((p) => `\`${p}\``).join(", ")}, which \`unique_key\` [${uk.join(", ")}] doesn't include, so the merge can't prune target partitions and scans the whole table. Add the partition column to \`unique_key\` (or \`incremental_predicates\`).`, + ), + ) + } + // The incremental filter should constrain the partition column, or each run scans every partition. + const filter = incrementalFilter(newSql) + if (parts.length && filter && !parts.some((p) => new RegExp(`\\b${p}\\b`, "i").test(filter))) + out.push( + finding( + file, + "incremental-filter-not-partition", + "suggestion", + "warehouse_cost", + "incremental filter doesn't use the partition column", + `The \`is_incremental()\` filter never constrains ${parts.map((p) => `\`${p}\``).join(", ")}, the partition column, so each run reads every partition of the source/target. Filter on the partition column (alone or alongside the timestamp).`, + ), + ) + // Filtering on a creation timestamp misses updates to rows created earlier. + // Rows are expected to change when the model carries an update timestamp or merges on a key. + const rowsChange = /\b\w*(updated|modified|changed)(_at|_on|_date|_ts|_time)\b/i.test(newSql) || (uk.length > 0 && strategy !== "append") + if (filter && /\b\w*(created|inserted)(_at|_on|_date|_ts|_time)\b/i.test(filter) && !/\b\w*(updated|modified|changed)\w*\b/i.test(filter) && rowsChange) + out.push( + finding( + file, + "incremental-filter-created-only", + "suggestion", + "materialization", + "incremental filter reads new rows only, not updated ones", + "The `is_incremental()` filter keys on a creation timestamp while the model also carries an update timestamp. Rows created before the last run and changed since are never picked up again. Filter on the update timestamp, or explain why rows never change.", + ), + ) + } + return out +} + +// ── ref() targets ─────────────────────────────────────────────────────────── + +function editDistance(a: string, b: string): number { + const d = Array.from({ length: b.length + 1 }, (_, i) => i) + for (let i = 1; i <= a.length; i++) { + let prev = d[0] + d[0] = i + for (let j = 1; j <= b.length; j++) { + const tmp = d[j] + d[j] = Math.min(d[j] + 1, d[j - 1] + 1, prev + (a[i - 1] === b[j - 1] ? 0 : 1)) + prev = tmp + } + } + return d[b.length] +} + +/** Single-argument `ref('x')` on added lines whose target no resource carries. */ +export function missingRefFindings(file: ChangedFile, names: Set): Finding[] { + const refs = new Set() + for (const l of addedLines(file.diff)) + for (const m of l.matchAll(/\bref\s*\(\s*['"]([^'"]+)['"]\s*(?:,\s*v(?:ersion)?\s*=\s*[^)]*)?\)/g)) refs.add(m[1]) + const missing = [...refs].filter((r) => !names.has(r.toLowerCase()) && !names.has(r.toLowerCase().replace(/_v\d+$/, ""))) + return missing.map((r) => { + const near = [...names].filter((n) => Math.abs(n.length - r.length) <= 2 && editDistance(n, r.toLowerCase()) <= 2) + return finding( + file, + "ref-target-missing", + "warning", + "sql_correctness", + `\`ref('${r}')\` points at no model`, + `No model, seed or snapshot named \`${r}\` exists in this project or its installed packages, so dbt fails to parse the project.${near.length ? ` Did you mean ${near.slice(0, 3).map((n) => `\`${n}\``).join(", ")}?` : " Add the model in this PR or fix the name."}`, + ) + }) +} + +// ── join keys vs declared uniqueness ──────────────────────────────────────── + +export interface JoinTarget { + /** The model the joined relation reads (directly, or through a pass-through CTE). */ + model: string + /** Columns of the joined side compared with `=` in the ON clause. */ + keys: string[] + /** The JOIN text, for the finding. */ + join: string + /** LEFT [OUTER] JOIN: a lookup that should keep each left row once. */ + left: boolean +} + +const REF = /\{\{\s*ref\(\s*['"]([^'"]+)['"]\s*\)\s*\}\}/ + +/** CTEs that pass one ref through unchanged in grain (no GROUP BY / DISTINCT / JOIN / QUALIFY / UNION). */ +function passThroughCtes(sql: string): Map { + const out = new Map() + for (const m of sql.matchAll(/(?:\bwith\b|,)\s*(\w+)\s+as\s*\(/gi)) { + const body = parenBody(sql, m.index! + m[0].length - 1) + const top = topLevel(body) + const ref = REF.exec(body) + // `{{ ref(...) }}` sits in parens that topLevel blanks, so test grain-changing keywords on the top level. + if (ref && /\bfrom\b/i.test(top) && !/\b(group\s+by|distinct|join|qualify|union|limit)\b/i.test(top)) out.set(m[1].toLowerCase(), ref[1]) + } + return out +} + +const ON_STOP = /^(?:join|left|right|inner|full|cross|where|group|order|qualify|having|union|limit|window|select|\{%)\b/i + +/** Joins whose ON clause or JOIN line is on an added line, with the joined model and its key columns. */ +/** The top-level `and`-separated conditions of an ON clause. */ +function conjuncts(on: string): string[] { + const top = topLevel(on) + const out: string[] = [] + let from = 0 + for (const m of top.matchAll(/\band\b/gi)) { + out.push(on.slice(from, m.index)) + from = m.index! + m[0].length + } + out.push(on.slice(from)) + return out +} + +export function joinTargets(sql: string, added: string[]): JoinTarget[] { + const ctes = passThroughCtes(sql) + const changed = added.map((l) => l.replace(/--.*$/, "").trim()).filter((l) => /\w{2}/.test(l)) + const out: JoinTarget[] = [] + const re = /\b(left\s+(?:outer\s+)?)?join\s+(\{\{\s*ref\(\s*['"][^'"]+['"]\s*\)\s*\}\}|\w+)\s*(?:as\s+)?(\w+)?\s+on\b/gi + for (const m of sql.matchAll(re)) { + const rel = m[2] + const alias = (m[3] && !/^on$/i.test(m[3]) ? m[3] : (REF.exec(rel)?.[1] ?? rel)).toLowerCase() + const model = REF.exec(rel)?.[1] ?? ctes.get(rel.toLowerCase()) + if (!model) continue + // ON clause: up to the next clause keyword at paren depth 0. + let i = m.index! + m[0].length + let depth = 0 + const start = i + for (; i < sql.length; i++) { + const c = sql[i] + if (c === "(") depth++ + else if (c === ")") { + if (depth === 0) break + depth-- + } else if (depth === 0 && /\s/.test(sql[i - 1] ?? " ") && ON_STOP.test(sql.slice(i, i + 10))) break + } + const on = sql.slice(start, i) + const text = sql.slice(sql.lastIndexOf("\n", m.index!) + 1, i) + if (!changed.some((l) => text.includes(l))) continue + // Range conditions (`between`, `<`) and Jinja-generated conditions constrain the join + // in ways the equality keys don't show. + if (/\{\{|\bbetween\b|<|>|!=|\bor\b/i.test(on)) continue + const keys = new Set() + for (const conjunct of conjuncts(on)) { + const eq = conjunct.split("=") + if (eq.length !== 2) continue + for (const side of eq) { + // `alias.col`, also inside a wrapper: `cast(alias.col as varchar)`, `lower(alias.col)`. + const cols = [...side.matchAll(/\b(\w+)\.(\w+)\b/g)].filter((c) => c[1].toLowerCase() === alias) + if (cols.length === 1) keys.add(cols[0][2].toLowerCase()) + } + } + if (keys.size) + out.push({ model, keys: [...keys], join: text.replace(/\s+/g, " ").trim().slice(0, 160), left: !!m[1] }) + } + return out +} + +// ── tests on columns the SQL sets to NULL ─────────────────────────────────── + +/** Columns a model projects as a bare NULL (`null as c`, `cast(null as t) as c`, `null::t as c`). */ +export function literalNullColumns(sql: string): Set { + const s = sql.replace(/--[^\n]*/g, " ").replace(/\{#[\s\S]*?#\}/g, " ") + const out = new Set() + for (const m of s.matchAll(/(?:^|[,(\s])(?:null(?:\s*::\s*\w+(?:\([^)]*\))?)?|cast\s*\(\s*null\s+as\s+[^)]*\))\s+as\s+["`]?(\w+)/gi)) out.add(m[1].toLowerCase()) + return out +} + +const testNames = (tests: unknown): string[] => + Array.isArray(tests) ? tests.map((t) => (typeof t === "string" ? t : t && typeof t === "object" ? Object.keys(t)[0] : "")).filter(Boolean) : [] + +/** not_null / relationships tests, added in this diff, on a column the model always sets to NULL. */ +export function testsOnNullColumns(file: ChangedFile, ymlContent: string | undefined, sqlByModel: Map): Finding[] { + const doc = parseYaml(ymlContent) + if (!Array.isArray(doc?.models)) return [] + const added = addedLines(file.diff).join("\n") + const out: Finding[] = [] + for (const m of doc.models) { + const sql = m?.name && sqlByModel.get(String(m.name).toLowerCase()) + if (!sql || !Array.isArray(m.columns)) continue + const nulls = literalNullColumns(sql) + for (const c of m.columns) { + const col = String(c?.name ?? "").toLowerCase() + if (!nulls.has(col)) continue + const failing = [...testNames(c.data_tests), ...testNames(c.tests)].filter((t) => /(^|\.)(not_null|relationships)$/.test(t)) + if (!failing.length || !new RegExp(`\\b${col}\\b|not_null|relationships`, "i").test(added)) continue + out.push( + finding( + file, + "test-on-null-column", + "warning", + "test_coverage", + `\`${m.name}.${c.name}\` is tested but the model sets it to NULL`, + `\`${m.name}\` selects \`${c.name}\` as a literal NULL, so its ${failing.map((t) => `\`${t}\``).join(" / ")} test fails on every row. Drop the test until the column is populated, or populate it.`, + ), + ) + } + } + return out +} diff --git a/packages/opencode/src/altimate/review/rule-catalog.ts b/packages/opencode/src/altimate/review/rule-catalog.ts index 3b20660055..7c6b8f04ed 100644 --- a/packages/opencode/src/altimate/review/rule-catalog.ts +++ b/packages/opencode/src/altimate/review/rule-catalog.ts @@ -61,6 +61,22 @@ export interface Rule { portabilityBody?: string /** Match `added` outside quoted strings only (e.g. `'2024/01/15'` is not a division). */ ignoreStrings?: boolean + /** + * Structural condition on the whole new file (comments and string contents + * blanked), for shapes a regex can't see: real nesting depth, CASE/END + * balance, the final SELECT. ANDed with the other conditions. + */ + check?: (sql: string) => boolean + /** Give `check` the string literals too (comments still blanked), for checks that compare literal text. */ + checkKeepsStrings?: boolean + /** + * Matched against each JOIN's whole ON clause (comments and strings blanked), + * so a predicate split across lines is seen whole. Relevant only when one of + * the clause's lines was added. + */ + onClause?: RegExp + /** Also run on changed macros: the shape means the same thing in a SQL template. */ + macros?: boolean example: RuleExample counter?: RuleExample } @@ -92,7 +108,7 @@ export interface RuleInput { export function ruleMatches(rule: Rule, input: RuleInput): boolean { const kind = fileKind(input.file) const wantKind = rule.kind ?? "model_sql" - if (wantKind !== "any" && kind !== wantKind) return false + if (wantKind !== "any" && kind !== wantKind && !(kind === "macro" && rule.macros)) return false if (rule.pathGlob && !rule.pathGlob.test(input.file.path)) return false // Relevance gate: a rule only fires when its signal is in THE DIFF, never on @@ -120,9 +136,141 @@ export function ruleMatches(rule: Rule, input: RuleInput): boolean { if (rule.absentNewSql) { if (rule.absentNewSql.test(stripComments(input.newSql))) return false } + if (rule.check && !rule.check(rule.checkKeepsStrings ? stripComments(input.newSql) : stripStrings(stripComments(input.newSql)))) return false + if (rule.onClause) { + const changed = input.added.map((l) => stripStrings(stripComments(l)).trim()).filter((l) => /\w{2}/.test(l)) + const hit = onClauses(input.newSql).some((c) => rule.onClause!.test(c) && changed.some((l) => c.includes(l))) + if (!hit) return false + relevant = true + } return relevant } +const ON_END = /^(?:join|where|group\s+by|order\s+by|qualify|having|union|limit|window|left|right|inner|full|cross|natural|lateral|select|from)\b|^;|^\{%/i +const BEFORE_ON = /\b(join|on|where|select|from|group|having|qualify|order|merge|using|into)\b/gi + + +/** Blank everything inside nested parentheses, keeping the top level of `s`. */ +export function topLevel(s: string): string { + let depth = 0 + let out = "" + for (const ch of s) { + if (ch === "(") depth++ + out += depth > 0 ? " " : ch + if (ch === ")") depth = Math.max(0, depth - 1) + } + return out +} + +/** Text inside the parentheses that open at `start` (exclusive), or the rest of `s` if unbalanced. */ +export function parenBody(s: string, start: number): string { + let depth = 0 + for (let i = start; i < s.length; i++) { + if (s[i] === "(") depth++ + else if (s[i] === ")" && --depth === 0) return s.slice(start + 1, i) + } + return s.slice(start + 1) +} + +/** Top level of each CTE body (`name as ( select … )`). */ +export function cteBodies(sql: string): string[] { + const out: string[] = [] + for (const m of sql.matchAll(/\bas\s*(\()\s*select\b/gi)) out.push(topLevel(parenBody(sql, m.index! + m[0].indexOf("(")))) + return out +} + +/** Each SELECT block at one paren level: whether its FROM joins, and whether its select list has a bare column. */ +export function selectBlocks(sql: string): { joins: boolean; bareColumns: boolean }[] { + const scopes = [topLevel(sql), ...[...sql.matchAll(/\(\s*select\b/gi)].map((m) => topLevel(parenBody(sql, m.index!)))] + const out: { joins: boolean; bareColumns: boolean }[] = [] + for (const scope of scopes) { + for (const blk of scope.split(/\bunion(?:\s+all|\s+distinct)?\b/i)) { + const m = /\bselect\b(?:\s+distinct\b)?([\s\S]*?)\bfrom\b([\s\S]*?)(?=\b(?:where|group\s+by|having|qualify|order\s+by|limit|window)\b|$)/i.exec(blk) + if (!m) continue + const items = m[1].split(",").map((x) => x.trim()) + out.push({ + joins: /\bjoin\b/i.test(m[2]), + bareColumns: items.some((x) => /^[a-z_]\w*(\s+as\s+[a-z_]\w*)?$/i.test(x) && !/^(\*|true|false|null)$/i.test(x)), + }) + } + } + return out +} + +/** The text of every JOIN's ON clause (comments and strings blanked), up to the next clause keyword at the same paren depth. */ +export function onClauses(sql: string): string[] { + const s = stripStrings(stripComments(sql)) + const out: string[] = [] + for (const m of s.matchAll(/\bon\b/gi)) { + const at = m.index! + const prior = [...s.slice(Math.max(0, at - 2000), at).matchAll(BEFORE_ON)].pop() + if (prior?.[1].toLowerCase() !== "join") continue + let depth = 0 + let i = at + 2 + for (; i < s.length; i++) { + const ch = s[i] + if (ch === "(") depth++ + else if (ch === ")") { + if (depth === 0) break + depth-- + } else if (depth === 0 && /\W/.test(s[i - 1]) && ON_END.test(s.slice(i, i + 12))) break + } + out.push(s.slice(at, i)) + } + return out +} + +/** Deepest stack of parenthesised subqueries: `(select … (select …))` is 2; sibling CTEs are 1 each. */ +export function subqueryDepth(sql: string): number { + const stack: boolean[] = [] + let depth = 0 + let max = 0 + for (let i = 0; i < sql.length; i++) { + const ch = sql[i] + if (ch === "(") { + const sub = /^\(\s*select\b/i.test(sql.slice(i, i + 40)) + stack.push(sub) + if (sub) max = Math.max(max, ++depth) + } else if (ch === ")") { + if (stack.pop()) depth-- + } + } + return max +} + +const JINJA_BRANCH = /\{%-?\s*(if|for|elif|else)\b/i + +/** CASE…END nesting depth, or -1 when CASE and END don't balance. */ +export function caseDepth(sql: string): number { + let depth = 0 + let max = 0 + for (const m of sql.matchAll(/\b(case|end)\b/gi)) { + if (m[1].toLowerCase() === "case") max = Math.max(max, ++depth) + else if (depth > 0) depth-- + } + return depth === 0 ? max : -1 +} + +/** True when the last top-level SELECT is `select *` reading a relation that is not a CTE of this file. */ +export function finalSelectStarFromRelation(sql: string): boolean { + const s = sql.replace(/\{%[\s\S]*?%\}/g, " ") + let depth = 0 + let last = -1 + for (let i = 0; i < s.length; i++) { + const ch = s[i] + if (ch === "(") depth++ + else if (ch === ")") depth = Math.max(0, depth - 1) + else if (depth === 0 && /^select\b/i.test(s.slice(i, i + 7)) && !/\w/.test(s[i - 1] ?? " ")) last = i + } + if (last < 0) return false + const m = /^select\s+(?:distinct\s+)?\*\s+from\s+([\w."`]+|\{\{[^}]*\}\})/i.exec(s.slice(last)) + if (!m) return false + const target = m[1].replace(/["`]/g, "").toLowerCase() + if (target.startsWith("{{")) return true + const ctes = new Set([...s.matchAll(/\b(\w+)\s+as\s*\(/gi)].map((c) => c[1].toLowerCase())) + return !ctes.has(target) +} + /** The project's engine, for engine-dependent rules. Omitted = unknown. */ export interface CatalogContext { dialect: string @@ -244,11 +392,200 @@ const r = ( | "dialectGate" | "portabilityBody" | "ignoreStrings" + | "check" + | "onClause" + | "macros" + | "checkKeepsStrings" >, example: RuleExample, counter?: RuleExample, ): Rule => ({ id, category, severity, title, body, ...match, example, counter }) + +/** Columns named in a comma list, without qualifiers, lowercased. */ +const colList = (s: string) => + s.split(",").map((c) => c.trim().replace(/\s+(asc|desc)(\s+nulls\s+(first|last))?$/i, "").replace(/^.*\./, "").toLowerCase()).filter(Boolean) + +/** An OVER clause whose ORDER BY repeats a PARTITION BY column (constant within each partition). */ +function windowOrderRepeatsPartition(sql: string): boolean { + for (const m of sql.matchAll(/\bover\s*\(/gi)) { + const body = topLevel(parenBody(sql, m.index! + m[0].length - 1)) + const pm = /\bpartition\s+by\b([\s\S]*?)(?=\border\s+by\b|\brows\b|\brange\b|$)/i.exec(body) + const om = /\border\s+by\b([\s\S]*?)(?=\brows\b|\brange\b|$)/i.exec(body) + if (!pm || !om) continue + const parts = new Set(colList(pm[1])) + if (colList(om[1]).some((c) => /^\w+$/.test(c) && parts.has(c))) return true + } + return false +} + +/** A CASE with two WHEN branches whose conditions are identical: the second is unreachable. */ +function duplicateCaseWhen(sql: string): boolean { + for (const m of sql.matchAll(/\bcase\b/gi)) { + // The CASE's own level: nested CASEs and calls are blanked by parens only, so cut at the matching END. + let depth = 0 + const conds: string[] = [] + const re = /\b(case|end|when)\b([\s\S]*?)(?=\b(?:case|end|when|then)\b)/gi + re.lastIndex = m.index! + for (let x = re.exec(sql); x; x = re.exec(sql)) { + const kw = x[1].toLowerCase() + if (kw === "case") depth++ + else if (kw === "end") { + if (--depth === 0) break + } else if (depth === 1) conds.push(x[2].replace(/\s+/g, " ").trim().toLowerCase()) + } + if (new Set(conds).size < conds.length) return true + } + return false +} + +/** A LEFT JOIN whose right side is filtered in the WHERE of the same SELECT: the join behaves as INNER. */ +export function leftJoinNullifiedByWhere(sql: string): boolean { + const scopes = [topLevel(sql), ...[...sql.matchAll(/\(\s*select\b/gi)].map((m) => topLevel(parenBody(sql, m.index!)))] + for (const scope of scopes) { + for (const blk of scope.split(/\bunion(?:\s+all|\s+distinct)?\b/i)) { + const where = /\bwhere\b([\s\S]*?)(?=\b(?:group\s+by|having|qualify|order\s+by|limit|window)\b|$)/i.exec(blk)?.[1] + if (!where) continue + // The alias, or the relation's own name when it has none. + const aliases = [...blk.matchAll(/\bleft\s+(?:outer\s+)?join\s+(\{\{[^}]*\}\}|[\w."`]+)\s*(?:as\s+)?(\w+)?/gi)] + .map((m) => (m[2] && !/^(on|using|where|left|right|inner|full|cross|join)$/i.test(m[2]) ? m[2] : /^[\w."`]+$/.test(m[1]) ? m[1].replace(/[`"]/g, "").split(".").pop() : undefined)) + .filter((a): a is string => !!a) + const conjuncts = where.split(/\band\b/i) + for (const a of aliases) { + const ref = new RegExp(`\\b${a}\\.\\w+`, "i") + if ( + conjuncts.some( + (c) => ref.test(c) && !/\bor\b|coalesce|ifnull|nvl/i.test(c) && !new RegExp(`\\b${a}\\.\\w+\\s+is\\s+null\\b`, "i").test(c), + ) + ) + return true + } + } + } + return false +} + +const FEASIBLE: Rule[] = [ + r( + "left-join-filtered-in-where", + "join_risk", + "warning", + "LEFT JOIN filtered in WHERE acts as an INNER JOIN", + "A WHERE predicate on the right side of a LEFT JOIN drops the rows where that side is NULL, so the LEFT JOIN behaves as an INNER JOIN. Move the predicate into the ON clause to keep unmatched rows, or write INNER JOIN if dropping them is intended.", + { added: /\b(where|and)\b|\bleft\s+(outer\s+)?join\b/i, check: leftJoinNullifiedByWhere }, + { add: "select a.id, b.x\nfrom a\nleft join b on a.id = b.id\nwhere b.status = 'active'" }, + { add: "select a.id, b.x\nfrom a\nleft join b on a.id = b.id and b.status = 'active'\nwhere a.kind = 'x'" }, + ), + r( + "join-case-folded-one-side", + "sql_correctness", + "suggestion", + "join compares a case-folded key to a raw one", + "`lower()`/`upper()` on one side of the join only: values on the other side with different casing never match. Fold both sides, or normalize the key upstream.", + { + onClause: /\b(lower|upper)\s*\(\s*[\w.]+\s*\)\s*=\s*(?!\s*(lower|upper)\s*\()[a-z_][\w.]*|(?:^|[\s(])(?`). With `=`, rows whose key is NULL on both sides no longer match, so a join drops them and a comparison treats them as different.", + { removed: /\bis\s+not\s+distinct\s+from\b|<=>/i, absentNewSql: /\bis\s+not\s+distinct\s+from\b|<=>/i, added: /=/ }, + { remove: "on a.k is not distinct from b.k", add: "on a.k = b.k", newSql: "select 1 from a join b on a.k = b.k" }, + { remove: "on a.k is not distinct from b.k", add: "on a.k is not distinct from b.k and a.x = b.x" }, + ), + r( + "window-order-repeats-partition", + "warehouse_cost", + "suggestion", + "window ORDER BY repeats a PARTITION BY column", + "Within a partition the PARTITION BY columns are constant, so ordering by them changes nothing and only adds sort keys. Order by the columns that actually vary within the partition.", + { added: /\border\s+by\b|\bpartition\s+by\b/i, check: windowOrderRepeatsPartition }, + { add: "sum(x) over (partition by account_id, source_relation order by account_id, source_relation, d) as r" }, + { add: "sum(x) over (partition by account_id order by d) as r" }, + ), + r( + "case-duplicate-when", + "sql_correctness", + "suggestion", + "CASE has a duplicate WHEN condition", + "Two WHEN branches test the same condition; CASE returns the first match, so the later branch never runs. Merge them, or fix the condition that was meant.", + { added: /\bwhen\b/i, check: duplicateCaseWhen, checkKeepsStrings: true }, + { add: "case\n when spend = 0 then 'no spend'\n when clicks = 0 then 'no clicks'\n when spend = 0 then 'zero'\nend" }, + { add: "case\n when spend = 0 then 'no spend'\n when clicks = 0 then 'no clicks'\nend" }, + ), + r( + "qualify-unsupported", + "sql_correctness", + "warning", + "QUALIFY isn't supported on every engine", + "Trino, Athena and Postgres have no QUALIFY clause, so the model fails to parse there. Filter on the window result in an outer SELECT instead.", + { added: /\bqualify\b/i, macros: true, portability: { worksOn: ["snowflake", "bigquery", "databricks", "spark", "duckdb", "redshift", "clickhouse", "teradata", "exasol"] } }, + { add: "qualify row_number() over (partition by id order by d desc) = 1" }, + { add: "where rn = 1" }, + ), + r( + "ignore-nulls-inside-call", + "sql_correctness", + "warning", + "IGNORE NULLS inside the function call is BigQuery syntax", + "`f(x IGNORE NULLS)` parses on BigQuery only. Trino, Snowflake and others take it after the call: `last_value(x) IGNORE NULLS OVER (…)`.", + { + added: /\bignore\s+nulls\b/i, + check: (sql) => /\(\s*[^()]*?\bignore\s+nulls\s*\)/i.test(sql), + macros: true, + portability: { worksOn: ["bigquery"] }, + }, + { add: "last_value(x ignore nulls) over (partition by k order by d)" }, + { add: "last_value(x) ignore nulls over (partition by k order by d)" }, + ), + r( + "min-max-by-portability", + "sql_correctness", + "suggestion", + "MIN_BY/MAX_BY don't exist on Postgres or Redshift", + "`min_by`/`max_by` run on BigQuery, Snowflake, Databricks, Trino and DuckDB but not on Postgres or Redshift. In a package that supports those adapters, use a dispatched macro or a `row_number()` pick.", + { added: /\b(min_by|max_by)\s*\(/i, macros: true, portability: { worksOn: ["bigquery", "snowflake", "databricks", "spark", "trino", "athena", "duckdb"] } }, + { add: "max_by(status, updated_at) as latest_status" }, + { add: "max(updated_at) as latest_at" }, + ), + r( + "clickhouse-mutation-async", + "sql_correctness", + "suggestion", + "ClickHouse ALTER … UPDATE/DELETE runs asynchronously", + "A ClickHouse mutation returns before it is applied unless `mutations_sync` is set, so the next statement can read the old rows. Add `SETTINGS mutations_sync = 2` (or 1) when later steps depend on the result.", + { added: /\balter\s+table\b[^;]*\b(update|delete)\b/i, absentNewSql: /mutations_sync/i, macros: true }, + { add: "alter table {{ relation }} update col = null where col = ''" }, + { add: "alter table {{ relation }} update col = null where col = '' settings mutations_sync = 2", newSql: "alter table t update col = null where col = '' settings mutations_sync = 2" }, + ), + r( + "datename-language-dependent", + "sql_correctness", + "suggestion", + "DATENAME depends on the session language", + "SQL Server's `DATENAME(weekday|month, …)` returns names in the session's language, so comparisons against English names break on servers with another default. Use `DATEPART` with `@@DATEFIRST`-safe arithmetic.", + { added: /\bdatename\s*\(\s*(weekday|dw|month|mm|m)\b/i, macros: true }, + { add: "datename(weekday, created_at) = 'Monday'" }, + { add: "datepart(weekday, created_at) = 2" }, + ), + r( + "hardcoded-year-filter", + "sql_correctness", + "suggestion", + "hardcoded year in a filter won't roll forward", + "A literal year in the filter freezes the model to that year. Read it from a var or derive it from the current date, or say in a comment why it is fixed.", + { added: /\b\w*(year|yr)\w*\s*(=|>=|<=|<|>)\s*20\d\d\b/i, unless: /var\s*\(/i }, + { add: "where academic_year = 2024" }, + { add: "where academic_year = {{ var('current_academic_year') }}" }, + ), +] + export const CATALOG: Rule[] = [] const CORRECTNESS: Rule[] = [ @@ -348,7 +685,7 @@ const CORRECTNESS: Rule[] = [ "suggestion", "GROUP BY positional refs are fragile", "`GROUP BY 1,2` breaks silently when the SELECT list reorders; group by explicit names.", - { added: /\bgroup\s+by\s+\d+(\s*,\s*\d+)+/i }, + { added: /\bgroup\s+by\s+\d+(\s*,\s*\d+)+/i, macros: true }, { add: "group by 1, 2" }, { add: "group by customer_id" }, ), @@ -588,20 +925,29 @@ const JOINS: Rule[] = [ "join_risk", "suggestion", "join key wrapped in CAST/function", - "Casting a column inside the ON clause defeats key pruning and signals a type mismatch.", - { added: /\bon\b[^;]*\bcast\s*\(/i }, - { add: "on cast(a.id as string) = b.id" }, - { add: "on a.id = b.id" }, + "A CAST or function around a column in the ON clause defeats partition and cluster pruning on that side and usually signals a type mismatch; convert once upstream, or on the smaller side.", + { + macros: true, + onClause: + /\b(?:cast|try_cast|safe_cast|date|datetime|timestamp|to_date|to_timestamp|to_char|to_varchar|date_trunc|lower|upper|trim|ltrim|rtrim|substr|substring)\s*\(\s*(?:''\s*,\s*)?[a-z_]\w*(?:\.[a-z_]\w*)?\s*(?:\)|,|\bas\b)/i, + }, + { add: "left join b\n on a.id = b.id\n and a.code = cast(b.code as string)" }, + { add: "join b on a.id = b.id and b.day >= cast('2024-01-01' as date)" }, ), r( "join-or-condition", "join_risk", "warning", "OR in a join condition — explosive/slow", - "`JOIN … ON a=b OR c=d` often produces a near-cartesian and disables hash joins. Split into UNION.", - { added: /\bon\b[^;]*\bor\b/i }, - { add: "on a.id=b.id or a.alt=b.alt" }, - { add: "on a.id=b.id and a.k=b.k" }, + "An OR in the ON clause can match one row through more than one alternative (duplicates downstream), and an OR between join keys leaves no single key to hash on. Add a uniqueness test on the output grain, or split into a UNION of one join per alternative.", + // An open-ended interval guard (`b.valid_to is null or a.d < b.valid_to`) matches each + // row once by design, so an OR whose disjunct is an IS [NOT] NULL test is excluded. + { + onClause: /(?= b.valid_from\n and (b.valid_to is null\n or a.d < b.valid_to)" }, ), r( "lateral-join", @@ -619,9 +965,11 @@ const JOINS: Rule[] = [ "suggestion", "unqualified column with multiple tables", "With joins present, unqualified columns are ambiguous and break when both sides add the name.", - { newSql: /select\s+\w+\s*,[\s\S]*\bjoin\b/i }, - { add: "select id, name from a join b on a.k=b.k" }, - { add: "select a.id from a join b on a.k=b.k" }, + // A changed line that is a bare column in a select list, in a model where some + // query block selects a bare column from a FROM that joins. + { added: /^\s*,?\s*[a-z_]\w*\s*,?\s*$/i, unless: /^\s*,?\s*(select|from|where|and|or|on|as|end|else|then|union|all|distinct|group|order|by|having|qualify|limit|with|join)\b/i, check: (sql) => selectBlocks(sql).some((b) => b.joins && b.bareColumns) }, + { add: "select\n a.id,\n name\nfrom a\njoin b on a.k = b.k" }, + { add: "select\n a.id,\n b.name\nfrom a\njoin b on a.k = b.k" }, ), r( "self-join-no-inequality", @@ -788,7 +1136,9 @@ const PERF: Rule[] = [ "suggestion", "ORDER BY inside a CTE/subquery is ignored", "Ordering inside a CTE has no guaranteed effect downstream and just adds a sort cost.", - { newSql: /\bas\s*\(\s*select[\s\S]*?\border\s+by\b[\s\S]*?\)/i }, + // ORDER BY at a CTE's own top level (window/aggregate ORDER BYs sit in nested parens), + // and not a top-N, where the order decides which rows are kept. + { added: /\border\s+by\b/i, check: (sql) => cteBodies(sql).some((b) => /\border\s+by\b/i.test(b) && !/\b(limit|top|qualify|fetch\s+first)\b/i.test(b)) }, { add: "with x as (select a from t order by a) select * from x" }, { add: "with x as (select a from t) select * from x" }, ), @@ -817,10 +1167,10 @@ const PERF: Rule[] = [ "warehouse_cost", "suggestion", "SELECT * in final projection", - "A trailing `select *` carries every upstream column forward, bloating the model and storage.", - { added: /^\s*select\s+\*\s*$/i }, - { add: "select *" }, - { add: "select id, name" }, + "The model's final `select *` reads straight from another relation, so every upstream column (including ones added later) lands in this model.", + { added: /\bselect\s+(distinct\s+)?\*/i, check: finalSelectStarFromRelation }, + { add: "select * from {{ ref('orders') }}" }, + { add: "with final as (select id from {{ ref('orders') }})\nselect * from final" }, ), r( "large-in-list", @@ -838,9 +1188,9 @@ const PERF: Rule[] = [ "suggestion", "deeply nested subqueries hurt readability/plans", "Several nested `(select … (select …))` levels are hard to optimize; flatten into CTEs.", - { newSql: /\(\s*select[\s\S]*\(\s*select[\s\S]*\(\s*select/i }, + { added: /\(\s*select\b/i, check: (s) => subqueryDepth(s) >= 3 }, { add: "select (select (select (select 1)))" }, - { add: "select a from t" }, + { add: "with a as (select 1), b as (select 2), c as (select 3)\nselect * from a" }, ), r( "distinct-star", @@ -979,16 +1329,6 @@ const MATERIALIZATION: Rule[] = [ ] const DBT_CONV: Rule[] = [ - r( - "ref-double-quotes", - "sql_quality", - "suggestion", - "ref() uses double quotes", - "dbt convention is single-quoted `ref('model')`; double quotes are inconsistent and can confuse linters.", - { added: /\{\{\s*ref\(\s*"/i }, - { add: 'from {{ ref("x") }}' }, - { add: "from {{ ref('x') }}" }, - ), r( "information-schema", "sql_quality", @@ -1740,9 +2080,9 @@ const STYLE: Rule[] = [ "suggestion", "deeply nested CASE expression", "Nested `CASE WHEN … CASE WHEN` is hard to verify; flatten with combined conditions or a mapping.", - { newSql: /case\s+when[\s\S]*\bcase\s+when/i }, + { added: /\bcase\b/i, check: (s) => caseDepth(s) >= 2 }, { add: "case when a then case when b then 1 end end" }, - { add: "case when a then 1 end" }, + { add: "case when a then 1 end as x,\ncase when b then 2 end as y" }, ), r( "hardcoded-env-name", @@ -1790,7 +2130,7 @@ const STYLE: Rule[] = [ "suggestion", "TOP n without ORDER BY", "`SELECT TOP n` without `ORDER BY` returns arbitrary rows; add an explicit order.", - { added: /select\s+top\s+\d+/i, unless: /order\s+by/i }, + { added: /select\s+top\s+\d+/i, unless: /order\s+by/i, macros: true }, { add: "select top 10 * from t" }, { add: "select top 10 * from t order by d" }, ), @@ -2021,7 +2361,8 @@ const MISC: Rule[] = [ "warning", "SELECT * in a UNION", "`select *` on either side of a UNION breaks (column count/order) the moment a column is added.", - { newSql: /select\s+\*[\s\S]*\bunion\b/i }, + // A UNION branch that is itself `select *` (not a `select *` elsewhere in the model). + { newSql: /\bselect\s+\*\s+from\s+(?:(?!\bselect\b|\bunion\b)[\s\S])*?\bunion\b|\bunion(?:\s+all|\s+distinct)?\s*\(?\s*select\s+\*/i }, { add: "select * from a union all select * from b" }, { add: "select id from a union all select id from b" }, ), @@ -2130,10 +2471,12 @@ const MISC: Rule[] = [ "sql_correctness", "warning", "CASE without END", - "A `CASE WHEN` with no matching `END` on the line/expression is a syntax error.", - { added: /\bcase\s+when\b[^;]*$/i, unless: /\bend\b|\{\{|\{%/i }, + "The model has more `CASE` than `END` keywords, which is a syntax error.", + // Whole-file balance, not per line: a CASE whose END sits on a later line is fine. + // Jinja branches can emit different CASE/END counts per branch, so they abstain. + { added: /\bcase\s+when\b/i, check: (s) => caseDepth(s) === -1 && !JINJA_BRANCH.test(s) }, { add: "case when x > 0 then 1" }, - { add: "case when x > 0 then 1 end" }, + { add: "case when x > 0\n then 1\n else 0\nend as flag" }, ), r( "double-aggregation-alias", @@ -2344,7 +2687,7 @@ const MISC2: Rule[] = [ "suggestion", "GREATEST/LEAST NULL behavior varies", "Some warehouses return NULL if any GREATEST/LEAST argument is NULL — guard nullable inputs.", - { added: /\b(greatest|least)\s*\(/i, unless: /coalesce|nvl|ifnull/i }, + { added: /\b(greatest|least)\s*\(/i, unless: /coalesce|nvl|ifnull/i, macros: true }, { add: "greatest(a, b)" }, { add: "greatest(coalesce(a,0), coalesce(b,0))" }, ), @@ -2437,6 +2780,7 @@ CATALOG.push( ...STYLE, ...MISC, ...MISC2, + ...FEASIBLE, ) // NOTE: the large generated families (dialect-specific functions, reserved // words, non-portable types, operators) used to live in `rule-generators.ts`. diff --git a/packages/opencode/src/altimate/review/run.ts b/packages/opencode/src/altimate/review/run.ts index fd1911a67c..33f6f99738 100644 --- a/packages/opencode/src/altimate/review/run.ts +++ b/packages/opencode/src/altimate/review/run.ts @@ -1,5 +1,6 @@ import path from "node:path" -import { readFile, realpath, stat, access } from "node:fs/promises" +import { readFile, realpath, stat, access, readdir } from "node:fs/promises" +import YAML from "yaml" import { Installation } from "../../installation" import { loadReviewPolicy, resolveRubric } from "./config" import type { Severity } from "./finding" @@ -9,6 +10,8 @@ import { resolveReviewBase, type ResolvedBase, gitRepoRoot, + listFilesAt, + grepMatchesAt, manifestHash, showAtRef, } from "./git" @@ -17,6 +20,7 @@ import { scopeToProject } from "./diff-filter" import { buildCatalogSchemaContext } from "./schema-context" import { createDispatcherRunner } from "./runner" import { runReview } from "./orchestrate" +import { ADAPTERS } from "./dbt-patterns" import { runAiReview } from "./ai-review" import { loadConsumerLists } from "./consumer-lists" import type { ReviewMode, VerdictEnvelope } from "./verdict" @@ -398,6 +402,7 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise : [] return runReview({ + resourceNames: () => projectResourceNames(opts.cwd, opts.head, projectRootRel, dbtRoot), changedFiles, config, rubric, @@ -425,6 +430,7 @@ export async function reviewPullRequest(opts: ReviewPullRequestOptions): Promise staleManifest, layout, suppressPortability: config.portability === undefined ? dialectKnown : !config.portability, + portabilityTargets: await projectAdapters(opts.cwd, opts.head, projectRootRel), dialectKnown, consumerLists, policy: { source: policy.source, hash: policy.hash, changedInPr: policy.changedInPr.length ? policy.changedInPr : undefined }, @@ -519,3 +525,79 @@ async function repoRelative(gitRoot: string, dir: string, known: boolean): Promi if (rel.startsWith("..") || path.isAbsolute(rel)) return undefined return rel } + +const RESOURCE_FILE = /\.(sql|py|csv)$/i +const resourceName = (f: string) => path.basename(f).replace(RESOURCE_FILE, "").toLowerCase() + +/** + * Adapters the project's own macros carry variants for (`postgres__x`, + * `snowflake__x`, ...). A project with variants for several adapters is meant + * to run on all of them, so portability advice is judged against each one, + * not just the adapter this checkout is configured for. + */ +export async function projectAdapters( + cwd: string, + head: string | undefined, + projectRootRel: string | undefined, +): Promise { + if (projectRootRel === undefined) return [] + const prefix = projectRootRel ? projectRootRel + "/" : "" + const lines = await grepMatchesAt(cwd, head, `macro[[:space:]]+(${ADAPTERS.join("|")})__`, `${prefix}*.sql`) + const seen = new Set() + for (const line of lines ?? []) { + if (line.includes("/dbt_packages/")) continue + const m = /macro\s+(\w+?)__$/.exec(line) + if (m) seen.add(m[1] === "presto" ? "trino" : m[1]) + } + return [...seen].sort() +} + +/** + * Names `ref()` can resolve to: model/seed/snapshot files in the repo at head, snapshots + * defined in YAML, and + * models of installed packages (`dbt_packages/`). Undefined when that can't be known: + * no project root, git can't list the tree, or packages are declared but not installed. + */ +export async function projectResourceNames( + cwd: string, + head: string | undefined, + projectRootRel: string | undefined, + dbtRoot: string, +): Promise | undefined> { + if (projectRootRel === undefined) return undefined + const files = await listFilesAt(cwd, head) + if (!files) return undefined + const prefix = projectRootRel ? projectRootRel + "/" : "" + const names = new Set() + for (const f of files) if (f.startsWith(prefix) && RESOURCE_FILE.test(f)) names.add(resourceName(f)) + // dbt 1.9+ snapshots can be YAML-only (`snapshots: - name: x relation: ...`), with no .sql file. + for (const line of (await grepMatchesAt(cwd, head, "^snapshots:", `${prefix}*.yml`)) ?? []) { + const rel = line.slice(line.indexOf(":") + 1, line.lastIndexOf(":snapshots:")) + if (rel.includes("/dbt_packages/")) continue + let doc: { snapshots?: { name?: unknown }[] } | null = null + try { + doc = YAML.parse((await showAtRef(cwd, head ?? "HEAD", rel)) ?? "") + } catch { + continue + } + for (const snap of Array.isArray(doc?.snapshots) ? doc.snapshots : []) if (typeof snap?.name === "string") names.add(snap.name.toLowerCase()) + } + if (files.includes(prefix + "packages.yml") || files.includes(prefix + "dependencies.yml")) { + const pkgDir = path.join(dbtRoot, "dbt_packages") + const walk = async (dir: string, depth: number): Promise => { + let entries + try { + entries = await readdir(dir, { withFileTypes: true }) + } catch { + return false + } + for (const e of entries) { + if (e.isDirectory() && depth < 12 && e.name !== ".git") await walk(path.join(dir, e.name), depth + 1) + else if (e.isFile() && RESOURCE_FILE.test(e.name)) names.add(resourceName(e.name)) + } + return true + } + if (!(await walk(pkgDir, 0))) return undefined + } + return names +} diff --git a/packages/opencode/src/altimate/review/runner.ts b/packages/opencode/src/altimate/review/runner.ts index 96c85ebcff..1f8d28751c 100644 --- a/packages/opencode/src/altimate/review/runner.ts +++ b/packages/opencode/src/altimate/review/runner.ts @@ -442,6 +442,29 @@ export function createDispatcherRunner(opts: DispatcherRunnerOptions): ReviewRun } }, + async declaredUniqueKeys(model: string): Promise { + try { + const raw = loadRawManifest(opts.manifestPath) + if (!raw) return [] + const nodes = Object.entries(raw.nodes ?? {}) + const uid = nodes.find(([id, n]) => id.startsWith("model.") && n?.name === model)?.[0] + if (!uid) return [] + const keys: string[][] = [] + for (const [, n] of nodes) { + if (n?.resource_type !== "test" || n.attached_node !== uid) continue + const name = String(n.test_metadata?.name ?? "") + const kw = n.test_metadata?.kwargs ?? {} + if (name === "unique" && n.column_name) keys.push([String(n.column_name)]) + const combo = kw.combination_of_columns ?? kw.arguments?.combination_of_columns + if (name === "unique_combination_of_columns" && Array.isArray(combo)) keys.push(combo.map(String)) + } + return keys + } catch (err) { + fail("declaredUniqueKeys", err) + return [] + } + }, + async modelConfig(name: string) { try { const raw = loadRawManifest(opts.manifestPath) diff --git a/packages/opencode/test/altimate/review-ai-inputs.test.ts b/packages/opencode/test/altimate/review-ai-inputs.test.ts new file mode 100644 index 0000000000..7ce92fa428 --- /dev/null +++ b/packages/opencode/test/altimate/review-ai-inputs.test.ts @@ -0,0 +1,112 @@ +import { describe, test, expect } from "bun:test" +import { runReview, type ReviewRunner } from "../../src/altimate/review/orchestrate" +import { DEFAULT_REVIEW_CONFIG } from "../../src/altimate/review/config" +import { DEFAULT_RUBRIC } from "../../src/altimate/review/rubric" +import { buildUserMessage, selectAiFiles, type AiReviewFile, type AiReviewInput } from "../../src/altimate/review/ai-review" +import type { ChangedFile } from "../../src/altimate/review/diff-filter" + +/** + * Defects in macros, singular tests and yml never show in a model diff, so the + * advisory lane receives every changed dbt file, each labeled with its role. + */ + +const runner: ReviewRunner = { + async impact() { + return { hasManifest: true, severity: "SAFE", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + async grade() { + return { grade: "B" } + }, + async check() { + return { issues: [], ran: true } + }, + async equivalence() { + return { decided: true, equivalent: true } + }, + async detectPii() { + return { columns: [] } + }, +} as ReviewRunner + +const FILES: Record = { + "models/marts/m.sql": "select 2 as id", + "macros/to_cents.sql": "{% macro to_cents(c) %}round({{ c }} * 100){% endmacro %}", + "models/marts/_marts.yml": "models:\n - name: m\n columns:\n - name: id\n", + "tests/assert_positive.sql": "select * from {{ ref('m') }} where id < 0", + "seeds/country_codes.csv": "code,name\nUS,United States\n", +} + +async function aiInputFor(changed: ChangedFile[]): Promise { + let seen: AiReviewInput | undefined + await runReview({ + changedFiles: changed, + config: { ...DEFAULT_REVIEW_CONFIG }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + getContent: async (f, side) => (side === "old" ? undefined : FILES[f]), + generatedAt: "2026-11-14T00:00:00Z", + aiReview: async (input) => { + seen = input + return { findings: [], status: { state: "ok" } } + }, + }) + return seen +} + +const mod = (path: string, diff = "+x\n"): ChangedFile => ({ path, status: "modified", diff }) + +describe("files sent to the advisory AI lane", () => { + test("macros, singular tests, yml and seeds go with the model, labeled by role", async () => { + const input = await aiInputFor(Object.keys(FILES).map((p) => mod(p))) + const byPath = new Map(input!.files.map((f) => [f.path, f])) + expect([...byPath.keys()].sort()).toEqual(Object.keys(FILES).sort()) + expect(byPath.get("macros/to_cents.sql")).toMatchObject({ kind: "macro", sql: FILES["macros/to_cents.sql"] }) + expect(byPath.get("tests/assert_positive.sql")?.kind).toBe("test") + expect(byPath.get("models/marts/_marts.yml")).toMatchObject({ kind: "schema_yml", sql: FILES["models/marts/_marts.yml"] }) + // A seed goes as its diff only. + expect(byPath.get("seeds/country_codes.csv")).toMatchObject({ kind: "seed", sql: undefined }) + }) + + test("a macro-only PR reaches the lane instead of being skipped", async () => { + const input = await aiInputFor([mod("macros/to_cents.sql", "-round({{ c }})\n+round({{ c }} * 100)\n")]) + expect(input?.files.map((f) => f.path)).toEqual(["macros/to_cents.sql"]) + }) + + test("deleted files are not sent", async () => { + const input = await aiInputFor([mod("models/marts/m.sql"), { path: "macros/old.sql", status: "deleted", diff: "-x\n" }]) + expect(input?.files.some((f) => f.path === "macros/old.sql")).toBe(false) + }) +}) + +describe("selection and prompt layout", () => { + const f = (path: string, kind: string, status = "modified"): AiReviewFile => ({ path, status, kind, model: path, diff: "+x\n" }) + + test("models first, then macros, tests, yml; deleted dropped; capped at 20", () => { + const files = [ + f("_s.yml", "schema_yml"), + f("t.sql", "test"), + f("gone.sql", "macro", "deleted"), + f("mac.sql", "macro"), + ...Array.from({ length: 19 }, (_, i) => f(`m${i}.sql`, "model_sql")), + ] + const picked = selectAiFiles(files).map((x) => x.path) + expect(picked).toHaveLength(20) + expect(picked.slice(0, 19).every((p) => /^m\d+\.sql$/.test(p))).toBe(true) + expect(picked[19]).toBe("mac.sql") + }) + + test("each file's header names its role and its content is fenced by type", () => { + const msg = buildUserMessage({ + grounding: [], + files: [ + { path: "macros/m.sql", status: "modified", kind: "macro", model: "m", sql: "{% macro m() %}1{% endmacro %}" }, + { path: "models/_s.yml", status: "added", kind: "schema_yml", model: "_s.yml", sql: "models: []" }, + { path: "models/a.sql", status: "modified", model: "a", sql: "select 1" }, + ], + }) + expect(msg).toContain("### macros/m.sql (modified, macro (Jinja; changes every model that calls it))") + expect(msg).toContain("### models/_s.yml (added, schema yml (tests, contracts, descriptions))\nCurrent file:\n```yaml") + expect(msg).toContain("### models/a.sql (modified)\nCurrent SQL:\n```sql") + }) +}) diff --git a/packages/opencode/test/altimate/review-contracts-dialect.test.ts b/packages/opencode/test/altimate/review-contracts-dialect.test.ts index 2670029a36..e67f5418d6 100644 --- a/packages/opencode/test/altimate/review-contracts-dialect.test.ts +++ b/packages/opencode/test/altimate/review-contracts-dialect.test.ts @@ -139,6 +139,21 @@ describe("consumers outside the DAG", () => { expect(plain.findings.some((x) => x.title.includes("declared consumers"))).toBe(false) }) + test("nothing declared: a final-layer model still gets a suggestion, an intermediate model does not", async () => { + const none = { exposures: [], contractEnforced: false, contractColumns: [] } + const mart = (await review(none)).findings.find((x) => x.title.includes("final-layer")) + expect(mart?.severity).toBe("suggestion") + expect(mart?.body).toContain("`email`") + const inter = await review(none, { + changedFiles: [{ ...changed[0], path: "models/intermediate/int_x.sql" }], + }) + expect(inter.findings.some((x) => x.title.includes("final-layer"))).toBe(false) + const nested = await review(none, { + changedFiles: [{ ...changed[0], path: "models/extracts/tableau/intermediate/int_roster.sql" }], + }) + expect(nested.findings.some((x) => x.title.includes("final-layer"))).toBe(false) + }) + const optIn = { config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["lineage_breakage", "contract_violation"], checkContractColumns: true } } test("opt-in: a new column missing from the enforced contract is reported, and never blocks", async () => { diff --git a/packages/opencode/test/altimate/review-dbt-patterns.test.ts b/packages/opencode/test/altimate/review-dbt-patterns.test.ts index 5c5099dfab..2dd4a14751 100644 --- a/packages/opencode/test/altimate/review-dbt-patterns.test.ts +++ b/packages/opencode/test/altimate/review-dbt-patterns.test.ts @@ -69,19 +69,11 @@ describe("dbt-patterns detectors", () => { expect(has(f, "sql_correctness", "warning")).toBe(true) }) - test("SELECT * added → warehouse_cost suggestion; passthrough select * from {{ ref }} is NOT flagged", () => { - const flagged = detectModelPatterns( - modelFile("models/marts/m.sql", "select *, 1 from x", [" select *, 1 from x"]), - "x", - DEFAULT_RUBRIC, - ) - expect(has(flagged, "warehouse_cost")).toBe(true) - const passthrough = detectModelPatterns( - modelFile("models/staging/m.sql", "select * from {{ ref('x') }}", ["select * from {{ ref('x') }}"]), - "x", - DEFAULT_RUBRIC, - ) - expect(has(passthrough, "warehouse_cost")).toBe(false) + test("a per-line SELECT * is not flagged (import CTEs and CTE chains are the dbt idiom; the catalog covers the final projection)", () => { + for (const sql of ["select *, 1 from x", "select * from {{ ref('x') }}"]) { + const f = detectModelPatterns(modelFile("models/marts/m.sql", sql, [sql]), "x", DEFAULT_RUBRIC) + expect(f.some((x) => /SELECT \* scans all columns/.test(x.title))).toBe(false) + } }) test("ROW_NUMBER() dedup with NO order by → dedup warning; any order by → clean (tie-prone case is core L039)", () => { @@ -129,14 +121,14 @@ describe("dbt-patterns detectors", () => { expect(has(f, "sql_correctness", "warning")).toBe(true) }) - test("fanout: new join into an aggregating model → fanout warning", () => { + test("fanout: new join into an aggregating model → fanout suggestion", () => { const sql = `select customer_id, sum(amount) from o left join {{ ref('items') }} i on i.oid = o.id group by 1` const f = detectModelPatterns( modelFile("models/marts/m.sql", sql, ["left join {{ ref('items') }} i on i.oid = o.id"]), sql, DEFAULT_RUBRIC, ) - expect(has(f, "fanout", "warning")).toBe(true) + expect(has(f, "fanout", "suggestion")).toBe(true) }) test("schema.yml: removed unique/not_null test → test_coverage finding", () => { @@ -788,7 +780,7 @@ models: [] // `not_null` coverage on the same model (constraint if contracted, data_test // if view). Directly targets PR D×2 + PR A×2 human findings in the R20 // corpus study; explicit rule in DBT_GUIDELINES.md. - test("R20 S1: unique_combination_of_columns with grain col missing not_null → warning finding", () => { + test("R20 S1: unique_combination_of_columns with grain col missing not_null → suggestion finding", () => { // `price_start_time` is grain but has no not_null coverage → gap. // `metastore_id` / `sku_name` have not_null via constraints on the // contracted model → covered. Test scoped as a newly-added model to @@ -824,7 +816,9 @@ models: ) const gap = f.find((x) => (x.evidence?.result as any)?.rule === "grain_key_not_null_missing") expect(gap).toBeDefined() - expect(gap!.severity).toBe("warning") + // Suggestion: unique_combination_of_columns still catches duplicates (NULL keys group together). + expect(gap!.severity).toBe("suggestion") + expect(gap!.body).toContain("still catches duplicates") expect(gap!.model).toBe("mrt_x") expect(gap!.column).toBe("price_start_time") // Contract is enforced → recommendation should point at `constraints:`. diff --git a/packages/opencode/test/altimate/review-false-positives.test.ts b/packages/opencode/test/altimate/review-false-positives.test.ts new file mode 100644 index 0000000000..ea9be5d018 --- /dev/null +++ b/packages/opencode/test/altimate/review-false-positives.test.ts @@ -0,0 +1,399 @@ +import { describe, test, expect } from "bun:test" +import { runReview, definedVars, inlineConfigSplats, type ReviewRunner } from "../../src/altimate/review/orchestrate" +import { detectMacroPatterns, detectModelPatterns, detectSchemaYmlPatterns } from "../../src/altimate/review/dbt-patterns" +import { CATALOG, ruleMatches, subqueryDepth, caseDepth, finalSelectStarFromRelation } from "../../src/altimate/review/rule-catalog" +import { incrementalFilter } from "../../src/altimate/review/incremental-semantics" +import { DEFAULT_RUBRIC } from "../../src/altimate/review/rubric" +import { DEFAULT_REVIEW_CONFIG } from "../../src/altimate/review/config" + +/** + * False positives found by replaying real PRs from public dbt repos (generic shapes, + * not tied to any one project). Each case checks both directions: the wrong finding + * is gone, and the real one still fires. + */ + +const rule = (id: string) => CATALOG.find((r) => r.id === id)! +const fires = (id: string, sql: string, added = sql.split("\n")) => + ruleMatches(rule(id), { file: { path: "models/m.sql", status: "modified" }, newSql: sql, added, removed: [] }) + +describe("var with no default", () => { + test("vars defined in dbt_project.yml are collected, top level and project-scoped", () => { + const vars = definedVars("name: shop\nvars:\n start_date: '2020-01-01'\n shop:\n window_days: 3\n") + expect([...vars].sort()).toEqual(["shop", "start_date", "window_days"]) + expect(definedVars(undefined).size).toBe(0) + expect(definedVars("vars: [oops").size).toBe(0) + }) + + const runner = (rules: { rule: string; message: string }[]) => + ({ + async impact() { + return { hasManifest: true, severity: "SAFE", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + async grade() { + return { grade: "B" } + }, + async check() { + return { issues: [], ran: true } + }, + async detectPii() { + return { columns: [] } + }, + async dbtConfigLint() { + return rules.map((r) => ({ ...r, code: "DBT005", severity: "warning" })) + }, + }) as unknown as ReviewRunner + + const review = async (projectYml: string, model: string) => { + const env = await runReview({ + changedFiles: [{ path: "models/m.sql", status: "added", diff: model.split("\n").map((l) => "+" + l).join("\n") }], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["dbt_patterns"] }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner: runner([{ rule: "var_no_default", message: "`var('start_date')` has no default" }]), + getContent: async (f) => (f === "dbt_project.yml" ? projectYml : model), + }) + return env.findings.filter((f) => /var no default/.test(f.title)) + } + + test("defined in dbt_project.yml, or defaulted elsewhere in the model: silent", async () => { + expect(await review("vars:\n start_date: '2020-01-01'\n", "select * from t where d > '{{ var(\"start_date\") }}'")).toHaveLength(0) + expect(await review("name: x\n", "{% if var('start_date', none) %}select '{{ var(\"start_date\") }}'{% endif %}")).toHaveLength(0) + }) + + test("not defined anywhere: still reported", async () => { + expect(await review("name: x\n", "select * from t where d > '{{ var(\"start_date\") }}'")).toHaveLength(1) + }) +}) + +describe("structural catalog checks", () => { + test("nested subqueries count real nesting, not sibling CTEs", () => { + const ctes = "with a as (select 1), b as (select 2), c as (select 3)\nselect * from a join b on true join c on true" + expect(subqueryDepth(ctes)).toBe(1) + expect(fires("nested-subqueries-deep", ctes)).toBe(false) + expect(fires("nested-subqueries-deep", "select (select (select (select 1)))")).toBe(true) + }) + + test("nested CASE means a CASE inside a CASE, not two CASEs in one model", () => { + const two = "select\n case when a then 1 end as x,\n case when b then 2 end as y\nfrom t" + expect(caseDepth(two)).toBe(1) + expect(fires("nested-case", two)).toBe(false) + expect(fires("nested-case", "select case when a then case when b then 1 end end from t")).toBe(true) + }) + + test("CASE without END checks the whole model, so a multi-line CASE is fine", () => { + const multi = "select\n case\n when a > 0 then 'pos'\n else 'neg'\n end as sign\nfrom t" + expect(fires("case-when-no-end", multi, [" case", " when a > 0 then 'pos'"])).toBe(false) + expect(fires("case-when-no-end", "select case when a > 0 then 1 from t")).toBe(true) + // Jinja branches can emit a CASE per branch with one shared END: abstain. + const jinja = "select {% if x %}case when a then 1{% else %}case when b then 2{% endif %} end from t" + expect(fires("case-when-no-end", jinja)).toBe(false) + }) + + test("SELECT * in final projection only when it reads straight from another relation", () => { + const viaCte = "with final as (\n select id, name from {{ ref('x') }}\n)\nselect * from final" + expect(finalSelectStarFromRelation(viaCte)).toBe(false) + expect(fires("select-star-in-cte-final", viaCte)).toBe(false) + const importCte = "with src as (\n select * from {{ source('s', 't') }}\n)\nselect id from src" + expect(fires("select-star-in-cte-final", importCte)).toBe(false) + expect(fires("select-star-in-cte-final", "select * from {{ ref('orders') }}")).toBe(true) + }) +}) + +describe("function on a column in WHERE", () => { + const run = (line: string) => + detectModelPatterns({ path: "models/m.sql", status: "modified", diff: `+${line}` }, `select * from t\n${line}`, DEFAULT_RUBRIC).filter((f) => + /partition pruning/.test(f.title), + ) + test("a function around a literal, a var or the clock: silent", () => { + expect(run("where is_deleted = cast(0 as boolean)")).toHaveLength(0) + expect(run("and event_date >= cast('{{ var(\"start\") }}' as date)")).toHaveLength(0) + expect(run("and event_date >= date_trunc('day', current_date)")).toHaveLength(0) + }) + test("a function around a column: still reported", () => { + expect(run("where year(event_date) = 2024")).toHaveLength(1) + expect(run("and date_trunc('day', created_at) = '2024-01-01'")).toHaveLength(1) + expect(run("and cast(o.created_at as date) = current_date")).toHaveLength(1) + }) +}) + +describe("data test removed vs moved with a column rename", () => { + const yml = (col: string) => `version: 2 +models: + - name: dim_ports + columns: + - name: ${col} + data_tests: + - unique + - not_null +` + const removed = (oldCol: string, newContent: string) => + detectSchemaYmlPatterns({ path: "models/marts/_dim.yml", status: "modified", diff: undefined }, DEFAULT_RUBRIC, { + oldContent: yml(oldCol), + newContent, + }).filter((f) => /test removed/.test(f.title)) + + test("tests that moved to the renamed column are not removals", () => { + expect(removed("port_sk", yml("port_key"))).toHaveLength(0) + }) + test("a test dropped for real is still reported", () => { + const dropped = "version: 2\nmodels:\n - name: dim_ports\n columns:\n - name: port_sk\n data_tests:\n - not_null\n" + expect(removed("port_sk", dropped).map((f) => f.title)).toEqual(["_dim.yml: unique test removed from dim_ports.port_sk"]) + }) +}) + +describe("incremental filter = predicates of the is_incremental() branch", () => { + const model = (selectExpr: string, where: string) => `with src as (select * from {{ ref('raw') }}) +{% if is_incremental() %} +, merged as ( + select n.id, ${selectExpr} as tags + from src as n left join {{ this }} as b on n.id = b.id + ${where} +) +{% else %} +, merged as (select id, tags from src) +{% endif %} +select * from merged` + + test("a select-list edit inside the block is not a filter change", () => { + expect(incrementalFilter(model("array_distinct(n.tags)", "where n.ts > (select max(ts) from {{ this }})"))).toBe( + incrementalFilter(model("{{ array_distinct('n.tags') }}", "where n.ts > (select max(ts) from {{ this }})")), + ) + }) + test("a changed predicate is", () => { + expect(incrementalFilter(model("n.tags", "where n.ts > (select max(ts) from {{ this }})"))).not.toBe( + incrementalFilter(model("n.tags", "where n.ts >= (select max(ts) - interval 3 day from {{ this }})")), + ) + }) + test("a bare predicate fragment and the else branch", () => { + const frag = (op: string) => `select * from t\n{% if is_incremental() %}\n where updated_at ${op} (select max(updated_at) from {{ this }})\n{% else %}\n where true\n{% endif %}` + expect(incrementalFilter(frag(">"))).not.toBe(incrementalFilter(frag(">="))) + const elseOnly = (w: string) => `select * from t\n{% if is_incremental() %}\n where u > 1\n{% else %}\n ${w}\n{% endif %}` + expect(incrementalFilter(elseOnly("where a"))).toBe(incrementalFilter(elseOnly("where b"))) + }) +}) + +describe("config shared through a dict splat", () => { + test("a flat literal dict is inlined into config(**dict)", () => { + const sql = `{% set meta_config = {\n "materialized": "incremental",\n "unique_key": ["day", "code"],\n} %}\n{{ config(meta=meta_config, **meta_config) }}\nselect 1` + const out = inlineConfigSplats(sql) + expect(out).toContain(`materialized= "incremental"`) + expect(out).toContain(`unique_key= ["day", "code"]`) + expect(out).not.toContain("**meta_config") + }) + test("no literal dict: untouched", () => { + const sql = "{{ config(**get_config()) }}\nselect 1" + expect(inlineConfigSplats(sql)).toBe(sql) + }) +}) + +describe("changed macros are reviewed", () => { + const macro = (body: string) => ({ path: "macros/m.sql", status: "modified" as const, diff: body.split("\n").map((l) => "+" + l).join("\n") }) + const titles = (body: string, vars = new Set()) => + detectMacroPatterns(macro(body), body, DEFAULT_RUBRIC, vars).map((f) => f.title) + + test("SQL shapes that hold in a template fire", () => { + expect(titles("{% macro m(t) %}\nselect top 1 x from {{ t }}\n{% endmacro %}").some((t) => /TOP/i.test(t))).toBe(true) + expect(titles("{% macro m(a, b) %}\nselect * from {{ a }} as a join {{ b }} as b\n on a.id = b.id\n or a.alt = b.alt\n{% endmacro %}").some((t) => /OR in a join/.test(t))).toBe(true) + }) + test("var with no default: reported unless the project sets it or it is defaulted elsewhere", () => { + const body = "{% macro m() %}\n{% if var('dbt_artifacts_exclude_all_results') %}x{% endif %}\n{% endmacro %}" + expect(titles(body).some((t) => /dbt_artifacts_exclude_all_results/.test(t))).toBe(true) + expect(titles(body, new Set(["dbt_artifacts_exclude_all_results"]))).toHaveLength(0) + expect(titles(body + "\n{{ var('dbt_artifacts_exclude_all_results', false) }}")).toHaveLength(0) + }) + test("model-only rules stay off macros", () => { + expect(titles("{% macro m() %}\nselect * from {{ ref('x') }}\n{% endmacro %}")).toHaveLength(0) + }) +}) + +describe("incremental unique_key vs the declared uniqueness test", () => { + const review = async (uniqueKey: unknown, declared: string[][]) => { + const runner = { + async impact() { + return { hasManifest: true, severity: "SAFE", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + async grade() { + return { grade: "B" } + }, + async check() { + return { issues: [], ran: true } + }, + async detectPii() { + return { columns: [] } + }, + async modelConfig() { + return { materialized: "incremental", unique_key: uniqueKey } + }, + async declaredUniqueKeys() { + return declared + }, + } as unknown as ReviewRunner + const env = await runReview({ + changedFiles: [{ path: "models/int_latest.sql", status: "added", diff: "+select 1" }], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["dbt_patterns"] }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + getContent: async () => "select 1", + }) + return env.findings.filter((f) => /unique_key differs/.test(f.title)) + } + test("a merge key with a column the test lacks is reported", async () => { + const [f] = await review(["charge_point_id", "connector_id", "port_id"], [["charge_point_id", "connector_id"]]) + expect(f.body).toContain("[charge_point_id, connector_id, port_id]") + }) + test("same set (any order or case), or no test: silent", async () => { + expect(await review(["B", "a"], [["a", "b"]])).toHaveLength(0) + expect(await review("id", [["ID"]])).toHaveLength(0) + expect(await review(["a", "b"], [])).toHaveLength(0) + }) + test("declared key plus date/partition columns: silent; plus an entity column or a narrower key: reported", async () => { + expect(await review(["blockchain", "tx_hash", "evt_index", "block_month"], [["blockchain", "tx_hash", "evt_index"]])).toHaveLength(0) + expect(await review(["course_pk", "effective_date"], [["course_pk"]])).toHaveLength(0) + expect((await review(["address", "chain_id"], [["address"]]))[0].severity).toBe("suggestion") + expect((await review(["date_start", "operator"], [["blockchain", "date_start", "operator"]]))[0].severity).toBe("warning") + }) +}) + +describe("LEFT JOIN key vs the joined model's declared unique key", () => { + const review = async (sql: string, declared: string[][]) => { + const runner = { + async impact() { + return { hasManifest: true, severity: "SAFE", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + async grade() { + return { grade: "B" } + }, + async check() { + return { issues: [], ran: true } + }, + async detectPii() { + return { columns: [] } + }, + async modelConfig() { + return { materialized: "table" } + }, + async declaredUniqueKeys(model: string) { + return model === "dim_course_run" ? declared : [] + }, + } as unknown as ReviewRunner + const env = await runReview({ + changedFiles: [{ path: "models/fct.sql", status: "added", diff: sql.split("\n").map((l) => "+" + l).join("\n") }], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["dbt_patterns"] }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + getContent: async () => sql, + }) + return env.findings.filter((f) => /declared unique key/.test(f.title)) + } + const join = (kind: string) => `select * from e\n${kind} {{ ref('dim_course_run') }} as cr on e.courserun_readable_id = cr.courserun_readable_id` + test("an SCD2 dimension joined without is_current: reported", async () => { + const [f] = await review(join("left join"), [["courserun_pk"], ["courserun_readable_id", "is_current"]]) + expect(f.body).toContain("[courserun_readable_id, is_current]") + }) + test("join on the declared key, an inner join, or no declared key: silent", async () => { + expect(await review(join("left join"), [["courserun_readable_id"]])).toHaveLength(0) + expect(await review(join("join"), [["courserun_pk"]])).toHaveLength(0) + expect(await review(join("left join"), [])).toHaveLength(0) + }) +}) + +describe("OR in a join condition", () => { + const sql = (on: string) => `select * from a\njoin b\n on ${on}` + test("alternative keys or overlapping ranges: reported", () => { + expect(fires("join-or-condition", sql("(a.courserun_fk = b.courserun_fk\n or a.program_fk = b.program_fk)\n and a.platform_fk = b.platform_fk"))).toBe(true) + expect(fires("join-or-condition", sql("a.id = b.id\n and (\n a.start_date between b.entrydate and b.exitdate\n or a.end_date between b.entrydate and b.exitdate\n )"))).toBe(true) + }) + test("an open-ended interval or null-safe key guard: silent", () => { + expect(fires("join-or-condition", sql("d.ts >= lp.day and (lp.next_update_day is null or d.ts < lp.next_update_day)"))).toBe(false) + expect(fires("join-or-condition", sql("dt.day >= date(t.valid_from)\n and (dt.day < date(t.valid_to)\n or t.valid_to is null)"))).toBe(false) + expect(fires("join-or-condition", sql("a.k = b.k or (a.k is null and b.k is null)"))).toBe(false) + }) +}) + +describe("engine check results in a dbt model", () => { + const review = async (issues: { rule: string; message: string; severity?: string }[]) => { + const runner = { + async impact() { + return { hasManifest: true, severity: "SAFE", directCount: 0, transitiveCount: 0, testCount: 0 } + }, + async grade() { + return { grade: "B" } + }, + async check() { + return { issues: issues.map((i) => ({ severity: "error", ...i })), ran: true } + }, + async detectPii() { + return { columns: [] } + }, + } as unknown as ReviewRunner + const env = await runReview({ + changedFiles: [{ path: "models/m.sql", status: "modified", diff: "+select 1" }], + config: { ...DEFAULT_REVIEW_CONFIG, reviewers: ["sql_quality"] }, + rubric: DEFAULT_RUBRIC, + mode: "comment", + runner, + getContent: async () => "select 1", + }) + return env.findings.filter((f) => f.evidence?.tool === "altimate_core.check") + } + test("injection rules and engine planner gaps: silent", async () => { + expect( + await review([ + { rule: "union_injection", message: "UNION SELECT injection detected" }, + { rule: "comment_injection", message: "Comment injection detected (line comment)" }, + { rule: "unbalanced_quote", message: "Unbalanced string quote (possible injection breakout)" }, + { rule: "validate", message: "Error during planning: Cannot coerce arithmetic expression Int64 * Interval(MonthDayNano)" }, + { rule: "validate", message: "This feature is not implemented: Arrays with unspecified type is not supported" }, + ]), + ).toHaveLength(0) + }) + test("a parser syntax error is a suggestion; an unresolved column stays a warning", async () => { + const fs = await review([ + { rule: "validate", message: "Syntax error: sql parser error: Expected: ), found: omit" }, + { rule: "validate", message: "Column 'group_name' not found in table 'run_results'" }, + ]) + expect(fs.find((f) => /Syntax/.test(f.body))?.severity).toBe("suggestion") + expect(fs.find((f) => /not found/.test(f.body))?.severity).toBe("warning") + }) +}) + +describe("whole-model shapes need the shape itself, not its words anywhere", () => { + test("SELECT * in a UNION: only a branch that is select *", () => { + expect(fires("union-select-star", "with a as (select id from x union all select id from y)\nselect * from a")).toBe(false) + expect(fires("union-select-star", "select * from {{ ref('a') }}\nunion all\nselect * from {{ ref('b') }}")).toBe(true) + }) + test("ORDER BY in a CTE: not a window or aggregate ORDER BY, not a top-N", () => { + expect(fires("order-by-in-cte", "with x as (\n select a, row_number() over (partition by k order by ts) as rn from t\n)\nselect * from x")).toBe(false) + expect(fires("order-by-in-cte", "with x as (\n select a from t\n order by a\n limit 10\n)\nselect * from x")).toBe(false) + expect(fires("order-by-in-cte", "with x as (\n select a from t\n order by a\n)\nselect * from x")).toBe(true) + }) + test("unqualified column: a bare column on a changed line of a joining SELECT", () => { + const sql = "select\n a.id,\n name\nfrom a\njoin b on a.k = b.k" + expect(fires("unqualified-column-multi-table", sql, [" name"])).toBe(true) + expect(fires("unqualified-column-multi-table", "select\n a.id,\n b.name\nfrom a\njoin b on a.k = b.k", [" b.name"])).toBe(false) + // A bare column in a SELECT without a join, while another block joins with qualified columns. + expect(fires("unqualified-column-multi-table", "with s as (select id, name from t)\nselect s.id, u.x from s join u on s.id = u.id", ["with s as (select id, name from t)"])).toBe(false) + }) +}) + +describe("window function without PARTITION BY", () => { + const titles = (sql: string) => + detectModelPatterns({ path: "models/m.sql", status: "modified", diff: sql.split("\n").map((l) => "+" + l).join("\n") }, sql, DEFAULT_RUBRIC).map((f) => f.title) + test("PARTITION BY on the line after `over (`: silent", () => { + expect(titles("select\n row_number() over (\n partition by k\n order by ts\n ) as rn\nfrom t").some((t) => /PARTITION BY/.test(t))).toBe(false) + }) + test("a whole-table window: reported", () => { + expect(titles("select\n row_number() over (\n order by ts\n ) as rn\nfrom t").some((t) => /PARTITION BY/.test(t))).toBe(true) + }) +}) + +describe("config dict with a nested dict", () => { + test("a BigQuery partition_by dict inside the splat dict keeps its own keys", () => { + const sql = `{% set meta_config = {\n "materialized": "incremental",\n "batch_size": batch_size,\n "partition_by": {"field": "day", "granularity": "day"},\n} %}\n{{ config(meta=meta_config, **meta_config) }}\nselect 1` + const out = inlineConfigSplats(sql) + expect(out).toContain(`materialized= "incremental"`) + expect(out).toContain(`partition_by= {"field": "day", "granularity": "day"}`) + }) +}) diff --git a/packages/opencode/test/altimate/review-noise.test.ts b/packages/opencode/test/altimate/review-noise.test.ts index dfd0208133..2b9a0349d2 100644 --- a/packages/opencode/test/altimate/review-noise.test.ts +++ b/packages/opencode/test/altimate/review-noise.test.ts @@ -9,7 +9,7 @@ import { } from "../../src/altimate/review/orchestrate" import { DEFAULT_REVIEW_CONFIG } from "../../src/altimate/review/config" import { DEFAULT_RUBRIC } from "../../src/altimate/review/rubric" -import { makeFinding, type Finding } from "../../src/altimate/review/finding" +import { makeFinding, mergeRepeats, type Finding } from "../../src/altimate/review/finding" import { findingMarker, inlineComments, @@ -144,10 +144,12 @@ describe("summary grouping and budget", () => { expect(md).toContain("4 findings across 4 model(s): `a`, `b`, `c`, `d`") }) - test("two occurrences stay as separate bullets", () => { + test("two occurrences group; a single one stays its own bullet", () => { const fs = ["a", "b"].map((m) => finding({ model: m, file: `models/${m}.sql`, title: `${m}: new model has no uniqueness/grain test` })) const md = renderSummary(buildEnvelope({ findings: fs, tier: "lite", mode: "comment" })) - expect(md.match(/no uniqueness\/grain test/g)?.length).toBe(2) + expect(md).toContain("2 findings across 2 model(s)") + const one = renderSummary(buildEnvelope({ findings: fs.slice(0, 1), tier: "lite", mode: "comment" })) + expect(one).toContain("a: new model has no uniqueness/grain test") }) test("backticked column names do not split a group", () => { @@ -215,6 +217,13 @@ describe("portability noise", () => { expect(isSuppressiblePortability(untagged, "snowflake")).toBe(true) }) + test("a package with macros for several adapters keeps advice that fails on any of them", () => { + expect(isSuppressiblePortability(portability, "postgres", ["duckdb"])).toBe(true) + expect(isSuppressiblePortability(portability, "postgres", ["duckdb", "snowflake"])).toBe(false) + const untagged = { ...portability, evidence: { tool: "rule-catalog", result: { rule: "pivot-portability", portability: true } } } + expect(isSuppressiblePortability(untagged, "postgres", ["snowflake"])).toBe(true) + }) + test("catalog portability advice is recognised; core's dialect-aware check is not", () => { expect(isCatalogPortability(portability)).toBe(true) expect(isCatalogPortability(coreMissing)).toBe(false) @@ -269,3 +278,27 @@ describe("moved files", () => { expect(renderSummary(env)).toContain("`+schema`") }) }) + +describe("repeats of one rule within a file", () => { + const grain = (col: string, severity: "suggestion" | "critical" = "suggestion") => + makeFinding({ + severity, + category: "test_coverage", + title: `_schema.yml: grain column \`${col}\` in unique_combination_of_columns lacks \`not_null\``, + body: `\`${col}\` can be NULL.`, + file: "models/_schema.yml", + ruleKey: `grain:${col}`, + }) + test("merge into one finding listing every instance", () => { + const [m, ...rest] = mergeRepeats([grain("tx_hash"), grain("evt_index"), grain("block_month")]) + expect(rest).toHaveLength(0) + expect(m.title).toBe("_schema.yml: grain column `block_month` in unique_combination_of_columns lacks `not_null` (+2 more)") + expect(m.body).toContain("- grain column `evt_index`") + expect(m.body).toContain("- grain column `tx_hash`") + }) + test("other files, other rules, and critical findings stay separate", () => { + const other = { ...grain("tx_hash"), id: "x", file: "models/other.yml" } + const crit = [grain("a", "critical"), { ...grain("b", "critical"), id: "y" }] + expect(mergeRepeats([grain("tx_hash"), other, ...crit])).toHaveLength(4) + }) +}) diff --git a/packages/opencode/test/altimate/review-project-rules.test.ts b/packages/opencode/test/altimate/review-project-rules.test.ts new file mode 100644 index 0000000000..b54514f6c4 --- /dev/null +++ b/packages/opencode/test/altimate/review-project-rules.test.ts @@ -0,0 +1,232 @@ +import { describe, test, expect } from "bun:test" +import { + dbtProjectFindings, + jinjaBalance, + jinjaFindings, + joinTargets, + literalNullColumns, + missingRefFindings, + modelConfigFindings, + packagesFindings, + testsOnNullColumns, +} from "../../src/altimate/review/project-rules" +import { CATALOG, ruleMatches, leftJoinNullifiedByWhere } from "../../src/altimate/review/rule-catalog" +import { detectMacroPatterns } from "../../src/altimate/review/dbt-patterns" +import { DEFAULT_RUBRIC } from "../../src/altimate/review/rubric" +import type { ChangedFile } from "../../src/altimate/review/diff-filter" + +/** + * Rules written from defects human reviewers found on public dbt PRs that a + * deterministic check can catch. Each test pairs the defect's shape with the + * nearby shape that must stay silent. + */ + +const plus = (s: string) => s.split("\n").map((l) => "+" + l).join("\n") +const file = (path: string, added: string, status: ChangedFile["status"] = "modified"): ChangedFile => ({ path, status, diff: plus(added) }) +const titles = (fs: { title: string }[]) => fs.map((f) => f.title) +const rule = (id: string) => CATALOG.find((r) => r.id === id)! +const fires = (id: string, sql: string, added = sql.split("\n"), removed: string[] = []) => + ruleMatches(rule(id), { file: { path: "models/m.sql", status: "modified" }, newSql: sql, added, removed }) + +describe("packages.yml", () => { + const yml = (rev: string) => `packages:\n - git: https://github.com/fivetran/dbt_stripe_source.git\n revision: ${rev}\n` + test("a git branch pin this PR touched is reported", () => { + expect(titles(packagesFindings(file("packages.yml", " revision: feature/new-cols"), yml("feature/new-cols")))).toEqual([ + "packages: package installed from a git branch", + ]) + }) + test("a release tag, a commit sha, a hub package, or an untouched pin: silent", () => { + expect(packagesFindings(file("packages.yml", " revision: v0.15.1"), yml("v0.15.1"))).toHaveLength(0) + expect(packagesFindings(file("packages.yml", " revision: 1f2e3d4c5b6a79881f2e3d4c5b6a79881f2e3d4c"), yml("1f2e3d4c5b6a79881f2e3d4c5b6a79881f2e3d4c"))).toHaveLength(0) + expect(packagesFindings(file("packages.yml", ' version: [">=0.15.0", "<0.16.0"]'), 'packages:\n - package: fivetran/stripe_source\n version: [">=0.15.0", "<0.16.0"]\n')).toHaveLength(0) + expect(packagesFindings(file("packages.yml", " - package: dbt-labs/dbt_utils"), yml("main"))).toHaveLength(0) + }) +}) + +describe("dbt_project.yml", () => { + const f = (added: string) => file("dbt_project.yml", added) + test("version going down, a duplicate grantee, a newly disabled resource", () => { + expect(titles(dbtProjectFindings(f("version: '1.0.0'"), "name: x\nversion: '1.3.0'\n", "name: x\nversion: '1.0.0'\n"))).toContain("dbt_project: project version goes down") + const grants = "models:\n shop:\n +grants:\n select: ['bi', 'reverse_etl', 'bi']\n" + expect(titles(dbtProjectFindings(f(" select: ['bi', 'reverse_etl', 'bi']"), "models:\n shop:\n +grants:\n select: ['bi']\n", grants))[0]).toContain("duplicate grantee") + expect(titles(dbtProjectFindings(f(" +enabled: false"), "models:\n shop: {}\n", "models:\n shop:\n legacy:\n +enabled: false\n"))).toContain("dbt_project: resources disabled in dbt_project.yml") + }) + test("version going up, distinct grantees: silent", () => { + expect(dbtProjectFindings(f("version: '2.0.0'"), "version: '1.3.0'\n", "version: '2.0.0'\n")).toHaveLength(0) + expect(dbtProjectFindings(f(" select: ['bi', 'reverse_etl']"), "models: {}\n", "models:\n shop:\n +grants:\n select: ['bi', 'reverse_etl']\n")).toHaveLength(0) + }) +}) + +describe("Jinja structure", () => { + test("a stray endif, a missing endmacro, a macro without a parameter list", () => { + expect(jinjaBalance("{% if a %}x{% endif %}\n{% endif -%}")).toContain("closes no open") + expect(jinjaBalance("{% macro redshift__type_string() %}varchar{% macro x() %}y{% endmacro %}")).toContain("never closed") + expect(jinjaBalance("{% macro postgres__upload_dbt_invocation %}x{% endmacro %}")).toContain("no parameter list") + }) + test("set blocks, inline sets, raw blocks and whitespace control are balanced", () => { + expect(jinjaBalance("{%- set q -%}select 1{%- endset -%}{% set y = 2 %}{% raw %}{% endif %}{% endraw %}{%- for c in cols -%}{{ c }}{%- endfor %}")).toBeUndefined() + }) + test("reported only when this change broke it", () => { + const broken = "{% if a %}x{% endif %}{% endif %}" + expect(jinjaFindings(file("macros/m.sql", "{% endif %}"), "{% if a %}x{% endif %}", broken).map((f) => f.title)).toContain("m: Jinja block structure is broken") + expect(jinjaFindings(file("macros/m.sql", "x"), broken, broken + "x").some((f) => /Jinja/.test(f.title))).toBe(false) + }) + test("a macro parameter inserted before existing ones; appended is fine", () => { + const old = "{% macro ghost(datatype, col_name, alias=none) %}{% endmacro %}" + const inserted = "{% macro ghost(datatype, col_name, precision=none, scale=none, alias=none) %}{% endmacro %}" + const appended = "{% macro ghost(datatype, col_name, alias=none, precision=none) %}{% endmacro %}" + expect(titles(jinjaFindings(file("macros/ghost.sql", inserted), old, inserted))).toContain("ghost: `ghost` parameters moved position") + expect(jinjaFindings(file("macros/ghost.sql", appended), old, appended).some((f) => /moved/.test(f.title))).toBe(false) + }) + test("a var() key renamed without the old key as a fallback", () => { + const old = "{%- set concat = var('concat_string', '||') -%}" + const renamed = "{%- set concat = var('datavault4dbt.concat_string', '||') -%}" + const fallback = "{%- set concat = var('datavault4dbt.concat_string', var('concat_string', '||')) -%}" + expect(titles(jinjaFindings(file("macros/hash.sql", renamed), old, renamed))).toContain("hash: var() key renamed without a fallback") + expect(jinjaFindings(file("macros/hash.sql", fallback), old, fallback).some((f) => /renamed/.test(f.title))).toBe(false) + + const old2 = "select * from {{ var('issue_merged') }}" + const handedOn = "{{ github_union_connections(connection_dictionary=var('github_sources'), single_table_name='issue_merged') }}" + expect(jinjaFindings(file("models/tmp/t.sql", handedOn), old2, handedOn).some((f) => /renamed/.test(f.title))).toBe(false) + }) + test("code its own comment marks temporary", () => { + const sql = "where mod(abs(xxhash64(address)), 1000) = 0 -- TEMP CI VALIDATION SAMPLE - remove before merge" + expect(titles(jinjaFindings(file("models/m.sql", sql), "select 1", sql))).toContain("m: code marked temporary is part of the change") + const ok = "where mod(abs(xxhash64(address)), 1000) = 0 -- one in a thousand, as a template example" + expect(jinjaFindings(file("models/m.sql", ok), "select 1", ok).some((f) => /temporary/.test(f.title))).toBe(false) + }) +}) + +describe("model config combinations", () => { + const run = (sql: string, dialect = "bigquery") => titles(modelConfigFindings(file("models/m.sql", sql), sql, dialect)) + test("unique_key on a table", () => { + expect(run("{{ config(materialized='table', unique_key='id') }}\nselect 1")).toContain("m: `unique_key` has no effect on a table") + expect(run("{{ config(materialized='incremental', unique_key='id') }}\nselect 1")).not.toContain("m: `unique_key` has no effect on a table") + }) + test("microbatch batch_size that can differ from the partition granularity", () => { + const sql = `{{ config(materialized='incremental', incremental_strategy='microbatch', event_time='closed_at',\n batch_size=('year' if flags.FULL_REFRESH else 'day'),\n partition_by={'field': 'day', 'data_type': 'date', 'granularity': 'day'}) }}\nselect 1` + expect(run(sql, "unknown")).toContain("m: microbatch `batch_size` differs from the partition granularity") + expect(run(sql.replace("'year' if flags.FULL_REFRESH else 'day'", "'day'"))).not.toContain("m: microbatch `batch_size` differs from the partition granularity") + }) + test("partition column missing from the merge key, and an incremental filter that doesn't prune", () => { + const sql = `{{ config(materialized='incremental', incremental_strategy='merge', partition_by=['block_month'], unique_key=['tx_hash', 'evt_index']) }}\nselect * from t\n{% if is_incremental() %}\nwhere block_time >= now() - interval '1' day\n{% endif %}` + const t = run(sql, "trino") + expect(t).toContain("m: partition column is not in the merge `unique_key`") + expect(t).toContain("m: incremental filter doesn't use the partition column") + const ok = sql.replace("['tx_hash', 'evt_index']", "['block_month', 'tx_hash', 'evt_index']").replace("where block_time", "where block_month >= date_trunc('month', now()) and block_time") + expect(run(ok, "trino")).toHaveLength(0) + }) + test("merging on a key while filtering on the creation time misses updates", () => { + const sql = `{{ config(materialized='incremental', unique_key='enrollment_key') }}\nselect * from e\n{% if is_incremental() %}\nwhere enrollment_created_on > (select max(enrollment_created_on) from {{ this }})\n{% endif %}` + expect(run(sql, "trino")).toContain("m: incremental filter reads new rows only, not updated ones") + expect(run(sql.replace(/enrollment_created_on/g, "enrollment_updated_on"), "trino")).not.toContain("m: incremental filter reads new rows only, not updated ones") + }) +}) + +describe("ref() targets", () => { + const names = new Set(["stg_assets", "balancer_v3_avalanche_c_er4626_token_mapping", "dim_customer"]) + test("an unknown name is reported, with a near match when there is one", () => { + const [f] = missingRefFindings(file("models/m.sql", "from {{ ref('recognized_assets') }}"), names) + expect(f.title).toBe("m: `ref('recognized_assets')` points at no model") + const [typo] = missingRefFindings(file("models/m.sql", "from {{ ref('balancer_v3_avalanche_c_erc4626_token_mapping') }}"), names) + expect(typo.body).toContain("Did you mean `balancer_v3_avalanche_c_er4626_token_mapping`") + }) + test("existing, versioned and package-qualified refs: silent", () => { + expect(missingRefFindings(file("models/m.sql", "from {{ ref('stg_assets') }} join {{ ref('dim_customer', v=2) }}"), names)).toHaveLength(0) + expect(missingRefFindings(file("models/m.sql", "from {{ ref('dbt_utils', 'some_model') }}"), names)).toHaveLength(0) + }) +}) + +describe("join keys", () => { + test("a ref joined directly and through a pass-through CTE", () => { + const sql = `with dim_course_run as (\n select courserun_pk, courserun_readable_id from {{ ref('dim_course_run') }}\n)\nselect *\nfrom e\nleft join dim_course_run as cr\n on e.courserun_readable_id = cr.courserun_readable_id\njoin {{ ref('orders') }} o on o.order_id = e.order_id` + const t = joinTargets(sql, ["left join dim_course_run as cr", "join {{ ref('orders') }} o on o.order_id = e.order_id"]) + expect(t.map((x) => [x.model, x.keys, x.left])).toEqual([ + ["dim_course_run", ["courserun_readable_id"], true], + ["orders", ["order_id"], false], + ]) + }) + test("keys inside casts are read; range, OR and Jinja conditions skip the join", () => { + const cast = `select * from o\nleft join {{ ref('dim_product') }} as p on cast(o.product_id as varchar) = cast(p.source_product_id as varchar) and o.platform = p.platform` + expect(joinTargets(cast, ["left join {{ ref('dim_product') }} as p"]).map((x) => x.keys)).toEqual([["source_product_id", "platform"]]) + const skipped = [ + `select * from w\nleft join {{ ref('terms') }} as t on w.year = t.year and w.day between t.start_date and t.end_date`, + `select * from w\nleft join {{ ref('terms') }} as t on w.year = t.year and w.day >= t.start_date`, + `select * from w\nleft join {{ ref('terms') }} as t on w.id = t.id or w.alt_id = t.id`, + `select * from w\nleft join {{ ref('terms') }} as t on w.id = t.id and {{ union_dataset_join_clause('w', 't') }}`, + ] + for (const sql of skipped) expect(joinTargets(sql, ["left join {{ ref('terms') }} as t"])).toHaveLength(0) + }) + test("a CTE that aggregates changes the grain, and an unchanged join is not this PR's: skipped", () => { + const sql = `with o as (\n select order_id, count(*) n from {{ ref('orders') }} group by 1\n)\nselect * from e join o on o.order_id = e.order_id\njoin {{ ref('users') }} u on u.id = e.user_id` + expect(joinTargets(sql, ["select * from e join o on o.order_id = e.order_id"])).toHaveLength(0) + }) +}) + +describe("tests on columns the SQL sets to NULL", () => { + test("literal NULL projections", () => { + expect([...literalNullColumns("select id, null as platform_fk, cast(null as varchar) as note, null::int as n, coalesce(x, null) as y")]).toEqual(["platform_fk", "note", "n"]) + }) + test("a not_null/relationships test added on such a column", () => { + const yml = "models:\n - name: tfact_order\n columns:\n - name: platform_fk\n data_tests:\n - not_null\n - relationships:\n to: ref('dim_platform')\n field: platform_pk\n - name: order_id\n data_tests: [not_null]\n" + const sql = new Map([["tfact_order", "select order_id, null as platform_fk from o"]]) + const fs = testsOnNullColumns(file("models/_fact_tables.yml", " - not_null"), yml, sql) + expect(fs.map((f) => f.title)).toEqual(["_fact_tables: `tfact_order.platform_fk` is tested but the model sets it to NULL"]) + }) +}) + +describe("SQL shapes from the catalog", () => { + test("LEFT JOIN filtered in WHERE; an IS NULL anti-join or a predicate in ON is fine", () => { + expect(leftJoinNullifiedByWhere("select * from uc left join {{ ref('cd') }} as cd on uc.id = cd.id where cd.kind = ''")).toBe(true) + expect(leftJoinNullifiedByWhere("select * from s left join g on s.id = g.id where g.id is not null")).toBe(true) + expect(leftJoinNullifiedByWhere("select * from s left join g on s.id = g.id where g.id is null")).toBe(false) + expect(leftJoinNullifiedByWhere("select * from s left join g on s.id = g.id and g.kind = '' where s.x = 1")).toBe(false) + expect(leftJoinNullifiedByWhere("select * from s left join g on s.id = g.id where (g.kind = '' or g.kind is null)")).toBe(false) + }) + test("case-folded on one side of a join only", () => { + expect(fires("join-case-folded-one-side", "select * from c\njoin u\n on lower(c.user_email) = u.email")).toBe(true) + expect(fires("join-case-folded-one-side", "select * from c\njoin u\n on lower(c.user_email) = lower(u.email)")).toBe(false) + }) + test("null-safe comparison downgraded to =", () => { + const sql = "select * from a join b on a.program_id = b.program_id" + expect(fires("null-safe-join-removed", sql, [" on a.program_id = b.program_id"], [" on a.program_id is not distinct from b.program_id"])).toBe(true) + }) + test("duplicate WHEN compares literal text, so different literals are distinct", () => { + expect(fires("case-duplicate-when", "case\n when status = 'a' then 1\n when status = 'b' then 2\nend")).toBe(false) + expect(fires("case-duplicate-when", "case\n when spend = 0 then 1\n when clicks = 0 then 2\n when spend = 0 then 3\nend")).toBe(true) + }) + test("window ORDER BY repeating the partition key", () => { + expect(fires("window-order-repeats-partition", "row_number() over (partition by tx_hash, evt_index order by tx_hash) as rn")).toBe(true) + expect(fires("window-order-repeats-partition", "row_number() over (partition by tx_hash order by block_time desc) as rn")).toBe(false) + }) + test("IGNORE NULLS inside the call, across lines", () => { + const sql = "select\n last_value(\n content_title\n ignore nulls\n ) over (partition by k order by d) as t\nfrom x" + expect(fires("ignore-nulls-inside-call", sql, [" ignore nulls"])).toBe(true) + expect(fires("ignore-nulls-inside-call", "select last_value(t) ignore nulls over (partition by k order by d) from x")).toBe(false) + }) +}) + +describe("portability in an adapter's own macro", () => { + const macro = (path: string, body: string) => detectMacroPatterns({ path, status: "modified", diff: plus(body) }, body, DEFAULT_RUBRIC, new Set()) + test("QUALIFY in a BigQuery variant is valid there; in a generic macro it is reported", () => { + const body = "{% macro default__sat(src) %}\nselect * from {{ src }}\nqualify row_number() over (partition by hk order by ldts) = 1\n{% endmacro %}" + expect(macro("macros/tables/bigquery/sat_v0.sql", body).some((f) => /QUALIFY/.test(f.title))).toBe(false) + expect(macro("macros/tables/sat_v0.sql", body).some((f) => /QUALIFY/.test(f.title))).toBe(true) + }) + test("MIN_BY in a Postgres variant is still reported: Postgres lacks it", () => { + const body = "{% macro postgres__latest(c) %}\nselect max_by(status, updated_at) from t\n{% endmacro %}" + expect(macro("macros/latest.sql", body).some((f) => /MIN_BY/.test(f.title))).toBe(true) + }) +}) + +describe("config read from Jinja variables and project defaults", () => { + test("batch_size set through a {% set %} variable", () => { + const sql = `{% set batch_size = 'year' if flags.FULL_REFRESH else 'day' %}\n{{ config(materialized='incremental', incremental_strategy='microbatch', batch_size=batch_size, partition_by={'field': 'day', 'granularity': 'day'}) }}\nselect 1` + expect(titles(modelConfigFindings(file("models/m.sql", sql), sql, "unknown"))).toContain("m: microbatch `batch_size` differs from the partition granularity") + }) + test("materialization from the manifest when the file doesn't set it", () => { + const sql = "{{ config(unique_key=['contract_id']) }}\nselect 1" + expect(titles(modelConfigFindings(file("models/m.sql", sql), sql, "bigquery", { materialized: "table" }))).toContain("m: `unique_key` has no effect on a table") + expect(titles(modelConfigFindings(file("models/m.sql", sql), sql, "bigquery", { materialized: "incremental" }))).not.toContain("m: `unique_key` has no effect on a table") + }) +}) diff --git a/packages/opencode/test/altimate/review-run-stale.test.ts b/packages/opencode/test/altimate/review-run-stale.test.ts index 61164e0c79..5239e555c4 100644 --- a/packages/opencode/test/altimate/review-run-stale.test.ts +++ b/packages/opencode/test/altimate/review-run-stale.test.ts @@ -1,5 +1,9 @@ import { describe, test, expect } from "bun:test" -import { isManifestAffecting } from "../../src/altimate/review/run" +import { mkdtempSync, mkdirSync, writeFileSync } from "node:fs" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { execFileSync } from "node:child_process" +import { isManifestAffecting, projectAdapters } from "../../src/altimate/review/run" /** * Guards on `warnIfStale`'s changed-file filter. The stale warning gates on a @@ -58,3 +62,68 @@ describe("isManifestAffecting", () => { expect(isManifestAffecting(rel)).toBe(false) }) }) + +describe("projectAdapters", () => { + function repo(files: Record): string { + const dir = mkdtempSync(join(tmpdir(), "adapters-")) + for (const [rel, body] of Object.entries(files)) { + mkdirSync(join(dir, rel, ".."), { recursive: true }) + writeFileSync(join(dir, rel), body) + } + const git = (...a: string[]) => execFileSync("git", a, { cwd: dir, stdio: "ignore" }) + git("init", "-q") + git("add", "-A") + git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "init") + return dir + } + + test("reads adapter variants from the project's own macros, mapping presto to trino", async () => { + const dir = repo({ + "dbt/macros/first.sql": "{% macro postgres__first(x) %}{% endmacro %}\n{%- macro snowflake__first(x) -%}{% endmacro %}", + "dbt/macros/other.sql": "{% macro presto__other() %}{% endmacro %}{% macro default__other() %}{% endmacro %}", + "elsewhere/macros/x.sql": "{% macro bigquery__x() %}{% endmacro %}", + }) + expect(await projectAdapters(dir, "HEAD", "dbt")).toEqual(["postgres", "snowflake", "trino"]) + expect(await projectAdapters(dir, "HEAD", undefined)).toEqual([]) + }) + + test("a project without adapter-specific macros has none", async () => { + const dir = repo({ "macros/m.sql": "{% macro cents(x) %}{{ x }} / 100{% endmacro %}" }) + expect(await projectAdapters(dir, "HEAD", "")).toEqual([]) + }) +}) + +describe("grepMatchesAt", () => { + test("paths are repo-root relative from a subdirectory", async () => { + const { grepMatchesAt } = await import("../../src/altimate/review/git") + const dir = mkdtempSync(join(tmpdir(), "grep-")) + mkdirSync(join(dir, "a/b"), { recursive: true }) + writeFileSync(join(dir, "a/b/s.yml"), "snapshots:\n - name: snap\n") + const git = (...x: string[]) => execFileSync("git", x, { cwd: dir, stdio: "ignore" }) + git("init", "-q") + git("add", "-A") + git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "init") + expect(await grepMatchesAt(join(dir, "a"), "HEAD", "^snapshots:", "a/*.yml")).toEqual(["HEAD:a/b/s.yml:snapshots:"]) + expect(await grepMatchesAt(dir, "HEAD", "^models:", "*.yml")).toEqual([]) + }) +}) + +describe("projectResourceNames", () => { + test("includes snapshots defined only in YAML", async () => { + const { projectResourceNames } = await import("../../src/altimate/review/run") + const dir = mkdtempSync(join(tmpdir(), "names-")) + for (const [rel, body] of Object.entries({ + "p/models/stg_a.sql": "select 1", + "p/snapshots/kippadb.yml": "snapshots:\n - name: snapshot_rollup\n relation: source('s', 't')\n", + "p/models/props.yml": "models:\n - name: stg_a\n", + })) { + mkdirSync(join(dir, rel, ".."), { recursive: true }) + writeFileSync(join(dir, rel), body) + } + const git = (...x: string[]) => execFileSync("git", x, { cwd: dir, stdio: "ignore" }) + git("init", "-q") + git("add", "-A") + git("-c", "user.name=t", "-c", "user.email=t@t", "commit", "-qm", "init") + expect([...(await projectResourceNames(dir, "HEAD", "p", join(dir, "p")))!].sort()).toEqual(["snapshot_rollup", "stg_a"]) + }) +}) diff --git a/packages/opencode/test/altimate/review-scope-diff.test.ts b/packages/opencode/test/altimate/review-scope-diff.test.ts index b4515e22be..ff79de16e5 100644 --- a/packages/opencode/test/altimate/review-scope-diff.test.ts +++ b/packages/opencode/test/altimate/review-scope-diff.test.ts @@ -163,6 +163,25 @@ describe("collectChangedFiles: only the PR's own changes", () => { expect(f.status).toBe("renamed") expect(f.similarity).toBe(100) }) + + test("run from a dbt project in a subdirectory, each file still gets its hunk diff", async () => { + const root = await newRepo() + await write(root, "src/dbt/shop/dbt_project.yml", "name: shop\n") + await write(root, "src/dbt/shop/models/a.sql", "select 1 as id\n") + await write(root, "src/dbt/shop/models/old.sql", "select 1 as id\nfrom src\nwhere true\n") + await commitAll(root, "seed") + const base = await g(root, "rev-parse", "HEAD") + await write(root, "src/dbt/shop/models/a.sql", "select 1 as id\ngroup by 1\n") + await g(root, "mv", "src/dbt/shop/models/old.sql", "src/dbt/shop/models/new.sql") + await write(root, "src/dbt/shop/models/new.sql", "select 1 as id\nfrom src\nwhere false\n") + await commitAll(root, "pr") + const files = await collectChangedFiles({ base, head: "HEAD", cwd: path.join(root, "src/dbt/shop") }) + const byPath = Object.fromEntries(files.map((f) => [f.path, f])) + expect(byPath["src/dbt/shop/models/a.sql"].diff).toContain("+group by 1") + const moved = byPath["src/dbt/shop/models/new.sql"] + expect(moved.status).toBe("renamed") + expect(moved.diff).toContain("+where false") + }) }) describe("scopeToProject: the dbt project being reviewed, nothing else", () => { diff --git a/packages/opencode/test/altimate/review.test.ts b/packages/opencode/test/altimate/review.test.ts index 1e7b7833e6..5bf462a992 100644 --- a/packages/opencode/test/altimate/review.test.ts +++ b/packages/opencode/test/altimate/review.test.ts @@ -1877,7 +1877,7 @@ describe("orchestrate", () => { expect(env.verdict).not.toBe("REQUEST_CHANGES") }) - test("modified high-fanout model → warning (not critical), does not block", async () => { + test("modified high-fanout model → no lineage finding (blast radius goes to the tier), does not block", async () => { const files: ChangedFile[] = [{ path: "models/staging/stg_orders.sql", status: "modified", diff: "+select 1\n" }] const runner: ReviewRunner = { ...fakeRunner({}), @@ -1894,8 +1894,8 @@ describe("orchestrate", () => { getContent: content("select 1", "select 1"), // identical → no semantic finding, isolates lineage generatedAt: "2026-05-29T00:00:00Z", }) - const lin = env.findings.find((f) => f.category === "lineage_breakage") - expect(lin?.severity).toBe("warning") + expect(env.findings.find((f) => f.category === "lineage_breakage")).toBeUndefined() + expect(env.tierReasons?.some((r) => /42 downstream models/.test(r))).toBe(true) expect(env.verdict).not.toBe("REQUEST_CHANGES") })