Skip to content

Analytics: read a Databricks metric view as a declared model (analytics 1.0.2) - #242

Open
andrew-paterson-atlan-com wants to merge 12 commits into
mainfrom
feat/databricks-metric-view-reader
Open

andrew-paterson-atlan-com wants to merge 12 commits into
mainfrom
feat/databricks-metric-view-reader

Conversation

@andrew-paterson-atlan-com

@andrew-paterson-atlan-com andrew-paterson-atlan-com commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #241 (alias quoting). This PR's base is that branch, so the diff here is the reader alone; without #241 no Databricks card passes the live check. It needs rebasing onto main once #241 merges, and its version bump redone if main has moved.

What changed

The metrics engine reads dbt, LookML, Cube and Snowflake semantic views as declared models. It could not read a Databricks Unity Catalog metric view. This adds a reader, databricks-metric-view, in the Databricks package.

What it takes. A view's YAML, its CREATE VIEW ... WITH METRICS DDL, or a saved DESCRIBE TABLE EXTENDED ... AS JSON. A folder is read view by view. No query can fetch a definition, and the read-only gate admits queries alone, so the reader works from a saved definition and makes no connection.

What it reads. The source, nested joins, fields and measures. MEASURE(x) is replaced by the measure it names, so a measure built on measures is written out in full. Every column is bound to its table. Each measure becomes a declaration: formula, entity, the joins it needs, the view's dimensions with their comments and synonyms.

The rule it holds. A formula it emits returns the number the view returns, or the measure is refused by name with its reason in the reader's notes. Refused today: a window measure, an ordered aggregate (WITHIN GROUP), a one-to-many join, a query inside an expression, a lambda, a parameter marker, a path inside a value (column:key), IDENTIFIER(), a measure with a field's name, a name used twice, and anything the SQL writer would change (below).

The view's filter. A top-level filter applies to every query of the view. A declaration has no model-wide predicate, so the filter is written into every aggregate as FILTER (WHERE ...). A measure the filter cannot be shown to reach is refused.

What the SQL writer would change is not read. Expressions are written back by the parser's SQL writer, which renders some forms as other SQL. An expression is read only when what is written reads back as the same expression and calls no function with a number of arguments the text did not give it. That refuses TO_DATE with a format (the format is dropped), UNIX_DATE (rewritten around a date of the writer's own), two-argument DATEDIFF (written as the three-argument form, which counts whole days where the two-argument form counts calendar days), and a name in backticks where a date part belongs, which was written out bare, as SQL.

A link back to the view. When a saved description carries the workspace host, the declaration's asset gets the view's Catalog Explorer address, and X3 copies it into the card's provenance.observed_in. The host must be a Databricks one and the three-part name plain.

A fixed date becomes a parameter. Atlan's format check refuses a formula that holds a fixed date, and export stops the whole run when one metric fails it. A view filter often holds one (order_date >= DATE'2021-01-01'), so one such view blocked every view in the run. The measure's own formula keeps the date, so its statement runs. The declaration writes it as {{order_date_from}} and lists the parameter with the view's date as its default.

Small changes to shared code, each off unless a declaration asks for it.

Where Change
lib/declared.py complete Adds a declaration's parameters to the card's source.parameters.
lib/card_sql.py bound Writes each parameter's default into a statement, standard library only. Only a plain value is written in: a number, TRUE or FALSE, or text with no quote, backslash or control character. Any other default stays a marker.
stages/x3_emit.py The live check runs a card at its parameters' defaults. Before, a card with a marker went to the warehouse with the marker in it. _asset_pages carries an asset's address to observed_in.
bi_facts, candidates, question_units schemas Optional url on an asset and parameters on a declaration.

Design notes are in docs/metrics-engine/design/declared-models-contract.md, section "Databricks metric views".

What changes for users

A person can point a metrics run at a metric view's saved definition, or a folder of them, and get its measures as metrics with their joins, dimensions, saved queries and a link back to the view. Ships in analytics 1.0.2.

How it was tested

  • tools/tests/test_databricks_metric_view.py: 36 tests, one rule each, on made-up views. Each refusal above has a case.
  • 22 real metric views saved from a Databricks workspace, 184 measures: the reader's output was snapshotted before and after each round of review fixes and is identical formula for formula, so the stricter checks refuse nothing that was read before.
  • Seven test views created over samples.tpch (nested joins, a view filter on a source and a joined column, fields on fields, measures on measures, a measure named like its column): each declared formula over the base tables was compared live with the view's own MEASURE(), whole table and grouped by each dimension. 384 of 384 values equal.
  • End to end on a Databricks SQL warehouse with witness_mode: execute: four of those views read from one folder, 21 statements, 18 metrics after merging measures that several views declare, 21 saved queries, 32 links, nothing held. Saved and released to a demo workspace, read back, and checked against the plan by the plugin's receipt helper. Two questions were then answered from the released definitions and compared with the views: revenue per order by region through three joins (five of five regions equal), and a count under the view filter at the parameter's default and with the date moved (both equal).
  • Full suite with the engine's dependencies: the same five tests fail on main and here, plus the two that check the plugin's file count (see the first decision below). Bare Python 3.13: drift passes, the same four fail on both, plus the same two. Lint fails on the file count alone.
  • Rebased onto main at analytics 1.0.0 on 2 Dec, with X3: quote a card's aliases the way its dialect does (analytics 1.0.1) #241 (and on 30 Nov before that). The version files were the only conflict this time; the engine code merged cleanly. Drift, lint and both test passes were rerun, and the rebased reader gives the same 21 declarations for the four test views as the run saved to the demo workspace.
  • Two independent adversarial reviews. The second found the SQL-writer cases, the bare date-part name, and the unsafe default in bound; all are fixed, each with a test.

Decisions and known limits for the owners

  • This PR is one file over the plugin directory's cap, and lint fails on it. Main now holds exactly 512 files under plugins/atlanai-analytics, the most tools/lint.py allows (DIRECTORY_MAX_FILES). This PR adds one, metric_view.py, so make check reports 513. It was inside the cap until main reached 512 on 30 Nov (Usage telemetry for the core's hooks and the analytics journey, built as the dashboard's is (atlanai 0.5.66) #236). Nothing else fails. Which file should give way is the owners' call: the Databricks package's 8-line artifacts.py could fold into its __init__.py, or the reader could live in plug.py, or a file elsewhere could go.
  • Measures that differ only in a date are one metric. Two measures counting orders in two years declare one formula with two sets of defaults. Run both ways: as two cards X3 refuses them ("one metric under two names"); merged, the card takes the first measure's name and dates, and both statements stay as examples and saved queries.
  • A measure declared in several views is one metric, with one view's joins and dimensions. declared.complete uses the first declaration.
  • Names repeat across views. A card takes the measure's name, so a filtered view's customers can hold the plain name while an unfiltered one keeps the author's. The description says which is which; the name does not.
  • The filter sits in the aggregate, not a WHERE. One value is the view's. Grouped, a group with no row in scope shows 0 or null where the view shows no row.
  • A join is trusted to be many-to-one. Databricks documents that a default join takes the first matching row when the joined table repeats a key. The reader writes a plain LEFT JOIN, which would multiply rows there. Not probed live.
  • Saved queries from a view with fixed values are marked unreviewed by the literal-grounding gate when the run has no schema input. The metrics are unaffected.
  • Export stops the whole run on one format failure. Parameters avoid it here; holding the one metric, as privacy holds do, would protect other sources.

Documentation

No change to the public docs is included, and none is open that covers this. The plugin's page on platform.atlan.com ("Set up Analytics in your coding agent") names Power BI and Tableau dashboards, tables and past queries as inputs, and Databricks only as a warehouse and a source of query history. Once this merges it needs to say that a Databricks metric view is an input, how to hand one over (its YAML, its DDL, or a saved DESCRIBE TABLE EXTENDED ... AS JSON, singly or as a folder), what is refused and where the reasons are shown, and that a fixed date in a view becomes a parameter on the metric. The repository docs are updated here (docs/metrics-engine/design/declared-models-contract.md, docs/metrics-engine/design/plugs.md, the card author's gate instructions). Could the docs owner say whether the docs lane will draft the public change from this PR, or whether it needs writing by hand?

  • make check passes apart from the tests that fail on main: not yet, lint reports 513 files against a cap of 512 (first decision above)
  • make validate not run locally; CI runs it
  • Generated files come from make build, not hand edits
  • A user-facing change bumps the plugin's version
  • User-facing text follows the Voice section of AGENTS.md
  • No tokens, tenant names, internal hosts or customer data in the diff, the description or any screenshot

🤖 Generated with Claude Code

@andrew-paterson-atlan-com
andrew-paterson-atlan-com force-pushed the feat/databricks-metric-view-reader branch from ebaadf2 to 5cf3a43 Compare November 30, 2026 12:37
@andrew-paterson-atlan-com
andrew-paterson-atlan-com force-pushed the fix/databricks-card-alias-quoting branch from 042f6d3 to 2ae8f38 Compare November 30, 2026 12:38
@andrew-paterson-atlan-com andrew-paterson-atlan-com changed the title Analytics: read a Databricks metric view as a declared model (analytics 0.9.95) Analytics: read a Databricks metric view as a declared model (analytics 0.9.108) Nov 30, 2026
A card's canonical statement wrote its output alias, and a derived source's alias, in double
quotes for every warehouse. On Databricks a double-quoted name is a string, so
`SELECT AVG(x) AS "avg_x"` is a syntax error (PARSE_SYNTAX_ERROR, SQLSTATE 42601). Any
Databricks run that checks its cards against the warehouse was refused at X3 for every card,
however the card was written.

The aliases are now quoted through the SQL parser for the card's dialect: backticks on
Databricks, BigQuery and MySQL, double quotes on Snowflake, Postgres and DuckDB. With no
dialect, or one the parser does not know, the statement is unchanged. X3 passes its run's
dialect explicitly to both places it composes a statement.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The project helper loads card_sql.py by file path and runs under the person's own
Python, with nothing installed, and so does CI. Building the quoted alias with the
parser made every metric check through the helper fail there. Quoting is plain
string work again: backticks where a double-quoted token is a string (Databricks,
Spark, Hive, BigQuery, the MySQL family), double quotes everywhere else, T-SQL
included so its statements keep their bytes. BigQuery takes backslash escapes.
A test builds a Databricks statement through the helper with the parser blocked.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
BigQuery reads a quoted name with backslash escapes and the engine's parser reads a
doubled backtick, so no spelling of either character is read the same by both. On
BigQuery a backtick or a backslash in an alias is written as an underscore.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ct says nothing

From a second review of the alias quoting.

- ClickHouse reads a backslash escape inside a double-quoted name, so a
  doubled quote did not hold there: `a\" , (SELECT 1) AS x --` ended the
  name and added a select item. Its backslash and double quote are written
  as underscores, as BigQuery's backslash and backtick already are. The same
  bytes were written before this branch.
- A blank dialect argument no longer beats the card's own dialect.
- Tests name every backtick dialect and a list of double-quote dialects one
  by one, and check that no alias in any of eleven dialects yields a second
  select item or reads another table.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…cs 1.0.2)

The source-of-truth order has named Databricks metric views as a declared model since the
Snowflake reader landed, and no reader read one. This adds it, in the Databricks package, as
`databricks-metric-view`.

The reader takes a view's YAML (specification 0.1 and 1.1), its CREATE VIEW ... WITH METRICS
DDL, or the JSON that DESCRIBE TABLE EXTENDED ... AS JSON or Unity Catalog's table API returns,
and writes the shared `bi_facts` contract with `measures[].declared`, through lib/declared like
the Snowflake semantic view reader. Each measure becomes one metric with the view's formula,
its fields with their comments, synonyms and types, and its many-to-one joins.

Where a metric view differs from a semantic view the reader settles it first:

* The view's table is the role `source`; a join is a role under its name, a nested join under
  its path. Every column is bound to its role, since a metric view writes source columns bare.
* MEASURE(x) is replaced by that measure's expression, and every measure is marked
  `referable: false`, a new optional field of the contract. Without it lib/declared read
  `source.revenue` as the measure `revenue`, so `revenue: SUM(revenue)` beside
  `avg_revenue: AVG(revenue)` compiled to AVG(SUM(revenue)).
* A top-level filter is written into every aggregate as FILTER (WHERE ...).

Refused by name in the reader's notes: window measures and measures built on them,
expressions that read a parameter, wildcard fields, columns that need quoting, one-to-many
joins, joins to a query, and a view whose own source is a query or another metric view.

`databricks-direct` declares the reader and reads the saved definition named by its `bi`
setting. It fetches nothing: a metric view's definition is returned by DESCRIBE and by no
query, and the plug's read-only gate admits queries alone. The design note lists the two ways
a fetch could be added; neither is taken here.

Checked on a Unity Catalog workspace, read-only: for 20 metric views and 184 measures, each
declared formula over its base tables returned the value MEASURE() returns on the view,
including a view with a top-level filter. One view was run through every stage with
`witness_mode: execute`: 6 metrics, 6 queries, no holds.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The view is recorded as the model's one asset. An asset may now carry url, its own
page in the tool it lives in (https, no query string or fragment). The reader gives
the view's Catalog Explorer page only when the saved description names its
workspace (workspace_host) and the view has a plain three-part name. X3 copies the
pages of a metric's statements' assets into the card's provenance.observed_in,
which the Trident metric contract already has.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
What the Databricks metric view reader reads and refuses, after review.

- Names: a source, a join source, a `using` column and a key column are
  plain identifiers (backticks allowed), or the view or join is refused.
- The view's filter reads the source's columns, never a field, as
  Databricks does. A field that redefines a column the filter reads is
  not listed, and a measure that reads it carries its expression.
- Under a filter, a measure with a column outside every recognised
  aggregate is refused. FILTER goes after WITHIN GROUP.
- Refused by name: a string literal with a quote or a backslash, a
  parameter marker, a query or lambda inside an expression, more than one
  expression, and an ordered aggregate the SQL writer would change.
- A folder gives only its readable views with a measure. Hidden folders
  and symbolic links are skipped, and no skipped file stops the run.
- Files are UTF-8 (BOM allowed), at most 1 MiB. Malformed shapes are
  checked, and any other error becomes a refusal that names the file.
- One field or measure is capped at 20,000 characters written out, and
  the expansion stops before it is built. DDL is recognised in one pass.
- X3 copies an asset page only when it is https with no query string,
  fragment or space, and gives none for provenance of another shape.
- Docs say what the code reads: a source that is a metric view is read
  as a table, and the run's live check catches it.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Atlan keeps no fixed date in a metric's formula, and export stops the whole
run when one metric holds one. The reader writes a view's filter into every
measure, so a view filtered on a date (`order_date >= DATE'2021-01-01'`)
passed every gate and then blocked the export of every view in the run.

The reader now leaves the date in the measure's formula, so the statement
compiled from it runs, and writes it as `{{name}}` in the declaration with
the view's date as the parameter's default. A date compared with one column
is named for the column and the side it bounds (`order_date_from`); any other
is named for the date. A date inside a longer text value cannot be lifted
out, and that measure is not read, by name.

- declared: a declaration can list `parameters`; `complete` adds them to the
  card's `source.parameters`, and the model's value leads over an author's.
- card_sql: `bound` writes each parameter's default into a statement, with
  the standard library alone.
- X3: the live check runs a card at its parameters' defaults. Before, a card
  with a marker went to the warehouse with the marker still in it.
- The three schemas carry `parameters` on a declaration.

Tests: two new reader tests (dates in the view filter, a measure's own
filter, a typed value, one date used two ways, a date in text; a card
completed and run at its defaults), and the standard-library statement test
covers `bound`. The full suite has the same failures with and without the
change.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
From an independent review of the reader's review fixes and the date
parameter.

Wrong numbers
- Two measures that differed only in a fixed date were given one declared
  formula once the date was lifted out, so downstream they read as one
  metric. A parameter's name now ends in its date
  (`order_date_from_2021_01_01`), so the formulas differ in their text. Two
  spellings of one moment get two names, and a timestamp with a zone is a
  plain timestamp.
- The parser's SQL writer renders some forms as other SQL: `TO_DATE` loses
  its format, `UNIX_DATE` gains a date of the writer's own, and two-argument
  `DATEDIFF` becomes the three-argument form, which counts whole days. An
  expression is now read only when what is written reads back as the same
  expression and calls no function with a number of arguments the text did
  not give it.
- A path inside a value (`column:key`) is refused: a key with a table's name
  was rewritten as that table. So are `IDENTIFIER()`, ANY or SOME under a
  view filter, a measure with a field's name, and a name used twice.

SQL that is not the view's
- A name in backticks where a date part belongs was written out bare, as SQL,
  past every check. The same read-back test refuses it, and a date part must
  be a plain word.
- `card_sql.bound` writes a default into a statement that is run. It now
  writes only a plain value: a number, TRUE or FALSE, or text with no quote,
  backslash or control character. Any other default stays a marker, so the
  statement does not run. A marker inside any quoted run is left alone.

Time and words
- A view of more than 1,000,000 characters of measures, or 100,000 pairs of a
  field and a measure, is refused before the work is done. A YAML alias no
  longer turns a small file into a long read.
- A view that lists both `fields` and `dimensions`, and one whose fields build
  on each other too deeply, are refused in words.

The 22 real views read the same as before, formula for formula.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…date

The second review fixes named every date parameter after its date, so that two
measures differing only in a date would keep two formulas. That works against
the card gate, which reads two cards with one formula as one metric under two
names, and against what a parameter is: a date a reader may move.

A date compared with one column is again named for the column and the side it
bounds (`order_date_from`). Two measures that differ only in a date declare one
formula with two defaults. Run through the engine both ways on a view with two
such measures: as two cards X3 refuses them and says they are one metric; merged
they are one metric that takes the first measure's name and dates, with both
statements kept as its examples and saved queries.

Two dates that would share a name inside one formula still each carry their
date, told apart by separator, fraction and zone, so two spellings of one moment
no longer take one name.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Main moved the engine's design docs to docs/metrics-engine/design. The reader's
header and the Databricks package's README named the old path.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@andrew-paterson-atlan-com
andrew-paterson-atlan-com force-pushed the fix/databricks-card-alias-quoting branch from 2ae8f38 to 260528e Compare December 2, 2026 21:48
@andrew-paterson-atlan-com
andrew-paterson-atlan-com force-pushed the feat/databricks-metric-view-reader branch from 5cf3a43 to 4a7c840 Compare December 2, 2026 21:48
@andrew-paterson-atlan-com andrew-paterson-atlan-com changed the title Analytics: read a Databricks metric view as a declared model (analytics 0.9.108) Analytics: read a Databricks metric view as a declared model (analytics 1.0.2) Dec 2, 2026
Base automatically changed from fix/databricks-card-alias-quoting to main December 3, 2026 21:14
Main now carries the alias quoting fix as one squashed commit (#241), which
this branch was stacked on as four commits. The two sides hold the same
quoting change, so every conflict resolves to this branch: the version files
and telemetry_config.json stay at 1.0.2, and x3_emit.py and the two test
files keep the reader's additions on top of the quoting fix.

The merged tree is identical to the branch before the merge.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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