Skip to content

mdcode: binding profiles for the semantic model - #358

Merged
libei merged 8 commits into
GoogleCloudPlatform:mainfrom
libei:mdcode-profiles-pr1-authoring-surface
Aug 30, 2026
Merged

mdcode: binding profiles for the semantic model#358
libei merged 8 commits into
GoogleCloudPlatform:mainfrom
libei:mdcode-profiles-pr1-authoring-surface

Conversation

@libei

@libei libei commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

Adds binding profiles to the kcmd semantic model: one logical model
(declarations) paired at push time with a chosen physical binding profile
via kcmd push --profile <name>. This relaxes today's 1:1 entity→source
binding so a single model can deploy against different stores or physical
layouts, and lets a profile bind only the subset of fields its store serves.
The single-file (inline default) form keeps working unchanged. Deploy legs
are unchanged — BigQuery Graph + Knowledge Catalog only.

What's here (four commits)

  1. Authoring surface — the loader accepts the guide's syntax: entities:
    alias for datasets:, first-class deployment_target:, bare-string
    expression:, and unbound: true on a field. Sugars normalize pre-Zod, so
    the IR and validation stay untouched.
  2. Merge + availability<model>.profiles/*.yaml discovery; mergeProfile
    overlays a profile's physical bindings onto the logical model by name (with a
    binding-only guard); pruneUnavailable drops fields a profile leaves unbound
    and propagates that up to the metrics/relationships built on them, with a
    per-profile report. kcmd push --profile <name> is orthogonal to --target.
  3. Command + configkcmd profiles (read-only listing: target, sources,
    "cannot answer" report) and default_profile: in catalog.yaml.
  4. Generator fix — the BigQuery Graph generator emitted logical field names
    at structural sites (node KEY, edge SOURCE/DESTINATION KEY, REFERENCES) and
    as MEASURE operands. That was invisible while name == column but broke a
    profile that renames a column. Now resolved to the bound physical column;
    output is unchanged when name == column, so goldens do not move.

Validation

  • bun test under toolbox/mdcode: full semantic + tool suites green, no
    golden churn.
  • Live (sqlgen-testing): one logical model deployed under two profiles to
    BigQuery Graph. analytical (all fields bound) answers order_count and
    total_items; lite (item_count unbound) answers order_count and
    correctly rejects total_items as pruned. Both CREATE PROPERTY GRAPH
    statements succeed and the measures return through GRAPH_EXPAND + AGG.

Supersedes the stacked split; #359 and #360 fold in here.

…ader

The binding-profiles guide authors models with a readable surface the loader
did not accept. Teach the loader that surface so a single combined file using
it loads correctly (profiles themselves come next):

- entities: is an alias for datasets:
- a model-level deployment_target: URI folds into the GOOGLE custom_extensions
  block the deploy leg reads (the existing form still works; the two must agree
  when both are present)
- a field expression: may be a one-line string, shorthand for a single
  target-dialect variant
- a field may be marked unbound: true -- declared logically but with no
  physical column under this binding, structurally absent rather than null

Sugars are normalized before schema validation; unbound is carried onto the IR.
A field that is neither bound nor unbound is a load error naming the field.
Adds the mechanism behind binding profiles: one logical model paired at push
time with a chosen physical binding, deployed to BigQuery + Knowledge Catalog as
before. Deploy legs are unchanged; this is loader/push work.

- Profile discovery: SemanticModelLayout reads <model>.profiles/*.yaml beside
  the model (single-level, so a profile file is never taken for a model).
- Merge (resolve_profiles.mergeProfile): overlays a profile onto the logical
  model by name -- entity source, field expression, deployment target -- and
  enforces the binding-only contract (a profile may set physical bindings and
  may leave a field unbound; it may not add/remove elements or change what
  anything means). A field the profile omits is carried through as unbound.
- Availability pruning (resolve_profiles.pruneUnavailable): drops each unbound
  field and everything that depends on it -- a metric that reads it, a
  relationship whose join column is unbound, a cross-entity metric over a
  dropped relationship -- and returns a per-profile report of withheld coverage.
- kcmd push --profile <name>: merges the named profile before load, prunes after,
  then runs the existing validation + deploy. Orthogonal to --target. The
  implicit 'default' profile keeps today's inline single-file behavior.
- Sources may be resource-name URIs: a //bigquery.googleapis.com/... table is
  normalized to project.dataset.table; a non-BigQuery URI (Spanner/AlloyDB/...)
  rides through and is skipped by the BigQuery pre-flight probe.

Tested: mergeProfile (by-name merge, contract violations, omission->unbound,
non-mutation), pruneUnavailable (field/metric/relationship pruning + report),
profile discovery, and an end-to-end run of the guide's three-file example.
Completes binding profiles with the reporting surface and the CI default.

- kcmd profiles: lists each of a model's binding profiles and, per profile, its
  resolved deployment target and sources plus what it cannot answer (the unbound
  fields and the metrics/relationships that fall with them). Read-only -- it
  merges and prunes each profile the way push does but deploys nothing and makes
  no network call -- so coverage can be inspected before choosing a profile.
- default_profile in catalog.yaml: the profile a bare kcmd push uses when
  --profile is omitted. Precedence is --profile, then default_profile, then the
  inline 'default' binding. Round-trips through manifest load/save.

Tested: the profiles command's listing, default marking, resolved sources, and
withheld coverage; default_profile load/save round-trip.
The BigQuery Graph generator used a field's logical name at every structural
site -- node KEY, edge SOURCE KEY / DESTINATION KEY / REFERENCES, association
REFERENCES -- and as the operand of a MEASURE. That was invisible while every
binding named its column after the field (name == column, as all fixtures do),
but a binding profile that maps a field to a differently named physical column
made the generator emit the logical name where BigQuery requires the physical
column, so the CREATE PROPERTY GRAPH failed with "Column not found" and
"Unrecognized name".

Resolve each structural column to its bound physical column (physicalColumn /
physicalColumns), and let a MEASURE aggregate a declared field by its exposed
property name even when the binding renamed the underlying column. Output is
unchanged when name == column, so existing goldens do not move.
@libei libei changed the title mdcode: accept authoring sugars and unbound fields in the semantic loader mdcode: binding profiles for the semantic model Aug 30, 2026
libei added 4 commits August 30, 2026 00:27
The BigQuery generator's physical-column resolution had no offline test:
every fixture bound its columns name == column, so reverting the fix left the
whole suite green while a renamed profile binding failed on the live backend
("Column not found", "Unrecognized name"). This adds the missing regression.

A binding profile is two files, so it does not fit the single-file corpus
golden loop. Add fixtures/profiles/profile_binding.yaml (logical declarations)
and profile_binding.analytical.yaml (a profile that renames every physical
column and leaves one field unbound), run the same merge -> prune -> IR -> DDL
path push takes, and check the emitted DDL plus the availability report against
a committed golden. The golden shows the physical column at each structural
site (node KEY, edge SOURCE KEY / DESTINATION KEY / REFERENCES) and the
exposed property alias as each MEASURE operand -- the shape live BigQuery
accepts -- and the pruned field and metric. Reverting the generator fix now
fails this test.

The profiles/ subtree is sugared authoring input, not standalone OSI (a
logical model declares fields with no expression; a profile carries
deployment_target/unbound sugars), so the OSI-schema guardrail skips it; the
loader, merge, and this golden validate it instead.
The profile golden suite had one case: a purely logical model bound entirely
by a profile. It did not exercise a model that already carries inline bindings
and is then overridden by a profile -- the combined single-file form, which is
where a model starts before any profile exists.

Add a corpus of logical/profile pairs, each with its own golden, spanning the
distinct cases:
  - profile_binding + analytical : logical base, profile supplies all bindings
    and renames every column (the existing case).
  - profile_binding + operational: the same logical base bound a second way, a
    different field unbound -- one model, two physical realizations.
  - partial_binding + prod       : a combined file (fully bound inline) whose
    profile overrides only the sources and target and inherits every omitted
    column (an environment swap).
  - partial_binding + remap      : the same combined base, a profile that
    renames one column and unbinds another, inheriting the rest.

The prod golden pins the inherit-on-omission rule: a field a profile does not
mention keeps its base binding rather than becoming unbound.
One canonical predicate for "is a field bound" (ir.ts fieldBinding), used by
both availability pruning and the BigQuery generator so they never disagree:

- A field awaiting transpilation (column carried on importedExpression, target
  expression not yet filled) is bound, not unbound. Prune no longer drops such
  fields or the metrics over them.
- An unbound KEY field now makes the whole entity unavailable: prune drops the
  entity and cascades to its relationships and metrics, and reports it
  (AvailabilityReport.droppedEntities), instead of emitting an unkeyed node that
  only fails at BigQuery deploy.
- A structural site (KEY / SOURCE KEY / DESTINATION KEY / REFERENCES) bound to a
  computed expression now warns statically rather than emitting invalid DDL.
- push and profiles share one mergeProfileOntoDoc helper; profiles now surfaces
  merge warnings (it silently dropped them before).

Adds regression tests for each: imported-only field stays bound, unbound key
drops the entity and its dependents, and a computed key warns.
Drop the proposed/not-implemented banner for a scope note; the feature
ships (loader sugars, merge, prune, --profile, kcmd profiles,
default_profile) with BigQuery Graph + Knowledge Catalog as the only
deploy targets.

Correct claims that overran the code:
- profiles merge only entities and fields by name; relationships and
  metrics never appear in a profile (mergeProfile rejects them).
- a profile cannot rebind a relationship's junction source.
- availability prunes entities (unbound key drops the whole node),
  metrics, and relationships; there is no action pruning.
- validation probes the source table, not columns; a mistyped column
  surfaces at deploy, not at validate.
- push prints a per-profile availability summary; kcmd profiles lists
  each withheld item.
- a Spanner-bound profile is merged and reported but not deployed.
@libei
libei merged commit d5cb6e9 into GoogleCloudPlatform:main Aug 30, 2026
7 checks passed
libei added a commit that referenced this pull request Aug 30, 2026
…ath segment (#366)

The Spanner graph generator reduced a source to its final DOTTED segment, which
is correct for a `project.dataset.table` reference but mangles a resource-name
URI: a binding profile's `//spanner.googleapis.com/.../tables/Customer` source
(kept verbatim by the loader, unlike a BigQuery URI which is rewritten to dotted
form) emitted `com/projects/.../tables/Customer` as the table name, which
Spanner rejects. Reduce a resource-name (or any scheme://) URI to its final PATH
segment first, mirroring the loader's BigQuery-URI handling. Localized to the
Spanner leg so the Knowledge Catalog leg still records the full resource name.

Surfaces alongside binding profiles (#358) + Spanner push (#361): profiles.md's
operational example uses spanner:// sources and would fail live.
libei added a commit to libei/knowledge-catalog that referenced this pull request Aug 30, 2026
Rework codelab section 5 and update the binding-profiles guide to reflect the
Spanner Graph target (GoogleCloudPlatform#361) and binding profiles (GoogleCloudPlatform#358) together.

Codelab:
- Step 1 now names the deployment target with the first-class `deployment_target`
  key instead of a GOOGLE custom_extensions JSON string.
- Section 5 replaces the `sed` target-swap with a real Spanner binding profile
  (`sales.profiles/operational.yaml`): different table names, remapped columns
  (including keys), and an unbound `net_amount` so `revenue` is withheld
  operationally. It shows `kcmd profiles`' availability report, the generated
  Spanner DDL, a live deploy, and a GQL query -- every output captured from a
  live ENTERPRISE Spanner run. Cleanup drops the Spanner database.

profiles.md:
- Scope note updated: Spanner graphs now deploy (was "non-BigQuery not yet
  supported").
- Spanner sources use bare table names, not `//spanner.../tables/X` resource
  URIs, which a Spanner graph cannot reference; added a note on the rule.
- `--target` gains `spanner`; command examples deploy the operational (Spanner)
  profile.
libei added a commit that referenced this pull request Aug 30, 2026
* docs(mdcode): codelab deploys to Spanner via a binding profile

Rework codelab section 5 and update the binding-profiles guide to reflect the
Spanner Graph target (#361) and binding profiles (#358) together.

Codelab:
- Step 1 now names the deployment target with the first-class `deployment_target`
  key instead of a GOOGLE custom_extensions JSON string.
- Section 5 replaces the `sed` target-swap with a real Spanner binding profile
  (`sales.profiles/operational.yaml`): different table names, remapped columns
  (including keys), and an unbound `net_amount` so `revenue` is withheld
  operationally. It shows `kcmd profiles`' availability report, the generated
  Spanner DDL, a live deploy, and a GQL query -- every output captured from a
  live ENTERPRISE Spanner run. Cleanup drops the Spanner database.

profiles.md:
- Scope note updated: Spanner graphs now deploy (was "non-BigQuery not yet
  supported").
- Spanner sources use bare table names, not `//spanner.../tables/X` resource
  URIs, which a Spanner graph cannot reference; added a note on the rule.
- `--target` gains `spanner`; command examples deploy the operational (Spanner)
  profile.

* docs(mdcode): make the codelab profile-first from step 1

Author the model as a logical model plus a named `analytical` binding
profile from the start, instead of an inline single-file binding, so the
profile feature is the default path a reader learns. Set it as
`default_profile` so steps 3-4 keep using a bare `kcmd push`; section 5
then adds the `operational` Spanner profile as a second binding rather
than introducing the concept.

Add a "simple case" callout showing the single-file inline binding (the
`default` profile) for models that only ever bind to one store.

Update the section-5 `kcmd profiles` output to show both profiles side by
side (analytical withholds nothing; operational withholds revenue).
Verified live: `kcmd profiles`, and `kcmd push --target kc|bq
--validate-only` off the split layout produce the documented output.

* docs(mdcode): restructure codelab as author-logical -> govern -> bind

Reorder the codelab to match how a semantic model is actually adopted, now
that a logical-only model can be pushed to Knowledge Catalog (#367):

  1. Author the logical model (no source, no column, no target)
  2. Govern it in Knowledge Catalog -- pure logical, needs no tables or data
  3. Deploy to BigQuery -- add the analytical binding, hydrate, query
  4. Deploy the same model to Spanner -- add the operational binding

The Knowledge Catalog push moves ahead of hydration and binding: it governs
meaning, so it needs neither. The analytical binding authoring and the table
hydration fold into the BigQuery step, where they are actually required. The
"why tables come first" note is rescoped to the BigQuery push, which does
validate source tables, unlike the KC push.

Deploy prose drops "...Graph" (deploy to BigQuery / to Spanner); the graph
names stay in the generated DDL, command output, and feature references. The
Spanner leg now states explicitly that Knowledge Catalog is not re-pushed --
a binding profile changes nothing logical.

* docs(mdcode): drop profiles.md changes from this branch (handled separately)

* docs(mdcode): soften KC framing so it doesn't preclude governing bindings

* docs(mdcode): state the develop/govern/bind arc plainly, drop defensive framing

* docs(mdcode): drop redundant no-bindings-yet paragraph in step 1

* docs(mdcode): fix intro claim that the metric computes identically everywhere

* docs(mdcode): intro no longer implies KC governs only the logical model

* docs(mdcode): use entities: throughout and make outputs match live runs

Verified the codelab end to end against live instances (Knowledge Catalog on
autopush, BigQuery, and an ENTERPRISE Spanner instance) and reconciled it with
what the commands actually print.

- Author the model and both binding profiles under `entities:` rather than
  `datasets:`, matching the prose, which calls them entities throughout. The two
  keys are aliases; the OWL-import output still shows `datasets:` (the original
  spelling) and a note points that out.
- Step 2's Knowledge Catalog plan now shows the real output: the `Validating...`
  framing, the two relationship-name-normalization warnings (with a note that
  they are expected and repeat on every KC push), and the completion footer.
- Step 3's BigQuery DDL block shows the `Pushing...` line, the `-- BigQuery
  Graph --` banner, and the deployment-target comment it was missing, matching
  the Spanner block in step 4 and the real console output.

Depends on the logical-only KC emitter fix (source.resources) so step 2's
govern-before-you-bind push succeeds against the live server.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant