diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8fbc5b17d..605744498 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -86,6 +86,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
## Next Release
- **New**: `DatabaseRegion(table: tableName)` is deprecated. Use `Table(tableName)` instead.
+- **New**: The `ABS` and `LENGTH` SQL functions are now available on association aggregates, through `abs(aggregate)` and `length(aggregate)`.
## 5.16.0
diff --git a/Documentation/AssociationsBasics.md b/Documentation/AssociationsBasics.md
index cb53be71c..8ad0b3920 100644
--- a/Documentation/AssociationsBasics.md
+++ b/Documentation/AssociationsBasics.md
@@ -2653,6 +2653,23 @@ Aggregates can be modified and combined with Swift operators:
let request = Team.annotated(with: Team.players.min(Column("score")) ?? 0)
```
+- SQL functions `ABS` and `LENGTH` are available as the `abs` and `length` Swift functions:
+
+
+ SQL
+
+ ```sql
+ SELECT "team".*, ABS(MAX("player"."score"))
+ FROM "team"
+ LEFT JOIN "player" ON ("player"."teamId" = "team"."id")
+ GROUP BY "team"."id"
+ ```
+
+
+
+ ```swift
+ let request = Team.annotated(with: abs(Team.players.max(Column("score"))))
+ ```
### Isolation of Multiple Aggregates
diff --git a/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift b/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift
index 1813bb570..f340141c0 100644
--- a/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift
+++ b/GRDB/QueryInterface/Request/Association/AssociationAggregate.swift
@@ -1095,5 +1095,22 @@ public func ?? (
.with { $0.key = lhs.key } // Preserve key
}
-// TODO: add support for ABS(aggregate)
-// TODO: add support for LENGTH(aggregate)
+// MARK: - ABS(...)
+
+/// Returns an aggregate that evaluates to the absolute value of the
+/// input aggregate.
+public func abs(_ aggregate: AssociationAggregate)
+-> AssociationAggregate
+{
+ aggregate.map(abs)
+}
+
+// MARK: - LENGTH(...)
+
+/// Returns an aggregate that evaluates the `LENGTH` SQL function on the
+/// input aggregate.
+public func length(_ aggregate: AssociationAggregate)
+-> AssociationAggregate
+{
+ aggregate.map(length)
+}
diff --git a/Tests/GRDBTests/AssociationAggregateTests.swift b/Tests/GRDBTests/AssociationAggregateTests.swift
index 982dd828b..0d5bdd953 100644
--- a/Tests/GRDBTests/AssociationAggregateTests.swift
+++ b/Tests/GRDBTests/AssociationAggregateTests.swift
@@ -1412,4 +1412,54 @@ class AssociationAggregateTests: GRDBTestCase {
XCTAssertEqual(teamInfos[3].minPlayerScore, 0)
}
}
+
+ func testAbs() throws {
+ let dbQueue = try makeDatabaseQueue()
+ try dbQueue.read { db in
+ do {
+ let request = Team.annotated(with: abs(Team.players.max(Column("score"))))
+ try assertEqualSQL(db, request, """
+ SELECT "team".*, ABS(MAX("player"."score")) \
+ FROM "team" \
+ LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
+ GROUP BY "team"."id"
+ """)
+ }
+ do {
+ let request = Team.annotated(with: abs(Team.players.max(Column("score"))).forKey("foo"))
+ try assertEqualSQL(db, request, """
+ SELECT "team".*, ABS(MAX("player"."score")) AS "foo" \
+ FROM "team" \
+ LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
+ GROUP BY "team"."id"
+ """)
+ }
+ }
+ }
+
+ func testLength() throws {
+ let dbQueue = try makeDatabaseQueue()
+ try dbQueue.read { db in
+ do {
+ // This is a meaningless request, but this is enough for this test
+ let request = Team.annotated(with: length(Team.players.max(Column("score"))))
+ try assertEqualSQL(db, request, """
+ SELECT "team".*, LENGTH(MAX("player"."score")) \
+ FROM "team" \
+ LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
+ GROUP BY "team"."id"
+ """)
+ }
+ do {
+ // This is a meaningless request, but this is enough for this test
+ let request = Team.annotated(with: length(Team.players.max(Column("score"))).forKey("foo"))
+ try assertEqualSQL(db, request, """
+ SELECT "team".*, LENGTH(MAX("player"."score")) AS "foo" \
+ FROM "team" \
+ LEFT JOIN "player" ON "player"."teamId" = "team"."id" \
+ GROUP BY "team"."id"
+ """)
+ }
+ }
+ }
}