SimpleAggregateFunction stores pre-aggregated values that ClickHouse automatically merges when combining rows with the same primary key. Use with AggregatingMergeTree tables.
Define columns that automatically aggregate during merges:
from moose_lib import ( simple_aggregated, Key, OlapTable, OlapConfig, AggregatingMergeTreeEngine)from pydantic import BaseModelfrom datetime import datetime class DailyStats(BaseModel): date: datetime user_id: str total_views: simple_aggregated('sum', int) max_score: simple_aggregated('max', float) min_latency: simple_aggregated('min', float) last_seen: simple_aggregated('anyLast', datetime) stats_table = OlapTable[DailyStats]( "daily_stats", OlapConfig( engine=AggregatingMergeTreeEngine(), order_by_fields=["date", "user_id"] ))| Function | Description | Typical Use |
|---|---|---|
sum | Sum of values | Totals, counts |
max | Maximum value | Peak metrics |
min |
| Minimum value |
| Minimum thresholds |
any | Any single value | Non-deterministic pick |
anyLast | Last inserted value | Latest timestamp, status |
argMax | Value at max of another column | Value when metric was highest |
argMin | Value at min of another column | Value when metric was lowest |
SimpleAggregateFunction columns are combined using their specified function// Two rows with same (date, userId):{ date: "2025-01-01", userId: "u1", totalViews: 10 }{ date: "2025-01-01", userId: "u1", totalViews: 15 } // After merge:{ date: "2025-01-01", userId: "u1", totalViews: 25 } // sumUse when you need real-time aggregations on high-volume data. Instead of storing every event and aggregating at query time, pre-aggregate during ingestion for faster queries.
SimpleAggregateFunction only works with AggregatingMergeTree or ReplicatedAggregatingMergeTree table engines. The table's ORDER BY clause defines which rows get merged.
AggregatingMergeTree