dbt-model-style
Apply dbt + Redshift style and structure when writing, editing, or reviewing a dbt SQL model; excludes logic and layer design.
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- dbt-model-style
- description
- Apply dbt + Redshift style and structure when writing, editing, or reviewing a dbt SQL model; excludes logic and layer design.
02 资源文件 5
- 参考文档checklists/dbt-model-self-check.md4.2 KB
- 参考文档README.md1.7 KB
- 参考文档references/dotstar-passthrough.md4.0 KB
- 参考文档references/example-model.sql4.4 KB
- 脚本scripts/validate_header.py12.5 KB
03 SKILL.md 正文
dbt Model Style
Purpose & scope
An opinionated dbt + Redshift model-writing template. Tune (adapt) items per project; treat MUST as required, SHOULD as the default, and MAY as situational.
<skill-dir> is this skill's folder: ${CLAUDE_SKILL_DIR} on Claude Code; on any other host, the directory that holds this SKILL.md.
When it applies: whenever you author / edit / review any dbt model. Write comment text and frontmatter values in the user's working language (the examples in this doc stay in Chinese as a demonstration).
Style & structure only — not computation. This skill covers how CTEs are arranged, how columns are named, how comments are written, how JOINs are declared. It does not cover calculation logic, business rules, metric formulas, NULL/denominator semantics, or layer-dependency design — those are a separate matter, out of scope here.
If a request mixes style and logic, do the style part here and identify the calculation or business-rule part as out of scope. Split the edit; do not silently change computation or refuse the whole request.
One boundary that genuinely blurs — name-vs-content. A few style rules can only be checked by glancing at the computation: the name-matches-content MUST (§3) says a __paid column must not smuggle in trial rows, so confirming the name matches what the column computes requires reading the logic. That read-to-verify-the-name is in scope; changing the computation to fix a mismatch is not — flag the mismatch and hand the logic fix back.
The one MUST to remember: the
finalCTE has zero logic — column selection + comments only. Every other MUST is structural discipline that keeps a model traceable and machine-parseable.
Scope of enforcement (read first — don't run wild)
This template applies to newly created models and the model you're currently editing — whichever you touch, bring it up to standard.
- Do not proactively mass-backfill / rewrite existing models just to satisfy this guide. That's pure churn and risk; don't do it unless the user explicitly asks.
- When editing an existing model, use the boy-scout rule (bounded): bring the part you touch (that CTE, those few columns, the header) up to standard — don't rewrite the whole file on the way.
- An existing model that doesn't match this guide but you aren't touching this time → leave it; it's not a bug.
- A large-scale rollout is a separate, explicitly-authorized task, not a side effect of writing a model.
The whole picture: writing order
Use the complete runnable model in references/example-model.sql. Write inside-out:
- Decide
grainandkeysfirst — what one row represents, the primary / join keys. This shapes the whole model. - Write the reference CTE (UPPERCASE,
SELECT *off the upstream). - Write the intermediate transform CTEs — one CTE per step, all calculation/logic lives here.
- Write
final— explicit column list + aligned comments + tags, no logic and noASaliases (every column arrives already named). - Close with
SELECT * FROM final, addconfig. - Backfill both headers:
summary/purpose/sources/relatedand business rules.
1. CTE structure
1.1 The three CTE roles
- MUST — the CTE that first references an external model is UPPERCASE, drops the
stg_/int_/mart_prefix, and containsSELECT *(no column-by-column list). The name reflects the referenced model's business content; for over-long names, extract the key dimensions (int_dim__metric_by_segment_region→SEGMENT_REGION). - MUST — don't rename CTEs. Once a CTE name is set, keep it — the name must trace back to the model it references; renaming breaks that thread. (This is about the CTE's own name, distinct from §3's column aliases.)
- MUST — intermediate transform CTEs use lowercase descriptive names (
daily_with_week/weekly_aggregation) reflecting what the step does. - MUST — one concern per CTE. Better to add a CTE than to cram two derivations together to "save a CTE" — split, and a future change touches only one, with no cross-contamination.
- MUST — the model always ends with
SELECT * FROM final.
1.2 The final CTE iron law (highest priority)
MUST — the final CTE must contain no business logic and no renaming. Only column selection + comments. Every CASE, WHERE filter, arithmetic expression, and every output-name alias (x AS y) moves up into a dedicated preceding CTE: a column reaches final already carrying the name it will have in the table.
Move every CASE, WHERE, arithmetic expression, and rename into a preceding CTE. This makes final a stable, directly diffable column-schema contract.
Renaming counts as logic here even though SQL treats an alias as free. An AS in final means the published column name exists nowhere upstream in the model, so a reader who greps the file for the name they saw in the warehouse lands on one line — the alias — and has to re-read the whole model to find where the value came from. It also breaks the diff property: final should compare line-for-line against the table's column list, and an alias makes the two sides disagree by design. Name the column in the CTE that computes it.
2. Minimize explicit declaration / maximize .* passthrough
SHOULD / MAY — an optional conciseness idiom. Pass columns through with .* so adding an upstream column changes only one line in final. Be concise when you can; fall back to explicit declaration the moment it doesn't fit. It never overrides final zero-logic (§1.2).
Choose among four cases:
- Single source: use
SOURCE_CTE.*plus derived columns. - JOIN:
JOIN ... USING (key)plus unqualifiedSELECT *keeps one key;a.*, b.*duplicates it. - Few renames: rename them in the preceding CTE (§1.2 forbids
ASinfinal), thensource_cte.*passes everything through. - Fall back to an explicit list for value collisions or differently named keys.
When a CTE JOINs ≥2 sources and you intend
.*passthrough, readreferences/dotstar-passthrough.mdin full before writing it — that file holds the exactUSING+ unqualified-*rule, the duplicate-column trap, and the fallback chain. (Single-source passthrough — the common case — doesn't need it.)
3. Naming
- MUST — model / column names are a user-facing contract. Once pushed and documented, don't rename them for cosmetics — downstream models, Tableau filter keys, and BI reports depend on them. When you need to change what's shown, change the display label; keep the technical column id unchanged.
- MUST — name matches content. A column name's qualifier is its promise: a
__paidmetric must not smuggle in trial rows. To add coverage, open a parallel column (__trial/__total) — don't loosen the existing column's semantics. - MUST — variant-naming principle (stable prefix, suffix distinguishes the variant): keep the shared base and distinguish time, class, source, or scope with a
__-separated suffix. - MUST — prefix per the dbt official style guide: layer prefix +
__separating source/entity (stg_<source>__<entity>,int_, marts usefct_/dim_). This is a general dbt convention, not a claim unique to this template. - MUST — always qualify column references with the source CTE (
source_cte.col); no bare column names, no short table aliases (t/o/ci); write the full CTE name inside JOINs too. - (adapt) — define project-specific prefix and variant-suffix vocabularies. Put version suffixes only on new models; never bulk-rename existing ones.
4. Column layout & comments
-
MUST — 4-space indent; vertically align
SELECTcolumns and inline--comments. -
MUST — every
finalcolumn has a purpose comment; group columns with section headers. -
MUST — a
finalcolumn comment is the column's business definition, written for someone who only ever sees thisfinalCTE. Shape:<human-readable name>:<what the number means in business terms>. Assume the reader has the published schema and nothing else — no upstream model, no column list, no idea what your internal buckets or channel prefixes are — because that is literally what a schema catalogue or a BI tooltip gives them. Three failure modes:-- 淨額(銷售 − 退款)— no name to anchor on, and nothing about what this number covers.-- credit_card__net_amount:綁定信用卡的淨額,…— opens by repeating the technical identifier already printed on that same line.-- 綁定信用卡淨額:將寬表對應三個通路的 net_amount 欄位在這一週涵蓋的每一天加總— assembly instructions over upstream columns. Accurate, and useless to the reader: they cannot see those columns, and it still does not say what the number means. Passing:-- 綁定信用卡淨額:這一週顧客用綁定信用卡付掉的金額,已扣掉退款. Say what the number counts, what moves it, and what the reader must not do with it (「不可與其他類別相加」). Name an upstream identifier only when the reader genuinely needs it to act — not to show your work.
-
MUST — no emoji in column comments. A warning is stated in words (「不可與其他類別相加」), never carried by a
⚠️: the glyph survives copy-paste into a schema catalogue, a BI tooltip or a terminal that renders it as tofu, and it says nothing the sentence does not. Ordinary symbols are fine — ASCII operators, and notation that is the calculation (SUM(...),COUNT(DISTINCT ...),+,×) — since that is the answer to "how is this computed". -
MAY — continue long column comments on aligned
--lines so they remain attached to the column. -
(adapt) A schema generator may merge aligned continuation lines and normalize their indentation.
-
SHOULD — tag column roles such as
[OUTPUT],[METADATA], and[AUDIT]; extend tags per project. -
SHOULD — 3-site comment sync: when you change a column's description, update the header frontmatter/narrative, the internal inline comment, and the
finalcolumn comment all three at once. Drift leaves readers with contradictory descriptions of the same column. -
MAY — align CJK comments/tables by visual width (CJK char = 2, ASCII = 1), not character count, or mixed CJK/ASCII goes ragged.
-
(adapt) — if your project generates schema YAML from inline comments (instead of hand-written
.yml), then inline comments = the single source of truth for the schema; comment quality is load-bearing, not decoration.
5. The header: two blocks (frontmatter payload + human-facing prose)
The header splits into two separate /* */ blocks — this split is functional, not cosmetic:
Mechanism (adapt): this two-block design assumes your project (a) uses dbt
persist_docsto write the model description into the warehouse table comment (column comments into column comments), and (b) has tooling that takes the first/* ... */block as the description (e.g. some sql→yml scripts use a non-greedy regex that grabs only the first block). Under that assumption, only the first block reaches the table comment — seen by humans reading the comment and by SQL-aware LLMs/MCPs (MCP = a Model Context Protocol server an agent queries, e.g. a Redshift comment server); the second block stays in the.sqlfor humans. If your project doesn't enablepersist_docs, the two-block split is still a good readability convention — it just loses the "reaches the comment" incentive.
Is it wired up? (check once, per project) — the "first block reaches the comment" payoff is invisible from the
.sqlalone, so confirm the machinery before maintaining the header on faith:
dbt_project.ymlhaspersist_docs: {relation: true, columns: true}(or the model/folder-level equivalent).- After a
dbt run, the comment actually landed — check with\d+ <schema>.<table>inpsql, orget_table_commentvia the redshift-comment MCP.If step 2 confirms the comment landed → the comment-reaching incentives apply (field-order-for-truncation at the ~1000-char list/search cut,
summary/purposekeywords feedingsearch_tables, first block as the agent-facing description). If the comment did not land (whetherpersist_docsis off or just misconfigured) → treat §5 as readability-only: keep the two-block split + YAML shape (they still help humans and the validator), and keepsummary/purposekeyword-rich (it still aids a human reading the.sql) — the keyword MUST is not cancelled; only itssearch_tables-discoverability incentive and the truncation-ordering rule go moot. Don't optimize for a comment nothing reads.
scripts/validate_header.pychecks the header's shape (fields present, parseable YAML, balanced/* */) — it does not verify the comment persisted. The two steps above are the only persistence check.
5.1 First /* */: frontmatter + short narrative (→ table comment, for MCP/agent)
/* ---
title: 區域 B 每日指標值(雙來源合併)
summary: 來源 A(本地)+ 來源 B(forecast)合併後的每日指標值,來源 B 優先 / 來源 A fallback
grain: 一列 = 一個 (metric_date, entity_id)
purpose: 區域 B 每日指標值的日/月趨勢、entity 別分析;每日營運報表的指標來源
keys:
primary: [metric_date, entity_id]
join: [metric_date, entity_id]
related:
- { table: int_dim__entity_dimension, join_on: entity_id, adds: entity 基本資料 / 地區 / 分類 }
- { table: int_metric__daily_dimension, join_on: [metric_date, entity_id], adds: 其他每日指標 }
sources: [int_metric__daily_value__region_b__source_a, int_metric__daily_value__region_b__source_b__by_sub]
refresh: 日更 T+1(依上游 FDW 延遲)
---
來源 B-only 的 row 沒對到來源 A 是正常現象(來源 A 只攤本地)。分析總值時用 daily_value(已 merge),勿用 from_source_a / from_source_b 的透明欄位相加。
*/
- MUST — the first
/* */contains: ① a----fenced YAML frontmatter, ② followed by a 1–3 sentence unstructured narrative (context the frontmatter can't express — also written for the MCP/agent).
Field requirements are layered (staging isn't queried directly by analysts/MCP and is usually 1:1 with its source, so it's relaxed):
- All layers MUST:
title(use the user's working language for the model name, replacing the old# heading),summary,grain,sources. - Consumer layers (intermediate / marts / dash) additionally MUST:
purpose,keys(primary+join). - Consumer layers SHOULD:
related(joinable dimension tables +join_onkey +adds= what it expands — key to agent analysis efficiency; accept the manual-upkeep / possible-staleness cost),refresh. - Staging layer: the four all-layer MUST fields suffice;
purpose/keys/relatedare usually trivial and may be omitted. - MAY field:
layer(omit if inferable from name/path; if set, it overrides the validator's layer inference). - Order fields by importance (
title→summary→grain→purpose→keys→related→sources→refresh): if the description persists into a warehouse comment, tooling often truncates to ~1000 chars in list/search, so the most important must come first to survive.
Frontmatter content requirements
- MUST —
grainrequired and precise: what one row represents ("one entity per day" / "one record per month per category"). This is the single most important field for understanding content + joining correctly. - MUST — strictly parseable YAML: quote values containing
:/#/ leading whitespace and other special chars; lists use[...]; objects use{ key: val }. - MUST —
summary/purposecarry analytical keywords: an MCP'ssearch_tablesmatches on comment keywords, so good ones improve the table's discoverability. keys.joinsemantics: the keys downstream can JOIN this table on to expand analysis. If there's no join key beyond the primary key (common for a single-upstream terminal mart), settingjoin=primaryor omittingjoinis fine — don't fill it just for the sake of it.- Omit
relatedwhen there's none: list only tables that really exist and can be joined to expand analysis; if none, omit the wholerelated— don't fabricate. - Use
join_on, noton: the related join-key field isjoin_on. A bareonis a reserved boolean in YAML 1.1 — PyYAML parseson:asTrueand the frontmatter breaks — which is exactly why we avoid it.
5.2 Second /* */: detailed business rules / mapping (human-facing, not in the comment)
/* 重要業務規則 / 過濾邏輯
* 來源 B 優先、來源 A fallback,單一單位為 canonical
* source flag: 'source_b' | 'source_a'
業務邏輯 Mapping (如適用):
- 分類名稱 來源代碼 來源名稱
*/
- SHOULD — put verbose business rules, filter logic, and mapping tables here, to avoid bloating the table comment.
- Position: immediately after the first block and before
config(the two comment blocks are adjacent, thenconfig, then the first CTE).
5.3 Common
- MUST — every
/* */must be closed. An unclosed/* ==makes the rest of the file get eaten as comment text, breaking Redshift parsing anddbt run. - SHOULD — when
config(sort/tags) or the schema changes, keep the frontmatter (especiallygrain/keys/sources) and the narrative in sync — no drift.
6. SQL syntax style (Redshift)
Style-level syntax choices (not calculation logic):
- MUST — use
LISTAGG(col, ' | '), notSTRING_AGG(Redshift has none). - MUST — small mapping/lookup uses
UNION ALL, notFROM (VALUES ...)(Redshift has none). (adapt) inline UNION ALL table vs a dbt seed — per project convention.
FROM (
SELECT 'code1' AS code, 'name1' AS category
UNION ALL SELECT 'code2' AS code, 'name2' AS category
) AS mapping_table
- MUST — always specify the JOIN type explicitly (
INNER/LEFT); no implicitFROM a, b WHEREjoins. - MUST — same-named join columns use
USING (col, ...), terser than a verboseON a.x = b.x AND ...; for the precise preconditions when combined with.*passthrough see §2 /references/dotstar-passthrough.md. - note — Redshift doesn't support
SELECT * EXCEPTorFILTER (WHERE ...); rewrite the latter with an inlineCASEor a JOIN.
7. Config block
- MUST — wrap table materialization in an environment guard:
{% if target.type=='redshift' %}
{{ config(
materialized = 'table',
sort = ['primary_key_column', 'date_column'],
tags = ['some_tag']
) }}
{% endif %}
- MUST — the key is
materialized, notmaterialization(the latter is invalid; there are mis-spelled cases in the wild — don't copy them). - SHOULD — choose
sortkeys that match the columns actually filtered / joined.
Anti-pattern quick reference
| Anti-pattern | Fix |
|---|---|
CASE / WHERE / arithmetic / any AS alias inside final |
Move into a dedicated preceding CTE; final selects only (§1.2) |
| Declaring every column just to rename a few | Rename in the preceding CTE + .* for the rest (§1.2 §2) |
Renaming a CTE / using short aliases t o |
Keep the CTE name; qualify columns with the full CTE name (§1.1 §3) |
| Cosmetically renaming an already-pushed column | Change the display label; keep the technical id (§3) |
Forgetting to close /* == |
Every /* needs a */ (§5) |
STRING_AGG / FROM (VALUES) / materialization= |
LISTAGG / UNION ALL / materialized= (§6 §7) |
| Cramming multiple derivations into one CTE | One concern per CTE (§1.1) |
After writing / editing
- Run
checklists/dbt-model-self-check.mditem by item. - Run mechanical validation:
A non-zero exit code = violations. Without# Always checked: layered required header fields + parseable, keys/related shape, /* */ balance, materialized spelling python <skill-dir>/scripts/validate_header.py models/ # Opt-in: related/sources tables actually exist in the manifest (cures `related` staleness) python <skill-dir>/scripts/validate_header.py --manifest target/manifest.json models/--manifestit stays zero-config and runs on a lone file (only PyYAML, usually already in a dbt env). (adapt) the layered required-key lists are at the top of the script (BASE_REQUIRED/CONSUMER_EXTRA) — tune to your frontmatter schema; adopters wire it into pre-commit / CI / a dbt step. - Ship / open the PR only after it passes.
04 同仓库更多 Skills
using-loom-workflow
Route broad Loom workflow-tool requests to the right skill. Use when the request spans tools or the right workflow skill is unclear.
skill-retro
Review selected past or current Claude Code or Codex sessions against readable target skill sources and related integration. Produce source-linked proposals without applying changes.
recap-state
Give an in-session recap of current progress for 'where were we', '我們剛剛在幹嘛', '剛剛講到哪', '我跟丟了', 'ちょっと振り返って', '今どこだっけ', or '振り返り'. The built-in /recap is an away-summary.
project-retro
Review one current or specified coding session for source-backed project development improvements. Use for a requested retrospective, including 振り返り seeking improvements; not for current-progress recaps or recurring skill mining.
loom-visualization
Show comparisons, flows, decisions, states or reasoning chains as tables, ASCII or Mermaid in coding chat, including when a Loom station reports to the user (intent restatement, choices, acceptance test results); not Obsidian notes.
loom-memory
Record repo-wide lessons when saving to your own memory or asked to remember; recall, reconcile, or forget them when a task needs it.