Skip to content

Commit 5bc2f32

Browse files
committed
Merge window-functions into develop
2 parents 776f7aa + 7b8f5e9 commit 5bc2f32

File tree

7 files changed

+670
-1
lines changed

7 files changed

+670
-1
lines changed

docs/docs/dart_api/expressions.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Support for common JSON operators is provided through `package:drift/extensions/
146146
This provides things like `jsonExtract` to extract fields from JSON or `jsonEach` to query
147147
nested JSON structures. For more details, see the [JSON support](select.md#json-support) section on the page about selects or [this more complex example](../Examples/relationships.md#with-json-functions).
148148

149-
## Aggregate functions (like count and sum)
149+
## Aggregate functions (like count and sum)
150150

151151
[Aggregate functions](https://www.sqlite.org/lang_aggfunc.html) are available
152152
from the Dart api. Unlike regular functions, aggregate functions operate on multiple rows at
@@ -225,6 +225,21 @@ Stream<String> allTodoContent() {
225225
The separator defaults to a comma without surrounding whitespace, but it can be changed
226226
with the `separator` argument on `groupConcat`.
227227

228+
### Window functions
229+
230+
In addition to aggregate expressions and `groupBy`, drift supports [window functions](https://en.wikipedia.org/wiki/Window_function_(SQL)).
231+
Unlike regular aggregates, which collapse a group of rows into a single value, window functions allow
232+
running aggregations over a subset of rows related to the current one.
233+
For instance, you could use this to track a running total of values:
234+
235+
{{ load_snippet('window','lib/snippets/dart_api/expressions.dart.excerpt.json') }}
236+
237+
An interesting use for window function is to determine the rank a row would have if rows were
238+
sorted by some column (without actually returning all rows, or sorting them by that column).
239+
This ranking can be attached to each row:
240+
241+
{{ load_snippet('window2','lib/snippets/dart_api/expressions.dart.excerpt.json') }}
242+
228243
## Mathematical functions and regexp
229244

230245
When using a `NativeDatabase`, a basic set of trigonometric functions will be available.

docs/lib/snippets/dart_api/expressions.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,40 @@ extension Snippets on CanUseCommonTables {
3434
await update(todoItems).write(change);
3535
}
3636
// #enddocregion date3
37+
38+
// #docregion window
39+
/// Returns all todo items, associating each item with the total length of all
40+
/// titles up until (and including) each todo item.
41+
Selectable<(TodoItem, int)> todosWithRunningLength() {
42+
final runningTitleLength = WindowFunctionExpression(
43+
todoItems.title.length.sum(),
44+
orderBy: [OrderingTerm.asc(todoItems.id)],
45+
);
46+
final query = select(todoItems).addColumns([runningTitleLength]);
47+
query.orderBy([OrderingTerm.asc(todoItems.id)]);
48+
49+
return query.map((row) {
50+
return (row.readTable(todoItems)!, row.read(runningTitleLength)!);
51+
});
52+
}
53+
// #enddocregion window
54+
55+
// #docregion window2
56+
/// Returns all todo items, also reporting the index (counting from 1) each
57+
/// todo item would have if all items were sorted by descending content
58+
/// length.
59+
Selectable<(TodoItem, int)> todosWithLengthRanking() {
60+
final lengthRanking = WindowFunctionExpression(
61+
todoItems.id.count(),
62+
orderBy: [OrderingTerm.desc(todoItems.content.length)],
63+
);
64+
final query = select(todoItems).addColumns([lengthRanking]);
65+
66+
return query.map((row) {
67+
return (row.readTable(todoItems)!, row.read(lengthRanking)!);
68+
});
69+
}
70+
// #enddocregion window2
3771
}
3872

3973
// #docregion bitwise

drift/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.26.0-dev
2+
3+
- Add support for window functions with `WindowFunctionExpression`.
4+
15
## 2.25.1
26

37
- Fix shared worker feature detection when the main app is compiled with

0 commit comments

Comments
 (0)