Skip to content

Commit c695800

Browse files
authored
Support AT semantics for window measures (#23)
* Support AT semantics for window measures * Fix window clause detection and derived window checks
1 parent f23e1ca commit c695800

4 files changed

Lines changed: 330 additions & 20 deletions

File tree

LIMITATIONS.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,23 @@ Implementation of Julian Hyde's "Measures in SQL" paper (arXiv:2406.00251).
2323

2424
## Known Limitations
2525

26-
### 1. No Window Function Measures
26+
### 1. Ambiguous AT Contexts for Window Measures
2727

2828
```sql
29-
-- NOT SUPPORTED: Window functions in measure definitions
29+
-- Supported: window measures in view definitions
3030
CREATE VIEW v AS
3131
SELECT year,
3232
SUM(revenue) OVER (ORDER BY year) AS MEASURE running_total
3333
FROM t;
34+
35+
-- Supported: direct query and AGGREGATE() without AT
36+
SEMANTIC SELECT year, AGGREGATE(running_total)
37+
FROM v
38+
GROUP BY year;
39+
40+
-- Supported: AT modifiers when the evaluated window result is single-valued
41+
SEMANTIC SELECT AGGREGATE(running_total) AT (WHERE year = 2024) FROM v;
42+
43+
-- Error: AT context produced multiple distinct window values
44+
SEMANTIC SELECT year, AGGREGATE(running_total) AT (ALL) FROM v GROUP BY year;
3445
```

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ The extension will be at `build/release/extension/yardstick/yardstick.duckdb_ext
160160
See [LIMITATIONS.md](LIMITATIONS.md) for known issues and workarounds.
161161

162162
Key limitations:
163-
- Window function measures not supported
163+
- Window-defined measures with `AT (...)` must evaluate to a single value per context, or they error
164164

165165
## Testimonials
166166

test/sql/measures.test

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,77 @@ B 10
13161316
# Test: DuckDB generate_series / UNNEST
13171317
# =============================================================================
13181318

1319+
# =============================================================================
1320+
# Test: Window function in AS MEASURE
1321+
# =============================================================================
1322+
1323+
statement ok
1324+
CREATE TABLE window_measure_orders (year INT, revenue INT);
1325+
1326+
statement ok
1327+
INSERT INTO window_measure_orders VALUES
1328+
(2021, 10),
1329+
(2022, 20),
1330+
(2023, 30);
1331+
1332+
statement ok
1333+
CREATE VIEW window_measure_v AS
1334+
SELECT
1335+
year,
1336+
SUM(revenue) OVER (ORDER BY year) AS MEASURE running_total
1337+
FROM window_measure_orders;
1338+
1339+
query IT rowsort
1340+
SELECT year, running_total::VARCHAR
1341+
FROM window_measure_v
1342+
ORDER BY year;
1343+
----
1344+
2021 10
1345+
2022 30
1346+
2023 60
1347+
1348+
query IT rowsort
1349+
SEMANTIC SELECT year, AGGREGATE(running_total)::VARCHAR
1350+
FROM window_measure_v
1351+
GROUP BY year
1352+
ORDER BY year;
1353+
----
1354+
2021 10
1355+
2022 30
1356+
2023 60
1357+
1358+
query IT rowsort
1359+
SEMANTIC SELECT year, AGGREGATE(running_total) AT (WHERE year = 2022)::VARCHAR
1360+
FROM window_measure_v;
1361+
----
1362+
2021 20
1363+
2022 20
1364+
2023 20
1365+
1366+
statement error
1367+
SEMANTIC SELECT year, AGGREGATE(running_total) AT (ALL)
1368+
FROM window_measure_v
1369+
GROUP BY year;
1370+
----
1371+
Window measure running_total returned multiple values for the evaluation context
1372+
1373+
statement ok
1374+
CREATE VIEW window_total_v AS
1375+
SELECT
1376+
year,
1377+
SUM(revenue) OVER () AS MEASURE global_total
1378+
FROM window_measure_orders;
1379+
1380+
query IT rowsort
1381+
SEMANTIC SELECT year, AGGREGATE(global_total) AT (ALL)::VARCHAR
1382+
FROM window_total_v
1383+
GROUP BY year
1384+
ORDER BY year;
1385+
----
1386+
2021 60
1387+
2022 60
1388+
2023 60
1389+
13191390
statement ok
13201391
CREATE VIEW series_v AS
13211392
SELECT x, SUM(x) AS MEASURE total

0 commit comments

Comments
 (0)