>sum_duration AggregateFunction(sum, Int32),
>error_count Nullable(UInt64)
>ENGINE = AggregatingMergeTree()
>sumState(duration) as sum_focus_duration,
>countIf(severity = 'error') as error_count
Because AggregatingMergeTree does not sums numeric columns by default. It just do any() as documented.
And it's all slightly overengineered and incorrect.
I would not use AggregateFunction I would use SummingMT.
CREATE TABLE test.session_target
( session_ts UInt32,
session_uuid UUID,
appid UInt32,
client_ip String,
sum_duration Int32,
error_count UInt64
ENGINE = SummingMergeTree()
PARTITION BY toStartOfWeek(toDateTime(session_ts))
ORDER BY (session_ts, session_uuid, appid, client_ip);
CREATE MATERIALIZED VIEW test.session_agg_MV TO test.session_target
as select session_ts, session_uuid, appid, client_ip,
sumState(duration) as sum_focus_duration,
countIf(severity = 'error') as error_count
from event
where toDateTime(session_ts)>='2020-09-10 00:00:00'
group by session_ts, session_uuid, appid, client_ip
select from MV
select
session_ts, session_uuid, appid, client_ip,
sum(sum_duration) as sum_duration,
sum(error_count) as error_count,
from test.session_agg_MV
group by session_ts, session_uuid, appid, client_ip
If you really need AggregatingMergeTree for something else, I would use SimepleAggregateFunction
SimepleAggregateFunction are twice faster and take less space than AggregateFunction because they don't need state.
and sum -- does not need state.
CREATE TABLE test.session_target
( session_ts UInt32,
session_uuid UUID,
appid UInt32,
client_ip String,
sum_duration SimepleAggregateFunction(sum, Int32),
error_count SimepleAggregateFunction(sum, UInt64)
ENGINE = ENGINE = AggregatingMergeTree()
PARTITION BY toStartOfWeek(toDateTime(session_ts))
ORDER BY (session_ts, session_uuid, appid, client_ip);
There are by the way countStateIf and countMerge -- but they useless in this case.