Skip to content

fix: auto-quote backtick-rendered refs in metric_view YAML body (#1361)#1430

Open
sd-db wants to merge 4 commits into1.12.latestfrom
sd-db/fix/1361-metric-view-yaml-auto-quote
Open

fix: auto-quote backtick-rendered refs in metric_view YAML body (#1361)#1430
sd-db wants to merge 4 commits into1.12.latestfrom
sd-db/fix/1361-metric-view-yaml-auto-quote

Conversation

@sd-db
Copy link
Copy Markdown
Collaborator

@sd-db sd-db commented Apr 28, 2026

Closes #1361.

Bug

Bare {{ ref('x') }} in a metric_view source: field renders to `db`.`schema`.`x`, which the YAML scanner rejects:

[METRIC_VIEW_INVALID_VIEW_DEFINITION] Failed to parse YAML:
found character '`' that cannot start any token

Fix

Add SqlUtils.yaml_quote_backtick_values — wraps backtick-rendered identifiers in mapping values with "..." before the body is embedded in $$..$$. Preserves SQL identifier escaping for non-trivial names (hyphens, reserved words). Called from the metric_view create and alter macros.

Same shape as SqlUtils.clean_sql and the STREAM strip in streaming_table/create.sql.

Test plan

  • Unit: 10 parametrized cases (tests/unit/test_handle.py) — idempotence, multi-line, indented, inline backticks left alone, etc.
  • Functional: TestMetricViewBareRef runs a bare-ref metric_view + round-trips a MEASURE() query
  • Full tests/functional/adapter/metric_views/ suite (18 tests) — no regressions
  • pre-commit run --all-files clean

Bare `{{ ref(...) }}` in a metric_view source: line rendered to
`db`.`schema`.`name` and broke the YAML scanner with
METRIC_VIEW_INVALID_VIEW_DEFINITION. Wrap such values in YAML double
quotes before embedding the body, preserving SQL identifier escaping.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 28, 2026

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  dbt/adapters/databricks
  handle.py
  impl.py 985, 989
Project Total  

This report was generated by python-coverage-comment-action

@sd-db sd-db self-assigned this Apr 28, 2026
@sd-db
Copy link
Copy Markdown
Collaborator Author

sd-db commented Apr 28, 2026

/integration-test

@github-actions
Copy link
Copy Markdown

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

@github-actions
Copy link
Copy Markdown

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

Run details.

Copy link
Copy Markdown
Collaborator

@tejassp-db tejassp-db left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this parse the whole YAML file or just the source field? If we intend to only change source field can we make it more specific and not whole YAML wide.

Also the regex might need tightening. Valid identifiers as mentioned in this identifier syntax doc. Following samples break in the current implementation

pytest.param(
    "source: `schema`.`name` # a comment",
    'source: "`schema`.`name` # a comment',
    id="two_part_with_comment",
),

# Very rare case, but it is valid syntax. I don't know if we should solve for this?
pytest.param(
    "source: `a``b`.`schema`.`table`",
    'source: "`a``b`.`schema`.`table`"',
    id="escaped_backtick_in_identifier",
),

@sd-db
Copy link
Copy Markdown
Collaborator Author

sd-db commented May 8, 2026

Does this parse the whole YAML file or just the source field? If we intend to only change source field can we make it more specific and not whole YAML wide.

Good point, will limit this to only source field

pytest.param(
    "source: `schema`.`name` # a comment",
    'source: "`schema`.`name` # a comment',
    id="two_part_with_comment",
),

Yes this is something we should look to solve for

# Very rare case, but it is valid syntax. I don't know if we should solve for this?
pytest.param(
    "source: `a``b`.`schema`.`table`",
    'source: "`a``b`.`schema`.`table`"',
    id="escaped_backtick_in_identifier",
),

Here pragmatically I think we should not solve for such cases and it is fine if this fails. We are specifically looking for well intentioned patterns for source ref that we want to parse for

sd-db added 2 commits May 8, 2026 10:42
Address #1430 review:
- Scope regex to source: key only (top-level + joins) instead of any \w+:
- Preserve trailing YAML # comment
- Allow space before colon (source :)
- Use space-only whitespace (YAML rejects tabs)
- Switch to named groups
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