← AltertableCONTENT HISTORY
WHAT CHANGED
Update to Altertable
Snapshot Nov 23, 2026 · 12:11 UTC · version 1.13.2
Collection source: downloaded plugin package.
First saved snapshot
No earlier snapshot is available to establish a change.
Save Altertable and check future changes in Following.
Up to 3 plugins free · no card required.
Compare saved observations
Download comparison JSONFull technical diff · 0 changed fields
Full snapshot data
{
"description": "Discovers, queries, verifies, and creates Altertable semantic views with SEMANTIC_VIEW(...) and AGG(...), including breakdowns, metric filters, period-over-period, running totals, and fan-out-safe metrics across joined tables. Use for governed metrics and KPIs, defining reusable metrics, or Snowflake semantic view syntax.",
"included_files": [
{
"relative_path": "agents/openai.yaml",
"size_in_bytes": 128
},
{
"relative_path": "references/modeling.md",
"size_in_bytes": 12232
},
{
"relative_path": "references/query-syntax.md",
"size_in_bytes": 10117
}
],
"name": "use-semantic-layer",
"skill_md_contents": "---\nname: use-semantic-layer\ncompatibility: Requires Altertable MCP server\ndescription: \"Discovers, queries, verifies, and creates Altertable semantic views with SEMANTIC_VIEW(...) and AGG(...), including breakdowns, metric filters, period-over-period, running totals, and fan-out-safe metrics across joined tables. Use for governed metrics and KPIs, defining reusable metrics, or Snowflake semantic view syntax.\"\nmetadata:\n author: Altertable\n requires: \"altertable-mcp\"\n---\n\n# Use the Semantic Layer\n\nAltertable serves governed metrics as semantic views, powered by its open-source [`semantic_layer` DuckDB extension](https://github.com/altertable-ai/duckdb-semantic_layer). The syntax is Snowflake's semantic views, extended with time intelligence, metric filters, many-to-many bridges, and a query-by-name form.\n\nA semantic query names the dimensions and metrics it wants. The extension generates the joins, aggregates each metric at its own grain, and rejects combinations that would fan out. When a governed metric answers the question, query it through its semantic view instead of rebuilding the aggregation in SQL.\n\nMetrics are defined in **Knowledge**, by hand or imported from connected sources, and each semantic layer is served as a semantic view named `memory.main.<view_name>`.\n\n## Discover\n\n1. Call `list_semantic_metrics`. Match the request against each metric's name, label, description, and `ai_context`. Keep its `view_name`, its member (`<table alias>.<metric>`), and its `dimensions`.\n2. Answer structural questions with read-only statements through `query_lakehouse`. Each is a table expression, so project only the needed columns:\n - `SHOW SEMANTIC METRICS IN memory.main.<view>`: metrics with their types, synonyms, and comments.\n - `SHOW SEMANTIC DIMENSIONS IN memory.main.<view> FOR METRIC <alias>.<metric>`: exactly the dimensions that metric can be grouped by. `required = true` marks dimensions a window metric needs.\n - `DESCRIBE SEMANTIC VIEW memory.main.<view>`: tables, keys, relationships, and expressions, one property per row. Filter on `object_kind`.\n - `SHOW SEMANTIC VIEWS IN SCHEMA memory.main`: every view loaded in the session.\n\n## Query\n\nBoth forms compile to the same SQL. Always qualify the view as `memory.main.<view>`.\n\n```sql\nSELECT * FROM SEMANTIC_VIEW(\n memory.main.sales\n DIMENSIONS customers.segment, date_trunc('month', orders.created_at) AS month\n METRICS orders.revenue, orders.revenue / customers.customer_count AS revenue_per_customer\n WHERE orders.created_at >= TIMESTAMP '2026-01-01'\n)\nORDER BY month, segment\n```\n\n```sql\nSELECT customers.segment, AGG(orders.revenue) AS revenue\nFROM memory.main.sales\nWHERE orders.status = 'paid'\nGROUP BY ALL\nHAVING AGG(orders.revenue) > 1000\nORDER BY revenue DESC\nLIMIT 10\n```\n\n- `SEMANTIC_VIEW(...)` is a table expression: join it with other tables, wrap it, or use it in CTEs. Choose it when the result feeds further SQL.\n- The query-by-name form reads like `GROUP BY` SQL: `GROUP BY` items become dimensions and metrics are wrapped in `AGG(...)`. `HAVING`, window functions, and `QUALIFY` evaluate over the aggregated rows. It does not support `SELECT *`, joins with the view, or `ROLLUP`/`CUBE`/`GROUPING SETS`; produce subtotals with `UNION ALL` of separately grouped queries.\n- `WHERE` filters rows before aggregation and accepts only dimensions, facts, and uncorrelated subqueries. Filter on metric values with `HAVING` or in an outer query.\n- `DIMENSIONS` accepts expressions over dimensions, such as `date_trunc('month', <time member>) AS month`. `METRICS` accepts expressions over metrics, each aggregated at its own grain before they combine.\n- Ad-hoc aggregates over dimensions or facts are allowed: `COUNT(DISTINCT orders.status) AS statuses`. In the query-by-name form, an ad-hoc aggregate must read a column (`COUNT(orders.id)`, not `COUNT(*)`).\n- Write `AS` before every alias and alias every computed item.\n\n### Query-Time Metric Filters\n\n`AGG(orders.revenue) FILTER (WHERE orders.status = 'paid') AS paid_revenue` filters a metric without defining a new one. The condition is added to every aggregate the metric is built from, nested metrics included. Unlike `WHERE`, it removes no groups: a group without matching rows shows 0 or NULL beside the unfiltered metrics. The condition reads row-level values only.\n\n### The Grain Rule\n\nA dimension can group a metric only if it lies on the metric's table, on a table that table references (many-to-one), or across a many-to-many bridge. Otherwise the binder rejects the query:\n\n```text\nInvalid dimension specified: The dimension entity 'orders' must be related to and have an equal or\nlower level of granularity compared to the base metric or dimension entity 'customers'.\n```\n\nTreat this as a semantic answer, not a syntax problem. Do not bypass it with hand-written joins, which reintroduce the fan-out the view prevents. Pick a dimension from `SHOW SEMANTIC DIMENSIONS ... FOR METRIC`, use a metric on the finer table, or explain why the breakdown is undefined.\n\n## Time Intelligence\n\nQuery-time transforms add period comparisons and accumulations to any metric:\n\n```sql\nSELECT * FROM SEMANTIC_VIEW(\n memory.main.sales\n DIMENSIONS date_trunc('month', orders.created_at) AS month\n METRICS orders.revenue,\n time_shift(orders.revenue, INTERVAL 1 YEAR, dimension := orders.created_at) AS revenue_ly,\n orders.revenue / time_shift(orders.revenue, INTERVAL 1 YEAR, dimension := orders.created_at) - 1 AS yoy,\n cumulative(orders.revenue, to_date := 'year', dimension := orders.created_at) AS revenue_ytd\n WHERE orders.created_at >= TIMESTAMP '2026-01-01' AND orders.created_at < TIMESTAMP '2026-07-01'\n)\nORDER BY month\n```\n\n- `dimension` defaults to `metric_time.day` only when the view declares `TIME`; otherwise pass a time member defined as a plain column. Group by it or by an expression of it, such as `date_trunc`, at any grain.\n- A positive `time_shift` interval reads the past. Shifted metrics also produce periods after the last data, so bound the time range at both ends with `WHERE` on the same column. `WHERE` selects the periods to report, while the shifted metric still reads the earlier rows it needs.\n- `cumulative` takes `to_date := 'week' | 'month' | 'quarter' | 'year'` or `trailing_window := INTERVAL '7 days'`, plus `period := 'last'` (default), `'first'`, or `'average'` for the value reported per coarser group. It computes from rows, so distinct counts stay exact and gaps do not shorten windows.\n- `cumulative` anchors on every distinct value of the dimension. Over a raw timestamp of a large table, cost grows quadratically. Bound the range tightly; for an additive metric, prefer `SUM(AGG(m)) OVER (ORDER BY month)` in the query-by-name form.\n- Never compare periods with `LAG`: it reads the previous existing row, so a missing period silently changes the comparison. Use `time_shift`.\n- Transforms belong in `METRICS`, or in the query-by-name select list, `HAVING`, `QUALIFY`, and `ORDER BY` with metrics wrapped in `AGG(...)`. They are rejected in `WHERE`, `DIMENSIONS`, and inside `AGG(...)`. Put a filter inside the call: `time_shift(AGG(m) FILTER (WHERE ...), ...)`.\n\nRead [query syntax](references/query-syntax.md) for window functions, `PARTITION BY EXCLUDING`, row-level `FACTS` queries, name resolution, and every transform constraint.\n\n## Verify\n\n- `SELECT sql FROM semantic_view_sql('<semantic query>')` returns the expanded DuckDB SQL without running it. Use it to explain how a number is computed, audit joins, or debug unexpected rows; double the single quotes inside the literal.\n- State the metric's source (view and member) with the answer, so the user can tell a governed number from an ad-hoc one.\n\n## Create a Semantic View\n\nWhen no governed metric fits a business question that will come back, create a semantic view instead of leaving the logic in one-off SQL. Read [modeling](references/modeling.md) before writing the definition.\n\n1. Inspect the source tables with `get_catalog`. Confirm every declared primary key is actually unique: a false key silently breaks fan-out protection.\n2. Write `CREATE SEMANTIC VIEW` with fully qualified sources, many-to-one relationships, and one metric per business definition. Declare `TIME (<column>)` on fact tables to get `metric_time.day` / `week` / `month` / `quarter` / `year` and a default `dimension` for transforms.\n3. Add a `COMMENT` and `WITH SYNONYMS` to the view and its members so later agents and users can find them.\n4. Run it with `query_lakehouse` and reuse the returned `session_id`. Query the new view and compare one result with a hand-written aggregate before relying on it.\n\nConfirm the view name and metric definitions with the user before creating it. Never replace an existing view with `CREATE OR REPLACE` unless the user asked to change it.\n"
}SHA-256 of public snapshot: 38fc1f1f40784149d3b3d8a6bb9284327c8b68b156d996fb71b5f90bb24d7b54