Skip to content

Conversation

stephencelis
Copy link
Member

We introduced the @DatabaseFunction macro back in #151, which allowed for user-defined scalar database functions. SQLite also offers the ability to define aggregate functions, so this PR augments the macro to do just that.

If a function is annotated with @DatabaseFunction and takes some Sequence of database-decodable values, it will be installed as an aggregate.

As a silly example, we could rewrite the sum function in Swift:

@DatabaseFunction
func sum(_ ints: some Sequence<Int>) -> Int {
  ints.reduce(0, +)
}

Reminder.select { $sum($0.id) }
// SELECT "sum"("reminders"."id")
// FROM "reminders"
// => 55

@stephencelis stephencelis marked this pull request as ready for review October 11, 2025 20:26
var order: QueryFragment?
var filter: QueryFragment?

public init<each Argument: QueryExpression>(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this public, should we rename AggregateFunction to AggregateFunctionExpression?

Should we also rename QueryFunction to FunctionExpression (or ScalarFunctionExpression) and publicize its initializer?

@stephencelis stephencelis merged commit 3c8af53 into main Oct 16, 2025
3 checks passed
@stephencelis stephencelis deleted the agg branch October 16, 2025 00:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants