Yardstick
A DuckDB extension implementing Julian Hyde's "Measures in SQL" paper (arXiv:2406.00251).
What is this?
Yardstick adds measure-aware SQL to DuckDB. Measures are aggregations that know how to re-aggregate themselves when the query context changes. This enables:
- Percent of total calculations without CTEs or window functions
- Year-over-year comparisons with simple syntax
- Drill-down analytics that automatically adjust aggregation context
How can I use this with any database?
Sidemantic is a fully featured semantic layer from Sidequery that supports the yardstick syntax across many different databases. Even mix and match yardstick syntax with your existing semantic model definitions.
Quick Start & Demo
-- Load the extension
INSTALL yardstick FROM community;
LOAD yardstick;
-- Create the sales table
CREATE TABLE sales (
id INTEGER PRIMARY KEY,
year INTEGER,
region VARCHAR(50),
amount DECIMAL(10, 2)
);
-- Insert sample data
INSERT INTO sales (id, year, region, amount) VALUES
(1, 2023, 'North', 15000.00),
(2, 2023, 'North', 22000.00),
(3, 2023, 'South', 18000.00),
(4, 2023, 'South', 12000.00),
(5, 2023, 'East', 25000.00),
(6, 2023, 'West', 19000.00),
(7, 2024, 'North', 28000.00),
(8, 2024, 'North', 31000.00),
(9, 2024, 'South', 21000.00),
(10, 2024, 'South', 16000.00),
(11, 2024, 'East', 33000.00),
(12, 2024, 'East', 29000.00),
(13, 2024, 'West', 24000.00),
(14, 2024, 'West', 27000.00);
-- Create a view with measures
CREATE VIEW sales_v AS
SELECT
year,
region,
SUM(amount) AS MEASURE revenue,
COUNT(*) AS MEASURE order_count
FROM sales;
-- Query with AGGREGATE() and AT modifiers
SELECT
year,
region,
AGGREGATE(revenue) AS revenue,
AGGREGATE(revenue) AT (ALL region) AS year_total,
AGGREGATE(revenue) / AGGREGATE(revenue) AT (ALL region) AS pct_of_year
FROM sales_v;
-- Variance from the global average
SELECT
region,
AGGREGATE(revenue) AS revenue,
AGGREGATE(revenue) AT (ALL) / 4.0 AS expected_if_equal, -- 4 regions
AGGREGATE(revenue) - (AGGREGATE(revenue) AT (ALL) / 4.0) AS variance
FROM sales_v;
-- Nested percentages (% of year, and that year's % of total)
SELECT
year,
region,
AGGREGATE(revenue) AS revenue,
100.0 * AGGREGATE(revenue) / AGGREGATE(revenue) AT (ALL region) AS pct_of_year,
100.0 * AGGREGATE(revenue) AT (ALL region) / AGGREGATE(revenue) AT (ALL) AS year_pct_of_total
FROM sales_v;
-- Compare 2024 performance to 2023 baseline for each region
SELECT
region,
AGGREGATE(revenue) AT (SET year = 2024) AS rev_2024,
AGGREGATE(revenue) AT (SET year = 2023) AS rev_2023,
AGGREGATE(revenue) AT (SET year = 2024) - AGGREGATE(revenue) AT (SET year = 2023) AS growth
FROM sales_v;
-- Filter to specific segments
SELECT
year,
AGGREGATE(revenue) AS total_revenue,
AGGREGATE(revenue) AT (SET region = 'North') AS north_revenue,
AGGREGATE(revenue) AT (SET region IN ('North', 'South')) AS north_south_combined
FROM sales_v;
Notebooks
Notebooks embed their dependencies via juv metadata.
uv tool install juv
# or run without install
uvx juv run docs/yardstick_adtech_kitchen_sink.ipynb
uvx juv run docs/yardstick_looker_demo.ipynb
These notebooks load the local extension if it exists at:
build/release/extension/yardstick/yardstick.duckdb_extension
and fall back to INSTALL yardstick FROM community.
Syntax
Defining Measures
CREATE VIEW view_name AS
SELECT
dimension1,
dimension2,
AGG(expr) AS MEASURE measure_name
FROM table;
Yardstick automatically handles the grouping. All DuckDB aggregate functions are supported; non-decomposable aggregates (COUNT(DISTINCT), MEDIAN, PERCENTILE_, QUANTILE_, MODE) are recomputed from base rows at query time and support AT modifiers, but can be more expensive.
Querying Measures
SELECT
dimensions,
AGGREGATE(measure_name) [AT modifier]
FROM view_name;
Choosing Where Definitions Are Stored
Set a fully qualified table name to keep semantic view definitions in any writable
DuckDB attachment:
-- Attach your database or lake using its normal DuckDB extension first.
SET yardstick_definition_table = 'lake.semantic.yardstick_views';
CREATE VIEW sales_v AS
SELECT region, SUM(amount) AS MEASURE revenue
FROM lake.sales.orders;
SELECT region, AGGREGATE(revenue) FROM sales_v;
The setting selects the attachment, schema, and table. The schema must already
exist; Yardstick creates the definitions table on the first permanent measure-view
declaration. It uses four VARCHAR columns: view_catalog, view_schema,
view_name, and definition_sql.
One reserved row has empty catalog, schema, and view names and holds the
transaction concurrency guard. To list only view definitions, filter with
WHERE view_name <> ''.
The selected table is the sole source of truth. CREATE VIEW ... AS MEASURE
inserts a definition, CREATE OR REPLACE VIEW updates it, and DROP VIEW removes
it. Replacing a managed view with an ordinary view definition keeps that definition
in the selected table. These writes use ordinary attachment table operations in
the caller's transaction, so ROLLBACK also rolls back definition changes. Source
data is never copied or materialized.
Definition writes update and verify that guard in the same transaction before
changing a view. Competing writers can receive a transaction conflict and should
retry in a fresh transaction. This works without requiring UNIQUE constraints
from the attachment. Direct external edits to the table bypass this protection.
An existing definitions table without a guard remains readable. Before writing,
initialize its guard once with exclusive writer access:
INSERT INTO lake.semantic.yardstick_views
(view_catalog, view_schema, view_name, definition_sql)
VALUES ('', '', '', 'yardstick_guard_v1:' || uuid()::VARCHAR);
In a later session, attach the same databases and set the same
yardstick_definition_table; queries resolve the stored definitions automatically.
The setting is session-local. Reattach source catalogs under the same names:
Yardstick qualifies table references when saving definitions so later USE or
search_path changes do not redirect them.
Configured definitions are resolved during binding, without creating duplicate
native view entries. Inspect the selected table to list them; they do not appear
in duckdb_views(). Ordinary unmanaged DuckDB views keep their usual behavior.
For a completely session-local store, use the temporary catalog:
SET yardstick_definition_table = 'temp.main.yardstick_views';
Queries that use stored definitions read the table during binding, using the
caller's attachment transaction and DuckDB's existing file/catalog caches.
Yardstick does not maintain a separate durable copy or a TTL cache. Simple queries
that directly read native tables skip the definitions scan. Prepared semantic
queries rebind so replacements and drops are visible on subsequent executions.
RESET yardstick_definition_table restores the existing in-memory catalog behavior.
On DuckDB 1.5+, queries containing AGGREGATE() are automatically intercepted by the extension's parser override, so no special prefix is needed. On older versions, the SEMANTIC prefix is still supported as a fallback.
The native frontend also supports measures directly in PIVOT:
PIVOT sales_v ON region USING AGGREGATE(revenue) GROUP BY year;
When GROUP BY is omitted, exposed dimensions other than the pivot inputs form
the groups; unused measure columns are excluded. Each measure is recomputed at
the final cell grain, preserving averages, distinct counts, and AT contexts.
AT Modifiers
| Modifier |
Description |
Example |
AT (ALL) |
Grand total across all dimensions |
AGGREGATE(revenue) AT (ALL) |
AT (ALL dim) |
Total excluding specific dimension |
AGGREGATE(revenue) AT (ALL region) |
AT (ALL expr) |
Total excluding ad hoc dimension |
AGGREGATE(revenue) AT (ALL MONTH(date)) |
AT (SET dim = val) |
Fix dimension to specific value |
AGGREGATE(revenue) AT (SET year = 2022) |
AT (SET dim = expr) |
Fix dimension to expression |
AGGREGATE(revenue) AT (SET year = year - 1) |
AT (SET expr = val) |
Fix ad hoc dimension to value |
AGGREGATE(revenue) AT (SET MONTH(date) = 6) |
AT (WHERE cond) |
Pre-aggregation filter |
AGGREGATE(revenue) AT (WHERE region = 'US') |
AT (VISIBLE) |
Use query's WHERE clause |
AGGREGATE(revenue) AT (VISIBLE) |
Modifiers within one AT (...) transform the context in written order.
Separate AT clauses compose from the outside inward: m AT (A B) is
equivalent to m AT (B) AT (A). WHERE replaces the context, while VISIBLE
adds the query's filters. A join does not restrict a measure by itself; a
filter on a joined column applies to the rows it matches.
To retain unrelated query filters while shifting a dimension with SET, include
VISIBLE explicitly:
SELECT year,
AGGREGATE(revenue) AT (VISIBLE SET year = year - 1) AS prior_revenue
FROM sales_v
WHERE year = 2024 AND region = 'North';
-- 2024, 37000.00
SET replaces the visible year restriction while retaining the North filter.
Without VISIBLE, an AT (SET ...) expression uses the grouping context rather
than carrying over filters on ungrouped dimensions. Yardstick warns when that
drops a filter, as it does for AT (ALL ...).
Building
On DuckDB builds with grammar-extension support, Yardstick recognizes AS MEASURE, AT (...), and CURRENT dimension / CURRENT(dimension) through native PEG grammar rules. LOAD yardstick enables this adapter without setting active_grammar_extensions. Native references retain their expression spans and local relation scope, including quoted identifiers and CTE shadowing. CURRENT belongs to an AT SET or WHERE expression; ordinary SQL aliases and nested queries keep their own scope. Measure registration and context semantics remain shared with DuckDB 1.5.5.
Native query traversal lowers CTE bodies, subqueries, and set-operation operands independently. Aggregate calls are discovered from expression nodes, including parenthesized AT