Skip to content

Commit 1ebb03d

Browse files
authored
Add target schema and this compile context (#68)
1 parent fe4ae4b commit 1ebb03d

15 files changed

Lines changed: 329 additions & 19 deletions

File tree

.agent/research/dbt-upstream-reference-map.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Every compatibility slice should record:
4747
| YAML properties and resource patches | `core/dbt/parser/schemas.py::SchemaParser`, `SourceParser`, `PatchParser`, `ModelPatchParser`, `MacroPatchParser`; `core/dbt/parser/schema_yaml_readers.py::ExposureParser`, `MetricParser`, `SemanticModelParser`, `SavedQueryParser` | `crates/dbt-parser/src/resolver.rs::resolve_package_waves` resource order: sources, seeds, snapshots, groups, models, analyses, functions, exposures, semantic models, metrics, saved queries, data tests, unit tests | Current `src/project.zig` YAML routines and `src/project/parse.zig` helpers; future `src/project/parse.zig` plus narrower modules if needed |
4848
| Exposure, source, ref, metric dependency resolution | `core/dbt/parser/manifest.py::_process_refs`, `_process_sources_for_node`, `_process_sources_for_exposure`, `_process_metrics_for_node` | `crates/dbt-jinja-utils/src/node_resolver.rs::resolve_dependencies`; `crates/dbt-parser/src/resolver.rs` access validation, relation uniqueness, primary-key inference | `src/project/resolve.zig`; current higher-level orchestration still in `src/project.zig` |
4949
| Parse vs runtime Jinja context | `core/dbt/context/providers.py::ParseProvider`, `RuntimeProvider`, `ProviderContext.ref`, `source`, `execute`, `var`, `graph`, `env_var`, `selected_resources`, `generate_parser_model_context`, `generate_runtime_model_context`, `generate_parse_exposure`, `generate_parse_semantic_models` | `crates/dbt-parser/src/renderer.rs::render_sql_file_inner`, `augment_sql_resources_with_static_sources`; `crates/dbt-parser/src/dbt_namespace.rs::DbtNamespace` parse-mode interception of `get_relation` and `get_columns_in_relation` | Current lexical `src/project/jinja.zig`; future parse context and compile/runtime context modules before M2 |
50-
| Profile and adapter identity | `core/dbt/config/profile.py::Profile.pick_profile_name`, `render_profile`, `from_raw_profile_info`, `_get_profile_data`, `_credentials_from_profile`, `to_target_dict`; `core/dbt/config/runtime.py::load_profile`, `RuntimeConfig.get_metadata`; `core/dbt/context/target.py::TargetContext.target` | `crates/dbt-loader/src/load_profiles.rs::load_profiles`; `crates/dbt-profile/src/resolve.rs`; `crates/dbt-schemas/src/schemas/profiles.rs::DbConfig::adapter_type`; `crates/dbt-adapter-core/src/lib.rs::AdapterType` | `src/project/profile.zig` owns the current scalar `profiles.yml` adapter-type parser; `src/project/config.zig` owns `dbt_project.yml` `profile:`; future profile/context modules own Jinja rendering, credentials, target context, and relation identity |
50+
| Profile, target context, and relation identity | `core/dbt/config/profile.py::Profile.pick_profile_name`, `render_profile`, `from_raw_profile_info`, `_get_profile_data`, `_credentials_from_profile`, `to_target_dict`; `core/dbt/config/runtime.py::load_profile`, `RuntimeConfig.get_metadata`; `core/dbt/context/target.py::TargetContext.target`; `core/dbt/context/providers.py::ModelContext.this`, `generate_parser_model_context`, `generate_runtime_model_context`; `core/dbt/parser/base.py::ConfiguredParser.update_parsed_node_relation_names`, `_update_node_relation_name`; `core/dbt/compilation.py::Compiler._create_node_context`, `_compile_code`, `compile_node` | `crates/dbt-loader/src/load_profiles.rs::load_profiles`; `crates/dbt-profile/src/resolve.rs`; `crates/dbt-jinja-utils/src/phases/utils.rs::build_target_context_map`; `crates/dbt-schemas/src/schemas/profiles.rs::DbConfig::adapter_type`, `TargetContext`, `CommonTargetContext`; `crates/dbt-jinja-utils/src/phases/compile/compile_node_context.rs`; `crates/dbt-adapter-core/src/lib.rs::AdapterType`; `crates/dbt-adapter/src/relation/relation_impl.rs`; `crates/dbt-schemas/src/schemas/relations/base.rs` | `src/project/profile.zig` owns the current scalar `profiles.yml` adapter-type and schema parser; `src/project/config.zig` owns `dbt_project.yml` `profile:`; `src/project/compiler.zig` owns current two-part quoted relation rendering and narrow `target.*`/`this` compile expressions; future profile/context/relation modules own Jinja rendering, credentials, adapter-specific target fields, target database, include policy, custom schema/alias/database generation, and full relation identity |
5151
| Manifest data model and maps | `core/dbt/contracts/graph/manifest.py::Manifest` maps for nodes, sources, macros, docs, exposures, metrics, groups, selectors, files, disabled, semantic_models, unit_tests, saved_queries, fixtures; `build_flat_graph`, `build_parent_and_child_maps`, lookup rebuilders, resource adders | `crates/dbt-schemas/src/schemas/manifest/manifest.rs::build_manifest`, `build_disabled_map`, `build_parent_and_child_maps`, path normalization, `nodes_from_dbt_manifest` | `src/project/types.zig`, `src/project/manifest.zig`, `src/project/resolve.zig` |
5252
| Selector grammar and methods | `core/dbt/graph/selector_spec.py`, `selector.py`, `selector_methods.py`, `cli.py`, `graph.py`, `queue.py`; methods include FQN, tag, group, access, source, exposure, metric, semantic_model, saved_query, unit_test, path, file, package, config, resource_type, test_name, test_type, state, result, source_status, version, selector | `crates/dbt-parser/src/resolver.rs` selector YAML loading; command flags in `crates/dbt-clap-core/src/commands.rs` | `src/project/selector.zig` and CLI validation in `src/root.zig`; future state/result/source-status work in `src/project/state.zig` |
5353
| Artifact schemas | `schemas/dbt/manifest/v12.json`, `schemas/dbt/run-results/v6.json`, `schemas/dbt/sources/v3.json`, `schemas/dbt/catalog/v1.json` | v2 still emits JSON for compatibility and adds Parquet artifacts per README; manifest builder in `crates/dbt-schemas/src/schemas/manifest/manifest.rs` | `src/project/manifest.zig`, future run/catalog/source writers and schema validators under tests/scripts |
@@ -87,6 +87,11 @@ Every compatibility slice should record:
8787
`src/project/resolve.zig` for static `adapter.dispatch(...)`
8888
`depends_on.macros` extraction. This is documented in
8989
`.agent/research/m2-project-dispatch-config.md`.
90+
- Profile-derived target schema is parsed in `src/project/profile.zig`, copied
91+
into the graph by `src/project/loader.zig`, and consumed by
92+
`src/project/compiler.zig` for render-only model/ref relation names and
93+
narrow `target.*` / `this` compile expressions. This is documented in
94+
`.agent/research/m2-target-schema-this-compile.md`.
9095
- The test base includes native Zig tests for module-level helpers and pytest
9196
integration tests for CLI/artifact fixtures plus a pinned local Manifest v12
9297
schema slice.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# M2 Target Schema And This Compile Context Slice
2+
3+
This slice extends dxt's render-only compiler with a narrow profile-derived
4+
target context and current-model relation context. It does not execute SQL,
5+
validate credentials, implement custom schema or alias generation, render
6+
arbitrary Jinja expressions, or open adapter connections.
7+
8+
## Upstream References
9+
10+
dbt Core v1, branch `1.latest`, commit `566b75d`:
11+
12+
- `core/dbt/config/profile.py::Profile.render_profile` selects the target from
13+
CLI override, profile `target:`, then `default`.
14+
- `core/dbt/config/profile.py::Profile.to_target_dict` exposes selected target
15+
credentials plus `type`, `threads`, `name`, `target_name`, and
16+
`profile_name`.
17+
- `core/dbt/context/target.py::TargetContext.target` defines the shared
18+
`target` context, including `name`, `schema`, `type`, and `threads`.
19+
- `core/dbt/context/providers.py::ModelContext.this` exposes the current model
20+
as an adapter relation; its documented values include `{{ this }}`,
21+
`{{ this.schema }}`, `{{ this.table }}`, and `{{ this.name }}`.
22+
- `core/dbt/context/providers.py::generate_parser_model_context` and
23+
`generate_runtime_model_context` keep the model context shape stable while
24+
provider behavior changes between parse and runtime.
25+
- `core/dbt/parser/base.py::ConfiguredParser.update_parsed_node_relation_names`
26+
and `_update_node_relation_name` assign relation names through the adapter
27+
relation class after database, schema, and alias resolution.
28+
- `core/dbt/compilation.py::Compiler._create_node_context`, `_compile_code`,
29+
and `compile_node` render raw SQL through the model context before writing
30+
compiled output.
31+
32+
dbt Core v2 / Fusion foundation, branch `main`, commit `0529e06`:
33+
34+
- `crates/dbt-profile/src/resolve.rs::resolve_target`,
35+
`resolve_with_env`, `ResolvedProfile::schema`, and
36+
`ResolvedProfile::database` keep profile target resolution and default
37+
database/schema identity as data.
38+
- `crates/dbt-jinja-utils/src/phases/utils.rs::build_target_context_map`
39+
inserts `profile_name`, `name`, and `target_name` into the target map.
40+
- `crates/dbt-schemas/src/schemas/profiles.rs::TargetContext`,
41+
`CommonTargetContext`, and `TryFrom<DbConfig> for TargetContext` define
42+
common `database`, `schema`, `type`, and `threads`; DuckDB defaults missing
43+
schema to `main`.
44+
- `crates/dbt-jinja-utils/src/phases/compile/compile_node_context.rs` builds
45+
compile-time `this` relation data and exposes `this`, `database`, `schema`,
46+
and `identifier`.
47+
- `crates/dbt-jinja-ctx/src/compile.rs::CompileNodeCtx` defines the typed
48+
compile context contract.
49+
- `crates/dbt-adapter/src/relation/relation_impl.rs` and
50+
`crates/dbt-schemas/src/schemas/relations/base.rs` define adapter relation
51+
rendering and include-policy behavior.
52+
53+
## dxt Ownership
54+
55+
- `src/project/profile.zig` parses selected profile output scalar `type` and
56+
now scalar `schema`.
57+
- `src/project/types.zig` carries `Graph.target_schema` and
58+
`AdapterIdentity.target_schema`.
59+
- `src/project/loader.zig` copies profile-derived target schema into the graph
60+
before parser and compiler use.
61+
- `src/project/compiler.zig` owns the render-only relation formatter and the
62+
narrow `target.*` / `this` expression surface.
63+
- `src/project.zig` keeps the compile orchestration path and assigns manifest
64+
`relation_name` through the compiler.
65+
66+
## Supported Surface
67+
68+
- Select profile from CLI `--profile`, falling back to `dbt_project.yml`
69+
`profile:`.
70+
- Select target from CLI `--target`, falling back to profile `target:`, then
71+
`default`.
72+
- Read selected output scalar `schema`; default to `main` only for selected
73+
DuckDB targets when missing.
74+
- Render supported model/ref relations with the graph target schema.
75+
- Render literal compile expressions:
76+
- `target.name`
77+
- `target.target_name`
78+
- `target.schema`
79+
- `target.type`
80+
- `target.profile_name`
81+
- `this`
82+
- `this.schema`
83+
- `this.name`
84+
- `this.table`
85+
- `this.identifier`
86+
87+
## Unsupported Surface
88+
89+
- General Jinja expression evaluation, filters, indexing, conditionals, loops,
90+
concatenation, macro execution, and adapter calls.
91+
- Adapter-specific target fields such as database, dbname, project, dataset,
92+
host, user, warehouse, or role.
93+
- Node-level custom schema, alias, database, config rendering, and
94+
`generate_schema_name` / `generate_alias_name` / `generate_database_name`.
95+
- Ephemeral/deferred/unit-test `this`, operation context, source freshness
96+
context, hooks, and materialization runtime behavior.
97+
- Adapter include policy beyond the current two-part quoted
98+
`schema.identifier` model relation format.
99+
100+
## Validation
101+
102+
- Native Zig tests cover profile schema extraction/defaulting, target-schema
103+
relation rendering for refs, and compile rendering for `target.*` plus
104+
`this` fields.
105+
- Pytest fixture `profile_target_context` validates the native CLI compile
106+
path, selected profile target schema/type/name/profile fields, ref rendering,
107+
compiled SQL files, manifest `relation_name`, and manifest schema slice.
108+
- Schema validation is run against the fixture `manifest.json`.
109+
110+
## Stop Conditions
111+
112+
- Stop before adding arbitrary Jinja expression support.
113+
- Stop before implementing custom schema/alias/database semantics.
114+
- Stop before adding live adapter connections, materialization SQL execution,
115+
catalog introspection, or run-results artifacts.
116+
- Stop if a fixture requires secrets, private profile values, live credentials,
117+
or local absolute paths.

PLAN.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,19 @@ root-project dispatch config parser. This slice lets static
329329
parse installed-package dispatch as root config, render Jinja in project config,
330330
or run adapters.
331331

332+
Current target schema and `this` compile context source note:
333+
`.agent/research/m2-target-schema-this-compile.md` maps upstream dbt Core v1
334+
profile target context, model `this`, relation-name assignment, and compile
335+
context behavior plus Fusion target-context and compile-context references to
336+
dxt's narrow render-only implementation. This slice lets `compile`, `docs
337+
generate`, and run/build preflight compile selected models with profile-derived
338+
target schema, `target.name`, `target.target_name`, `target.schema`,
339+
`target.type`, `target.profile_name`, `this`, `this.schema`, `this.name`,
340+
`this.table`, and `this.identifier`. It does not implement arbitrary Jinja,
341+
adapter-specific target fields, custom schema/alias/database generation, live
342+
adapter connections, materialization execution, catalog introspection, or
343+
run-results artifacts.
344+
332345
The next source-grounded M1/M2 slices after macro block variant support are:
333346

334347
1. Extend the render-only artifact boundary to adapter-free docs generation:
@@ -361,8 +374,10 @@ The next source-grounded M1/M2 slices after macro block variant support are:
361374
5. Grow artifact schema coverage only alongside emitted fields, using v1 JSON
362375
schemas and v2 manifest builder behavior while keeping dxt-specific metadata
363376
out of dbt schemas.
364-
6. Add adapter relation identity and target context values after the render-only
365-
compile boundary is stable, using v1 compile runner/compiler behavior plus
377+
6. Continue adapter relation identity beyond the current two-part
378+
profile-schema relation rendering: database include policy, quoting config,
379+
adapter-specific target fields, custom schema/alias/database generation, and
380+
source/model relation parity, using v1 compile runner/compiler behavior plus
366381
v2 adapter core/SQL identity references.
367382

368383
## Fixture Ladder

0 commit comments

Comments
 (0)