Skip to content

fix: honor expression on primary_key constraints in V1 materialization - #1551

Merged
sd-db merged 4 commits into
mainfrom
sd-db/fix/v1-pk-constraint-expression
Jun 22, 2026
Merged

fix: honor expression on primary_key constraints in V1 materialization#1551
sd-db merged 4 commits into
mainfrom
sd-db/fix/v1-pk-constraint-expression

Conversation

@sd-db

@sd-db sd-db commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

What

On the V1 materialization path (use_materialization_v2: false, the default), the persist_constraints flow rendered primary key(<cols>) and silently dropped the constraint's expression. A primary key declared with expression: RELY (or any trailing clause) was created without it — no error, no warning.

The V2 path (PrimaryKeyConstraint._render_suffix) and V1's own foreign_key branch already honor expression. This change makes the V1 primary_key branch in relations/constraints.sql append expression as a trailing suffix, gated on its presence so a PK without an expression renders identically to before.

Why

PRIMARY KEY (id) RELY is valid Databricks syntax for an informational primary key the optimizer can rely on. Before this fix, the only way to attach RELY at model-build time on the default path was a post-hook.

Given (default path, contract.enforced: true):

constraints:
  - type: primary_key
    columns: [id]
    expression: RELY
  • Before: ALTER TABLE … ADD CONSTRAINT pk PRIMARY KEY (id); ← RELY dropped
  • After: ALTER TABLE … ADD CONSTRAINT pk PRIMARY KEY (id) RELY;

Tests

  • Added test_macros_get_constraint_sql_primary_key_with_expression (tests/unit/macros/relations/test_constraint_macros.py) asserting the rendered SQL carries the expression suffix. Fails without the fix, passes with it.
  • PKs without an expression render byte-identically, so the pre-existing constraint macro unit tests are unchanged — full unit suite green.
  • Existing constraints/ + persist_constraints/ functional suites pass on a UC SQL warehouse (39 passed).

The V1 (default) persist_constraints path rendered `primary key(<cols>)` and silently dropped the constraint's `expression`, so a primary key declared with `expression: RELY` was created without it. The V2 path and V1's own foreign_key branch already honor `expression`; the primary_key branch now appends it as a trailing suffix.

Adds a get_constraint_sql macro unit test covering the expression suffix.
@sd-db
sd-db requested a review from jprakash-db as a code owner June 19, 2026 13:33
@github-actions

Copy link
Copy Markdown

Coverage report

This PR does not seem to contain any modification to coverable code.

sd-db added 2 commits June 19, 2026 19:38
When no explicit name is provided, append expression to the hash input so
unnamed PKs with different trailing clauses (e.g. RELY) get distinct names.
@sd-db

sd-db commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator Author

/integration-test

@github-actions

Copy link
Copy Markdown

Integration tests dispatched for PR #1551 by @sd-db. Track progress in the Actions tab.

@github-actions

Copy link
Copy Markdown

Integration results for PR #1551 — UC cluster ✅ success · SQL warehouse ❌ failure · All-purpose cluster ✅ success · Shard coverage ✅ success

Run details.

@github-actions

Copy link
Copy Markdown

Integration results for PR #1551 — UC cluster ✅ success · SQL warehouse ✅ success · All-purpose cluster ✅ success · Shard coverage ✅ success

Run details.

@sd-db
sd-db merged commit 0d51ec2 into main Jun 22, 2026
9 checks passed
@sd-db
sd-db deleted the sd-db/fix/v1-pk-constraint-expression branch June 22, 2026 13:55
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.

2 participants